]> git.sesse.net Git - vlc/blobdiff - modules/control/motion.c
Use module_* functions
[vlc] / modules / control / motion.c
index 98f53edcfe0c604a5edd6779cf02f62fc117b2ab..dc75305202e1a1fa59a66e12f6c3c683ce46c2eb 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
  * motion.c: control VLC with laptop built-in motion sensors
  *****************************************************************************
- * Copyright (C) 2006 the VideoLAN team
+ * Copyright (C) 2006 - 2007 the VideoLAN team
  * $Id$
  *
  * Author: Sam Hocevar <sam@zoy.org>
+ *         Jérôme Decoodt <djc@videolan.org> (unimotion integration)
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -26,6 +27,7 @@
  *****************************************************************************/
 #include <stdlib.h>                                      /* malloc(), free() */
 #include <string.h>
+#include <math.h>
 
 #include <vlc/vlc.h>
 #include <vlc_interface.h>
 #    include <unistd.h>
 #endif
 
+#ifdef __APPLE__
+#include "unimotion.h"
+#endif
+
 /*****************************************************************************
  * intf_sys_t: description and status of interface
  *****************************************************************************/
 struct intf_sys_t
 {
-    enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR } sensor;
-
+    enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR, UNIMOTION_SENSOR } sensor;
+#ifdef __APPLE__
+    enum sms_hardware unimotion_hw;
+#endif
     int i_calibrate;
 
     vlc_bool_t b_use_rotate;
@@ -110,6 +118,10 @@ int Open ( vlc_object_t *p_this )
         /* Apple Motion Sensor support */
         p_intf->p_sys->sensor = AMS_SENSOR;
     }
+#ifdef __APPLE__
+    else if( p_intf->p_sys->unimotion_hw = detect_sms() )
+        p_intf->p_sys->sensor = UNIMOTION_SENSOR;
+#endif
     else
     {
         /* No motion sensor support */
@@ -120,11 +132,6 @@ int Open ( vlc_object_t *p_this )
 
     p_intf->p_sys->b_use_rotate = config_GetInt( p_intf, "motion-use-rotate" );
 
-    if( p_intf->p_sys->b_use_rotate )
-    {
-        var_Create( p_intf->p_libvlc, "rotate_angle", VLC_VAR_INTEGER );
-    }
-
     return VLC_SUCCESS;
 }
 
@@ -141,14 +148,17 @@ void Close ( vlc_object_t *p_this )
 /*****************************************************************************
  * RunIntf: main loop
  *****************************************************************************/
+#define FILTER_LENGTH 16
+#define LOW_THRESHOLD 800
+#define HIGH_THRESHOLD 1000
 static void RunIntf( intf_thread_t *p_intf )
 {
-    int i_x, i_oldx = 0;
+    int i_x, i_oldx = 0, i_sum = 0, i = 0;
+    int p_oldx[FILTER_LENGTH];
+    memset( p_oldx, 0, FILTER_LENGTH * sizeof( int ) );
 
     while( !intf_ShouldDie( p_intf ) )
     {
-#define LOW_THRESHOLD 80
-#define HIGH_THRESHOLD 100
         vout_thread_t *p_vout;
         const char *psz_filter, *psz_type;
         vlc_bool_t b_change = VLC_FALSE;
@@ -157,14 +167,25 @@ static void RunIntf( intf_thread_t *p_intf )
         msleep( INTF_IDLE_SLEEP );
 
         i_x = GetOrientation( p_intf );
+        i_sum += i_x - p_oldx[i];
+        p_oldx[i++] = i_x;
+        if( i == FILTER_LENGTH ) i = 0;
+        i_x = i_sum / FILTER_LENGTH;
 
         if( p_intf->p_sys->b_use_rotate )
         {
             if( i_oldx != i_x )
             {
-                var_SetInteger( p_intf->p_libvlc, "rotate_angle",
-                            ((360+i_x/2)%360) );
-                i_oldx = i_x;
+                /* TODO: cache object pointer */
+                vlc_object_t *p_obj =
+                vlc_object_find_name( p_intf->p_libvlc, "rotate", FIND_CHILD );
+                if( p_obj )
+                {
+                    var_SetInteger( p_obj, "rotate-deciangle",
+                            ((3600+i_x/2)%3600) );
+                    i_oldx = i_x;
+                    vlc_object_release( p_obj );
+                }
             }
             continue;
         }
@@ -208,15 +229,20 @@ static void RunIntf( intf_thread_t *p_intf )
         i_oldx = i_x;
     }
 }
+#undef FILTER_LENGTH
+#undef LOW_THRESHOLD
+#undef HIGH_THRESHOLD
 
 /*****************************************************************************
- * GetOrientation: get laptop orientation, range -180 / +180
+ * GetOrientation: get laptop orientation, range -1800 / +1800
  *****************************************************************************/
 static int GetOrientation( intf_thread_t *p_intf )
 {
     FILE *f;
     int i_x, i_y;
-
+#ifdef __APPLE__
+    int i_z;
+#endif
     switch( p_intf->p_sys->sensor )
     {
     case HDAPS_SENSOR:
@@ -230,7 +256,7 @@ static int GetOrientation( intf_thread_t *p_intf )
         fscanf( f, "(%d,%d)", &i_x, &i_y );
         fclose( f );
 
-        return i_x - p_intf->p_sys->i_calibrate;
+        return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
 
     case AMS_SENSOR:
         f = fopen( "/sys/devices/ams/x", "r" );
@@ -242,8 +268,23 @@ static int GetOrientation( intf_thread_t *p_intf )
         fscanf( f, "%d", &i_x);
         fclose( f );
 
-        return - i_x * 3; /* FIXME: arbitrary */
-
+        return - i_x * 30; /* FIXME: arbitrary */
+#ifdef __APPLE__
+    case UNIMOTION_SENSOR:
+        if( read_sms_raw( p_intf->p_sys->unimotion_hw, &i_x, &i_y, &i_z ) )
+        {
+            double d_norm = sqrt( i_x*i_x+i_z*i_z );
+            if( d_norm < 100 )
+                return 0;
+            double d_x = i_x / d_norm;
+            if( i_z > 0 )
+                return -asin(d_x)*3600/3.141;
+            else
+                return 3600 + asin(d_x)*3600/3.141;
+        }
+        else
+            return 0;
+#endif
     default:
         return 0;
     }