d52697114a09ac4ec63fd01984456adc3f190492
[vlc.git] / 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     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 int gesture( int i_pattern, int 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 )
167     {
168         var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
169         vlc_object_release( p_sys->p_input );
170     }
171
172     if( p_sys->p_vout )
173     {
174         var_DelCallback( p_sys->p_vout, "mouse-moved", MovedEvent, p_intf );
175         var_DelCallback( p_sys->p_vout, "mouse-button-down",
176                          ButtonEvent, p_intf );
177         vlc_object_release( p_sys->p_vout );
178     }
179
180     /* Destroy structure */
181     vlc_mutex_destroy( &p_sys->lock );
182     free( p_sys );
183 }
184
185 static void ProcessGesture( intf_thread_t *p_intf )
186 {
187     intf_sys_t *p_sys = p_intf->p_sys;
188     playlist_t *p_playlist = pl_Get( p_intf );
189
190     /* Do something */
191     /* If you modify this, please try to follow this convention:
192        Start with LEFT, RIGHT for playback related commands
193        and UP, DOWN, for other commands */
194     switch( p_sys->i_pattern )
195     {
196         case LEFT:
197         {
198             msg_Dbg( p_intf, "Go backward in the movie!" );
199
200             input_thread_t *p_input = playlist_CurrentInput( p_playlist );
201             if( p_input == NULL )
202                 break;
203
204             int it = var_InheritInteger( p_intf , "short-jump-size" );
205             if( it > 0 )
206                 var_SetTime( p_input, "time-offset", -CLOCK_FREQ * it );
207             vlc_object_release( p_input );
208             break;
209         }
210
211         case RIGHT:
212         {
213             msg_Dbg( p_intf, "Go forward in the movie!" );
214
215             input_thread_t *p_input = playlist_CurrentInput( p_playlist );
216             if( p_input == NULL )
217                 break;
218
219             int it = var_InheritInteger( p_intf , "short-jump-size" );
220             if( it > 0 )
221                 var_SetTime( p_input, "time-offset", CLOCK_FREQ * it );
222             vlc_object_release( p_input );
223             break;
224         }
225
226         case GESTURE(LEFT,UP,NONE,NONE):
227             msg_Dbg( p_intf, "Going slower." );
228             var_TriggerCallback( p_playlist, "rate-slower" );
229             break;
230
231         case GESTURE(RIGHT,UP,NONE,NONE):
232             msg_Dbg( p_intf, "Going faster." );
233             var_TriggerCallback( p_playlist, "rate-faster" );
234             break;
235
236         case GESTURE(LEFT,RIGHT,NONE,NONE):
237         case GESTURE(RIGHT,LEFT,NONE,NONE):
238         {
239             msg_Dbg( p_intf, "Play/Pause" );
240
241             input_thread_t *p_input = playlist_CurrentInput( p_playlist );
242             if( p_input == NULL )
243                 break;
244
245             int i_state = var_GetInteger( p_input, "state" );
246             i_state = (i_state == PLAYING_S) ? PAUSE_S : PLAYING_S;
247             var_SetInteger( p_input, "state", i_state );
248             vlc_object_release( p_input );
249             break;
250         }
251
252         case GESTURE(LEFT,DOWN,NONE,NONE):
253             playlist_Prev( p_playlist );
254             break;
255
256         case GESTURE(RIGHT,DOWN,NONE,NONE):
257             playlist_Next( p_playlist );
258             break;
259
260         case UP:
261             msg_Dbg(p_intf, "Louder");
262             playlist_VolumeUp( p_playlist, 1, NULL );
263             break;
264
265         case DOWN:
266             msg_Dbg(p_intf, "Quieter");
267             playlist_VolumeDown( p_playlist, 1, NULL );
268             break;
269
270         case GESTURE(UP,DOWN,NONE,NONE):
271         case GESTURE(DOWN,UP,NONE,NONE):
272             msg_Dbg( p_intf, "Mute sound" );
273             playlist_MuteToggle( p_playlist );
274             break;
275
276         case GESTURE(UP,RIGHT,NONE,NONE):
277         {
278             input_thread_t *p_input = playlist_CurrentInput( p_playlist );
279             if( p_input == NULL )
280                 break;
281
282             vlc_value_t list, list2;
283             var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
284                         &list, &list2 );
285
286             if( list.p_list->i_count > 1 )
287             {
288                 int i_audio_es = var_GetInteger( p_input, "audio-es" );
289                 int i;
290
291                 for( i = 0; i < list.p_list->i_count; i++ )
292                      if( i_audio_es == list.p_list->p_values[i].i_int )
293                          break;
294                 /* value of audio-es was not in choices list */
295                 if( i == list.p_list->i_count )
296                 {
297                     msg_Warn( p_input,
298                               "invalid current audio track, selecting 0" );
299                     i = 0;
300                 }
301                 else if( i == list.p_list->i_count - 1 )
302                     i = 1;
303                 else
304                     i++;
305                 var_SetInteger( p_input, "audio-es",
306                                 list.p_list->p_values[i].i_int );
307             }
308             var_FreeList( &list, &list2 );
309             vlc_object_release( p_input );
310             break;
311         }
312
313         case GESTURE(DOWN,RIGHT,NONE,NONE):
314         {
315             input_thread_t *p_input = playlist_CurrentInput( p_playlist );
316             if( p_input == NULL )
317                 break;
318
319             vlc_value_t list, list2;
320             var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
321                         &list, &list2 );
322
323             if( list.p_list->i_count > 1 )
324             {
325                 int i_audio_es = var_GetInteger( p_input, "spu-es" );
326                 int i;
327
328                 for( i = 0; i < list.p_list->i_count; i++ )
329                      if( i_audio_es == list.p_list->p_values[i].i_int )
330                          break;
331                 /* value of audio-es was not in choices list */
332                 if( i == list.p_list->i_count )
333                 {
334                     msg_Warn( p_input,
335                               "invalid current subtitle track, selecting 0" );
336                     i = 0;
337                 }
338                 else if( i == list.p_list->i_count - 1 )
339                     i = 1;
340                 else
341                     i++;
342                 var_SetInteger( p_input, "audio-es",
343                                 list.p_list->p_values[i].i_int );
344             }
345             var_FreeList( &list, &list2 );
346             vlc_object_release( p_input );
347             break;
348         }
349
350         case GESTURE(UP,LEFT,NONE,NONE):
351         {
352             bool val = var_ToggleBool( pl_Get( p_intf ), "fullscreen" );
353             if( p_sys->p_vout )
354                 var_SetBool( p_sys->p_vout, "fullscreen", val );
355             break;
356         }
357
358         case GESTURE(DOWN,LEFT,NONE,NONE):
359             /* FIXME: Should close the vout!"*/
360             libvlc_Quit( p_intf->p_libvlc );
361             break;
362
363         case GESTURE(DOWN,LEFT,UP,RIGHT):
364         case GESTURE(UP,RIGHT,DOWN,LEFT):
365             msg_Dbg( p_intf, "a square was drawn!" );
366             break;
367     }
368
369     p_sys->i_num_gestures = 0;
370     p_sys->i_pattern = 0;
371 }
372
373 static int MovedEvent( vlc_object_t *p_this, char const *psz_var,
374                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
375 {
376     intf_thread_t *p_intf = (intf_thread_t *)p_data;
377     intf_sys_t    *p_sys = p_intf->p_sys;
378
379     (void) p_this; (void) psz_var; (void) oldval;
380
381     vlc_mutex_lock( &p_sys->lock );
382     if( p_sys->b_button_pressed )
383     {
384         int i_horizontal = newval.coords.x - p_sys->i_last_x;
385         int i_vertical = newval.coords.y - p_sys->i_last_y;
386         int pattern = 0;
387
388         i_horizontal = i_horizontal / p_sys->i_threshold;
389         i_vertical = i_vertical / p_sys->i_threshold;
390
391         if( i_horizontal < 0 )
392         {
393             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
394             pattern = LEFT;
395         }
396         else if( i_horizontal > 0 )
397         {
398             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
399             pattern = RIGHT;
400         }
401         if( i_vertical < 0 )
402         {
403             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
404             pattern = UP;
405         }
406         else if( i_vertical > 0 )
407         {
408             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
409             pattern = DOWN;
410         }
411
412         if( pattern )
413         {
414             p_sys->i_last_x = newval.coords.x;
415             p_sys->i_last_y = newval.coords.y;
416             if( gesture( p_sys->i_pattern, p_sys->i_num_gestures - 1 )
417                     != pattern )
418             {
419                 p_sys->i_pattern |= pattern << ( p_sys->i_num_gestures * 4 );
420                 p_sys->i_num_gestures++;
421             }
422         }
423
424     }
425     vlc_mutex_unlock( &p_sys->lock );
426
427     return VLC_SUCCESS;
428 }
429
430 static int ButtonEvent( vlc_object_t *p_this, char const *psz_var,
431                         vlc_value_t oldval, vlc_value_t val, void *p_data )
432 {
433     intf_thread_t *p_intf = p_data;
434     intf_sys_t *p_sys = p_intf->p_sys;
435
436     (void) psz_var; (void) oldval;
437
438     vlc_mutex_lock( &p_sys->lock );
439     if( val.i_int & p_sys->i_button_mask )
440     {
441         if( !p_sys->b_button_pressed )
442         {
443             p_sys->b_button_pressed = true;
444             var_GetCoords( p_this, "mouse-moved",
445                            &p_sys->i_last_x, &p_sys->i_last_y );
446         }
447     }
448     else
449     {
450         if( p_sys->b_button_pressed )
451         {
452             p_sys->b_button_pressed = false;
453             ProcessGesture( p_intf );
454         }
455     }
456     vlc_mutex_unlock( &p_sys->lock );
457
458     return VLC_SUCCESS;
459 }
460
461 static int InputEvent( vlc_object_t *p_this, char const *psz_var,
462                        vlc_value_t oldval, vlc_value_t val, void *p_data )
463 {
464     input_thread_t *p_input = (input_thread_t *)p_this;
465     intf_thread_t *p_intf = p_data;
466     intf_sys_t *p_sys = p_intf->p_sys;
467
468     (void) psz_var; (void) oldval;
469
470     switch( val.i_int )
471     {
472       case INPUT_EVENT_DEAD:
473         vlc_object_release( p_input );
474         p_sys->p_input = NULL; /* FIXME: locking!! */
475         break;
476
477       case INPUT_EVENT_VOUT:
478         /* intf-event is serialized against itself and is the sole user of
479          * p_sys->p_vout. So there is no need to acquire the lock currently. */
480         if( p_sys->p_vout != NULL )
481         {   /* /!\ Beware of lock inversion with var_DelCallback() /!\ */
482             var_DelCallback( p_sys->p_vout, "mouse-moved", MovedEvent,
483                              p_intf );
484             var_DelCallback( p_sys->p_vout, "mouse-button-down", ButtonEvent,
485                              p_intf );
486             vlc_object_release( p_sys->p_vout );
487         }
488
489         p_sys->p_vout = input_GetVout( p_input );
490         if( p_sys->p_vout != NULL )
491         {
492             var_AddCallback( p_sys->p_vout, "mouse-moved", MovedEvent,
493                              p_intf );
494             var_AddCallback( p_sys->p_vout, "mouse-button-down", ButtonEvent,
495                              p_intf );
496         }
497         break;
498     }
499     return VLC_SUCCESS;
500 }
501
502 static int PlaylistEvent( vlc_object_t *p_this, char const *psz_var,
503                           vlc_value_t oldval, vlc_value_t val, void *p_data )
504 {
505     intf_thread_t *p_intf = p_data;
506     intf_sys_t *p_sys = p_intf->p_sys;
507     input_thread_t *p_input = val.p_address;
508
509     (void) p_this; (void) psz_var; (void) oldval;
510
511     var_AddCallback( p_input, "intf-event", InputEvent, p_intf );
512     assert( p_sys->p_input == NULL );
513     p_sys->p_input = vlc_object_hold( p_input );
514     return VLC_SUCCESS;
515 }