]> git.sesse.net Git - vlc/blob - modules/control/joystick.c
8f0b6db671cbee73eb2b4462b6d8aeceda81dae8
[vlc] / modules / control / joystick.c
1 /*****************************************************************************
2  * joystick.c: control vlc with a joystick
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/select.h>
34
35 #include <errno.h>
36
37 #include <fcntl.h>
38
39 #include <vlc/vlc.h>
40 #include <vlc/intf.h>
41 #include <vlc/vout.h>
42
43 #include <linux/joystick.h>
44
45 #include "audio_output.h"
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
48
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 */
55
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}"
60
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
67
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
72
73 /*****************************************************************************
74  * intf_sys_t: description and status of interface
75  *****************************************************************************/
76
77 typedef int (*action)(intf_thread_t *p_intf);
78
79 struct joy_axis_t
80 {
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
87                                  * zone ? */
88 };
89
90 struct joy_button_t
91 {
92     action      pf_actup;  /* What to do when button is released */
93     action      pf_actdown;/* What to do when button is pressed */
94 };
95
96 struct intf_sys_t
97 {
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 */
109 };
110
111 /*****************************************************************************
112  * Local prototypes.
113  *****************************************************************************/
114
115 static int  Open   ( vlc_object_t * );
116 static void Close  ( vlc_object_t * );
117 static int  Init   ( intf_thread_t *p_intf );
118
119 static int handle_event   ( intf_thread_t *p_intf, struct js_event event );
120
121
122 /* Actions */
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);
129
130 static int dummy       (intf_thread_t *p_intf);
131
132 /* Exported functions */
133 static void Run       ( intf_thread_t *p_intf );
134
135 /*****************************************************************************
136  * Module descriptor
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)." )
142
143 #define DEVICE_TEXT N_( "Joystick device" )
144 #define DEVICE_LONGTEXT N_( \
145     "The joystick device (usually /dev/js0 or /dev/input/js0).")
146
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." )
151
152 #define WAIT_TEXT N_( "Wait time (ms)")
153 #define WAIT_LONGTEXT N_(\
154    "The time waited before the repeat starts, in milliseconds.")
155
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." )
159
160 #define MAP_TEXT N_( "Action mapping")
161 #define MAP_LONGTEXT N_( "Allows you to remap the actions." )
162
163 vlc_module_begin();
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 );
179 vlc_module_end();
180
181 /*****************************************************************************
182  * Open: initialize interface
183  *****************************************************************************/
184 static int Open ( vlc_object_t *p_this )
185 {
186     intf_thread_t *p_intf = (intf_thread_t *)p_this;
187
188     /* Allocate instance and initialize some members */
189     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
190
191     if( p_intf->p_sys == NULL )
192     {
193         return 1 ;
194     }
195
196     p_intf->pf_run = Run;
197
198     return 0 ;
199 }
200
201 /*****************************************************************************
202  * Close: destroy the interface
203  *****************************************************************************/
204 static void Close ( vlc_object_t *p_this )
205 {
206     intf_thread_t *p_intf = (intf_thread_t *)p_this;
207
208     /* Destroy structure */
209     if(p_intf->p_sys)
210         free( p_intf->p_sys );
211 }
212
213
214 /*****************************************************************************
215  * Run: main loop
216  *****************************************************************************/
217 static void Run( intf_thread_t *p_intf )
218 {
219     int i_sel_res = 0;
220     int i_read    = 0;
221     int i_axe     = 0;
222     struct js_event event;
223
224     if( Init( p_intf ) < 0 )
225     {
226         msg_Err( p_intf, "can't initialize intf" );
227         return;
228     }
229     msg_Dbg( p_intf, "intf initialized" );
230
231     /* Main loop */
232     while( !p_intf->b_die )
233     {
234         vlc_mutex_lock( &p_intf->change_lock );
235
236         FD_ZERO( &p_intf->p_sys->fds );
237         FD_SET( p_intf->p_sys->i_fd , &p_intf->p_sys->fds );
238
239         p_intf->p_sys->timeout.tv_sec  = 0;
240         p_intf->p_sys->timeout.tv_usec = p_intf->p_sys->i_repeat;
241
242
243         i_sel_res = select ( p_intf->p_sys->i_fd+1,
244                         &p_intf->p_sys->fds,
245                         NULL,
246                         NULL,
247                         &p_intf->p_sys->timeout );
248
249         p_intf->p_sys->p_input = (input_thread_t *)
250            vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
251
252         if( i_sel_res == -1 && errno != EINTR )
253         {
254             msg_Err( p_intf, "select error: %s",strerror(errno) );
255         }
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 ) ;
263         }
264         else if(i_sel_res == 0)
265         /*We have no event, but check if we have an action to repeat */
266         {
267             for(i_axe=0;i_axe<=1;i_axe++)
268             {
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 )
273                 {
274                     p_intf->p_sys->axes[i_axe].pf_actup(p_intf);
275                 }
276
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 )
281                 {
282                     p_intf->p_sys->axes[i_axe].pf_actdown(p_intf);
283                 }
284             }
285         }
286
287         if(p_intf->p_sys->p_input)
288                 vlc_object_release (p_intf->p_sys->p_input);
289
290         vlc_mutex_unlock ( &p_intf->change_lock );
291     }
292 }
293
294 /*****************************************************************************
295  * InitThread: Initialize the interface
296  *****************************************************************************/
297 static int Init( intf_thread_t * p_intf )
298 {
299     char *psz_device;
300     char *psz_parse;
301     char *psz_eof;  /* end of field */
302
303     if( !p_intf->b_die )
304     {
305         vlc_mutex_lock( &p_intf->change_lock );
306
307         psz_device=config_GetPsz( p_intf, "joystick-device");
308
309         if(!psz_device) /* strange... */
310             psz_device = strdup( DEFAULT_DEVICE );
311
312         p_intf->p_sys->i_fd = open ( psz_device , O_RDONLY|O_NONBLOCK );
313
314         if( p_intf->p_sys->i_fd == -1 )
315         {
316             msg_Warn( p_intf, "unable to open %s for reading: %s"
317                                 ,psz_device,strerror(errno));
318             return -1;
319         }
320
321         p_intf->p_sys->i_repeat = 1000*
322                         config_GetInt( p_intf, "joystick-repeat");
323
324         p_intf->p_sys->i_wait = 1000*
325                         config_GetInt( p_intf, "joystick-wait");
326
327         p_intf->p_sys->i_threshold =
328                         config_GetInt( p_intf, "motion-threshold" );
329
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;
333
334         p_intf->p_sys->i_maxseek =
335                         config_GetInt( p_intf, "joystick-max-seek" );
336
337
338         psz_parse = config_GetPsz( p_intf, "joystick-mapping" ) ;
339
340         if ( ! psz_parse)
341         {
342             msg_Warn (p_intf,"invalid mapping. aborting." );
343             return -1;
344         }
345         if( !strlen( psz_parse ) )
346         {
347             msg_Warn( p_intf, "invalid mapping, aborting." );
348             return -1;
349         }
350
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;
355
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;
360
361 /* Macro to parse the command line */
362 #define PARSE(name,function)                                                  \
363     if(!strncmp( psz_parse , name , strlen( name ) ) )                        \
364     {                                                                         \
365         psz_parse += strlen( name );                                          \
366         psz_eof = strchr( psz_parse , ',' );                                  \
367         if( !psz_eof)                                                         \
368             psz_eof = strchr( psz_parse, '}' );                               \
369         if( !psz_eof)                                                         \
370             psz_eof = psz_parse + strlen(psz_parse);                          \
371         if( psz_eof )                                                         \
372         {                                                                     \
373             *psz_eof = '\0' ;                                                 \
374         }                                                                     \
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;                                                  \
383         psz_parse ++;                                                         \
384         continue;                                                             \
385     }                                                                         \
386
387         while(1)
388         {
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    );
393
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 );
398
399             if( *psz_parse )
400                 psz_parse++;
401             else
402                 break;
403          }
404
405         p_intf->p_sys->axes[0].b_trigered = VLC_FALSE;
406         p_intf->p_sys->axes[0].l_time     = 0;
407
408         p_intf->p_sys->axes[1].b_trigered = VLC_FALSE;
409         p_intf->p_sys->axes[1].l_time     = 0;
410
411         vlc_mutex_unlock( &p_intf->change_lock );
412
413         return 0;
414     }
415     else
416     {
417         return -1;
418     }
419 }
420
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)
425 {
426     unsigned int i_axe;
427
428     if( event.type == JS_EVENT_AXIS )
429     {
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 )
436         {
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 );
441             return 0;
442         }
443
444         p_intf->p_sys->axes[event.number].b_dowork = VLC_FALSE;
445         p_intf->p_sys->axes[event.number].i_value  = event.value;
446
447         if( abs(event.value) > p_intf->p_sys->i_threshold &&
448              p_intf->p_sys->axes[event.number].b_trigered == VLC_FALSE)
449         {
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();
454         }
455         else if(abs(event.value) > p_intf->p_sys->i_threshold &&
456             p_intf->p_sys->axes[event.number].b_trigered == VLC_TRUE)
457         {
458         /* The axis moved but remained in the trigger zone
459          * Do nothing at this time */
460         }
461         else if ( abs(event.value) < p_intf->p_sys->i_threshold )
462         {
463         /* The axis is not in the trigger zone */
464             p_intf->p_sys->axes[event.number].b_trigered = VLC_FALSE;
465         }
466
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);
472
473
474         /* Handle the first two axes. */
475         for(i_axe = 0; i_axe <= 1 ; i_axe ++)
476         {
477             if(p_intf->p_sys->axes[i_axe].b_dowork == VLC_TRUE)
478             {
479                 if( p_intf->p_sys->axes[i_axe].i_value
480                               > p_intf->p_sys->i_threshold )
481                 {
482                     msg_Dbg(p_intf,"up for axis %i\n",i_axe);
483                     p_intf->p_sys->axes[i_axe].pf_actup(p_intf);
484                 }
485                 else if( p_intf->p_sys->axes[i_axe].i_value
486                                 < -p_intf->p_sys->i_threshold )
487                 {
488                     msg_Dbg(p_intf,"down for axis %i\n",i_axe);
489                     p_intf->p_sys->axes[i_axe].pf_actdown(p_intf);
490                 }
491
492             }
493         }
494     }
495     else if( event.type == JS_EVENT_BUTTON)
496     {
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 */
501         {
502             if(p_intf->p_sys->buttons[event.number].pf_actdown)
503                 p_intf->p_sys->buttons[event.number].pf_actdown(p_intf);
504         }
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);
508     }
509     return 0;
510 }
511
512 /****************************************************************************
513  * The actions
514  ****************************************************************************/
515
516 /* Go to next item in the playlist */
517 static int Next( intf_thread_t *p_intf)
518 {
519     playlist_t *p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
520                                    FIND_ANYWHERE );
521     if( p_playlist == NULL )
522     {
523         return -1;
524     }
525     playlist_Next( p_playlist );
526     vlc_object_release( p_playlist );
527     return 0;
528 }
529
530 /* Go to previous item in the playlist */
531 static int Prev( intf_thread_t *p_intf)
532 {
533     playlist_t *p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
534                                    FIND_ANYWHERE );
535     if( p_playlist == NULL )
536     {
537         return -1;
538     }
539     playlist_Prev( p_playlist );
540     vlc_object_release( p_playlist );
541     return 0;
542 }
543
544 /* Seek forward */
545 static int Forward(intf_thread_t *p_intf)
546 {
547     if(p_intf->p_sys->p_input)
548     {
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) );
552         return 0;
553     }
554     return -1;
555 }
556
557 /* Seek backwards */
558 static int Back(intf_thread_t *p_intf)
559 {
560     if(p_intf->p_sys->p_input)
561     {
562         msg_Dbg(p_intf,"seeking -%f seconds",p_intf->p_sys->f_seconds);
563
564         var_SetTime( p_intf->p_sys->p_input, "time-offset",
565                      -(int64_t)p_intf->p_sys->f_seconds * I64C(1000000) );
566         return 0;
567     }
568     return -1;
569 }
570
571 /* Toggle Play/Pause */
572 static int Play(intf_thread_t *p_intf)
573 {
574     if(p_intf->p_sys->p_input)
575     {
576         var_SetInteger( p_input, "state", PAUSE_S );
577         return 0;
578     }
579     return -1;
580 }
581
582 /* Toggle fullscreen mode */
583 static int Fullscreen(intf_thread_t *p_intf)
584 {
585     vout_thread_t * p_vout=vlc_object_find(p_intf,
586                           VLC_OBJECT_VOUT, FIND_ANYWHERE );
587     if(p_vout)
588     {
589         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
590         vlc_object_release(p_vout);
591     }
592     return 0;
593 }
594
595 /* dummy event. Use it if you don't wan't anything to happen */
596 static int dummy(intf_thread_t *p_intf)
597 {
598    return 0;
599 }