]> git.sesse.net Git - vlc/blob - modules/control/motion.c
fixed compilation on debian etch.
[vlc] / modules / control / motion.c
1 /*****************************************************************************
2  * motion.c: control VLC with laptop built-in motion sensors
3  *****************************************************************************
4  * Copyright (C) 2006 - 2007 the VideoLAN team
5  * $Id$
6  *
7  * Author: Sam Hocevar <sam@zoy.org>
8  *         Jérôme Decoodt <djc@videolan.org> (unimotion integration)
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <math.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_interface.h>
38 #include <vlc_vout.h>
39
40 #ifdef HAVE_UNISTD_H
41 #    include <unistd.h>
42 #endif
43
44 #ifdef __APPLE__
45 #include "unimotion.h"
46 #endif
47
48 /*****************************************************************************
49  * intf_sys_t: description and status of interface
50  *****************************************************************************/
51 struct intf_sys_t
52 {
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;
59
60     bool b_use_rotate;
61 };
62
63 /*****************************************************************************
64  * Local prototypes.
65  *****************************************************************************/
66 static int  Open   ( vlc_object_t * );
67 static void Close  ( vlc_object_t * );
68
69 static void RunIntf( intf_thread_t *p_intf );
70 static int GetOrientation( intf_thread_t *p_intf );
71
72 #define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")
73
74 /*****************************************************************************
75  * Module descriptor
76  *****************************************************************************/
77 vlc_module_begin();
78     set_shortname( N_("motion"));
79     set_category( CAT_INTERFACE );
80     set_description( N_("motion control interface") );
81     set_help( N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion sensors " \
82                  "to rotate the video") )
83
84     add_bool( "motion-use-rotate", 0, NULL,
85               USE_ROTATE_TEXT, USE_ROTATE_TEXT, false );
86
87     set_capability( "interface", 0 );
88     set_callbacks( Open, Close );
89 vlc_module_end();
90
91 /*****************************************************************************
92  * OpenIntf: initialise interface
93  *****************************************************************************/
94 int Open ( vlc_object_t *p_this )
95 {
96     intf_thread_t *p_intf = (intf_thread_t *)p_this;
97     FILE *f;
98     int i_x, i_y;
99
100     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
101     if( p_intf->p_sys == NULL )
102     {
103         return VLC_ENOMEM;
104     }
105
106     if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
107     {
108         /* IBM HDAPS support */
109         f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
110         if( f )
111         {
112             i_x = i_y = 0;
113             fscanf( f, "(%d,%d)", &i_x, &i_y );
114             fclose( f );
115             p_intf->p_sys->i_calibrate = i_x;
116             p_intf->p_sys->sensor = HDAPS_SENSOR;
117         }
118         else
119         {
120             p_intf->p_sys->sensor = NO_SENSOR;
121         }
122     }
123     else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
124     {
125         /* Apple Motion Sensor support */
126         p_intf->p_sys->sensor = AMS_SENSOR;
127     }
128     else if( access( "/sys/devices/applesmc.768/position", R_OK ) == 0 )
129     {
130         /* Apple SMC (newer macbooks) */
131         /* Should be factorised with HDAPS */
132         f = fopen( "/sys/devices/applesmc.768/calibrate", "r" );
133         if( f )
134         {
135             i_x = i_y = 0;
136             fscanf( f, "(%d,%d)", &i_x, &i_y );
137             fclose( f );
138             p_intf->p_sys->i_calibrate = i_x;
139             p_intf->p_sys->sensor = APPLESMC_SENSOR;
140         }
141         else
142         {
143             p_intf->p_sys->sensor = NO_SENSOR;
144         }
145     }
146 #ifdef __APPLE__
147     else if( p_intf->p_sys->unimotion_hw = detect_sms() )
148         p_intf->p_sys->sensor = UNIMOTION_SENSOR;
149 #endif
150     else
151     {
152         /* No motion sensor support */
153         p_intf->p_sys->sensor = NO_SENSOR;
154     }
155
156     p_intf->pf_run = RunIntf;
157
158     p_intf->p_sys->b_use_rotate = config_GetInt( p_intf, "motion-use-rotate" );
159
160     return VLC_SUCCESS;
161 }
162
163 /*****************************************************************************
164  * CloseIntf: destroy interface
165  *****************************************************************************/
166 void Close ( vlc_object_t *p_this )
167 {
168     intf_thread_t *p_intf = (intf_thread_t *)p_this;
169
170     free( p_intf->p_sys );
171 }
172
173 /*****************************************************************************
174  * RunIntf: main loop
175  *****************************************************************************/
176 #define FILTER_LENGTH 16
177 #define LOW_THRESHOLD 800
178 #define HIGH_THRESHOLD 1000
179 static void RunIntf( intf_thread_t *p_intf )
180 {
181     int i_x, i_oldx = 0, i_sum = 0, i = 0;
182     int p_oldx[FILTER_LENGTH];
183     memset( p_oldx, 0, FILTER_LENGTH * sizeof( int ) );
184
185     while( !intf_ShouldDie( p_intf ) )
186     {
187         vout_thread_t *p_vout;
188         const char *psz_filter, *psz_type;
189         bool b_change = false;
190
191         /* Wait a bit, get orientation, change filter if necessary */
192         msleep( INTF_IDLE_SLEEP );
193
194         i_x = GetOrientation( p_intf );
195         i_sum += i_x - p_oldx[i];
196         p_oldx[i++] = i_x;
197         if( i == FILTER_LENGTH ) i = 0;
198         i_x = i_sum / FILTER_LENGTH;
199
200         if( p_intf->p_sys->b_use_rotate )
201         {
202             if( i_oldx != i_x )
203             {
204                 /* TODO: cache object pointer */
205                 vlc_object_t *p_obj =
206                 vlc_object_find_name( p_intf->p_libvlc, "rotate", FIND_CHILD );
207                 if( p_obj )
208                 {
209                     var_SetInteger( p_obj, "rotate-deciangle",
210                             ((3600+i_x/2)%3600) );
211                     i_oldx = i_x;
212                     vlc_object_release( p_obj );
213                 }
214             }
215             continue;
216         }
217
218         if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
219         {
220             b_change = true;
221             psz_filter = "transform";
222             psz_type = "270";
223         }
224         else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
225                  || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
226         {
227             b_change = true;
228             psz_filter = "";
229             psz_type = "";
230         }
231         else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
232         {
233             b_change = true;
234             psz_filter = "transform";
235             psz_type = "90";
236         }
237
238         if( !b_change )
239         {
240             continue;
241         }
242
243         p_vout = (vout_thread_t *)
244             vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
245         if( !p_vout )
246         {
247             continue;
248         }
249
250         config_PutPsz( p_vout, "transform-type", psz_type );
251         var_SetString( p_vout, "vout-filter", psz_filter );
252         vlc_object_release( p_vout );
253
254         i_oldx = i_x;
255     }
256 }
257 #undef FILTER_LENGTH
258 #undef LOW_THRESHOLD
259 #undef HIGH_THRESHOLD
260
261 /*****************************************************************************
262  * GetOrientation: get laptop orientation, range -1800 / +1800
263  *****************************************************************************/
264 static int GetOrientation( intf_thread_t *p_intf )
265 {
266     FILE *f;
267     int i_x, i_y, i_z = 0;
268
269     switch( p_intf->p_sys->sensor )
270     {
271     case HDAPS_SENSOR:
272         f = fopen( "/sys/devices/platform/hdaps/position", "r" );
273         if( !f )
274         {
275             return 0;
276         }
277
278         i_x = i_y = 0;
279         fscanf( f, "(%d,%d)", &i_x, &i_y );
280         fclose( f );
281
282         return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
283
284     case AMS_SENSOR:
285         f = fopen( "/sys/devices/ams/x", "r" );
286         if( !f )
287         {
288             return 0;
289         }
290
291         fscanf( f, "%d", &i_x);
292         fclose( f );
293
294         return - i_x * 30; /* FIXME: arbitrary */
295
296     case APPLESMC_SENSOR:
297         f = fopen( "/sys/devices/applesmc.768/position", "r" );
298         if( !f )
299         {
300             return 0;
301         }
302
303         i_x = i_y = i_z = 0;
304         fscanf( f, "(%d,%d,%d)", &i_x, &i_y, &i_z );
305         fclose( f );
306
307         return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
308
309 #ifdef __APPLE__
310     case UNIMOTION_SENSOR:
311         if( read_sms_raw( p_intf->p_sys->unimotion_hw, &i_x, &i_y, &i_z ) )
312         {
313             double d_norm = sqrt( i_x*i_x+i_z*i_z );
314             if( d_norm < 100 )
315                 return 0;
316             double d_x = i_x / d_norm;
317             if( i_z > 0 )
318                 return -asin(d_x)*3600/3.141;
319             else
320                 return 3600 + asin(d_x)*3600/3.141;
321         }
322         else
323             return 0;
324 #endif
325     default:
326         return 0;
327     }
328 }
329