1 /*****************************************************************************
2 * motion.c: control VLC with laptop built-in motion sensors
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Author: Sam Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc_interface.h>
38 /*****************************************************************************
39 * intf_sys_t: description and status of interface
40 *****************************************************************************/
43 enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR } sensor;
47 vlc_bool_t b_use_rotate;
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Open ( vlc_object_t * );
54 static void Close ( vlc_object_t * );
56 static void RunIntf( intf_thread_t *p_intf );
57 static int GetOrientation( intf_thread_t *p_intf );
59 #define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")
61 /*****************************************************************************
63 *****************************************************************************/
65 set_shortname( _("motion"));
66 set_category( CAT_INTERFACE );
67 set_description( _("motion control interface") );
69 add_bool( "motion-use-rotate", 0, NULL,
70 USE_ROTATE_TEXT, USE_ROTATE_TEXT, VLC_FALSE );
72 set_capability( "interface", 0 );
73 set_callbacks( Open, Close );
76 /*****************************************************************************
77 * OpenIntf: initialise interface
78 *****************************************************************************/
79 int Open ( vlc_object_t *p_this )
81 intf_thread_t *p_intf = (intf_thread_t *)p_this;
85 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
86 if( p_intf->p_sys == NULL )
91 if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
93 /* IBM HDAPS support */
94 f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
98 fscanf( f, "(%d,%d)", &i_x, &i_y );
100 p_intf->p_sys->i_calibrate = i_x;
101 p_intf->p_sys->sensor = HDAPS_SENSOR;
105 p_intf->p_sys->sensor = NO_SENSOR;
108 else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
110 /* Apple Motion Sensor support */
111 p_intf->p_sys->sensor = AMS_SENSOR;
115 /* No motion sensor support */
116 p_intf->p_sys->sensor = NO_SENSOR;
119 p_intf->pf_run = RunIntf;
121 p_intf->p_sys->b_use_rotate = config_GetInt( p_intf, "motion-use-rotate" );
123 if( p_intf->p_sys->b_use_rotate )
125 var_Create( p_intf->p_libvlc, "rotate_angle", VLC_VAR_INTEGER );
131 /*****************************************************************************
132 * CloseIntf: destroy interface
133 *****************************************************************************/
134 void Close ( vlc_object_t *p_this )
136 intf_thread_t *p_intf = (intf_thread_t *)p_this;
138 free( p_intf->p_sys );
141 /*****************************************************************************
143 *****************************************************************************/
144 static void RunIntf( intf_thread_t *p_intf )
148 while( !intf_ShouldDie( p_intf ) )
150 #define LOW_THRESHOLD 80
151 #define HIGH_THRESHOLD 100
152 vout_thread_t *p_vout;
153 const char *psz_filter, *psz_type;
154 vlc_bool_t b_change = VLC_FALSE;
156 /* Wait a bit, get orientation, change filter if necessary */
157 msleep( INTF_IDLE_SLEEP );
159 i_x = GetOrientation( p_intf );
161 if( p_intf->p_sys->b_use_rotate )
165 var_SetInteger( p_intf->p_libvlc, "rotate_angle",
172 if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
175 psz_filter = "transform";
178 else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
179 || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
185 else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
188 psz_filter = "transform";
197 p_vout = (vout_thread_t *)
198 vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
204 config_PutPsz( p_vout, "transform-type", psz_type );
205 var_SetString( p_vout, "vout-filter", psz_filter );
206 vlc_object_release( p_vout );
212 /*****************************************************************************
213 * GetOrientation: get laptop orientation, range -180 / +180
214 *****************************************************************************/
215 static int GetOrientation( intf_thread_t *p_intf )
220 switch( p_intf->p_sys->sensor )
223 f = fopen( "/sys/devices/platform/hdaps/position", "r" );
230 fscanf( f, "(%d,%d)", &i_x, &i_y );
233 return i_x - p_intf->p_sys->i_calibrate;
236 f = fopen( "/sys/devices/ams/x", "r" );
242 fscanf( f, "%d", &i_x);
245 return - i_x * 3; /* FIXME: arbitrary */