Netsync: kill 2 warnings
[vlc/asuraparaju-public.git] / modules / control / motion.c
blob2af60ef91fad316e507d477ddfe094c56a47211f
1 /*****************************************************************************
2 * motion.c: control VLC with laptop built-in motion sensors
3 *****************************************************************************
4 * Copyright (C) 2006 - 2007 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <math.h>
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_interface.h>
38 #include <vlc_vout.h>
40 #ifdef HAVE_UNISTD_H
41 # include <unistd.h>
42 #endif
44 #ifdef __APPLE__
45 #include "unimotion.h"
46 #endif
48 /*****************************************************************************
49 * intf_sys_t: description and status of interface
50 *****************************************************************************/
51 struct intf_sys_t
53 enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR, APPLESMC_SENSOR,
54 UNIMOTION_SENSOR } sensor;
55 #ifdef __APPLE__
56 enum sms_hardware unimotion_hw;
57 #endif
58 int i_calibrate;
60 bool b_use_rotate;
63 /*****************************************************************************
64 * Local prototypes.
65 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
69 static void RunIntf( intf_thread_t *p_intf );
70 static int GetOrientation( intf_thread_t *p_intf );
72 #define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")
74 /*****************************************************************************
75 * Module descriptor
76 *****************************************************************************/
77 vlc_module_begin ()
78 set_shortname( N_("motion"))
79 set_category( CAT_INTERFACE )
80 set_subcategory( SUBCAT_INTERFACE_CONTROL )
81 set_description( N_("motion control interface") )
82 set_help( N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion sensors " \
83 "to rotate the video") )
85 add_bool( "motion-use-rotate", false, NULL,
86 USE_ROTATE_TEXT, USE_ROTATE_TEXT, false )
88 set_capability( "interface", 0 )
89 set_callbacks( Open, Close )
90 vlc_module_end ()
92 /*****************************************************************************
93 * OpenIntf: initialise interface
94 *****************************************************************************/
95 int Open ( vlc_object_t *p_this )
97 intf_thread_t *p_intf = (intf_thread_t *)p_this;
98 FILE *f;
99 int i_x, i_y;
101 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
102 if( p_intf->p_sys == NULL )
104 return VLC_ENOMEM;
107 if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
109 /* IBM HDAPS support */
110 f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
111 if( f )
113 i_x = i_y = 0;
114 fscanf( f, "(%d,%d)", &i_x, &i_y );
115 fclose( f );
116 p_intf->p_sys->i_calibrate = i_x;
117 p_intf->p_sys->sensor = HDAPS_SENSOR;
119 else
121 p_intf->p_sys->sensor = NO_SENSOR;
124 else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
126 /* Apple Motion Sensor support */
127 p_intf->p_sys->sensor = AMS_SENSOR;
129 else if( access( "/sys/devices/applesmc.768/position", R_OK ) == 0 )
131 /* Apple SMC (newer macbooks) */
132 /* Should be factorised with HDAPS */
133 f = fopen( "/sys/devices/applesmc.768/calibrate", "r" );
134 if( f )
136 i_x = i_y = 0;
137 fscanf( f, "(%d,%d)", &i_x, &i_y );
138 fclose( f );
139 p_intf->p_sys->i_calibrate = i_x;
140 p_intf->p_sys->sensor = APPLESMC_SENSOR;
142 else
144 p_intf->p_sys->sensor = NO_SENSOR;
147 #ifdef __APPLE__
148 else if((p_intf->p_sys->unimotion_hw = detect_sms()))
149 p_intf->p_sys->sensor = UNIMOTION_SENSOR;
150 #endif
151 else
153 /* No motion sensor support */
154 p_intf->p_sys->sensor = NO_SENSOR;
157 p_intf->pf_run = RunIntf;
159 p_intf->p_sys->b_use_rotate =
160 var_InheritBool( p_intf, "motion-use-rotate" );
162 return VLC_SUCCESS;
165 /*****************************************************************************
166 * CloseIntf: destroy interface
167 *****************************************************************************/
168 void Close ( vlc_object_t *p_this )
170 intf_thread_t *p_intf = (intf_thread_t *)p_this;
172 free( p_intf->p_sys );
175 /*****************************************************************************
176 * RunIntf: main loop
177 *****************************************************************************/
178 #define FILTER_LENGTH 16
179 #define LOW_THRESHOLD 800
180 #define HIGH_THRESHOLD 1000
181 static void RunIntf( intf_thread_t *p_intf )
183 int i_x, i_oldx = 0, i_sum = 0, i = 0;
184 int p_oldx[FILTER_LENGTH];
185 memset( p_oldx, 0, FILTER_LENGTH * sizeof( int ) );
187 for( ;; )
189 vout_thread_t *p_vout;
190 const char *psz_filter, *psz_type;
191 bool b_change = false;
193 /* Wait a bit, get orientation, change filter if necessary */
194 msleep( INTF_IDLE_SLEEP );
196 int canc = vlc_savecancel();
197 i_x = GetOrientation( p_intf );
198 i_sum += i_x - p_oldx[i];
199 p_oldx[i++] = i_x;
200 if( i == FILTER_LENGTH ) i = 0;
201 i_x = i_sum / FILTER_LENGTH;
203 if( p_intf->p_sys->b_use_rotate )
205 if( i_oldx != i_x )
207 /* TODO: cache object pointer */
208 vlc_object_t *p_obj =
209 vlc_object_find_name( p_intf->p_libvlc, "rotate", FIND_CHILD );
210 if( p_obj )
212 var_SetInteger( p_obj, "rotate-deciangle",
213 ((3600+i_x/2)%3600) );
214 i_oldx = i_x;
215 vlc_object_release( p_obj );
218 goto loop;
221 if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
223 b_change = true;
224 psz_filter = "transform";
225 psz_type = "270";
227 else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
228 || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
230 b_change = true;
231 psz_filter = "";
232 psz_type = "";
234 else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
236 b_change = true;
237 psz_filter = "transform";
238 psz_type = "90";
241 if( b_change )
243 p_vout = (vout_thread_t *)
244 vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
245 if( p_vout )
247 config_PutPsz( p_vout, "transform-type", psz_type );
248 var_SetString( p_vout, "vout-filter", psz_filter );
249 vlc_object_release( p_vout );
251 i_oldx = i_x;
254 loop:
255 vlc_restorecancel( canc );
258 #undef FILTER_LENGTH
259 #undef LOW_THRESHOLD
260 #undef HIGH_THRESHOLD
262 /*****************************************************************************
263 * GetOrientation: get laptop orientation, range -1800 / +1800
264 *****************************************************************************/
265 static int GetOrientation( intf_thread_t *p_intf )
267 FILE *f;
268 int i_x, i_y, i_z = 0;
270 switch( p_intf->p_sys->sensor )
272 case HDAPS_SENSOR:
273 f = fopen( "/sys/devices/platform/hdaps/position", "r" );
274 if( !f )
276 return 0;
279 i_x = i_y = 0;
280 fscanf( f, "(%d,%d)", &i_x, &i_y );
281 fclose( f );
283 return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
285 case AMS_SENSOR:
286 f = fopen( "/sys/devices/ams/x", "r" );
287 if( !f )
289 return 0;
292 fscanf( f, "%d", &i_x);
293 fclose( f );
295 return - i_x * 30; /* FIXME: arbitrary */
297 case APPLESMC_SENSOR:
298 f = fopen( "/sys/devices/applesmc.768/position", "r" );
299 if( !f )
301 return 0;
304 i_x = i_y = i_z = 0;
305 fscanf( f, "(%d,%d,%d)", &i_x, &i_y, &i_z );
306 fclose( f );
308 return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
310 #ifdef __APPLE__
311 case UNIMOTION_SENSOR:
312 if( read_sms_raw( p_intf->p_sys->unimotion_hw, &i_x, &i_y, &i_z ) )
314 double d_norm = sqrt( i_x*i_x+i_z*i_z );
315 if( d_norm < 100 )
316 return 0;
317 double d_x = i_x / d_norm;
318 if( i_z > 0 )
319 return -asin(d_x)*3600/3.141;
320 else
321 return 3600 + asin(d_x)*3600/3.141;
323 else
324 return 0;
325 #endif
326 default:
327 return 0;