]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
Gestures: Fix two dead locks when changing audio track and subtitle track when there...
[vlc] / modules / control / gestures.c
1 /*****************************************************************************
2  * gestures.c: control vlc with mouse gestures
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc_interface.h>
32 #include <vlc_vout.h>
33 #include <vlc_aout.h>
34 #include <vlc_playlist.h>
35
36 #ifdef HAVE_UNISTD_H
37 #    include <unistd.h>
38 #endif
39
40 /*****************************************************************************
41  * intf_sys_t: description and status of interface
42  *****************************************************************************/
43 struct intf_sys_t
44 {
45     vlc_object_t *      p_vout;
46     vlc_bool_t          b_got_gesture;
47     vlc_bool_t          b_button_pressed;
48     int                 i_mouse_x, i_mouse_y;
49     int                 i_last_x, i_last_y;
50     unsigned int        i_pattern;
51     int                 i_num_gestures;
52     int                 i_threshold;
53     int                 i_button_mask;
54 };
55
56 /*****************************************************************************
57  * Local prototypes.
58  *****************************************************************************/
59 #define UP 1
60 #define DOWN 2
61 #define LEFT 3
62 #define RIGHT 4
63 #define NONE 0
64 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
65
66 int  E_(Open)   ( vlc_object_t * );
67 void E_(Close)  ( vlc_object_t * );
68 static int  InitThread     ( intf_thread_t *p_intf );
69 static void EndThread      ( intf_thread_t *p_intf );
70 static int  MouseEvent     ( vlc_object_t *, char const *,
71                              vlc_value_t, vlc_value_t, void * );
72
73 /* Exported functions */
74 static void RunIntf        ( intf_thread_t *p_intf );
75
76 /*****************************************************************************
77  * Module descriptor
78  *****************************************************************************/
79 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
80 #define THRESHOLD_LONGTEXT N_( \
81     "Amount of movement required for a mouse gesture to be recorded." )
82
83 #define BUTTON_TEXT N_( "Trigger button" )
84 #define BUTTON_LONGTEXT N_( \
85     "Trigger button for mouse gestures." )
86
87 static const char *button_list[] = { "left", "middle", "right" };
88 static const char *button_list_text[] =
89                                    { N_("Left"), N_("Middle"), N_("Right") };
90
91 vlc_module_begin();
92     set_shortname( _("Gestures"));
93     set_category( CAT_INTERFACE );
94     set_subcategory( SUBCAT_INTERFACE_CONTROL );
95     add_integer( "gestures-threshold", 30, NULL,
96                  THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
97     add_string( "gestures-button", "right", NULL,
98                 BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
99         change_string_list( button_list, button_list_text, 0 );
100     set_description( _("Mouse gestures control interface") );
101
102     set_capability( "interface", 0 );
103     set_callbacks( E_(Open), E_(Close) );
104 vlc_module_end();
105
106 /*****************************************************************************
107  * OpenIntf: initialize interface
108  *****************************************************************************/
109 int E_(Open) ( vlc_object_t *p_this )
110 {
111     intf_thread_t *p_intf = (intf_thread_t *)p_this;
112
113     /* Allocate instance and initialize some members */
114     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
115     if( p_intf->p_sys == NULL )
116     {
117         return( 1 );
118     };
119
120     p_intf->pf_run = RunIntf;
121
122     return( 0 );
123 }
124
125 /*****************************************************************************
126  * gesture: return a subpattern within a pattern
127  *****************************************************************************/
128 static int gesture( int i_pattern, int i_num )
129 {
130     return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
131 }
132
133 /*****************************************************************************
134  * input_from_playlist: don't forget to release the return value
135  *  Also this function should really be available from core.
136  *****************************************************************************/
137 static input_thread_t * input_from_playlist ( playlist_t *p_playlist )
138 {
139     input_thread_t * p_input;
140
141     PL_LOCK; 
142     p_input = p_playlist->p_input; 
143     if( p_input ) 
144         vlc_object_yield( p_input ); 
145     PL_UNLOCK;
146
147     return p_input;
148 }
149
150 /*****************************************************************************
151  * CloseIntf: destroy dummy interface
152  *****************************************************************************/
153 void E_(Close) ( vlc_object_t *p_this )
154 {
155     intf_thread_t *p_intf = (intf_thread_t *)p_this;
156
157     /* Destroy structure */
158     free( p_intf->p_sys );
159 }
160
161
162 /*****************************************************************************
163  * RunIntf: main loop
164  *****************************************************************************/
165 static void RunIntf( intf_thread_t *p_intf )
166 {
167     playlist_t * p_playlist = NULL;
168
169     vlc_mutex_lock( &p_intf->change_lock );
170     p_intf->p_sys->p_vout = NULL;
171     vlc_mutex_unlock( &p_intf->change_lock );
172
173     if( InitThread( p_intf ) < 0 )
174     {
175         msg_Err( p_intf, "can't initialize interface thread" );
176         return;
177     }
178     msg_Dbg( p_intf, "interface thread initialized" );
179
180     /* Main loop */
181     while( !intf_ShouldDie( p_intf ) )
182     {
183         vlc_mutex_lock( &p_intf->change_lock );
184
185         /*
186          * mouse cursor
187          */
188         if( p_intf->p_sys->b_got_gesture )
189         {
190             vlc_value_t val;
191             int i_interval = 0;
192             /* Do something */
193             /* If you modify this, please try to follow this convention:
194                Start with LEFT, RIGHT for playback related commands
195                and UP, DOWN, for other commands */
196             switch( p_intf->p_sys->i_pattern )
197             {
198             case LEFT:
199                 i_interval = config_GetInt( p_intf , "short-jump-size" );
200                 if ( i_interval > 0 ) {
201                     val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
202                     var_Set( p_intf, "time-offset", val );
203                 }
204                 msg_Dbg(p_intf, "Go backward in the movie!");
205                 break;
206             case RIGHT:
207                 i_interval = config_GetInt( p_intf , "short-jump-size" );
208                 if ( i_interval > 0 ) {
209                     val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
210                     var_Set( p_intf, "time-offset", val );
211                 }
212                 msg_Dbg(p_intf, "Go forward in the movie!");
213                 break;
214             case GESTURE(LEFT,UP,NONE,NONE):
215                 /*FIXME BF*/
216                 msg_Dbg(p_intf, "Going slower.");
217                 break;
218             case GESTURE(RIGHT,UP,NONE,NONE):
219                 /*FIXME FF*/
220                 msg_Dbg(p_intf, "Going faster.");
221                 break;
222             case GESTURE(LEFT,RIGHT,NONE,NONE):
223             case GESTURE(RIGHT,LEFT,NONE,NONE):
224                 {
225                     input_thread_t * p_input;
226                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
227                                               FIND_ANYWHERE );
228
229                    if( !p_playlist )
230                         break;
231
232                     p_input = input_from_playlist( p_playlist );
233                     vlc_object_release( p_playlist );
234                     
235                     if( !p_input )
236                         break;
237                     
238                     val.i_int = PLAYING_S;
239                     if( p_input )
240                     {
241                         var_Get( p_input, "state", &val);
242                         if( val.i_int == PAUSE_S )
243                         {
244                             val.i_int = PLAYING_S;
245                         }
246                         else
247                         {
248                             val.i_int = PAUSE_S;
249                         }
250                         var_Set( p_input, "state", val);
251                     }
252                     msg_Dbg(p_intf, "Play/Pause");
253                     vlc_object_release( p_input );
254                 }
255                 break;
256             case GESTURE(LEFT,DOWN,NONE,NONE):
257                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
258                                               FIND_ANYWHERE );
259                 if( p_playlist == NULL )
260                 {
261                     break;
262                 }
263
264                 playlist_Prev( p_playlist );
265                 vlc_object_release( p_playlist );
266                 break;
267             case GESTURE(RIGHT,DOWN,NONE,NONE):
268                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
269                                               FIND_ANYWHERE );
270                 if( p_playlist == NULL )
271                 {
272                     break;
273                 }
274
275                 playlist_Next( p_playlist );
276                 vlc_object_release( p_playlist );
277                 break;
278             case UP:
279                 {
280                     audio_volume_t i_newvol;
281                     aout_VolumeUp( p_intf, 1, &i_newvol );
282                     msg_Dbg(p_intf, "Louder");
283                 }
284                 break;
285             case DOWN:
286                 {
287                     audio_volume_t i_newvol;
288                     aout_VolumeDown( p_intf, 1, &i_newvol );
289                     msg_Dbg(p_intf, "Quieter");
290                 }
291                 break;
292             case GESTURE(UP,DOWN,NONE,NONE):
293             case GESTURE(DOWN,UP,NONE,NONE):
294                 {
295                     audio_volume_t i_newvol = -1;
296                     aout_VolumeMute( p_intf, &i_newvol );
297                     msg_Dbg(p_intf, "Mute sound");
298                 }
299                 break;
300             case GESTURE(UP,RIGHT,NONE,NONE):
301                 {
302                    input_thread_t * p_input;
303                    vlc_value_t val, list, list2;
304                    int i_count, i;
305
306                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
307                                               FIND_ANYWHERE );
308
309                    if( !p_playlist )
310                         break;
311
312                     p_input = input_from_playlist( p_playlist );
313
314                     vlc_object_release( p_playlist );
315
316                     if( !p_input )
317                         break;
318
319                    var_Get( p_input, "audio-es", &val );
320                    var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
321                                &list, &list2 );
322                    i_count = list.p_list->i_count;
323                    if( i_count <= 1 )
324                    {
325                        vlc_object_release( p_input );
326                        break;
327                    }
328                    for( i = 0; i < i_count; i++ )
329                    {
330                        if( val.i_int == list.p_list->p_values[i].i_int )
331                        {
332                            break;
333                        }
334                    }
335                    /* value of audio-es was not in choices list */
336                    if( i == i_count )
337                    {
338                        msg_Warn( p_input,
339                                "invalid current audio track, selecting 0" );
340                        var_Set( p_input, "audio-es",
341                                list.p_list->p_values[0] );
342                        i = 0;
343                    }
344                    else if( i == i_count - 1 )
345                    {
346                        var_Set( p_input, "audio-es",
347                                list.p_list->p_values[1] );
348                        i = 1;
349                    }
350                    else
351                    {
352                        var_Set( p_input, "audio-es",
353                                list.p_list->p_values[i+1] );
354                        i++;
355                    }
356                    vlc_object_release( p_input );
357                 }
358                 break;
359             case GESTURE(DOWN,RIGHT,NONE,NONE):
360                 {
361                     input_thread_t * p_input;
362                     vlc_value_t val, list, list2;
363                     int i_count, i;
364
365                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
366                                               FIND_ANYWHERE );
367
368                     if( !p_playlist )
369                         break;
370
371                     p_input = input_from_playlist( p_playlist );
372                     vlc_object_release( p_playlist );
373
374                     if( !p_input )
375                         break;
376
377                     var_Get( p_input, "spu-es", &val );
378
379                     var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
380                             &list, &list2 );
381                     i_count = list.p_list->i_count;
382                     if( i_count <= 1 )
383                     {
384                         vlc_object_release( p_input );
385                         break;
386                     }
387                     for( i = 0; i < i_count; i++ )
388                     {
389                         if( val.i_int == list.p_list->p_values[i].i_int )
390                         {
391                             break;
392                         }
393                     }
394                     /* value of spu-es was not in choices list */
395                     if( i == i_count )
396                     {
397                         msg_Warn( p_input,
398                                 "invalid current subtitle track, selecting 0" );
399                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
400                         i = 0;
401                     }
402                     else if( i == i_count - 1 )
403                     {
404                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
405                         i = 0;
406                     }
407                     else
408                     {
409                         var_Set( p_input, "spu-es", 
410                                 list.p_list->p_values[i+1] );
411                         i = i + 1;
412                     }
413                     vlc_object_release( p_input );
414                 }
415                 break;
416             case GESTURE(UP,LEFT,NONE,NONE):
417                 if (p_intf->p_sys->p_vout )
418                 {
419                     ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
420                         VOUT_FULLSCREEN_CHANGE;
421                 }
422                 break;
423             case GESTURE(DOWN,LEFT,NONE,NONE):
424                 /* FIXME: Should close the vout!"*/
425                 p_intf->p_libvlc->b_die = VLC_TRUE;
426                 break;
427             case GESTURE(DOWN,LEFT,UP,RIGHT):
428             case GESTURE(UP,RIGHT,DOWN,LEFT):
429                 msg_Dbg(p_intf, "a square was drawn!" );
430                 break;
431             default:
432                 break;
433             }
434             p_intf->p_sys->i_num_gestures = 0;
435             p_intf->p_sys->i_pattern = 0;
436             p_intf->p_sys->b_got_gesture = VLC_FALSE;
437         }
438
439         /*
440          * video output
441          */
442         if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
443         {
444             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
445                              MouseEvent, p_intf );
446             var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
447                              MouseEvent, p_intf );
448             vlc_object_release( p_intf->p_sys->p_vout );
449             p_intf->p_sys->p_vout = NULL;
450         }
451
452         if( p_intf->p_sys->p_vout == NULL )
453         {
454             p_intf->p_sys->p_vout = vlc_object_find( p_intf,
455                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
456             if( p_intf->p_sys->p_vout )
457             {
458                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
459                                  MouseEvent, p_intf );
460                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
461                                  MouseEvent, p_intf );
462             }
463         }
464
465         vlc_mutex_unlock( &p_intf->change_lock );
466
467         /* Wait a bit */
468         msleep( INTF_IDLE_SLEEP );
469     }
470
471     EndThread( p_intf );
472 }
473
474 /*****************************************************************************
475  * InitThread:
476  *****************************************************************************/
477 static int InitThread( intf_thread_t * p_intf )
478 {
479     char *psz_button;
480     /* we might need some locking here */
481     if( !intf_ShouldDie( p_intf ) )
482     {
483         /* p_intf->change_lock locking strategy:
484          * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
485          * - make sure there won't be  cross increment/decrement ref count
486          *   of p_intf->p_sys members p_intf->change_lock should be locked
487          *   during those operations */
488         vlc_mutex_lock( &p_intf->change_lock );
489
490         p_intf->p_sys->b_got_gesture = VLC_FALSE;
491         p_intf->p_sys->b_button_pressed = VLC_FALSE;
492         p_intf->p_sys->i_threshold =
493                      config_GetInt( p_intf, "gestures-threshold" );
494         psz_button = config_GetPsz( p_intf, "gestures-button" );
495         if ( !strcmp( psz_button, "left" ) )
496         {
497             p_intf->p_sys->i_button_mask = 1;
498         }
499         else if ( !strcmp( psz_button, "middle" ) )
500         {
501             p_intf->p_sys->i_button_mask = 2;
502         }
503         else if ( !strcmp( psz_button, "right" ) )
504         {
505             p_intf->p_sys->i_button_mask = 4;
506         }
507
508         p_intf->p_sys->i_pattern = 0;
509         p_intf->p_sys->i_num_gestures = 0;
510         vlc_mutex_unlock( &p_intf->change_lock );
511
512         return 0;
513     }
514     else
515     {
516         return -1;
517     }
518 }
519
520 /*****************************************************************************
521  * EndThread:
522  *****************************************************************************/
523 static void EndThread( intf_thread_t * p_intf )
524 {
525     vlc_mutex_lock( &p_intf->change_lock );
526
527     if( p_intf->p_sys->p_vout )
528     {
529         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
530                          MouseEvent, p_intf );
531         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
532                          MouseEvent, p_intf );
533         vlc_object_release( p_intf->p_sys->p_vout );
534     }
535
536     vlc_mutex_unlock( &p_intf->change_lock );
537 }
538
539 /*****************************************************************************
540  * MouseEvent: callback for mouse events
541  *****************************************************************************/
542 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
543                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
544 {
545     vlc_value_t val;
546     int pattern = 0;
547
548     signed int i_horizontal, i_vertical;
549     intf_thread_t *p_intf = (intf_thread_t *)p_data;
550
551     vlc_mutex_lock( &p_intf->change_lock );
552
553     /* don't process new gestures before the last events are processed */
554     if( p_intf->p_sys->b_got_gesture )
555     {
556         vlc_mutex_unlock( &p_intf->change_lock );
557         return VLC_SUCCESS;
558     }
559
560     if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
561     {
562         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
563         p_intf->p_sys->i_mouse_x = val.i_int;
564         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
565         p_intf->p_sys->i_mouse_y = val.i_int;
566         i_horizontal = p_intf->p_sys->i_mouse_x -
567             p_intf->p_sys->i_last_x;
568         i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
569         i_vertical = p_intf->p_sys->i_mouse_y
570             - p_intf->p_sys->i_last_y;
571         i_vertical = i_vertical / p_intf->p_sys->i_threshold;
572
573         if( i_horizontal < 0 )
574         {
575             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
576             pattern = LEFT;
577         }
578         else if( i_horizontal > 0 )
579         {
580             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
581             pattern = RIGHT;
582         }
583         if( i_vertical < 0 )
584         {
585             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
586             pattern = UP;
587         }
588         else if( i_vertical > 0 )
589         {
590             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
591             pattern = DOWN;
592         }
593         if( pattern )
594         {
595             p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
596             p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
597             if( gesture( p_intf->p_sys->i_pattern,
598                          p_intf->p_sys->i_num_gestures - 1 ) != pattern )
599             {
600                 p_intf->p_sys->i_pattern |=
601                     pattern << ( p_intf->p_sys->i_num_gestures * 4 );
602                 p_intf->p_sys->i_num_gestures++;
603             }
604         }
605
606     }
607     if( !strcmp( psz_var, "mouse-button-down" )
608         && newval.i_int & p_intf->p_sys->i_button_mask
609         && !p_intf->p_sys->b_button_pressed )
610     {
611         p_intf->p_sys->b_button_pressed = VLC_TRUE;
612         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
613         p_intf->p_sys->i_last_x = val.i_int;
614         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
615         p_intf->p_sys->i_last_y = val.i_int;
616     }
617     if( !strcmp( psz_var, "mouse-button-down" )
618         && !( newval.i_int & p_intf->p_sys->i_button_mask )
619         && p_intf->p_sys->b_button_pressed )
620     {
621         p_intf->p_sys->b_button_pressed = VLC_FALSE;
622         p_intf->p_sys->b_got_gesture = VLC_TRUE;
623     }
624
625     vlc_mutex_unlock( &p_intf->change_lock );
626
627     return VLC_SUCCESS;
628 }