1 /*****************************************************************************
2 * motion.c: laptop built-in motion sensors
3 *****************************************************************************
4 * Copyright (C) 2006 - 2012 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 *****************************************************************************/
33 #include <vlc_common.h>
36 # include "TargetConditionals.h"
37 # if !TARGET_OS_IPHONE
38 # define HAVE_MACOS_UNIMOTION
42 #ifdef HAVE_MACOS_UNIMOTION
43 # include "unimotion.h"
46 #include "motionlib.h"
48 struct motion_sensors_t
50 enum { HDAPS_SENSOR
, AMS_SENSOR
, APPLESMC_SENSOR
,
51 UNIMOTION_SENSOR
} sensor
;
52 #ifdef HAVE_MACOS_UNIMOTION
53 enum sms_hardware unimotion_hw
;
62 motion_sensors_t
*motion_create( vlc_object_t
*obj
)
67 motion_sensors_t
*motion
= malloc( sizeof( motion_sensors_t
) );
68 if( unlikely( motion
== NULL
) )
73 if( access( "/sys/devices/platform/hdaps/position", R_OK
) == 0
74 && ( f
= fopen( "/sys/devices/platform/hdaps/calibrate", "re" ) ) )
76 /* IBM HDAPS support */
77 motion
->i_calibrate
= fscanf( f
, "(%d,%d)", &i_x
, &i_y
) == 2 ? i_x
: 0;
79 motion
->sensor
= HDAPS_SENSOR
;
80 msg_Dbg( obj
, "HDAPS motion detection correctly loaded" );
82 else if( access( "/sys/devices/ams/x", R_OK
) == 0 )
84 /* Apple Motion Sensor support */
85 motion
->sensor
= AMS_SENSOR
;
86 msg_Dbg( obj
, "AMS motion detection correctly loaded" );
88 else if( access( "/sys/devices/platform/applesmc.768/position", R_OK
) == 0
89 && ( f
= fopen( "/sys/devices/platform/applesmc.768/calibrate", "re" ) ) )
91 /* Apple SMC (newer macbooks) */
92 /* Should be factorised with HDAPS */
93 motion
->i_calibrate
= fscanf( f
, "(%d,%d)", &i_x
, &i_y
) == 2 ? i_x
: 0;
95 motion
->sensor
= APPLESMC_SENSOR
;
96 msg_Dbg( obj
, "Apple SMC motion detection correctly loaded" );
98 #ifdef HAVE_MACOS_UNIMOTION
99 else if( (motion
->unimotion_hw
= detect_sms()) )
101 motion
->sensor
= UNIMOTION_SENSOR
;
102 msg_Dbg( obj
, "UniMotion motion detection correctly loaded" );
107 /* No motion sensor support */
108 msg_Err( obj
, "No motion sensor available" );
113 memset( motion
->p_oldx
, 0, sizeof( motion
->p_oldx
) );
119 void motion_destroy( motion_sensors_t
*motion
)
124 /*****************************************************************************
125 * GetOrientation: get laptop orientation, range -1800 / +1800
126 *****************************************************************************/
127 static int GetOrientation( motion_sensors_t
*motion
)
130 int i_x
= 0, i_y
= 0, i_z
= 0;
133 switch( motion
->sensor
)
136 f
= fopen( "/sys/devices/platform/hdaps/position", "re" );
142 i_ret
= fscanf( f
, "(%d,%d)", &i_x
, &i_y
);
148 return ( i_x
- motion
->i_calibrate
) * 10;
151 f
= fopen( "/sys/devices/ams/x", "re" );
157 i_ret
= fscanf( f
, "%d", &i_x
);
163 return - i_x
* 30; /* FIXME: arbitrary */
165 case APPLESMC_SENSOR
:
166 f
= fopen( "/sys/devices/platform/applesmc.768/position", "re" );
172 i_ret
= fscanf( f
, "(%d,%d,%d)", &i_x
, &i_y
, &i_z
);
178 return ( i_x
- motion
->i_calibrate
) * 10;
180 #ifdef HAVE_MACOS_UNIMOTION
181 case UNIMOTION_SENSOR
:
182 if( read_sms_raw( motion
->unimotion_hw
, &i_x
, &i_y
, &i_z
) )
184 double d_norm
= sqrt( i_x
*i_x
+i_z
*i_z
);
187 double d_x
= i_x
/ d_norm
;
189 return -asin(d_x
)*3600/3.141;
191 return 3600 + asin(d_x
)*3600/3.141;
201 /*****************************************************************************
202 * motion_get_angle: get averaged laptop orientation, range -1800 / +1800
203 *****************************************************************************/
204 int motion_get_angle( motion_sensors_t
*motion
)
206 const int filter_length
= ARRAY_SIZE( motion
->p_oldx
);
207 int i_x
= GetOrientation( motion
);
209 motion
->i_sum
+= i_x
- motion
->p_oldx
[motion
->i
];
210 motion
->p_oldx
[motion
->i
] = i_x
;
211 motion
->i
= ( motion
->i
+ 1 ) % filter_length
;
212 i_x
= motion
->i_sum
/ filter_length
;