]> git.sesse.net Git - vlc/blobdiff - modules/control/motion.c
vlc_plugin: fix non-LGPL plugins meta infos
[vlc] / modules / control / motion.c
index 4ab6739623a7a97983b02003b4425cb07648d4ff..6c2144b5c967ea3f767c4befb1b99ec3d7353865 100644 (file)
 # include "config.h"
 #endif
 
-#include <math.h>
+#include <assert.h>
+#include <unistd.h>
 
+#define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_interface.h>
 #include <vlc_playlist.h>
+#include <vlc_input.h>
 #include <vlc_vout.h>
 
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
-#ifdef __APPLE__
-# include "TargetConditionals.h"
-# if !TARGET_OS_IPHONE
-#  define HAVE_MACOS_UNIMOTION
-# endif
-#endif
-
-#ifdef HAVE_MACOS_UNIMOTION
-# include "unimotion.h"
-#endif
+#include "motionlib.h"
 
 /*****************************************************************************
  * intf_sys_t: description and status of interface
  *****************************************************************************/
 struct intf_sys_t
 {
-    enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR, APPLESMC_SENSOR,
-           UNIMOTION_SENSOR } sensor;
-#ifdef HAVE_MACOS_UNIMOTION
-    enum sms_hardware unimotion_hw;
-#endif
-    int i_calibrate;
-
-    bool b_use_rotate;
+    motion_sensors_t *p_motion;
+    vlc_thread_t thread;
 };
 
 /*****************************************************************************
@@ -74,10 +58,7 @@ struct intf_sys_t
 static int  Open   ( vlc_object_t * );
 static void Close  ( vlc_object_t * );
 
-static void RunIntf( intf_thread_t *p_intf );
-static int GetOrientation( intf_thread_t *p_intf );
-
-#define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")
+static void *RunIntf( void * );
 
 /*****************************************************************************
  * Module descriptor
@@ -90,8 +71,7 @@ vlc_module_begin ()
     set_help( N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion sensors " \
                  "to rotate the video") )
 
-    add_bool( "motion-use-rotate", false,
-              USE_ROTATE_TEXT, USE_ROTATE_TEXT, false )
+    add_obsolete_bool( "motion-use-rotate" ) /* since 2.1.0 */
 
     set_capability( "interface", 0 )
     set_callbacks( Open, Close )
@@ -103,66 +83,26 @@ vlc_module_end ()
 int Open ( vlc_object_t *p_this )
 {
     intf_thread_t *p_intf = (intf_thread_t *)p_this;
-    FILE *f;
-    int i_x = 0, i_y = 0;
-
-    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
-    if( p_intf->p_sys == NULL )
-    {
+    intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
+    if( unlikely(p_sys == NULL) )
         return VLC_ENOMEM;
-    }
 
-    if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
-    {
-        /* IBM HDAPS support */
-        f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
-        if( f )
-        {
-            p_intf->p_sys->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
-            fclose( f );
-            p_intf->p_sys->sensor = HDAPS_SENSOR;
-        }
-        else
-        {
-            p_intf->p_sys->sensor = NO_SENSOR;
-        }
-    }
-    else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
+    p_sys->p_motion = motion_create( VLC_OBJECT( p_intf ) );
+    if( p_sys->p_motion == NULL )
     {
-        /* Apple Motion Sensor support */
-        p_intf->p_sys->sensor = AMS_SENSOR;
-    }
-    else if( access( "/sys/devices/applesmc.768/position", R_OK ) == 0 )
-    {
-        /* Apple SMC (newer macbooks) */
-        /* Should be factorised with HDAPS */
-        f = fopen( "/sys/devices/applesmc.768/calibrate", "r" );
-        if( f )
-        {
-            p_intf->p_sys->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
-            fclose( f );
-            p_intf->p_sys->sensor = APPLESMC_SENSOR;
-        }
-        else
-        {
-            p_intf->p_sys->sensor = NO_SENSOR;
-        }
-    }
-#ifdef HAVE_MACOS_UNIMOTION
-    else if( (p_intf->p_sys->unimotion_hw = detect_sms()) )
-        p_intf->p_sys->sensor = UNIMOTION_SENSOR;
-#endif
-    else
-    {
-        /* No motion sensor support */
-        p_intf->p_sys->sensor = NO_SENSOR;
+error:
+        free( p_sys );
+        return VLC_EGENERIC;
     }
 
-    p_intf->pf_run = RunIntf;
+    p_intf->p_sys = p_sys;
 
-    p_intf->p_sys->b_use_rotate = var_InheritBool( p_intf, "motion-use-rotate" );
+    if( vlc_clone( &p_sys->thread, RunIntf, p_intf, VLC_THREAD_PRIORITY_LOW ) )
+    {
+        motion_destroy( p_sys->p_motion );
+        goto error;
+    }
 
-    msg_Dbg( p_intf, "Motion detection correctly loaded" );
     return VLC_SUCCESS;
 }
 
