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() */
38 /*****************************************************************************
39 * intf_sys_t: description and status of interface
40 *****************************************************************************/
43 enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR } sensor;
45 int i_last_x, i_calibrate;
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Open ( vlc_object_t * );
53 static void Close ( vlc_object_t * );
55 static void RunIntf( intf_thread_t *p_intf );
56 static int GetOrientation( intf_thread_t *p_intf );
58 /*****************************************************************************
60 *****************************************************************************/
62 set_shortname( _("motion"));
63 set_category( CAT_INTERFACE );
64 set_description( _("motion control interface") );
66 set_capability( "interface", 0 );
67 set_callbacks( Open, Close );
70 /*****************************************************************************
71 * OpenIntf: initialise interface
72 *****************************************************************************/
73 int Open ( vlc_object_t *p_this )
75 intf_thread_t *p_intf = (intf_thread_t *)p_this;
79 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
80 if( p_intf->p_sys == NULL )
85 if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
87 /* IBM HDAPS support */
88 f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
92 fscanf( f, "(%d,%d)", &i_x, &i_y );
94 p_intf->p_sys->i_calibrate = i_x;
95 p_intf->p_sys->sensor = HDAPS_SENSOR;
99 p_intf->p_sys->sensor = NO_SENSOR;
102 else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
104 /* Apple Motion Sensor support */
105 p_intf->p_sys->sensor = AMS_SENSOR;
109 /* No motion sensor support */
110 p_intf->p_sys->sensor = NO_SENSOR;
113 p_intf->pf_run = RunIntf;
118 /*****************************************************************************
119 * CloseIntf: destroy interface
120 *****************************************************************************/
121 void Close ( vlc_object_t *p_this )
123 intf_thread_t *p_intf = (intf_thread_t *)p_this;
125 free( p_intf->p_sys );
128 /*****************************************************************************
130 *****************************************************************************/
131 static void RunIntf( intf_thread_t *p_intf )
135 while( !intf_ShouldDie( p_intf ) )
137 #define LOW_THRESHOLD 80
138 #define HIGH_THRESHOLD 100
139 vout_thread_t *p_vout;
140 char *psz_filter, *psz_type;
141 vlc_bool_t b_change = VLC_FALSE;
143 /* Wait a bit, get orientation, change filter if necessary */
144 msleep( INTF_IDLE_SLEEP );
146 i_x = GetOrientation( p_intf );
148 if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
151 psz_filter = "transform";
154 else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
155 || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
161 else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
164 psz_filter = "transform";
173 p_vout = (vout_thread_t *)
174 vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
180 config_PutPsz( p_vout, "transform-type", psz_type );
181 var_SetString( p_vout, "vout-filter", psz_filter );
182 vlc_object_release( p_vout );
188 /*****************************************************************************
189 * GetOrientation: get laptop orientation, range -180 / +180
190 *****************************************************************************/
191 static int GetOrientation( intf_thread_t *p_intf )
196 switch( p_intf->p_sys->sensor )
199 f = fopen( "/sys/devices/platform/hdaps/position", "r" );
206 fscanf( f, "(%d,%d)", &i_x, &i_y );
209 return i_x - p_intf->p_sys->i_calibrate;
212 f = fopen( "/sys/devices/ams/x", "r" );
218 fscanf( f, "%d", &i_x);
221 return - i_x * 3; /* FIXME: arbitrary */