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