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