input: add input_SetProgramId
[vlc.git] / modules / control / motionlib.c
blob9937cae1a896bbcb3142107740cc1bd2f33ffe85
1 /*****************************************************************************
2 * motion.c: laptop built-in motion sensors
3 *****************************************************************************
4 * Copyright (C) 2006 - 2012 the VideoLAN team
6 * Author: Sam Hocevar <sam@zoy.org>
7 * Jérôme Decoodt <djc@videolan.org> (unimotion integration)
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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
29 #include <math.h>
30 #include <unistd.h>
32 #include <vlc_common.h>
34 #ifdef __APPLE__
35 # include "TargetConditionals.h"
36 # if !TARGET_OS_IPHONE
37 # define HAVE_MACOS_UNIMOTION
38 # endif
39 #endif
41 #ifdef HAVE_MACOS_UNIMOTION
42 # include "unimotion.h"
43 #endif
45 #include "motionlib.h"
47 struct motion_sensors_t
49 enum { HDAPS_SENSOR, AMS_SENSOR, APPLESMC_SENSOR,
50 UNIMOTION_SENSOR } sensor;
51 #ifdef HAVE_MACOS_UNIMOTION
52 enum sms_hardware unimotion_hw;
53 #endif
54 int i_calibrate;
56 int p_oldx[16];
57 int i;
58 int i_sum;
61 motion_sensors_t *motion_create( vlc_object_t *obj )
63 FILE *f;
64 int i_x = 0, i_y = 0;
66 motion_sensors_t *motion = malloc( sizeof( motion_sensors_t ) );
67 if( unlikely( motion == NULL ) )
69 return NULL;
72 if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0
73 && ( f = fopen( "/sys/devices/platform/hdaps/calibrate", "re" ) ) )
75 /* IBM HDAPS support */
76 motion->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
77 fclose( f );
78 motion->sensor = HDAPS_SENSOR;
79 msg_Dbg( obj, "HDAPS motion detection correctly loaded" );
81 else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
83 /* Apple Motion Sensor support */
84 motion->sensor = AMS_SENSOR;
85 msg_Dbg( obj, "AMS motion detection correctly loaded" );
87 else if( access( "/sys/devices/platform/applesmc.768/position", R_OK ) == 0
88 && ( f = fopen( "/sys/devices/platform/applesmc.768/calibrate", "re" ) ) )
90 /* Apple SMC (newer macbooks) */
91 /* Should be factorised with HDAPS */
92 motion->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
93 fclose( f );
94 motion->sensor = APPLESMC_SENSOR;
95 msg_Dbg( obj, "Apple SMC motion detection correctly loaded" );
97 #ifdef HAVE_MACOS_UNIMOTION
98 else if( (motion->unimotion_hw = detect_sms()) )
100 motion->sensor = UNIMOTION_SENSOR;
101 msg_Dbg( obj, "UniMotion motion detection correctly loaded" );
103 #endif
104 else
106 /* No motion sensor support */
107 msg_Err( obj, "No motion sensor available" );
108 free( motion );
109 return NULL;
112 memset( motion->p_oldx, 0, sizeof( motion->p_oldx ) );
113 motion->i = 0;
114 motion->i_sum = 0;
115 return motion;
118 void motion_destroy( motion_sensors_t *motion )
120 free( motion );
123 /*****************************************************************************
124 * GetOrientation: get laptop orientation, range -1800 / +1800
125 *****************************************************************************/
126 static int GetOrientation( motion_sensors_t *motion )
128 FILE *f;
129 int i_x = 0, i_y = 0, i_z = 0;
130 int i_ret;
132 switch( motion->sensor )
134 case HDAPS_SENSOR:
135 f = fopen( "/sys/devices/platform/hdaps/position", "re" );
136 if( !f )
138 return 0;
141 i_ret = fscanf( f, "(%d,%d)", &i_x, &i_y );
142 fclose( f );
144 if( i_ret < 2 )
145 return 0;
146 else
147 return ( i_x - motion->i_calibrate ) * 10;
149 case AMS_SENSOR:
150 f = fopen( "/sys/devices/ams/x", "re" );
151 if( !f )
153 return 0;
156 i_ret = fscanf( f, "%d", &i_x);
157 fclose( f );
159 if( i_ret < 1 )
160 return 0;
161 else
162 return - i_x * 30; /* FIXME: arbitrary */
164 case APPLESMC_SENSOR:
165 f = fopen( "/sys/devices/platform/applesmc.768/position", "re" );
166 if( !f )
168 return 0;
171 i_ret = fscanf( f, "(%d,%d,%d)", &i_x, &i_y, &i_z );
172 fclose( f );
174 if( i_ret < 3 )
175 return 0;
176 else
177 return ( i_x - motion->i_calibrate ) * 10;
179 #ifdef HAVE_MACOS_UNIMOTION
180 case UNIMOTION_SENSOR:
181 if( read_sms_raw( motion->unimotion_hw, &i_x, &i_y, &i_z ) )
183 double d_norm = sqrt( i_x*i_x+i_z*i_z );
184 if( d_norm < 100 )
185 return 0;
186 double d_x = i_x / d_norm;
187 if( i_z > 0 )
188 return -asin(d_x)*3600/3.141;
189 else
190 return 3600 + asin(d_x)*3600/3.141;
192 else
193 return 0;
194 #endif
195 default:
196 vlc_assert_unreachable();
200 /*****************************************************************************
201 * motion_get_angle: get averaged laptop orientation, range -1800 / +1800
202 *****************************************************************************/
203 int motion_get_angle( motion_sensors_t *motion )
205 const int filter_length = ARRAY_SIZE( motion->p_oldx );
206 int i_x = GetOrientation( motion );
208 motion->i_sum += i_x - motion->p_oldx[motion->i];
209 motion->p_oldx[motion->i] = i_x;
210 motion->i = ( motion->i + 1 ) % filter_length;
211 i_x = motion->i_sum / filter_length;
213 return i_x;