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