/*****************************************************************************
* 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
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <stdlib.h> /* malloc(), free() */
-#include <string.h>
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <assert.h>
+#include <unistd.h>
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
#include <vlc_interface.h>
+#include <vlc_playlist.h>
#include <vlc_vout.h>
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
+#include "motionlib.h"
/*****************************************************************************
* intf_sys_t: description and status of interface
*****************************************************************************/
struct intf_sys_t
{
- enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR } sensor;
-
- int i_calibrate;
-
- vlc_bool_t b_use_rotate;
+ motion_sensors_t *p_motion;
+ vlc_thread_t thread;
};
/*****************************************************************************
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
*****************************************************************************/
-vlc_module_begin();
- set_shortname( _("motion"));
- set_category( CAT_INTERFACE );
- set_description( _("motion control interface") );
+vlc_module_begin ()
+ set_shortname( N_("motion"))
+ set_category( CAT_INTERFACE )
+ set_subcategory( SUBCAT_INTERFACE_CONTROL )
+ set_description( N_("motion control interface") )
+ set_help( N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion sensors " \
+ "to rotate the video") )
- add_bool( "motion-use-rotate", 0, NULL,
- USE_ROTATE_TEXT, USE_ROTATE_TEXT, VLC_FALSE );
+ add_obsolete_bool( "motion-use-rotate" ) /* since 2.1.0 */
- set_capability( "interface", 0 );
- set_callbacks( Open, Close );
-vlc_module_end();
+ set_capability( "interface", 0 )
+ set_callbacks( Open, Close )
+vlc_module_end ()
/*****************************************************************************
* OpenIntf: initialise interface
int Open ( vlc_object_t *p_this )
{
intf_thread_t *p_intf = (intf_thread_t *)p_this;
- FILE *f;
- int i_x, i_y;
-
- 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 )
+ p_sys->p_motion = motion_create( VLC_OBJECT( p_intf ) );
+ if( p_sys->p_motion == NULL )
{
- /* IBM HDAPS support */
- f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
- if( f )
- {
- i_x = i_y = 0;
- fscanf( f, "(%d,%d)", &i_x, &i_y );
- fclose( f );
- p_intf->p_sys->i_calibrate = i_x;
- 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 )
- {
- /* Apple Motion Sensor support */
- p_intf->p_sys->sensor = AMS_SENSOR;
- }
- 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 = config_GetInt( p_intf, "motion-use-rotate" );
-
- if( p_intf->p_sys->b_use_rotate )
+ if( vlc_clone( &p_sys->thread, RunIntf, p_intf, VLC_THREAD_PRIORITY_LOW ) )
{
- var_Create( p_intf->p_libvlc, "rotate_angle", VLC_VAR_INTEGER );
+ motion_destroy( p_sys->p_motion );
+ goto error;
}
return VLC_SUCCESS;
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
*****************************************************************************/
-static void RunIntf( intf_thread_t *p_intf )
+#define LOW_THRESHOLD 800
+#define HIGH_THRESHOLD 1000
+static void *RunIntf( void *data )
{
- int i_x, i_oldx = 0;
+ intf_thread_t *p_intf = data;
+ int i_oldx = 0;
- while( !intf_ShouldDie( p_intf ) )
+ for( ;; )
{
-#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;
+ const char *psz_type;
+ bool b_change = false;
/* Wait a bit, get orientation, change filter if necessary */
+#warning FIXME: check once (or less) per picture, not once per interval
msleep( INTF_IDLE_SLEEP );
- i_x = GetOrientation( p_intf );
-
- 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;
- }
- continue;
- }
+ int canc = vlc_savecancel();
+ int i_x = motion_get_angle( p_intf->p_sys->p_motion );
if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
{
- b_change = VLC_TRUE;
- psz_filter = "transform";
- psz_type = "270";
+ b_change = true;
+ psz_type = "90";
}
else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
|| ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
{
- b_change = VLC_TRUE;
- psz_filter = "";
- psz_type = "";
+ b_change = true;
+ psz_type = NULL;
}
else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
{
- b_change = VLC_TRUE;
- psz_filter = "transform";
- psz_type = "90";
- }
-
- if( !b_change )
- {
- continue;
+ b_change = true;
+ psz_type = "270";
}
- p_vout = (vout_thread_t *)
- vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
- if( !p_vout )
+ if( b_change )
{
- continue;
- }
-
- config_PutPsz( p_vout, "transform-type", psz_type );
- var_SetString( p_vout, "vout-filter", psz_filter );
- vlc_object_release( p_vout );
-
- i_oldx = i_x;
- }
-}
-
-/*****************************************************************************
- * GetOrientation: get laptop orientation, range -180 / +180
- *****************************************************************************/
-static int GetOrientation( intf_thread_t *p_intf )
-{
- FILE *f;
- int i_x, i_y;
+#warning FIXME: refactor this plugin as a video filter!
+ input_thread_t *p_input;
- switch( p_intf->p_sys->sensor )
- {
- case HDAPS_SENSOR:
- f = fopen( "/sys/devices/platform/hdaps/position", "r" );
- if( !f )
- {
- return 0;
- }
-
- i_x = i_y = 0;
- fscanf( f, "(%d,%d)", &i_x, &i_y );
- fclose( f );
-
- return i_x - p_intf->p_sys->i_calibrate;
-
- case AMS_SENSOR:
- f = fopen( "/sys/devices/ams/x", "r" );
- if( !f )
- {
- return 0;
+ p_input = playlist_CurrentInput( pl_Get( p_intf ) );
+ if( p_input )
+ {
+ vout_thread_t *p_vout;
+
+ p_vout = input_GetVout( p_input );
+ if( p_vout )
+ {
+ 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;
+ }
}
- fscanf( f, "%d", &i_x);
- fclose( f );
-
- return - i_x * 3; /* FIXME: arbitrary */
-
- default:
- return 0;
+ vlc_restorecancel( canc );
}
+ assert(0);
}
-
+#undef LOW_THRESHOLD
+#undef HIGH_THRESHOLD