1 /*****************************************************************************
2 * motion.c: control VLC with laptop built-in motion sensors
3 *****************************************************************************
4 * Copyright (C) 2006 - 2007 the VideoLAN team
7 * Author: Sam Hocevar <sam@zoy.org>
8 * Jérôme Decoodt <djc@videolan.org> (unimotion integration)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
33 #include <vlc_interface.h>
41 #include "unimotion.h"
44 /*****************************************************************************
45 * intf_sys_t: description and status of interface
46 *****************************************************************************/
49 enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR, UNIMOTION_SENSOR } sensor;
50 enum sms_hardware unimotion_hw;
54 vlc_bool_t b_use_rotate;
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close ( vlc_object_t * );
63 static void RunIntf( intf_thread_t *p_intf );
64 static int GetOrientation( intf_thread_t *p_intf );
66 #define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")
68 /*****************************************************************************
70 *****************************************************************************/
72 set_shortname( _("motion"));
73 set_category( CAT_INTERFACE );
74 set_description( _("motion control interface") );
76 add_bool( "motion-use-rotate", 0, NULL,
77 USE_ROTATE_TEXT, USE_ROTATE_TEXT, VLC_FALSE );
79 set_capability( "interface", 0 );
80 set_callbacks( Open, Close );
83 /*****************************************************************************
84 * OpenIntf: initialise interface
85 *****************************************************************************/
86 int Open ( vlc_object_t *p_this )
88 intf_thread_t *p_intf = (intf_thread_t *)p_this;
92 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
93 if( p_intf->p_sys == NULL )
98 if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
100 /* IBM HDAPS support */
101 f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
105 fscanf( f, "(%d,%d)", &i_x, &i_y );
107 p_intf->p_sys->i_calibrate = i_x;
108 p_intf->p_sys->sensor = HDAPS_SENSOR;
112 p_intf->p_sys->sensor = NO_SENSOR;
115 else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
117 /* Apple Motion Sensor support */
118 p_intf->p_sys->sensor = AMS_SENSOR;
121 else if( p_intf->p_sys->unimotion_hw = detect_sms() )
122 p_intf->p_sys->sensor = UNIMOTION_SENSOR;
126 /* No motion sensor support */
127 p_intf->p_sys->sensor = NO_SENSOR;
130 p_intf->pf_run = RunIntf;
132 p_intf->p_sys->b_use_rotate = config_GetInt( p_intf, "motion-use-rotate" );
137 /*****************************************************************************
138 * CloseIntf: destroy interface
139 *****************************************************************************/
140 void Close ( vlc_object_t *p_this )
142 intf_thread_t *p_intf = (intf_thread_t *)p_this;
144 free( p_intf->p_sys );
147 /*****************************************************************************
149 *****************************************************************************/
150 #define FILTER_LENGTH 16
151 #define LOW_THRESHOLD 800
152 #define HIGH_THRESHOLD 1000
153 static void RunIntf( intf_thread_t *p_intf )
155 int i_x, i_oldx = 0, i_sum = 0, i = 0;
156 int p_oldx[FILTER_LENGTH];
157 memset( p_oldx, 0, FILTER_LENGTH * sizeof( int ) );
159 while( !intf_ShouldDie( p_intf ) )
161 vout_thread_t *p_vout;
162 const char *psz_filter, *psz_type;
163 vlc_bool_t b_change = VLC_FALSE;
165 /* Wait a bit, get orientation, change filter if necessary */
166 msleep( INTF_IDLE_SLEEP );
168 i_x = GetOrientation( p_intf );
169 i_sum += i_x - p_oldx[i];
171 if( i == FILTER_LENGTH ) i = 0;
172 i_x = i_sum / FILTER_LENGTH;
174 if( p_intf->p_sys->b_use_rotate )
178 /* TODO: cache object pointer */
179 vlc_object_t *p_obj =
180 vlc_object_find_name( p_intf->p_libvlc, "rotate", FIND_CHILD );
183 var_SetInteger( p_obj, "rotate-deciangle",
184 ((3600+i_x/2)%3600) );
186 vlc_object_release( p_obj );
192 if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
195 psz_filter = "transform";
198 else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
199 || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
205 else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
208 psz_filter = "transform";
217 p_vout = (vout_thread_t *)
218 vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
224 config_PutPsz( p_vout, "transform-type", psz_type );
225 var_SetString( p_vout, "vout-filter", psz_filter );
226 vlc_object_release( p_vout );
233 #undef HIGH_THRESHOLD
235 /*****************************************************************************
236 * GetOrientation: get laptop orientation, range -1800 / +1800
237 *****************************************************************************/
238 static int GetOrientation( intf_thread_t *p_intf )
243 switch( p_intf->p_sys->sensor )
246 f = fopen( "/sys/devices/platform/hdaps/position", "r" );
253 fscanf( f, "(%d,%d)", &i_x, &i_y );
256 return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
259 f = fopen( "/sys/devices/ams/x", "r" );
265 fscanf( f, "%d", &i_x);
268 return - i_x * 30; /* FIXME: arbitrary */
270 case UNIMOTION_SENSOR:
271 if( read_sms_raw( p_intf->p_sys->unimotion_hw, &i_x, &i_y, &i_z ) )
273 double d_norm = sqrt( i_x*i_x+i_z*i_z );
276 double d_x = i_x / d_norm;
278 return -asin(d_x)*3600/3.141;
280 return 3600 + asin(d_x)*3600/3.141;