]> git.sesse.net Git - vlc/blob - modules/control/motion.c
Motion: fix the vlc_object_find bug and flag the other bugs
[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_playlist.h>
39 #include <vlc_vout.h>
40
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44
45 #ifdef __APPLE__
46 # include "TargetConditionals.h"
47 # if !TARGET_OS_IPHONE
48 #  define HAVE_MACOS_UNIMOTION
49 # endif
50 #endif
51
52 #ifdef HAVE_MACOS_UNIMOTION
53 # include "unimotion.h"
54 #endif
55
56 /*****************************************************************************
57  * intf_sys_t: description and status of interface
58  *****************************************************************************/
59 struct intf_sys_t
60 {
61     enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR, APPLESMC_SENSOR,
62            UNIMOTION_SENSOR } sensor;
63 #ifdef HAVE_MACOS_UNIMOTION
64     enum sms_hardware unimotion_hw;
65 #endif
66     int i_calibrate;
67
68     bool b_use_rotate;
69 };
70
71 /*****************************************************************************
72  * Local prototypes.
73  *****************************************************************************/
74 static int  Open   ( vlc_object_t * );
75 static void Close  ( vlc_object_t * );
76
77 static void RunIntf( intf_thread_t *p_intf );
78 static int GetOrientation( intf_thread_t *p_intf );
79
80 #define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 vlc_module_begin ()
86     set_shortname( N_("motion"))
87     set_category( CAT_INTERFACE )
88     set_subcategory( SUBCAT_INTERFACE_CONTROL )
89     set_description( N_("motion control interface") )
90     set_help( N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion sensors " \
91                  "to rotate the video") )
92
93     add_bool( "motion-use-rotate", false,
94               USE_ROTATE_TEXT, USE_ROTATE_TEXT, false )
95
96     set_capability( "interface", 0 )
97     set_callbacks( Open, Close )
98 vlc_module_end ()
99
100 /*****************************************************************************
101  * OpenIntf: initialise interface
102  *****************************************************************************/
103 int Open ( vlc_object_t *p_this )
104 {
105     intf_thread_t *p_intf = (intf_thread_t *)p_this;
106     FILE *f;
107     int i_x = 0, i_y = 0;
108
109     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
110     if( p_intf->p_sys == NULL )
111     {
112         return VLC_ENOMEM;
113     }
114
115     if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
116     {
117         /* IBM HDAPS support */
118         f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
119         if( f )
120         {
121             p_intf->p_sys->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
122             fclose( f );
123             p_intf->p_sys->sensor = HDAPS_SENSOR;
124         }
125         else
126         {
127             p_intf->p_sys->sensor = NO_SENSOR;
128         }
129     }
130     else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
131     {
132         /* Apple Motion Sensor support */
133         p_intf->p_sys->sensor = AMS_SENSOR;
134     }
135     else if( access( "/sys/devices/applesmc.768/position", R_OK ) == 0 )
136     {
137         /* Apple SMC (newer macbooks) */
138         /* Should be factorised with HDAPS */
139         f = fopen( "/sys/devices/applesmc.768/calibrate", "r" );
140         if( f )
141         {
142             p_intf->p_sys->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
143             fclose( f );
144             p_intf->p_sys->sensor = APPLESMC_SENSOR;
145         }
146         else
147         {
148             p_intf->p_sys->sensor = NO_SENSOR;
149         }
150     }
151 #ifdef HAVE_MACOS_UNIMOTION
152     else if( (p_intf->p_sys->unimotion_hw = detect_sms()) )
153         p_intf->p_sys->sensor = UNIMOTION_SENSOR;
154 #endif
155     else
156     {
157         /* No motion sensor support */
158         p_intf->p_sys->sensor = NO_SENSOR;
159     }
160
161     p_intf->pf_run = RunIntf;
162
163     p_intf->p_sys->b_use_rotate = var_InheritBool( p_intf, "motion-use-rotate" );
164
165     msg_Dbg( p_intf, "Motion detection correctly loaded" );
166     return VLC_SUCCESS;
167 }
168
169 /*****************************************************************************
170  * CloseIntf: destroy interface
171  *****************************************************************************/
172 void Close ( vlc_object_t *p_this )
173 {
174     intf_thread_t *p_intf = (intf_thread_t *)p_this;
175
176     free( p_intf->p_sys );
177 }
178
179 /*****************************************************************************
180  * RunIntf: main loop
181  *****************************************************************************/
182 #define FILTER_LENGTH 16
183 #define LOW_THRESHOLD 800
184 #define HIGH_THRESHOLD 1000
185 static void RunIntf( intf_thread_t *p_intf )
186 {
187     int i_x, i_oldx = 0, i_sum = 0, i = 0;
188     int p_oldx[FILTER_LENGTH];
189     memset( p_oldx, 0, FILTER_LENGTH * sizeof( int ) );
190
191     for( ;; )
192     {
193         const char *psz_filter, *psz_type;
194         bool b_change = false;
195
196         /* Wait a bit, get orientation, change filter if necessary */
197 #warning FIXME: check once (or less) per picture, not once per interval
198         msleep( INTF_IDLE_SLEEP );
199
200         int canc = vlc_savecancel();
201         i_x = GetOrientation( p_intf );
202         i_sum += i_x - p_oldx[i];
203         p_oldx[i++] = i_x;
204         if( i == FILTER_LENGTH ) i = 0;
205         i_x = i_sum / FILTER_LENGTH;
206
207         if( p_intf->p_sys->b_use_rotate )
208         {
209             if( i_oldx != i_x )
210             {
211                 /* TODO: cache object pointer */
212                 vlc_object_t *p_obj =
213                 vlc_object_find_name( p_intf->p_libvlc, "rotate", FIND_CHILD );
214                 if( p_obj )
215                 {
216                     var_SetInteger( p_obj, "rotate-deciangle",
217                             ((3600+i_x/2)%3600) );
218                     i_oldx = i_x;
219                     vlc_object_release( p_obj );
220                 }
221             }
222             goto loop;
223         }
224
225         if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
226         {
227             b_change = true;
228             psz_filter = "transform";
229             psz_type = "270";
230         }
231         else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
232                  || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
233         {
234             b_change = true;
235             psz_filter = "";
236             psz_type = "";
237         }
238         else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
239         {
240             b_change = true;
241             psz_filter = "transform";
242             psz_type = "90";
243         }
244
245         if( b_change )
246         {
247 #warning FIXME: refactor this plugin as a video filter!
248             input_thread_t *p_input;
249
250             p_input = playlist_CurrentInput( pl_Get( p_intf ) );
251             if( p_input )
252             {
253                 vout_thread_t *p_vout;
254
255                 p_vout = input_GetVout( p_input );
256                 if( p_vout )
257                 {
258 #warning FIXME: do not override the permanent configuration!
259 #warning FIXME: transform-type does not exist anymore
260                     config_PutPsz( p_vout, "transform-type", psz_type );
261                     var_SetString( p_vout, "video-filter", psz_filter );
262                     vlc_object_release( p_vout );
263                 }
264                 vlc_object_release( p_input );
265                 i_oldx = i_x;
266             }
267         }
268 loop:
269         vlc_restorecancel( canc );
270     }
271 }
272 #undef FILTER_LENGTH
273 #undef LOW_THRESHOLD
274 #undef HIGH_THRESHOLD
275
276 /*****************************************************************************
277  * GetOrientation: get laptop orientation, range -1800 / +1800
278  *****************************************************************************/
279 static int GetOrientation( intf_thread_t *p_intf )
280 {
281     FILE *f;
282     int i_x = 0, i_y = 0, i_z = 0;
283     int i_ret;
284
285     switch( p_intf->p_sys->sensor )
286     {
287     case HDAPS_SENSOR:
288         f = fopen( "/sys/devices/platform/hdaps/position", "r" );
289         if( !f )
290         {
291             return 0;
292         }
293
294         i_ret = fscanf( f, "(%d,%d)", &i_x, &i_y );
295         fclose( f );
296
297         if( i_ret < 2 )
298             return 0;
299         else
300             return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
301
302     case AMS_SENSOR:
303         f = fopen( "/sys/devices/ams/x", "r" );
304         if( !f )
305         {
306             return 0;
307         }
308
309         i_ret = fscanf( f, "%d", &i_x);
310         fclose( f );
311
312         if( i_ret < 1 )
313             return 0;
314         else
315             return - i_x * 30; /* FIXME: arbitrary */
316
317     case APPLESMC_SENSOR:
318         f = fopen( "/sys/devices/applesmc.768/position", "r" );
319         if( !f )
320         {
321             return 0;
322         }
323
324         i_ret = fscanf( f, "(%d,%d,%d)", &i_x, &i_y, &i_z );
325         fclose( f );
326
327         if( i_ret < 3 )
328             return 0;
329         else
330             return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
331
332 #ifdef HAVE_MACOS_UNIMOTION
333     case UNIMOTION_SENSOR:
334         if( read_sms_raw( p_intf->p_sys->unimotion_hw, &i_x, &i_y, &i_z ) )
335         {
336             double d_norm = sqrt( i_x*i_x+i_z*i_z );
337             if( d_norm < 100 )
338                 return 0;
339             double d_x = i_x / d_norm;
340             if( i_z > 0 )
341                 return -asin(d_x)*3600/3.141;
342             else
343                 return 3600 + asin(d_x)*3600/3.141;
344         }
345         else
346             return 0;
347 #endif
348     case NO_SENSOR:
349     default:
350         return 0;
351     }
352 }
353