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