]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
a2fe30bfd11928c68594713c03d18751d71b29d2
[vlc] / modules / control / gestures.c
1 /*****************************************************************************
2  * gestures.c: control vlc with mouse gestures
3  *****************************************************************************
4  * Copyright (C) 2004-2009 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_playlist.h>
37 #include <vlc_input.h>
38 #include <assert.h>
39
40 /*****************************************************************************
41  * intf_sys_t: description and status of interface
42  *****************************************************************************/
43 struct intf_sys_t
44 {
45     vlc_mutex_t         lock;
46     input_thread_t     *p_input;
47     vout_thread_t      *p_vout;
48     bool                b_button_pressed;
49     int                 i_last_x, i_last_y;
50     unsigned int        i_pattern;
51     unsigned 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 static int  Open   ( vlc_object_t * );
67 static void Close  ( vlc_object_t * );
68
69 /*****************************************************************************
70  * Module descriptor
71  *****************************************************************************/
72 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
73 #define THRESHOLD_LONGTEXT N_( \
74     "Amount of movement required for a mouse gesture to be recorded." )
75
76 #define BUTTON_TEXT N_( "Trigger button" )
77 #define BUTTON_LONGTEXT N_( \
78     "Trigger button for mouse gestures." )
79
80 #define BUTTON_DEFAULT "left"
81
82 static const char *const button_list[] = { "left", "middle", "right" };
83 static const char *const button_list_text[] =
84                                    { N_("Left"), N_("Middle"), N_("Right") };
85
86 vlc_module_begin ()
87     set_shortname( N_("Gestures"))
88     set_category( CAT_INTERFACE )
89     set_subcategory( SUBCAT_INTERFACE_CONTROL )
90     add_integer( "gestures-threshold", 30,
91                  THRESHOLD_TEXT, THRESHOLD_LONGTEXT, true )
92     add_string( "gestures-button", BUTTON_DEFAULT,
93                 BUTTON_TEXT, BUTTON_LONGTEXT, false )
94         change_string_list( button_list, button_list_text )
95     set_description( N_("Mouse gestures control interface") )
96
97     set_capability( "interface", 0 )
98     set_callbacks( Open, Close )
99 vlc_module_end ()
100
101 static int PlaylistEvent( vlc_object_t *, char const *,
102                           vlc_value_t, vlc_value_t, void * );
103 static int InputEvent( vlc_object_t *, char const *,
104                        vlc_value_t, vlc_value_t, void * );
105 static int MovedEvent( vlc_object_t *, char const *,
106                        vlc_value_t, vlc_value_t, void * );
107 static int ButtonEvent( vlc_object_t *, char const *,
108                         vlc_value_t, vlc_value_t, void * );
109
110 /*****************************************************************************
111  * OpenIntf: initialize interface
112  *****************************************************************************/
113 static int Open ( vlc_object_t *p_this )
114 {
115     intf_thread_t *p_intf = (intf_thread_t *)p_this;
116
117     /* Allocate instance and initialize some members */
118     intf_sys_t *p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
119     if( unlikely(p_sys == NULL) )
120         return VLC_ENOMEM;
121
122     // Configure the module
123     vlc_mutex_init( &p_sys->lock );
124     p_sys->p_input = NULL;
125     p_sys->p_vout = NULL;
126     p_sys->b_button_pressed = false;
127     p_sys->i_threshold = var_InheritInteger( p_intf, "gestures-threshold" );
128
129     // Choose the tight button to use
130     char *psz_button = var_InheritString( p_intf, "gestures-button" );
131     if( psz_button && !strcmp( psz_button, "left" ) )
132         p_sys->i_button_mask = 1;
133     else if( psz_button && !strcmp( psz_button, "middle" ) )
134         p_sys->i_button_mask = 2;
135     else // psz_button == "right"
136         p_sys->i_button_mask = 4;
137     free( psz_button );
138
139     p_sys->i_pattern = 0;
140     p_sys->i_num_gestures = 0;
141
142     var_AddCallback( pl_Get(p_intf), "input-current", PlaylistEvent, p_intf );
143
144     return VLC_SUCCESS;
145 }
146
147 /*****************************************************************************
148  * gesture: return a subpattern within a pattern
149  *****************************************************************************/
150 static inline unsigned gesture( unsigned i_pattern, unsigned i_num )
151 {
152     return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
153 }
154
155 /*****************************************************************************
156  * CloseIntf: destroy dummy interface
157  *****************************************************************************/
158 static void Close ( vlc_object_t *p_this )
159 {
160     intf_thread_t *p_intf = (intf_thread_t *)p_this;
161     intf_sys_t *p_sys = p_intf->p_sys;
162
163     /* Destroy the callbacks (the order matters!) */
164     var_DelCallback( pl_Get(p_intf), "input-current", PlaylistEvent, p_intf );
165
166     if( p_sys->p_input != NULL )
167         var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
168
169     if( p_sys->p_vout )
170     {
171         var_DelCallback( p_sys->p_vout, "mouse-moved", MovedEvent, p_intf );
172         var_DelCallback( p_sys->p_vout, "mouse-button-down",
173                          ButtonEvent, p_intf );
174         vlc_object_release( p_sys->p_vout );
175     }
176
177     /* Destroy structure */
178     vlc_mutex_destroy( &p_sys->lock );
179     free( p_sys );
180 }
181
182 static void ProcessGesture( intf_thread_t *p_intf )
183 {
184     intf_sys_t *p_sys = p_intf->p_sys;
185     playlist_t *p_playlist = pl_Get( p_intf );
186
187     /* Do something */
188     /* If you modify this, please try to follow this convention:
189        Start with LEFT, RIGHT for playback related commands
190        and UP, DOWN, for other commands */
191     switch( p_sys->i_pattern )
192     {
193         case LEFT:
194         {
195             msg_Dbg( p_intf, "Go backward in the movie!" );
196
197             input_thread_t *p_input = playlist_CurrentInput( p_playlist );
198             if( p_input == NULL )
199                 break;
200
201             int it = var_InheritInteger( p_intf , "short-jump-size" );
202             if( it > 0 )
203                 var_SetTime( p_input, "time-offset", -CLOCK_FREQ * it );
204             vlc_object_release( p_input );
205             break;
206         }
207
208         case RIGHT:
209         {
210             msg_Dbg( p_intf, "Go forward in the movie!" );
211
212             input_thread_t *p_input = playlist_CurrentInput( p_playlist );
213             if( p_input == NULL )
214                 break;
215
216             int it = var_InheritInteger( p_intf , "short-jump-size" );
217             if( it > 0 )
218                 var_SetTime( p_input, "time-offset", CLOCK_FREQ * it );
219             vlc_object_release( p_input );
220             break;
221         }
222
223         case GESTURE(LEFT,UP,NONE,NONE):
224             msg_Dbg( p_intf, "Going slower." );
225             var_TriggerCallback( p_playlist, "rate-slower" );
226             break;
227
228         case GESTURE(RIGHT,UP,NONE,NONE):
229             msg_Dbg( p_intf, "Going faster." );
230             var_TriggerCallback( p_playlist, "rate-faster" );
231             break;
232
233         case GESTURE(LEFT,RIGHT,NONE,NONE):
234         case GESTURE(RIGHT,LEFT,NONE,NONE):
235         {
236             msg_Dbg( p_intf, "Play/Pause" );
237
238             input_thread_t *p_input = playlist_CurrentInput( p_playlist );
239             if( p_input == NULL )
240                 break;
241
242             int i_state = var_GetInteger( p_input, "state" );
243             i_state = (i_state == PLAYING_S) ? PAUSE_S : PLAYING_S;
244             var_SetInteger( p_input, "state", i_state );
245             vlc_object_release( p_input );
246             break;
247         }
248
249         case GESTURE(LEFT,DOWN,NONE,NONE):
250             playlist_Prev( p_playlist );
251             break;
252
253         case GESTURE(RIGHT,DOWN,NONE,NONE):
254             playlist_Next( p_playlist );
255             break;
256
257         case UP:
258             msg_Dbg(p_intf, "Louder");
259             playlist_VolumeUp( p_playlist, 1, NULL );
260             break;
261
262         case DOWN:
263             msg_Dbg(p_intf, "Quieter");
264             playlist_VolumeDown( p_playlist, 1, NULL );
265             break;
266
267         case GESTURE(UP,DOWN,NONE,NONE):
268         case GESTURE(DOWN,UP,NONE,NONE):
269             msg_Dbg( p_intf, "Mute sound" );
270             playlist_MuteToggle( p_playlist );
271             break;
272
273         case GESTURE(UP,RIGHT,NONE,NONE):
274         {
275             input_thread_t *p_input = playlist_CurrentInput( p_playlist );
276             if( p_input == NULL )
277                 break;
278
279             vlc_value_t list, list2;
280             var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
281                         &list, &list2 );
282
283             if( list.p_list->i_count > 1 )
284             {
285                 int i_audio_es = var_GetInteger( p_input, "audio-es" );
286                 int i;
287
288                 for( i = 0; i < list.p_list->i_count; i++ )
289                      if( i_audio_es == list.p_list->p_values[i].i_int )
290                          break;
291                 /* value of audio-es was not in choices list */
292                 if( i == list.p_list->i_count )
293                 {
294                     msg_Warn( p_input,
295                               "invalid current audio track, selecting 0" );
296                     i = 0;
297                 }
298                 else if( i == list.p_list->i_count - 1 )
299                     i = 1;
300                 else
301                     i++;
302                 var_SetInteger( p_input, "audio-es",
303                                 list.p_list->p_values[i].i_int );
304             }
305             var_FreeList( &list, &list2 );
306             vlc_object_release( p_input );
307             break;
308         }
309
310         case GESTURE(DOWN,RIGHT,NONE,NONE):
311         {
312             input_thread_t *p_input = playlist_CurrentInput( p_playlist );
313             if( p_input == NULL )
314                 break;
315
316             vlc_value_t list, list2;
317             var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
318                         &list, &list2 );
319
320             if( list.p_list->i_count > 1 )
321             {
322                 int i_audio_es = var_GetInteger( p_input, "spu-es" );
323                 int i;
324
325                 for( i = 0; i < list.p_list->i_count; i++ )
326                      if( i_audio_es == list.p_list->p_values[i].i_int )
327                          break;
328                 /* value of audio-es was not in choices list */
329                 if( i == list.p_list->i_count )
330                 {
331                     msg_Warn( p_input,
332                               "invalid current subtitle track, selecting 0" );
333                     i = 0;
334                 }
335                 else if( i == list.p_list->i_count - 1 )
336                     i = 1;
337                 else
338                     i++;
339                 var_SetInteger( p_input, "audio-es",
340                                 list.p_list->p_values[i].i_int );
341             }
342             var_FreeList( &list, &list2 );
343             vlc_object_release( p_input );
344             break;
345         }
346
347         case GESTURE(UP,LEFT,NONE,NONE):
348         {
349             bool val = var_ToggleBool( pl_Get( p_intf ), "fullscreen" );
350             if( p_sys->p_vout )
351                 var_SetBool( p_sys->p_vout, "fullscreen", val );
352             break;
353         }
354
355         case GESTURE(DOWN,LEFT,NONE,NONE):
356             /* FIXME: Should close the vout!"*/
357             libvlc_Quit( p_intf->p_libvlc );
358             break;
359
360         case GESTURE(DOWN,LEFT,UP,RIGHT):
361         case GESTURE(UP,RIGHT,DOWN,LEFT):
362             msg_Dbg( p_intf, "a square was drawn!" );
363             break;
364     }
365
366     p_sys->i_num_gestures = 0;
367     p_sys->i_pattern = 0;
368 }
369
370 static int MovedEvent( vlc_object_t *p_this, char const *psz_var,
371                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
372 {
373     intf_thread_t *p_intf = (intf_thread_t *)p_data;
374     intf_sys_t    *p_sys = p_intf->p_sys;
375
376     (void) p_this; (void) psz_var; (void) oldval;
377
378     vlc_mutex_lock( &p_sys->lock );
379     if( p_sys->b_button_pressed )
380     {
381         int i_horizontal = newval.coords.x - p_sys->i_last_x;
382         int i_vertical = newval.coords.y - p_sys->i_last_y;
383         unsigned int pattern = 0;
384
385         i_horizontal /= p_sys->i_threshold;
386         i_vertical /= p_sys->i_threshold;
387
388         if( i_horizontal < 0 )
389         {
390             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
391             pattern = LEFT;
392         }
393         else if( i_horizontal > 0 )
394         {
395             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
396             pattern = RIGHT;
397         }
398         if( i_vertical < 0 )
399         {
400             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
401             pattern = UP;
402         }
403         else if( i_vertical > 0 )
404         {
405             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
406             pattern = DOWN;
407         }
408
409         if( pattern )
410         {
411             p_sys->i_last_x = newval.coords.x;
412             p_sys->i_last_y = newval.coords.y;
413             if( p_sys->i_num_gestures > 0
414              && gesture( p_sys->i_pattern, p_sys->i_num_gestures - 1 )
415                     != pattern )
416             {
417                 p_sys->i_pattern |= pattern << ( p_sys->i_num_gestures * 4 );
418                 p_sys->i_num_gestures++;
419             }
420             else if( p_sys->i_num_gestures == 0 )
421             {
422                 p_sys->i_pattern = pattern;
423                 p_sys->i_num_gestures++;
424             }
425         }
426
427     }
428     vlc_mutex_unlock( &p_sys->lock );
429
430     return VLC_SUCCESS;
431 }
432
433 static int ButtonEvent( vlc_object_t *p_this, char const *psz_var,
434                         vlc_value_t oldval, vlc_value_t val, void *p_data )
435 {
436     intf_thread_t *p_intf = p_data;
437     intf_sys_t *p_sys = p_intf->p_sys;
438
439     (void) psz_var; (void) oldval;
440
441     vlc_mutex_lock( &p_sys->lock );
442     if( val.i_int & p_sys->i_button_mask )
443     {
444         if( !p_sys->b_button_pressed )
445         {
446             p_sys->b_button_pressed = true;
447             var_GetCoords( p_this, "mouse-moved",
448                            &p_sys->i_last_x, &p_sys->i_last_y );
449         }
450     }
451     else
452     {
453         if( p_sys->b_button_pressed )
454         {
455             p_sys->b_button_pressed = false;
456             ProcessGesture( p_intf );
457         }
458     }
459     vlc_mutex_unlock( &p_sys->lock );
460
461     return VLC_SUCCESS;
462 }
463
464 static int InputEvent( vlc_object_t *p_this, char const *psz_var,
465                        vlc_value_t oldval, vlc_value_t val, void *p_data )
466 {
467     input_thread_t *p_input = (input_thread_t *)p_this;
468     intf_thread_t *p_intf = p_data;
469     intf_sys_t *p_sys = p_intf->p_sys;
470
471     (void) psz_var; (void) oldval;
472
473     switch( val.i_int )
474     {
475       case INPUT_EVENT_VOUT:
476         /* intf-event is serialized against itself and is the sole user of
477          * p_sys->p_vout. So there is no need to acquire the lock currently. */
478         if( p_sys->p_vout != NULL )
479         {   /* /!\ Beware of lock inversion with var_DelCallback() /!\ */
480             var_DelCallback( p_sys->p_vout, "mouse-moved", MovedEvent,
481                              p_intf );
482             var_DelCallback( p_sys->p_vout, "mouse-button-down", ButtonEvent,
483                              p_intf );
484             vlc_object_release( p_sys->p_vout );
485         }
486
487         p_sys->p_vout = input_GetVout( p_input );
488         if( p_sys->p_vout != NULL )
489         {
490             var_AddCallback( p_sys->p_vout, "mouse-moved", MovedEvent,
491                              p_intf );
492             var_AddCallback( p_sys->p_vout, "mouse-button-down", ButtonEvent,
493                              p_intf );
494         }
495         break;
496     }
497     return VLC_SUCCESS;
498 }
499
500 static int PlaylistEvent( vlc_object_t *p_this, char const *psz_var,
501                           vlc_value_t oldval, vlc_value_t val, void *p_data )
502 {
503     intf_thread_t *p_intf = p_data;
504     intf_sys_t *p_sys = p_intf->p_sys;
505     input_thread_t *p_input = val.p_address;
506
507     (void) p_this; (void) psz_var;
508
509     if( p_sys->p_input != NULL )
510     {
511         assert( p_sys->p_input == oldval.p_address );
512         var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
513     }
514
515     p_sys->p_input = p_input;
516
517     if( p_input != NULL )
518         var_AddCallback( p_input, "intf-event", InputEvent, p_intf );
519
520     return VLC_SUCCESS;
521 }