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