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 *****************************************************************************/
32 #include <vlc_common.h>
35 # include "TargetConditionals.h"
36 # if !TARGET_OS_IPHONE
37 # define HAVE_MACOS_UNIMOTION
41 #ifdef HAVE_MACOS_UNIMOTION
42 # include "unimotion.h"
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
;
61 motion_sensors_t
*motion_create( vlc_object_t
*obj
)
66 motion_sensors_t
*motion
= malloc( sizeof( motion_sensors_t
) );
67 if( unlikely( motion
== 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;
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;
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" );
106 /* No motion sensor support */
107 msg_Err( obj
, "No motion sensor available" );
112 memset( motion
->p_oldx
, 0, sizeof( motion
->p_oldx
) );
118 void motion_destroy( motion_sensors_t
*motion
)
123 /*****************************************************************************
124 * GetOrientation: get laptop orientation, range -1800 / +1800
125 *****************************************************************************/
126 static int GetOrientation( motion_sensors_t
*motion
)
129 int i_x
= 0, i_y
= 0, i_z
= 0;
132 switch( motion
->sensor
)
135 f
= fopen( "/sys/devices/platform/hdaps/position", "re" );
141 i_ret
= fscanf( f
, "(%d,%d)", &i_x
, &i_y
);
147 return ( i_x
- motion
->i_calibrate
) * 10;
150 f
= fopen( "/sys/devices/ams/x", "re" );
156 i_ret
= fscanf( f
, "%d", &i_x
);
162 return - i_x
* 30; /* FIXME: arbitrary */
164 case APPLESMC_SENSOR
:
165 f
= fopen( "/sys/devices/platform/applesmc.768/position", "re" );
171 i_ret
= fscanf( f
, "(%d,%d,%d)", &i_x
, &i_y
, &i_z
);
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
);
186 double d_x
= i_x
/ d_norm
;
188 return -asin(d_x
)*3600/3.141;
190 return 3600 + asin(d_x
)*3600/3.141;
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
;