@@ -172,25 +112,27 @@ int Open ( vlc_object_t *p_this )
 void Close ( vlc_object_t *p_this )
 {
     intf_thread_t *p_intf = (intf_thread_t *)p_this;
+    intf_sys_t *p_sys = p_intf->p_sys;
 
-    free( p_intf->p_sys );
+    vlc_cancel( p_sys->thread );
+    vlc_join( p_sys->thread, NULL );
+    motion_destroy( p_sys->p_motion );
+    free( p_sys );
 }
 
 /*****************************************************************************
  * RunIntf: main loop
  *****************************************************************************/
-#define FILTER_LENGTH 16
 #define LOW_THRESHOLD 800
 #define HIGH_THRESHOLD 1000
-static void RunIntf( intf_thread_t *p_intf )
+static void *RunIntf( void *data )
 {
-    int i_x, i_oldx = 0, i_sum = 0, i = 0;
-    int p_oldx[FILTER_LENGTH];
-    memset( p_oldx, 0, FILTER_LENGTH * sizeof( int ) );
+    intf_thread_t *p_intf = data;
+    int i_oldx = 0;
 
     for( ;; )
     {
-        const char *psz_filter, *psz_type;
+        const char *psz_type;
         bool b_change = false;
 
         /* Wait a bit, get orientation, change filter if necessary */
@@ -198,56 +140,29 @@ static void RunIntf( intf_thread_t *p_intf )
         msleep( INTF_IDLE_SLEEP );
 
         int canc = vlc_savecancel();
-        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 )
-            {
-                /* 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 );
-                }
-            }
-            goto loop;
-        }
+        int i_x = motion_get_angle( p_intf->p_sys->p_motion );
 
         if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
         {
             b_change = true;
-            psz_filter = "transform";
-            psz_type = "270";
+            psz_type = "90";
         }
         else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
                  || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
         {
             b_change = true;
-            psz_filter = "";
-            psz_type = "";
+            psz_type = NULL;
         }
         else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
         {
             b_change = true;
-            psz_filter = "transform";
-            psz_type = "90";
+            psz_type = "270";
         }
 
         if( b_change )
         {
 #warning FIXME: refactor this plugin as a video filter!
-            input_thread_t *p_input;
-
-            p_input = playlist_CurrentInput( pl_Get( p_intf ) );
+            input_thread_t *p_input = pl_CurrentInput( p_intf );
             if( p_input )
             {
                 vout_thread_t *p_vout;
@@ -255,99 +170,26 @@ static void RunIntf( intf_thread_t *p_intf )
                 p_vout = input_GetVout( p_input );
                 if( p_vout )
                 {
-#warning FIXME: do not override the permanent configuration!
-#warning FIXME: transform-type does not exist anymore
-                    config_PutPsz( p_vout, "transform-type", psz_type );
-                    var_SetString( p_vout, "video-filter", psz_filter );
+                    if( psz_type != NULL )
+                    {
+                        var_Create( p_vout, "transform-type", VLC_VAR_STRING );
+                        var_SetString( p_vout, "transform-type", psz_type );
+                    }
+                    else
+                        var_Destroy( p_vout, "transform-type" );
+
+                    var_SetString( p_vout, "video-filter",
+                                   psz_type != NULL ? "transform" : "" );
                     vlc_object_release( p_vout );
                 }
                 vlc_object_release( p_input );
                 i_oldx = i_x;
             }
         }
-loop:
+
         vlc_restorecancel( canc );
     }
+    vlc_assert_unreachable();
 }
-#undef FILTER_LENGTH
 #undef LOW_THRESHOLD
 #undef HIGH_THRESHOLD
-
-/*****************************************************************************
- * GetOrientation: get laptop orientation, range -1800 / +1800
- *****************************************************************************/
-static int GetOrientation( intf_thread_t *p_intf )
-{
-    FILE *f;
-    int i_x = 0, i_y = 0, i_z = 0;
-    int i_ret;
-
-    switch( p_intf->p_sys->sensor )
-    {
-    case HDAPS_SENSOR:
-        f = fopen( "/sys/devices/platform/hdaps/position", "r" );
-        if( !f )
-        {
-            return 0;
-        }
-
-        i_ret = fscanf( f, "(%d,%d)", &i_x, &i_y );
-        fclose( f );
-
-        if( i_ret < 2 )
-            return 0;
-        else
-            return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
-
-    case AMS_SENSOR:
-        f = fopen( "/sys/devices/ams/x", "r" );
-        if( !f )
-        {
-            return 0;
-        }
-
-        i_ret = fscanf( f, "%d", &i_x);
-        fclose( f );
-
-        if( i_ret < 1 )
-            return 0;
-        else
-            return - i_x * 30; /* FIXME: arbitrary */
-
-    case APPLESMC_SENSOR:
-        f = fopen( "/sys/devices/applesmc.768/position", "r" );
-        if( !f )
-        {
-            return 0;
-        }
-
-        i_ret = fscanf( f, "(%d,%d,%d)", &i_x, &i_y, &i_z );
-        fclose( f );
-
-        if( i_ret < 3 )
-            return 0;
-        else
-            return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
-
-#ifdef HAVE_MACOS_UNIMOTION
-    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
-    case NO_SENSOR:
-    default:
-        return 0;
-    }
-}
-