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