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