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