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