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