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