2006-07-04 16:06:44 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* motion.c: control VLC with laptop built-in motion sensors
|
|
|
|
*****************************************************************************
|
2007-06-20 22:19:24 +02:00
|
|
|
* Copyright (C) 2006 - 2007 the VideoLAN team
|
2006-07-04 16:06:44 +02:00
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Author: Sam Hocevar <sam@zoy.org>
|
2007-06-20 22:19:24 +02:00
|
|
|
* Jérôme Decoodt <djc@videolan.org> (unimotion integration)
|
2006-07-04 16:06:44 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Preamble
|
|
|
|
*****************************************************************************/
|
2007-06-20 22:19:24 +02:00
|
|
|
#include <math.h>
|
2006-07-04 16:06:44 +02:00
|
|
|
|
2008-01-23 22:50:58 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2006-07-04 16:06:44 +02:00
|
|
|
#include <vlc/vlc.h>
|
2006-11-26 15:20:34 +01:00
|
|
|
#include <vlc_interface.h>
|
|
|
|
#include <vlc_vout.h>
|
2006-07-04 16:06:44 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2007-06-20 22:19:24 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include "unimotion.h"
|
|
|
|
#endif
|
|
|
|
|
2006-07-04 16:06:44 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* intf_sys_t: description and status of interface
|
|
|
|
*****************************************************************************/
|
|
|
|
struct intf_sys_t
|
|
|
|
{
|
2007-06-20 22:19:24 +02:00
|
|
|
enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR, UNIMOTION_SENSOR } sensor;
|
2007-06-20 22:42:24 +02:00
|
|
|
#ifdef __APPLE__
|
2007-06-20 22:19:24 +02:00
|
|
|
enum sms_hardware unimotion_hw;
|
2007-06-20 22:42:24 +02:00
|
|
|
#endif
|
2006-10-14 19:06:38 +02:00
|
|
|
int i_calibrate;
|
|
|
|
|
|
|
|
vlc_bool_t b_use_rotate;
|
2006-07-04 16:06:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Local prototypes.
|
|
|
|
*****************************************************************************/
|
|
|
|
static int Open ( vlc_object_t * );
|
|
|
|
static void Close ( vlc_object_t * );
|
|
|
|
|
|
|
|
static void RunIntf( intf_thread_t *p_intf );
|
|
|
|
static int GetOrientation( intf_thread_t *p_intf );
|
|
|
|
|
2006-10-14 19:06:38 +02:00
|
|
|
#define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")
|
|
|
|
|
2006-07-04 16:06:44 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* Module descriptor
|
|
|
|
*****************************************************************************/
|
|
|
|
vlc_module_begin();
|
|
|
|
set_shortname( _("motion"));
|
|
|
|
set_category( CAT_INTERFACE );
|
|
|
|
set_description( _("motion control interface") );
|
|
|
|
|
2006-10-14 19:06:38 +02:00
|
|
|
add_bool( "motion-use-rotate", 0, NULL,
|
|
|
|
USE_ROTATE_TEXT, USE_ROTATE_TEXT, VLC_FALSE );
|
|
|
|
|
2006-07-04 16:06:44 +02:00
|
|
|
set_capability( "interface", 0 );
|
|
|
|
set_callbacks( Open, Close );
|
|
|
|
vlc_module_end();
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* OpenIntf: initialise interface
|
|
|
|
*****************************************************************************/
|
|
|
|
int Open ( vlc_object_t *p_this )
|
|
|
|
{
|
|
|
|
intf_thread_t *p_intf = (intf_thread_t *)p_this;
|
|
|
|
FILE *f;
|
|
|
|
int i_x, i_y;
|
|
|
|
|
|
|
|
p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
|
|
|
|
if( p_intf->p_sys == NULL )
|
|
|
|
{
|
|
|
|
return VLC_ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
|
|
|
|
{
|
|
|
|
/* IBM HDAPS support */
|
|
|
|
f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
|
|
|
|
if( f )
|
|
|
|
{
|
|
|
|
i_x = i_y = 0;
|
|
|
|
fscanf( f, "(%d,%d)", &i_x, &i_y );
|
|
|
|
fclose( f );
|
|
|
|
p_intf->p_sys->i_calibrate = i_x;
|
|
|
|
p_intf->p_sys->sensor = HDAPS_SENSOR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p_intf->p_sys->sensor = NO_SENSOR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
|
|
|
|
{
|
|
|
|
/* Apple Motion Sensor support */
|
|
|
|
p_intf->p_sys->sensor = AMS_SENSOR;
|
|
|
|
}
|
2007-06-20 22:19:24 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
else if( p_intf->p_sys->unimotion_hw = detect_sms() )
|
|
|
|
p_intf->p_sys->sensor = UNIMOTION_SENSOR;
|
|
|
|
#endif
|
2006-07-04 16:06:44 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No motion sensor support */
|
|
|
|
p_intf->p_sys->sensor = NO_SENSOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_intf->pf_run = RunIntf;
|
|
|
|
|
2006-10-14 19:06:38 +02:00
|
|
|
p_intf->p_sys->b_use_rotate = config_GetInt( p_intf, "motion-use-rotate" );
|
|
|
|
|
2006-07-04 16:06:44 +02:00
|
|
|
return VLC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* CloseIntf: destroy interface
|
|
|
|
*****************************************************************************/
|
|
|
|
void Close ( vlc_object_t *p_this )
|
|
|
|
{
|
|
|
|
intf_thread_t *p_intf = (intf_thread_t *)p_this;
|
|
|
|
|
|
|
|
free( p_intf->p_sys );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* RunIntf: main loop
|
|
|
|
*****************************************************************************/
|
2007-06-20 22:19:24 +02:00
|
|
|
#define FILTER_LENGTH 16
|
|
|
|
#define LOW_THRESHOLD 800
|
|
|
|
#define HIGH_THRESHOLD 1000
|
2006-07-04 16:06:44 +02:00
|
|
|
static void RunIntf( intf_thread_t *p_intf )
|
|
|
|
{
|
2007-06-20 22:19:24 +02:00
|
|
|
int i_x, i_oldx = 0, i_sum = 0, i = 0;
|
|
|
|
int p_oldx[FILTER_LENGTH];
|
|
|
|
memset( p_oldx, 0, FILTER_LENGTH * sizeof( int ) );
|
2006-07-04 16:06:44 +02:00
|
|
|
|
2006-09-24 11:34:57 +02:00
|
|
|
while( !intf_ShouldDie( p_intf ) )
|
2006-07-04 16:06:44 +02:00
|
|
|
{
|
|
|
|
vout_thread_t *p_vout;
|
2006-12-24 15:18:21 +01:00
|
|
|
const char *psz_filter, *psz_type;
|
2006-07-04 16:06:44 +02:00
|
|
|
vlc_bool_t b_change = VLC_FALSE;
|
|
|
|
|
|
|
|
/* Wait a bit, get orientation, change filter if necessary */
|
|
|
|
msleep( INTF_IDLE_SLEEP );
|
|
|
|
|
|
|
|
i_x = GetOrientation( p_intf );
|
2007-06-20 22:19:24 +02:00
|
|
|
i_sum += i_x - p_oldx[i];
|
|
|
|
p_oldx[i++] = i_x;
|
|
|
|
if( i == FILTER_LENGTH ) i = 0;
|
|
|
|
i_x = i_sum / FILTER_LENGTH;
|
2006-07-04 16:06:44 +02:00
|
|
|
|
2006-10-14 19:06:38 +02:00
|
|
|
if( p_intf->p_sys->b_use_rotate )
|
|
|
|
{
|
|
|
|
if( i_oldx != i_x )
|
|
|
|
{
|
2007-04-21 01:48:27 +02:00
|
|
|
/* TODO: cache object pointer */
|
|
|
|
vlc_object_t *p_obj =
|
|
|
|
vlc_object_find_name( p_intf->p_libvlc, "rotate", FIND_CHILD );
|
|
|
|
if( p_obj )
|
|
|
|
{
|
2007-06-20 22:19:24 +02:00
|
|
|
var_SetInteger( p_obj, "rotate-deciangle",
|
|
|
|
((3600+i_x/2)%3600) );
|
2007-04-21 01:48:27 +02:00
|
|
|
i_oldx = i_x;
|
|
|
|
vlc_object_release( p_obj );
|
|
|
|
}
|
2006-10-14 19:06:38 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-07-04 16:06:44 +02:00
|
|
|
if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
|
|
|
|
{
|
|
|
|
b_change = VLC_TRUE;
|
|
|
|
psz_filter = "transform";
|
|
|
|
psz_type = "270";
|
|
|
|
}
|
|
|
|
else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
|
|
|
|
|| ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
|
|
|
|
{
|
|
|
|
b_change = VLC_TRUE;
|
|
|
|
psz_filter = "";
|
|
|
|
psz_type = "";
|
|
|
|
}
|
|
|
|
else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
|
|
|
|
{
|
|
|
|
b_change = VLC_TRUE;
|
|
|
|
psz_filter = "transform";
|
|
|
|
psz_type = "90";
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !b_change )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_vout = (vout_thread_t *)
|
|
|
|
vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
|
|
|
|
if( !p_vout )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
config_PutPsz( p_vout, "transform-type", psz_type );
|
|
|
|
var_SetString( p_vout, "vout-filter", psz_filter );
|
|
|
|
vlc_object_release( p_vout );
|
|
|
|
|
|
|
|
i_oldx = i_x;
|
|
|
|
}
|
|
|
|
}
|
2007-06-20 22:19:24 +02:00
|
|
|
#undef FILTER_LENGTH
|
|
|
|
#undef LOW_THRESHOLD
|
|
|
|
#undef HIGH_THRESHOLD
|
2006-07-04 16:06:44 +02:00
|
|
|
|
|
|
|
/*****************************************************************************
|
2007-06-20 22:19:24 +02:00
|
|
|
* GetOrientation: get laptop orientation, range -1800 / +1800
|
2006-07-04 16:06:44 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
static int GetOrientation( intf_thread_t *p_intf )
|
|
|
|
{
|
|
|
|
FILE *f;
|
2007-06-20 22:42:24 +02:00
|
|
|
int i_x, i_y;
|
|
|
|
#ifdef __APPLE__
|
|
|
|
int i_z;
|
|
|
|
#endif
|
2006-07-04 16:06:44 +02:00
|
|
|
switch( p_intf->p_sys->sensor )
|
|
|
|
{
|
|
|
|
case HDAPS_SENSOR:
|
|
|
|
f = fopen( "/sys/devices/platform/hdaps/position", "r" );
|
|
|
|
if( !f )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
i_x = i_y = 0;
|
|
|
|
fscanf( f, "(%d,%d)", &i_x, &i_y );
|
|
|
|
fclose( f );
|
|
|
|
|
2007-06-20 22:19:24 +02:00
|
|
|
return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
|
2006-07-04 16:06:44 +02:00
|
|
|
|
|
|
|
case AMS_SENSOR:
|
|
|
|
f = fopen( "/sys/devices/ams/x", "r" );
|
|
|
|
if( !f )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fscanf( f, "%d", &i_x);
|
|
|
|
fclose( f );
|
|
|
|
|
2007-06-20 22:19:24 +02:00
|
|
|
return - i_x * 30; /* FIXME: arbitrary */
|
|
|
|
#ifdef __APPLE__
|
|
|
|
case UNIMOTION_SENSOR:
|
|
|
|
if( read_sms_raw( p_intf->p_sys->unimotion_hw, &i_x, &i_y, &i_z ) )
|
|
|
|
{
|
|
|
|
double d_norm = sqrt( i_x*i_x+i_z*i_z );
|
|
|
|
if( d_norm < 100 )
|
|
|
|
return 0;
|
|
|
|
double d_x = i_x / d_norm;
|
|
|
|
if( i_z > 0 )
|
|
|
|
return -asin(d_x)*3600/3.141;
|
|
|
|
else
|
|
|
|
return 3600 + asin(d_x)*3600/3.141;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
2007-06-20 22:42:24 +02:00
|
|
|
#endif
|
2006-07-04 16:06:44 +02:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|