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