Commit 2879411e authored by Sam Hocevar's avatar Sam Hocevar

* modules/control/joystick.c:

    + Compilation and coding style fixes.
    + Initialise the joystick in Open(), not Run().
parent 4f6cc614
......@@ -58,10 +58,10 @@
/* Default Actions (used if there are missing actions in the default
* Available actions are: Next,Prev, Forward,Back,Play,Fullscreen,dummy */
#define AXE_0_UP_ACTION Forward
#define AXE_0_DOWN_ACTION Back
#define AXE_1_UP_ACTION Next
#define AXE_1_DOWN_ACTION Prev
#define AXIS_0_UP_ACTION Forward
#define AXIS_0_DOWN_ACTION Back
#define AXIS_1_UP_ACTION Next
#define AXIS_1_DOWN_ACTION Prev
#define BUTTON_1_PRESS_ACTION Play
#define BUTTON_1_RELEASE_ACTION dummy
......@@ -93,7 +93,6 @@ struct joy_button_t
struct intf_sys_t
{
fd_set fds; /* File descriptor set (select) */
int i_fd; /* File descriptor for joystick */
struct timeval timeout; /* Select timeout */
int i_threshold; /* motion threshold */
......@@ -159,9 +158,9 @@ static void Run ( intf_thread_t *p_intf );
#define MAP_LONGTEXT N_( "Allows you to remap the actions." )
vlc_module_begin();
add_integer( "motion-threshold", DEFAULT_THRESHOLD , NULL,
add_integer( "motion-threshold", DEFAULT_THRESHOLD, NULL,
THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
add_string( "joystick-device", DEFAULT_DEVICE , NULL,
add_string( "joystick-device", DEFAULT_DEVICE, NULL,
DEVICE_TEXT, DEVICE_LONGTEXT, VLC_TRUE );
add_integer ("joystick-repeat", DEFAULT_REPEAT,NULL,
REPEAT_TEXT, REPEAT_LONGTEXT, VLC_TRUE );
......@@ -188,12 +187,21 @@ static int Open ( vlc_object_t *p_this )
if( p_intf->p_sys == NULL )
{
return 1 ;
return VLC_ENOMEM;
}
if( Init( p_intf ) < 0 )
{
msg_Err( p_intf, "cannot initialize interface" );
free( p_intf->p_sys );
return VLC_EGENERIC;
}
msg_Dbg( p_intf, "interface initialized" );
p_intf->pf_run = Run;
return 0 ;
return VLC_SUCCESS;
}
/*****************************************************************************
......@@ -204,8 +212,10 @@ static void Close ( vlc_object_t *p_this )
intf_thread_t *p_intf = (intf_thread_t *)p_this;
/* Destroy structure */
if(p_intf->p_sys)
if( p_intf->p_sys )
{
free( p_intf->p_sys );
}
}
......@@ -216,33 +226,25 @@ static void Run( intf_thread_t *p_intf )
{
int i_sel_res = 0;
int i_read = 0;
int i_axe = 0;
int i_axis = 0;
struct js_event event;
if( Init( p_intf ) < 0 )
{
msg_Err( p_intf, "can't initialize intf" );
return;
}
msg_Dbg( p_intf, "intf initialized" );
/* Main loop */
while( !p_intf->b_die )
{
fd_set fds;
vlc_mutex_lock( &p_intf->change_lock );
FD_ZERO( &p_intf->p_sys->fds );
FD_SET( p_intf->p_sys->i_fd , &p_intf->p_sys->fds );
FD_ZERO( &fds );
FD_SET( p_intf->p_sys->i_fd, &fds );
p_intf->p_sys->timeout.tv_sec = 0;
p_intf->p_sys->timeout.tv_usec = p_intf->p_sys->i_repeat;
i_sel_res = select ( p_intf->p_sys->i_fd+1,
&p_intf->p_sys->fds,
NULL,
NULL,
&p_intf->p_sys->timeout );
i_sel_res = select( p_intf->p_sys->i_fd + 1, &fds,
NULL, NULL, &p_intf->p_sys->timeout );
p_intf->p_sys->p_input = (input_thread_t *)
vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
......@@ -251,33 +253,33 @@ static void Run( intf_thread_t *p_intf )
{
msg_Err( p_intf, "select error: %s",strerror(errno) );
}
else if(i_sel_res > 0 && FD_ISSET( p_intf->p_sys->i_fd,
&p_intf->p_sys->fds))
{ /* We got an event */
else if(i_sel_res > 0 && FD_ISSET( p_intf->p_sys->i_fd, &fds))
{
/* We got an event */
memset(&event,0,sizeof(struct js_event));
i_read = read( p_intf->p_sys->i_fd, &event ,
i_read = read( p_intf->p_sys->i_fd, &event,
sizeof(struct js_event));
handle_event( p_intf , event ) ;
handle_event( p_intf, event ) ;
}
else if(i_sel_res == 0)
/*We have no event, but check if we have an action to repeat */
{
for(i_axe=0;i_axe<=1;i_axe++)
/*We have no event, but check if we have an action to repeat */
for(i_axis = 0; i_axis <= 1; i_axis++)
{
if( p_intf->p_sys->axes[i_axe].b_trigered &&
mdate()-p_intf->p_sys->axes[i_axe].l_time >
if( p_intf->p_sys->axes[i_axis].b_trigered &&
mdate()-p_intf->p_sys->axes[i_axis].l_time >
p_intf->p_sys->i_wait &&
p_intf->p_sys->axes[i_axe].i_value > 0 )
p_intf->p_sys->axes[i_axis].i_value > 0 )
{
p_intf->p_sys->axes[i_axe].pf_actup(p_intf);
p_intf->p_sys->axes[i_axis].pf_actup(p_intf);
}
if( p_intf->p_sys->axes[i_axe].b_trigered &&
mdate()-p_intf->p_sys->axes[i_axe].l_time >
if( p_intf->p_sys->axes[i_axis].b_trigered &&
mdate()-p_intf->p_sys->axes[i_axis].l_time >
p_intf->p_sys->i_wait &&
p_intf->p_sys->axes[i_axe].i_value < 0 )
p_intf->p_sys->axes[i_axis].i_value < 0 )
{
p_intf->p_sys->axes[i_axe].pf_actdown(p_intf);
p_intf->p_sys->axes[i_axis].pf_actdown(p_intf);
}
}
}
......@@ -298,58 +300,49 @@ static int Init( intf_thread_t * p_intf )
char *psz_parse;
char *psz_eof; /* end of field */
if( !p_intf->b_die )
{
vlc_mutex_lock( &p_intf->change_lock );
psz_device = config_GetPsz( p_intf, "joystick-device");
psz_device=config_GetPsz( p_intf, "joystick-device");
if(!psz_device) /* strange... */
if( !psz_device ) /* strange... */
{
psz_device = strdup( DEFAULT_DEVICE );
}
p_intf->p_sys->i_fd = open ( psz_device , O_RDONLY|O_NONBLOCK );
p_intf->p_sys->i_fd = open( psz_device, O_RDONLY|O_NONBLOCK );
if( p_intf->p_sys->i_fd == -1 )
{
msg_Warn( p_intf, "unable to open %s for reading: %s"
,psz_device,strerror(errno));
return -1;
msg_Warn( p_intf, "unable to open %s for reading: %s",
psz_device, strerror(errno) );
return VLC_EGENERIC;
}
p_intf->p_sys->i_repeat = 1000*
config_GetInt( p_intf, "joystick-repeat");
p_intf->p_sys->i_repeat = 1000 * config_GetInt( p_intf, "joystick-repeat");
p_intf->p_sys->i_wait = 1000 * config_GetInt( p_intf, "joystick-wait");
p_intf->p_sys->i_wait = 1000*
config_GetInt( p_intf, "joystick-wait");
p_intf->p_sys->i_threshold =
config_GetInt( p_intf, "motion-threshold" );
if(p_intf->p_sys->i_threshold > 32767 ||
p_intf->p_sys->i_threshold < 0 )
p_intf->p_sys->i_threshold = config_GetInt( p_intf, "motion-threshold" );
if(p_intf->p_sys->i_threshold > 32767 || p_intf->p_sys->i_threshold < 0 )
p_intf->p_sys->i_threshold = DEFAULT_THRESHOLD;
p_intf->p_sys->i_maxseek =
config_GetInt( p_intf, "joystick-max-seek" );
p_intf->p_sys->i_maxseek = config_GetInt( p_intf, "joystick-max-seek" );
psz_parse = config_GetPsz( p_intf, "joystick-mapping" ) ;
if ( ! psz_parse)
if( ! psz_parse)
{
msg_Warn (p_intf,"invalid mapping. aborting." );
return -1;
msg_Warn (p_intf,"invalid mapping. aborting" );
return VLC_EGENERIC;
}
if( !strlen( psz_parse ) )
{
msg_Warn( p_intf, "invalid mapping, aborting." );
return -1;
msg_Warn( p_intf, "invalid mapping, aborting" );
return VLC_EGENERIC;
}
p_intf->p_sys->axes[0].pf_actup = AXE_0_UP_ACTION;
p_intf->p_sys->axes[0].pf_actdown = AXE_0_DOWN_ACTION;
p_intf->p_sys->axes[1].pf_actup = AXE_1_UP_ACTION;
p_intf->p_sys->axes[1].pf_actdown = AXE_1_DOWN_ACTION;
p_intf->p_sys->axes[0].pf_actup = AXIS_0_UP_ACTION;
p_intf->p_sys->axes[0].pf_actdown = AXIS_0_DOWN_ACTION;
p_intf->p_sys->axes[1].pf_actup = AXIS_1_UP_ACTION;
p_intf->p_sys->axes[1].pf_actdown = AXIS_1_DOWN_ACTION;
p_intf->p_sys->buttons[0].pf_actdown = BUTTON_1_PRESS_ACTION;
p_intf->p_sys->buttons[0].pf_actup = BUTTON_1_RELEASE_ACTION;
......@@ -358,10 +351,10 @@ static int Init( intf_thread_t * p_intf )
/* Macro to parse the command line */
#define PARSE(name,function) \
if(!strncmp( psz_parse , name , strlen( name ) ) ) \
if(!strncmp( psz_parse, name, strlen( name ) ) ) \
{ \
psz_parse += strlen( name ); \
psz_eof = strchr( psz_parse , ',' ); \
psz_eof = strchr( psz_parse, ',' ); \
if( !psz_eof) \
psz_eof = strchr( psz_parse, '}' ); \
if( !psz_eof) \
......@@ -371,33 +364,28 @@ static int Init( intf_thread_t * p_intf )
*psz_eof = '\0' ; \
} \
msg_Dbg(p_intf,"%s -> %s", name,psz_parse) ; \
if(!strcasecmp( psz_parse , "play" ) ) function = Play; \
if(!strcasecmp( psz_parse , "next" ) ) function = Next; \
if(!strcasecmp( psz_parse , "prev" ) ) function = Prev; \
if(!strcasecmp( psz_parse , "fullscreen" ) ) function = Fullscreen; \
if(!strcasecmp( psz_parse , "forward" ) ) function = Forward; \
if(!strcasecmp( psz_parse , "back" ) ) function = Back; \
if(!strcasecmp( psz_parse, "play" ) ) function = Play; \
if(!strcasecmp( psz_parse, "next" ) ) function = Next; \
if(!strcasecmp( psz_parse, "prev" ) ) function = Prev; \
if(!strcasecmp( psz_parse, "fullscreen" ) ) function = Fullscreen; \
if(!strcasecmp( psz_parse, "forward" ) ) function = Forward; \
if(!strcasecmp( psz_parse, "back" ) ) function = Back; \
psz_parse = psz_eof; \
psz_parse ++; \
continue; \
} \
while(1)
for( ; *psz_parse ; psz_parse++ )
{
PARSE("axis-0-up=" ,p_intf->p_sys->axes[0].pf_actup );
PARSE("axis-0-down=" ,p_intf->p_sys->axes[0].pf_actdown );
PARSE("axis-1-up=" ,p_intf->p_sys->axes[1].pf_actup );
PARSE("axis-1-down=" ,p_intf->p_sys->axes[1].pf_actdown );
PARSE("butt-1-up=" ,p_intf->p_sys->buttons[0].pf_actup );
PARSE("butt-1-down=" ,p_intf->p_sys->buttons[0].pf_actdown );
PARSE("butt-2-up=" ,p_intf->p_sys->buttons[1].pf_actup );
PARSE("butt-2-down=" ,p_intf->p_sys->buttons[1].pf_actdown );
PARSE("axis-0-up=", p_intf->p_sys->axes[0].pf_actup );
PARSE("axis-0-down=", p_intf->p_sys->axes[0].pf_actdown );
PARSE("axis-1-up=", p_intf->p_sys->axes[1].pf_actup );
PARSE("axis-1-down=", p_intf->p_sys->axes[1].pf_actdown );
if( *psz_parse )
psz_parse++;
else
break;
PARSE("butt-1-up=", p_intf->p_sys->buttons[0].pf_actup );
PARSE("butt-1-down=", p_intf->p_sys->buttons[0].pf_actdown );
PARSE("butt-2-up=", p_intf->p_sys->buttons[1].pf_actup );
PARSE("butt-2-down=", p_intf->p_sys->buttons[1].pf_actdown );
}
p_intf->p_sys->axes[0].b_trigered = VLC_FALSE;
......@@ -406,14 +394,7 @@ static int Init( intf_thread_t * p_intf )
p_intf->p_sys->axes[1].b_trigered = VLC_FALSE;
p_intf->p_sys->axes[1].l_time = 0;
vlc_mutex_unlock( &p_intf->change_lock );
return 0;
}
else
{
return -1;
}
return VLC_SUCCESS;
}
/*****************************************************************************
......@@ -421,19 +402,18 @@ static int Init( intf_thread_t * p_intf )
*****************************************************************************/
static int handle_event ( intf_thread_t *p_intf, struct js_event event)
{
unsigned int i_axe;
unsigned int i_axis;
if( event.type == JS_EVENT_AXIS )
{
/* Third axe is supposed to behave in a different way:
* it is a throttle, and will set a value, without
* triggering something */
/* Third axis is supposed to behave in a different way: it is a
* throttle, and will set a value, without triggering anything */
if( event.number == 2 &&
/* Try to avoid Parkinson joysticks */
abs(event.value - p_intf->p_sys->axes[2].i_value) > 200 )
{
p_intf->p_sys->axes[2].i_value = event.value;
msg_Dbg( p_intf , "updating volume" );
msg_Dbg( p_intf, "updating volume" );
/* This way, the volume is between 0 and 1024 */
aout_VolumeSet( p_intf, (32767-event.value)/64 );
return 0;
......@@ -443,66 +423,70 @@ static int handle_event ( intf_thread_t *p_intf, struct js_event event)
p_intf->p_sys->axes[event.number].i_value = event.value;
if( abs(event.value) > p_intf->p_sys->i_threshold &&
p_intf->p_sys->axes[event.number].b_trigered == VLC_FALSE)
p_intf->p_sys->axes[event.number].b_trigered == VLC_FALSE )
{
/* The axis entered the trigger zone. Start the event */
p_intf->p_sys->axes[event.number].b_trigered = VLC_TRUE;
p_intf->p_sys->axes[event.number].b_dowork = VLC_TRUE;
p_intf->p_sys->axes[event.number].l_time = mdate();
}
else if(abs(event.value) > p_intf->p_sys->i_threshold &&
p_intf->p_sys->axes[event.number].b_trigered == VLC_TRUE)
else if( abs(event.value) > p_intf->p_sys->i_threshold &&
p_intf->p_sys->axes[event.number].b_trigered == VLC_TRUE )
{
/* The axis moved but remained in the trigger zone
* Do nothing at this time */
}
else if ( abs(event.value) < p_intf->p_sys->i_threshold )
else if( abs(event.value) < p_intf->p_sys->i_threshold )
{
/* The axis is not in the trigger zone */
p_intf->p_sys->axes[event.number].b_trigered = VLC_FALSE;
}
/* Special for seeking */
p_intf->p_sys->f_seconds = 1+
(abs(event.value)-p_intf->p_sys->i_threshold)*
(p_intf->p_sys->i_maxseek - 1 )/
(32767-p_intf->p_sys->i_threshold);
p_intf->p_sys->f_seconds = 1 +
(abs(event.value) - p_intf->p_sys->i_threshold) *
(p_intf->p_sys->i_maxseek - 1 ) /
(32767 - p_intf->p_sys->i_threshold);
/* Handle the first two axes. */
for(i_axe = 0; i_axe <= 1 ; i_axe ++)
for(i_axis = 0; i_axis <= 1; i_axis ++)
{
if(p_intf->p_sys->axes[i_axe].b_dowork == VLC_TRUE)
if(p_intf->p_sys->axes[i_axis].b_dowork == VLC_TRUE)
{
if( p_intf->p_sys->axes[i_axe].i_value
if( p_intf->p_sys->axes[i_axis].i_value
> p_intf->p_sys->i_threshold )
{
msg_Dbg(p_intf,"up for axis %i\n",i_axe);
p_intf->p_sys->axes[i_axe].pf_actup(p_intf);
msg_Dbg(p_intf,"up for axis %i\n",i_axis);
p_intf->p_sys->axes[i_axis].pf_actup(p_intf);
}
else if( p_intf->p_sys->axes[i_axe].i_value
else if( p_intf->p_sys->axes[i_axis].i_value
< -p_intf->p_sys->i_threshold )
{
msg_Dbg(p_intf,"down for axis %i\n",i_axe);
p_intf->p_sys->axes[i_axe].pf_actdown(p_intf);
msg_Dbg(p_intf,"down for axis %i\n",i_axis);
p_intf->p_sys->axes[i_axis].pf_actdown(p_intf);
}
}
}
}
else if( event.type == JS_EVENT_BUTTON)
else if( event.type == JS_EVENT_BUTTON )
{
msg_Dbg(p_intf,"button %i %s",event.number,
event.value ? "pressed":"released");
if(event.number >1) return 0; /* Only trigger 2 buttons */
if(event.value == 1) /* Button pressed */
msg_Dbg( p_intf, "button %i %s", event.number,
event.value ? "pressed" : "released" );
if( event.number > 1 )
return 0; /* Only trigger 2 buttons */
if( event.value == 1 ) /* Button pressed */
{
if(p_intf->p_sys->buttons[event.number].pf_actdown)
p_intf->p_sys->buttons[event.number].pf_actdown(p_intf);
if( p_intf->p_sys->buttons[event.number].pf_actdown )
p_intf->p_sys->buttons[event.number].pf_actdown( p_intf );
}
else /* Button released */
if(p_intf->p_sys->buttons[event.number].pf_actup)
p_intf->p_sys->buttons[event.number].pf_actup(p_intf);
{
if( p_intf->p_sys->buttons[event.number].pf_actup )
p_intf->p_sys->buttons[event.number].pf_actup( p_intf );
}
}
return 0;
}
......@@ -512,86 +496,95 @@ static int handle_event ( intf_thread_t *p_intf, struct js_event event)
****************************************************************************/
/* Go to next item in the playlist */
static int Next( intf_thread_t *p_intf)
static int Next( intf_thread_t *p_intf )
{
playlist_t *p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
FIND_ANYWHERE );
playlist_t *p_playlist;
p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
if( p_playlist == NULL )
{
return -1;
return VLC_ENOOBJ;
}
playlist_Next( p_playlist );
vlc_object_release( p_playlist );
return 0;
return VLC_SUCCESS;
}
/* Go to previous item in the playlist */
static int Prev( intf_thread_t *p_intf)
static int Prev( intf_thread_t *p_intf )
{
playlist_t *p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
FIND_ANYWHERE );
playlist_t *p_playlist;
p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
if( p_playlist == NULL )
{
return -1;
return VLC_ENOOBJ;
}
playlist_Prev( p_playlist );
vlc_object_release( p_playlist );
return 0;
return VLC_SUCCESS;
}
/* Seek forward */
static int Forward(intf_thread_t *p_intf)
static int Forward( intf_thread_t *p_intf )
{
if(p_intf->p_sys->p_input)
if( p_intf->p_sys->p_input )
{
msg_Dbg(p_intf,"seeking %f seconds",p_intf->p_sys->f_seconds);
msg_Dbg( p_intf,"seeking %f seconds",p_intf->p_sys->f_seconds );
var_SetTime( p_intf->p_sys->p_input, "time-offset",
(int64_t)p_intf->p_sys->f_seconds * I64C(1000000) );
return 0;
return VLC_SUCCESS;
}
return -1;
return VLC_ENOOBJ;
}
/* Seek backwards */
static int Back(intf_thread_t *p_intf)
static int Back( intf_thread_t *p_intf )
{
if(p_intf->p_sys->p_input)
if( p_intf->p_sys->p_input )
{
msg_Dbg(p_intf,"seeking -%f seconds",p_intf->p_sys->f_seconds);
msg_Dbg( p_intf,"seeking -%f seconds", p_intf->p_sys->f_seconds );
var_SetTime( p_intf->p_sys->p_input, "time-offset",
-(int64_t)p_intf->p_sys->f_seconds * I64C(1000000) );
return 0;
return VLC_SUCCESS;
}
return -1;
return VLC_ENOOBJ;
}
/* Toggle Play/Pause */
static int Play(intf_thread_t *p_intf)
static int Play( intf_thread_t *p_intf )
{
if(p_intf->p_sys->p_input)
if( p_intf->p_sys->p_input )
{
var_SetInteger( p_input, "state", PAUSE_S );
return 0;
var_SetInteger( p_intf->p_sys->p_input, "state", PAUSE_S );
return VLC_SUCCESS;
}
return -1;
return VLC_ENOOBJ;
}
/* Toggle fullscreen mode */
static int Fullscreen(intf_thread_t *p_intf)
static int Fullscreen( intf_thread_t *p_intf )
{
vout_thread_t * p_vout=vlc_object_find(p_intf,
VLC_OBJECT_VOUT, FIND_ANYWHERE );
if(p_vout)
vout_thread_t * p_vout;
p_vout = vlc_object_find(p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
if( p_vout )
{
p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
vlc_object_release(p_vout);
}
return 0;
return VLC_SUCCESS;
}
/* dummy event. Use it if you don't wan't anything to happen */
static int dummy(intf_thread_t *p_intf)
static int dummy( intf_thread_t *p_intf )
{
return 0;
return VLC_SUCCESS;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment