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" );
126 /*****************************************************************************
127 * CloseIntf: destroy interface
128 *****************************************************************************/
129 void Close ( vlc_object_t *p_this )
131 intf_thread_t *p_intf = (intf_thread_t *)p_this;
133 free( p_intf->p_sys );
136 /*****************************************************************************
138 *****************************************************************************/
139 static void RunIntf( intf_thread_t *p_intf )
143 while( !intf_ShouldDie( p_intf ) )
145 #define LOW_THRESHOLD 80
146 #define HIGH_THRESHOLD 100
147 vout_thread_t *p_vout;
148 const char *psz_filter, *psz_type;
149 vlc_bool_t b_change = VLC_FALSE;
151 /* Wait a bit, get orientation, change filter if necessary */
152 msleep( INTF_IDLE_SLEEP );
154 i_x = GetOrientation( p_intf );
156 if( p_intf->p_sys->b_use_rotate )
160 /* TODO: cache object pointer */
161 vlc_object_t *p_obj =
162 vlc_object_find_name( p_intf->p_libvlc, "rotate", FIND_CHILD );
165 var_SetInteger( p_obj, "rotate-angle",((360+i_x/2)%360) );
167 vlc_object_release( p_obj );
173 if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
176 psz_filter = "transform";
179 else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
180 || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
186 else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
189 psz_filter = "transform";
198 p_vout = (vout_thread_t *)
199 vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
205 config_PutPsz( p_vout, "transform-type", psz_type );
206 var_SetString( p_vout, "vout-filter", psz_filter );
207 vlc_object_release( p_vout );
213 /*****************************************************************************
214 * GetOrientation: get laptop orientation, range -180 / +180
215 *****************************************************************************/
216 static int GetOrientation( intf_thread_t *p_intf )
221 switch( p_intf->p_sys->sensor )
224 f = fopen( "/sys/devices/platform/hdaps/position", "r" );
231 fscanf( f, "(%d,%d)", &i_x, &i_y );
234 return i_x - p_intf->p_sys->i_calibrate;
237 f = fopen( "/sys/devices/ams/x", "r" );
243 fscanf( f, "%d", &i_x);
246 return - i_x * 3; /* FIXME: arbitrary */