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