1 /*****************************************************************************
2 * motion.c: control VLC with laptop built-in motion sensors
3 *****************************************************************************
4 * Copyright (C) 2006 - 2007 the VideoLAN team
7 * Author: Sam Hocevar <sam@zoy.org>
8 * Jérôme Decoodt <djc@videolan.org> (unimotion integration)
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_interface.h>
45 # include "TargetConditionals.h"
46 # if !TARGET_OS_IPHONE
47 # define HAVE_MACOS_UNIMOTION
51 #ifdef HAVE_MACOS_UNIMOTION
52 # include "unimotion.h"
55 /*****************************************************************************
56 * intf_sys_t: description and status of interface
57 *****************************************************************************/
60 enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR, APPLESMC_SENSOR,
61 UNIMOTION_SENSOR } sensor;
62 #ifdef HAVE_MACOS_UNIMOTION
63 enum sms_hardware unimotion_hw;
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Close ( vlc_object_t * );
76 static void RunIntf( intf_thread_t *p_intf );
77 static int GetOrientation( intf_thread_t *p_intf );
79 #define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")
81 /*****************************************************************************
83 *****************************************************************************/
85 set_shortname( N_("motion"))
86 set_category( CAT_INTERFACE )
87 set_subcategory( SUBCAT_INTERFACE_CONTROL )
88 set_description( N_("motion control interface") )
89 set_help( N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion sensors " \
90 "to rotate the video") )
92 add_bool( "motion-use-rotate", false,
93 USE_ROTATE_TEXT, USE_ROTATE_TEXT, false )
95 set_capability( "interface", 0 )
96 set_callbacks( Open, Close )
99 /*****************************************************************************
100 * OpenIntf: initialise interface
101 *****************************************************************************/
102 int Open ( vlc_object_t *p_this )
104 intf_thread_t *p_intf = (intf_thread_t *)p_this;
106 int i_x = 0, i_y = 0;
108 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
109 if( p_intf->p_sys == NULL )
114 if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
116 /* IBM HDAPS support */
117 f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
120 p_intf->p_sys->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
122 p_intf->p_sys->sensor = HDAPS_SENSOR;
126 p_intf->p_sys->sensor = NO_SENSOR;
129 else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
131 /* Apple Motion Sensor support */
132 p_intf->p_sys->sensor = AMS_SENSOR;
134 else if( access( "/sys/devices/applesmc.768/position", R_OK ) == 0 )
136 /* Apple SMC (newer macbooks) */
137 /* Should be factorised with HDAPS */
138 f = fopen( "/sys/devices/applesmc.768/calibrate", "r" );
141 p_intf->p_sys->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
143 p_intf->p_sys->sensor = APPLESMC_SENSOR;
147 p_intf->p_sys->sensor = NO_SENSOR;
150 #ifdef HAVE_MACOS_UNIMOTION
151 else if( (p_intf->p_sys->unimotion_hw = detect_sms()) )
152 p_intf->p_sys->sensor = UNIMOTION_SENSOR;
156 /* No motion sensor support */
157 p_intf->p_sys->sensor = NO_SENSOR;
160 p_intf->pf_run = RunIntf;
162 p_intf->p_sys->b_use_rotate = var_InheritBool( p_intf, "motion-use-rotate" );
164 msg_Dbg( p_intf, "Motion detection correctly loaded" );
168 /*****************************************************************************
169 * CloseIntf: destroy interface
170 *****************************************************************************/
171 void Close ( vlc_object_t *p_this )
173 intf_thread_t *p_intf = (intf_thread_t *)p_this;
175 free( p_intf->p_sys );
178 /*****************************************************************************
180 *****************************************************************************/
181 #define FILTER_LENGTH 16
182 #define LOW_THRESHOLD 800
183 #define HIGH_THRESHOLD 1000
184 static void RunIntf( intf_thread_t *p_intf )
186 int i_x, i_oldx = 0, i_sum = 0, i = 0;
187 int p_oldx[FILTER_LENGTH];
188 memset( p_oldx, 0, FILTER_LENGTH * sizeof( int ) );
192 vout_thread_t *p_vout;
193 const char *psz_filter, *psz_type;
194 bool b_change = false;
196 /* Wait a bit, get orientation, change filter if necessary */
197 msleep( INTF_IDLE_SLEEP );
199 int canc = vlc_savecancel();
200 i_x = GetOrientation( p_intf );
201 i_sum += i_x - p_oldx[i];
203 if( i == FILTER_LENGTH ) i = 0;
204 i_x = i_sum / FILTER_LENGTH;
206 if( p_intf->p_sys->b_use_rotate )
210 /* TODO: cache object pointer */
211 vlc_object_t *p_obj =
212 vlc_object_find_name( p_intf->p_libvlc, "rotate", FIND_CHILD );
215 var_SetInteger( p_obj, "rotate-deciangle",
216 ((3600+i_x/2)%3600) );
218 vlc_object_release( p_obj );
224 if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
227 psz_filter = "transform";
230 else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
231 || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
237 else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
240 psz_filter = "transform";
246 p_vout = (vout_thread_t *)
247 vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
250 config_PutPsz( p_vout, "transform-type", psz_type );
251 var_SetString( p_vout, "video-filter", psz_filter );
252 vlc_object_release( p_vout );
258 vlc_restorecancel( canc );
263 #undef HIGH_THRESHOLD
265 /*****************************************************************************
266 * GetOrientation: get laptop orientation, range -1800 / +1800
267 *****************************************************************************/
268 static int GetOrientation( intf_thread_t *p_intf )
271 int i_x = 0, i_y = 0, i_z = 0;
274 switch( p_intf->p_sys->sensor )
277 f = fopen( "/sys/devices/platform/hdaps/position", "r" );
283 i_ret = fscanf( f, "(%d,%d)", &i_x, &i_y );
289 return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
292 f = fopen( "/sys/devices/ams/x", "r" );
298 i_ret = fscanf( f, "%d", &i_x);
304 return - i_x * 30; /* FIXME: arbitrary */
306 case APPLESMC_SENSOR:
307 f = fopen( "/sys/devices/applesmc.768/position", "r" );
313 i_ret = fscanf( f, "(%d,%d,%d)", &i_x, &i_y, &i_z );
319 return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
321 #ifdef HAVE_MACOS_UNIMOTION
322 case UNIMOTION_SENSOR:
323 if( read_sms_raw( p_intf->p_sys->unimotion_hw, &i_x, &i_y, &i_z ) )
325 double d_norm = sqrt( i_x*i_x+i_z*i_z );
328 double d_x = i_x / d_norm;
330 return -asin(d_x)*3600/3.141;
332 return 3600 + asin(d_x)*3600/3.141;