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