1 /*****************************************************************************
2 * joystick.c: control vlc with a joystick
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Clément Stenac <zorglub@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include <sys/types.h>
33 #include <sys/select.h>
43 #include <linux/joystick.h>
45 #include "audio_output.h"
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
49 /* Default values for parameters */
50 #define DEFAULT_MAX_SEEK 10 /* seconds */
51 #define DEFAULT_REPEAT 100
52 #define DEFAULT_WAIT 500
53 #define DEFAULT_DEVICE "/dev/input/js0"
54 #define DEFAULT_THRESHOLD 12000 /* 0 -> 32767 */
56 #define DEFAULT_MAPPING \
57 "{axis-0-up=forward,axis-0-down=back," \
58 "axis-1-up=next,axis-1-down=prev," \
59 "butt-1-down=play,butt-2-down=fullscreen}"
61 /* Default Actions (used if there are missing actions in the default
62 * Available actions are: Next,Prev, Forward,Back,Play,Fullscreen,dummy */
63 #define AXE_0_UP_ACTION Forward
64 #define AXE_0_DOWN_ACTION Back
65 #define AXE_1_UP_ACTION Next
66 #define AXE_1_DOWN_ACTION Prev
68 #define BUTTON_1_PRESS_ACTION Play
69 #define BUTTON_1_RELEASE_ACTION dummy
70 #define BUTTON_2_PRESS_ACTION Fullscreen
71 #define BUTTON_2_RELEASE_ACTION dummy
73 /*****************************************************************************
74 * intf_sys_t: description and status of interface
75 *****************************************************************************/
77 typedef int (*action)(intf_thread_t *p_intf);
81 int b_trigered; /* Are we in the trigger zone ? */
82 int i_value; /* Value of movement */
83 int b_dowork; /* Do we have to do the action ? */
84 action pf_actup; /* Action when axis is up */
85 action pf_actdown; /* Action when axis is down */
86 mtime_t l_time; /* When did the axis enter the trigger
92 action pf_actup; /* What to do when button is released */
93 action pf_actdown;/* What to do when button is pressed */
98 fd_set fds; /* File descriptor set (select) */
99 int i_fd; /* File descriptor for joystick */
100 struct timeval timeout; /* Select timeout */
101 int i_threshold; /* motion threshold */
102 int i_wait; /* How much to wait before repeat */
103 int i_repeat; /* Repeat time */
104 int i_maxseek; /* Maximum seek time */
105 struct joy_axis_t axes[3]; /* Axes descriptor */
106 struct joy_button_t buttons[2]; /* Buttons descriptor */
107 input_thread_t *p_input; /* Input thread (for seeking) */
108 float f_seconds; /* How much to seek */
111 /*****************************************************************************
113 *****************************************************************************/
115 static int Open ( vlc_object_t * );
116 static void Close ( vlc_object_t * );
117 static int Init ( intf_thread_t *p_intf );
119 static int handle_event ( intf_thread_t *p_intf, struct js_event event );
123 static int Next (intf_thread_t *p_intf);
124 static int Prev (intf_thread_t *p_intf);
125 static int Back (intf_thread_t *p_intf);
126 static int Forward (intf_thread_t *p_intf);
127 static int Play (intf_thread_t *p_intf);
128 static int Fullscreen (intf_thread_t *p_intf);
130 static int dummy (intf_thread_t *p_intf);
132 /* Exported functions */
133 static void Run ( intf_thread_t *p_intf );
135 /*****************************************************************************
137 *****************************************************************************/
138 #define THRESHOLD_TEXT N_( "Motion threshold" )
139 #define THRESHOLD_LONGTEXT N_( \
140 "Amount of joystick movement required for a movement to be " \
141 "recorded (0->32767)." )
143 #define DEVICE_TEXT N_( "Joystick device" )
144 #define DEVICE_LONGTEXT N_( \
145 "The joystick device (usually /dev/js0 or /dev/input/js0).")
147 #define REPEAT_TEXT N_( "Repeat time (ms)" )
148 #define REPEAT_LONGTEXT N_( \
149 "Delay waited before the action is repeated if it is still " \
150 "triggered, in milliseconds." )
152 #define WAIT_TEXT N_( "Wait time (ms)")
153 #define WAIT_LONGTEXT N_(\
154 "The time waited before the repeat starts, in milliseconds.")
156 #define SEEK_TEXT N_( "Max seek interval (seconds)")
157 #define SEEK_LONGTEXT N_(\
158 "The maximum number of seconds that will be sought at a time." )
160 #define MAP_TEXT N_( "Action mapping")
161 #define MAP_LONGTEXT N_( "Allows you to remap the actions." )
164 add_integer( "motion-threshold", DEFAULT_THRESHOLD , NULL,
165 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
166 add_string( "joystick-device", DEFAULT_DEVICE , NULL,
167 DEVICE_TEXT, DEVICE_LONGTEXT, VLC_TRUE );
168 add_integer ("joystick-repeat", DEFAULT_REPEAT,NULL,
169 REPEAT_TEXT, REPEAT_LONGTEXT, VLC_TRUE );
170 add_integer ("joystick-wait", DEFAULT_WAIT,NULL,
171 WAIT_TEXT, WAIT_LONGTEXT, VLC_TRUE );
172 add_integer ("joystick-max-seek",DEFAULT_MAX_SEEK,NULL,
173 SEEK_TEXT, SEEK_LONGTEXT, VLC_TRUE );
174 add_string("joystick-mapping",DEFAULT_MAPPING,NULL,
175 MAP_TEXT,MAP_LONGTEXT, VLC_TRUE );
176 set_description( _("Joystick control interface") );
177 set_capability( "interface", 0 );
178 set_callbacks( Open, Close );
181 /*****************************************************************************
182 * Open: initialize interface
183 *****************************************************************************/
184 static int Open ( vlc_object_t *p_this )
186 intf_thread_t *p_intf = (intf_thread_t *)p_this;
188 /* Allocate instance and initialize some members */
189 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
191 if( p_intf->p_sys == NULL )
196 p_intf->pf_run = Run;
201 /*****************************************************************************
202 * Close: destroy the interface
203 *****************************************************************************/
204 static void Close ( vlc_object_t *p_this )
206 intf_thread_t *p_intf = (intf_thread_t *)p_this;
208 /* Destroy structure */
210 free( p_intf->p_sys );
214 /*****************************************************************************
216 *****************************************************************************/
217 static void Run( intf_thread_t *p_intf )
222 struct js_event event;
224 if( Init( p_intf ) < 0 )
226 msg_Err( p_intf, "can't initialize intf" );
229 msg_Dbg( p_intf, "intf initialized" );
232 while( !p_intf->b_die )
234 vlc_mutex_lock( &p_intf->change_lock );
236 FD_ZERO( &p_intf->p_sys->fds );
237 FD_SET( p_intf->p_sys->i_fd , &p_intf->p_sys->fds );
239 p_intf->p_sys->timeout.tv_sec = 0;
240 p_intf->p_sys->timeout.tv_usec = p_intf->p_sys->i_repeat;
243 i_sel_res = select ( p_intf->p_sys->i_fd+1,
247 &p_intf->p_sys->timeout );
249 p_intf->p_sys->p_input = (input_thread_t *)
250 vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
252 if( i_sel_res == -1 && errno != EINTR )
254 msg_Err( p_intf, "select error: %s",strerror(errno) );
256 else if(i_sel_res > 0 && FD_ISSET( p_intf->p_sys->i_fd,
257 &p_intf->p_sys->fds))
258 { /* We got an event */
259 memset(&event,0,sizeof(struct js_event));
260 i_read = read( p_intf->p_sys->i_fd, &event ,
261 sizeof(struct js_event));
262 handle_event( p_intf , event ) ;
264 else if(i_sel_res == 0)
265 /*We have no event, but check if we have an action to repeat */
267 for(i_axe=0;i_axe<=1;i_axe++)
269 if( p_intf->p_sys->axes[i_axe].b_trigered &&
270 mdate()-p_intf->p_sys->axes[i_axe].l_time >
271 p_intf->p_sys->i_wait &&
272 p_intf->p_sys->axes[i_axe].i_value > 0 )
274 p_intf->p_sys->axes[i_axe].pf_actup(p_intf);
277 if( p_intf->p_sys->axes[i_axe].b_trigered &&
278 mdate()-p_intf->p_sys->axes[i_axe].l_time >
279 p_intf->p_sys->i_wait &&
280 p_intf->p_sys->axes[i_axe].i_value < 0 )
282 p_intf->p_sys->axes[i_axe].pf_actdown(p_intf);
287 if(p_intf->p_sys->p_input)
288 vlc_object_release (p_intf->p_sys->p_input);
290 vlc_mutex_unlock ( &p_intf->change_lock );
294 /*****************************************************************************
295 * InitThread: Initialize the interface
296 *****************************************************************************/
297 static int Init( intf_thread_t * p_intf )
301 char *psz_eof; /* end of field */
305 vlc_mutex_lock( &p_intf->change_lock );
307 psz_device=config_GetPsz( p_intf, "joystick-device");
309 if(!psz_device) /* strange... */
310 psz_device = strdup( DEFAULT_DEVICE );
312 p_intf->p_sys->i_fd = open ( psz_device , O_RDONLY|O_NONBLOCK );
314 if( p_intf->p_sys->i_fd == -1 )
316 msg_Warn( p_intf, "unable to open %s for reading: %s"
317 ,psz_device,strerror(errno));
321 p_intf->p_sys->i_repeat = 1000*
322 config_GetInt( p_intf, "joystick-repeat");
324 p_intf->p_sys->i_wait = 1000*
325 config_GetInt( p_intf, "joystick-wait");
327 p_intf->p_sys->i_threshold =
328 config_GetInt( p_intf, "motion-threshold" );
330 if(p_intf->p_sys->i_threshold > 32767 ||
331 p_intf->p_sys->i_threshold < 0 )
332 p_intf->p_sys->i_threshold = DEFAULT_THRESHOLD;
334 p_intf->p_sys->i_maxseek =
335 config_GetInt( p_intf, "joystick-max-seek" );
338 psz_parse = config_GetPsz( p_intf, "joystick-mapping" ) ;
342 msg_Warn (p_intf,"invalid mapping. aborting." );
345 if( !strlen( psz_parse ) )
347 msg_Warn( p_intf, "invalid mapping, aborting." );
351 p_intf->p_sys->axes[0].pf_actup = AXE_0_UP_ACTION;
352 p_intf->p_sys->axes[0].pf_actdown = AXE_0_DOWN_ACTION;
353 p_intf->p_sys->axes[1].pf_actup = AXE_1_UP_ACTION;
354 p_intf->p_sys->axes[1].pf_actdown = AXE_1_DOWN_ACTION;
356 p_intf->p_sys->buttons[0].pf_actdown = BUTTON_1_PRESS_ACTION;
357 p_intf->p_sys->buttons[0].pf_actup = BUTTON_1_RELEASE_ACTION;
358 p_intf->p_sys->buttons[1].pf_actdown = BUTTON_2_PRESS_ACTION;
359 p_intf->p_sys->buttons[1].pf_actup = BUTTON_2_RELEASE_ACTION;
361 /* Macro to parse the command line */
362 #define PARSE(name,function) \
363 if(!strncmp( psz_parse , name , strlen( name ) ) ) \
365 psz_parse += strlen( name ); \
366 psz_eof = strchr( psz_parse , ',' ); \
368 psz_eof = strchr( psz_parse, '}' ); \
370 psz_eof = psz_parse + strlen(psz_parse); \
375 msg_Dbg(p_intf,"%s -> %s", name,psz_parse) ; \
376 if(!strcasecmp( psz_parse , "play" ) ) function = Play; \
377 if(!strcasecmp( psz_parse , "next" ) ) function = Next; \
378 if(!strcasecmp( psz_parse , "prev" ) ) function = Prev; \
379 if(!strcasecmp( psz_parse , "fullscreen" ) ) function = Fullscreen; \
380 if(!strcasecmp( psz_parse , "forward" ) ) function = Forward; \
381 if(!strcasecmp( psz_parse , "back" ) ) function = Back; \
382 psz_parse = psz_eof; \
389 PARSE("axis-0-up=" ,p_intf->p_sys->axes[0].pf_actup );
390 PARSE("axis-0-down=" ,p_intf->p_sys->axes[0].pf_actdown );
391 PARSE("axis-1-up=" ,p_intf->p_sys->axes[1].pf_actup );
392 PARSE("axis-1-down=" ,p_intf->p_sys->axes[1].pf_actdown );
394 PARSE("butt-1-up=" ,p_intf->p_sys->buttons[0].pf_actup );
395 PARSE("butt-1-down=" ,p_intf->p_sys->buttons[0].pf_actdown );
396 PARSE("butt-2-up=" ,p_intf->p_sys->buttons[1].pf_actup );
397 PARSE("butt-2-down=" ,p_intf->p_sys->buttons[1].pf_actdown );
405 p_intf->p_sys->axes[0].b_trigered = VLC_FALSE;
406 p_intf->p_sys->axes[0].l_time = 0;
408 p_intf->p_sys->axes[1].b_trigered = VLC_FALSE;
409 p_intf->p_sys->axes[1].l_time = 0;
411 vlc_mutex_unlock( &p_intf->change_lock );
421 /*****************************************************************************
422 * handle_event : parse a joystick event and takes the appropriate action *
423 *****************************************************************************/
424 static int handle_event ( intf_thread_t *p_intf, struct js_event event)
428 if( event.type == JS_EVENT_AXIS )
430 /* Third axe is supposed to behave in a different way:
431 * it is a throttle, and will set a value, without
432 * triggering something */
433 if( event.number == 2 &&
434 /* Try to avoid Parkinson joysticks */
435 abs(event.value - p_intf->p_sys->axes[2].i_value) > 200 )
437 p_intf->p_sys->axes[2].i_value = event.value;
438 msg_Dbg( p_intf , "updating volume" );
439 /* This way, the volume is between 0 and 1024 */
440 aout_VolumeSet( p_intf, (32767-event.value)/64 );
444 p_intf->p_sys->axes[event.number].b_dowork = VLC_FALSE;
445 p_intf->p_sys->axes[event.number].i_value = event.value;
447 if( abs(event.value) > p_intf->p_sys->i_threshold &&
448 p_intf->p_sys->axes[event.number].b_trigered == VLC_FALSE)
450 /* The axis entered the trigger zone. Start the event */
451 p_intf->p_sys->axes[event.number].b_trigered = VLC_TRUE;
452 p_intf->p_sys->axes[event.number].b_dowork = VLC_TRUE;
453 p_intf->p_sys->axes[event.number].l_time = mdate();
455 else if(abs(event.value) > p_intf->p_sys->i_threshold &&
456 p_intf->p_sys->axes[event.number].b_trigered == VLC_TRUE)
458 /* The axis moved but remained in the trigger zone
459 * Do nothing at this time */
461 else if ( abs(event.value) < p_intf->p_sys->i_threshold )
463 /* The axis is not in the trigger zone */
464 p_intf->p_sys->axes[event.number].b_trigered = VLC_FALSE;
467 /* Special for seeking */
468 p_intf->p_sys->f_seconds = 1+
469 (abs(event.value)-p_intf->p_sys->i_threshold)*
470 (p_intf->p_sys->i_maxseek - 1 )/
471 (32767-p_intf->p_sys->i_threshold);
474 /* Handle the first two axes. */
475 for(i_axe = 0; i_axe <= 1 ; i_axe ++)
477 if(p_intf->p_sys->axes[i_axe].b_dowork == VLC_TRUE)
479 if( p_intf->p_sys->axes[i_axe].i_value
480 > p_intf->p_sys->i_threshold )
482 msg_Dbg(p_intf,"up for axis %i\n",i_axe);
483 p_intf->p_sys->axes[i_axe].pf_actup(p_intf);
485 else if( p_intf->p_sys->axes[i_axe].i_value
486 < -p_intf->p_sys->i_threshold )
488 msg_Dbg(p_intf,"down for axis %i\n",i_axe);
489 p_intf->p_sys->axes[i_axe].pf_actdown(p_intf);
495 else if( event.type == JS_EVENT_BUTTON)
497 msg_Dbg(p_intf,"button %i %s",event.number,
498 event.value ? "pressed":"released");
499 if(event.number >1) return 0; /* Only trigger 2 buttons */
500 if(event.value == 1) /* Button pressed */
502 if(p_intf->p_sys->buttons[event.number].pf_actdown)
503 p_intf->p_sys->buttons[event.number].pf_actdown(p_intf);
505 else /* Button released */
506 if(p_intf->p_sys->buttons[event.number].pf_actup)
507 p_intf->p_sys->buttons[event.number].pf_actup(p_intf);
512 /****************************************************************************
514 ****************************************************************************/
516 /* Go to next item in the playlist */
517 static int Next( intf_thread_t *p_intf)
519 playlist_t *p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
521 if( p_playlist == NULL )
525 playlist_Next( p_playlist );
526 vlc_object_release( p_playlist );
530 /* Go to previous item in the playlist */
531 static int Prev( intf_thread_t *p_intf)
533 playlist_t *p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
535 if( p_playlist == NULL )
539 playlist_Prev( p_playlist );
540 vlc_object_release( p_playlist );
545 static int Forward(intf_thread_t *p_intf)
547 if(p_intf->p_sys->p_input)
549 msg_Dbg(p_intf,"seeking %f seconds",p_intf->p_sys->f_seconds);
550 var_SetTime( p_intf->p_sys->p_input, "time-offset",
551 (int64_t)p_intf->p_sys->f_seconds * I64C(1000000) );
558 static int Back(intf_thread_t *p_intf)
560 if(p_intf->p_sys->p_input)
562 msg_Dbg(p_intf,"seeking -%f seconds",p_intf->p_sys->f_seconds);
564 var_SetTime( p_intf->p_sys->p_input, "time-offset",
565 -(int64_t)p_intf->p_sys->f_seconds * I64C(1000000) );
571 /* Toggle Play/Pause */
572 static int Play(intf_thread_t *p_intf)
574 if(p_intf->p_sys->p_input)
576 var_SetInteger( p_input, "state", PAUSE_S );
582 /* Toggle fullscreen mode */
583 static int Fullscreen(intf_thread_t *p_intf)
585 vout_thread_t * p_vout=vlc_object_find(p_intf,
586 VLC_OBJECT_VOUT, FIND_ANYWHERE );
589 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
590 vlc_object_release(p_vout);
595 /* dummy event. Use it if you don't wan't anything to happen */
596 static int dummy(intf_thread_t *p_intf)