]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
gestures: fix memleak with var_Change(VLC_VAR_GETCHOICES).
[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     vout_thread_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     intf_sys_t *p_sys = p_intf->p_sys;
182     playlist_t * p_playlist = NULL;
183     int canc = vlc_savecancel();
184     input_thread_t *p_input;
185
186     /* Main loop */
187     while( vlc_object_alive( p_intf ) )
188     {
189         vlc_mutex_lock( &p_sys->lock );
190
191         /*
192          * mouse cursor
193          */
194         if( p_sys->b_got_gesture )
195         {
196             vlc_value_t val;
197             int i_interval = 0;
198             /* Do something */
199             /* If you modify this, please try to follow this convention:
200                Start with LEFT, RIGHT for playback related commands
201                and UP, DOWN, for other commands */
202             switch( p_sys->i_pattern )
203             {
204             case LEFT:
205                 msg_Dbg( p_intf, "Go backward in the movie!" );
206                 p_playlist = pl_Hold( p_intf );
207                 p_input = playlist_CurrentInput( p_playlist );
208                 pl_Release( p_intf );
209                 if( p_input )
210                 {
211                     i_interval = config_GetInt( p_intf , "short-jump-size" );
212                     if ( i_interval > 0 )
213                     {
214                         val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
215                         var_Set( p_input, "time-offset", val );
216                     }
217                     vlc_object_release( p_input );
218                 }
219                 break;
220
221             case RIGHT:
222                 msg_Dbg( p_intf, "Go forward in the movie!" );
223                 p_playlist = pl_Hold( p_intf );
224                 p_input = playlist_CurrentInput( p_playlist );
225                 pl_Release( p_intf );
226
227                 if( p_input )
228                 {
229                     i_interval = config_GetInt( p_intf , "short-jump-size" );
230                     if ( i_interval > 0 )
231                     {
232                         val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
233                         var_Set( p_input, "time-offset", val );
234                     }
235                     vlc_object_release( p_input );
236                 }
237                 break;
238
239             case GESTURE(LEFT,UP,NONE,NONE):
240                 msg_Dbg( p_intf, "Going slower." );
241                 p_playlist = pl_Hold( p_intf );
242                 p_input = playlist_CurrentInput( p_playlist );
243                 pl_Release( p_intf );
244                 if( p_input )
245                 {
246                     var_SetVoid( p_input, "rate-slower" );
247                     vlc_object_release( p_input );
248                 }
249                 break;
250
251             case GESTURE(RIGHT,UP,NONE,NONE):
252                 msg_Dbg( p_intf, "Going faster." );
253                 p_playlist = pl_Hold( p_intf );
254                 p_input = playlist_CurrentInput( p_playlist );
255                 pl_Release( p_intf );
256                 if( p_input )
257                 {
258                     var_SetVoid( p_input, "rate-faster" );
259                     vlc_object_release( p_input );
260                 }
261                 break;
262
263             case GESTURE(LEFT,RIGHT,NONE,NONE):
264             case GESTURE(RIGHT,LEFT,NONE,NONE):
265                 msg_Dbg( p_intf, "Play/Pause" );
266                 p_playlist = pl_Hold( p_intf );
267                 p_input = playlist_CurrentInput( p_playlist );
268                 pl_Release( p_intf );
269  
270                 if( p_input )
271                 {
272                     var_Get( p_input, "state", &val);
273                     val.i_int = ( val.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
274                     var_Set( p_input, "state", val);
275                     vlc_object_release( p_input );
276                 }
277                 break;
278
279             case GESTURE(LEFT,DOWN,NONE,NONE):
280                 p_playlist = pl_Hold( p_intf );
281                 playlist_Prev( p_playlist );
282                 pl_Release( p_intf );
283                 break;
284
285             case GESTURE(RIGHT,DOWN,NONE,NONE):
286                 p_playlist = pl_Hold( p_intf );
287                 playlist_Next( p_playlist );
288                 pl_Release( p_intf );
289                 break;
290
291             case UP:
292                 msg_Dbg(p_intf, "Louder");
293                 aout_VolumeUp( p_intf, 1, NULL );
294                 break;
295
296             case DOWN:
297                 msg_Dbg(p_intf, "Quieter");
298                 aout_VolumeDown( p_intf, 1, NULL );
299                 break;
300
301             case GESTURE(UP,DOWN,NONE,NONE):
302             case GESTURE(DOWN,UP,NONE,NONE):
303                 msg_Dbg( p_intf, "Mute sound" );
304                 aout_VolumeMute( p_intf, NULL );
305                 break;
306
307             case GESTURE(UP,RIGHT,NONE,NONE):
308                 {
309                     vlc_value_t val, list, list2;
310                     int i_count, i;
311
312                     p_playlist = pl_Hold( p_intf );
313                     p_input = playlist_CurrentInput( p_playlist );
314                     pl_Release( p_intf );
315
316                     if( !p_input )
317                         break;
318
319                     var_Get( p_input, "audio-es", &val );
320                     var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
321                                 &list, &list2 );
322                     i_count = list.p_list->i_count;
323                     if( i_count <= 1 )
324                     {
325                         var_Change( p_input, "audio-es", VLC_VAR_FREELIST, &list,
326                                     &list2 );
327                         vlc_object_release( p_input );
328                         break;
329                     }
330                     for( i = 0; i < i_count; i++ )
331                     {
332                         if( val.i_int == list.p_list->p_values[i].i_int )
333                             break;
334                     }
335                     /* value of audio-es was not in choices list */
336                     if( i == i_count )
337                     {
338                         msg_Warn( p_input,
339                                   "invalid current audio track, selecting 0" );
340                         i = 0;
341                     }
342                     else if( i == i_count - 1 )
343                         i = 1;
344                     else
345                         i++;
346                     var_Set( p_input, "audio-es", list.p_list->p_values[i] );
347                     var_Change( p_input, "audio-es", VLC_VAR_FREELIST, &list,
348                                 &list2 );
349                     vlc_object_release( p_input );
350                 }
351                 break;
352             case GESTURE(DOWN,RIGHT,NONE,NONE):
353                 {
354                     vlc_value_t val, list, list2;
355                     int i_count, i;
356
357                     p_playlist = pl_Hold( p_intf );
358                     p_input = playlist_CurrentInput( p_playlist );
359                     pl_Release( p_intf );
360
361                     if( !p_input )
362                         break;
363
364                     var_Get( p_input, "spu-es", &val );
365
366                     var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
367                             &list, &list2 );
368                     i_count = list.p_list->i_count;
369                     if( i_count <= 1 )
370                     {
371                         vlc_object_release( p_input );
372                         var_Change( p_input, "spu-es", VLC_VAR_FREELIST,
373                                     &list, &list2 );
374                         break;
375                     }
376                     for( i = 0; i < i_count; i++ )
377                     {
378                         if( val.i_int == list.p_list->p_values[i].i_int )
379                         {
380                             break;
381                         }
382                     }
383                     /* value of spu-es was not in choices list */
384                     if( i == i_count )
385                     {
386                         msg_Warn( p_input,
387                                 "invalid current subtitle track, selecting 0" );
388                         i = 0;
389                     }
390                     else if( i == i_count - 1 )
391                         i = 0;
392                     else
393                         i++;
394                     var_Set( p_input, "spu-es", list.p_list->p_values[i] );
395                     var_Change( p_input, "spu-es", VLC_VAR_FREELIST,
396                                 &list, &list2 );
397                     vlc_object_release( p_input );
398                 }
399                 break;
400
401             case GESTURE(UP,LEFT,NONE,NONE):
402                 if( p_sys->p_vout )
403                 {
404                     var_Get( p_sys->p_vout, "fullscreen", &val );
405                     val.b_bool = !val.b_bool;
406                     var_Set( p_sys->p_vout, "fullscreen", val );
407                 }
408                 break;
409
410             case GESTURE(DOWN,LEFT,NONE,NONE):
411                 /* FIXME: Should close the vout!"*/
412                 libvlc_Quit( p_intf->p_libvlc );
413                 break;
414             case GESTURE(DOWN,LEFT,UP,RIGHT):
415             case GESTURE(UP,RIGHT,DOWN,LEFT):
416                 msg_Dbg( p_intf, "a square was drawn!" );
417                 break;
418             default:
419                 break;
420             }
421             p_sys->i_num_gestures = 0;
422             p_sys->i_pattern = 0;
423             p_sys->b_got_gesture = false;
424         }
425
426         /*
427          * video output
428          */
429         if( p_sys->p_vout && !vlc_object_alive( p_sys->p_vout ) )
430         {
431             var_DelCallback( p_sys->p_vout, "mouse-moved",
432                              MouseEvent, p_intf );
433             var_DelCallback( p_sys->p_vout, "mouse-button-down",
434                              MouseEvent, p_intf );
435             vlc_object_release( p_sys->p_vout );
436             p_sys->p_vout = NULL;
437         }
438
439         if( p_sys->p_vout == NULL )
440         {
441             p_playlist = pl_Hold( p_intf );
442             p_input = playlist_CurrentInput( p_playlist );
443             pl_Release( p_intf );
444             if( p_input )
445             {
446                 p_sys->p_vout = input_GetVout( p_input );
447                 vlc_object_release( p_input );
448             }
449             if( p_sys->p_vout )
450             {
451                 var_AddCallback( p_sys->p_vout, "mouse-moved",
452                                  MouseEvent, p_intf );
453                 var_AddCallback( p_sys->p_vout, "mouse-button-down",
454                                  MouseEvent, p_intf );
455             }
456         }
457
458         vlc_mutex_unlock( &p_sys->lock );
459
460         /* Wait a bit */
461         msleep( INTF_IDLE_SLEEP );
462     }
463
464     vlc_restorecancel( canc );
465 }
466
467 /*****************************************************************************
468  * MouseEvent: callback for mouse events
469  *****************************************************************************/
470 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
471                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
472 {
473     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
474     int pattern = 0;
475
476     signed int i_horizontal, i_vertical;
477     intf_thread_t *p_intf = (intf_thread_t *)p_data;
478     intf_sys_t    *p_sys = p_intf->p_sys;
479
480     vlc_mutex_lock( &p_sys->lock );
481
482     /* don't process new gestures before the last events are processed */
483     if( p_sys->b_got_gesture )
484     {
485         vlc_mutex_unlock( &p_sys->lock );
486         return VLC_SUCCESS;
487     }
488
489     if( !strcmp( psz_var, "mouse-moved" ) && p_sys->b_button_pressed )
490     {
491         p_sys->i_mouse_x = var_GetInteger( p_sys->p_vout, "mouse-x" );
492         p_sys->i_mouse_y = var_GetInteger( p_sys->p_vout, "mouse-y" );
493         i_horizontal = p_sys->i_mouse_x - p_sys->i_last_x;
494         i_horizontal = i_horizontal / p_sys->i_threshold;
495         i_vertical = p_sys->i_mouse_y - p_sys->i_last_y;
496         i_vertical = i_vertical / p_sys->i_threshold;
497
498         if( i_horizontal < 0 )
499         {
500             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
501             pattern = LEFT;
502         }
503         else if( i_horizontal > 0 )
504         {
505             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
506             pattern = RIGHT;
507         }
508         if( i_vertical < 0 )
509         {
510             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
511             pattern = UP;
512         }
513         else if( i_vertical > 0 )
514         {
515             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
516             pattern = DOWN;
517         }
518         if( pattern )
519         {
520             p_sys->i_last_y = p_sys->i_mouse_y;
521             p_sys->i_last_x = p_sys->i_mouse_x;
522             if( gesture( p_sys->i_pattern, p_sys->i_num_gestures - 1 )
523                     != pattern )
524             {
525                 p_sys->i_pattern |= pattern << ( p_sys->i_num_gestures * 4 );
526                 p_sys->i_num_gestures++;
527             }
528         }
529
530     }
531     else if( !strcmp( psz_var, "mouse-button-down" ) )
532     {
533         if( (newval.i_int & p_sys->i_button_mask) && !p_sys->b_button_pressed )
534         {
535             p_sys->b_button_pressed = true;
536             p_sys->i_last_x = var_GetInteger( p_sys->p_vout, "mouse-x" );
537             p_sys->i_last_y = var_GetInteger( p_sys->p_vout, "mouse-y" );
538         }
539         else if( !( newval.i_int & p_sys->i_button_mask ) && p_sys->b_button_pressed )
540         {
541             p_sys->b_button_pressed = false;
542             p_sys->b_got_gesture = true;
543         }
544     }
545
546     vlc_mutex_unlock( &p_sys->lock );
547
548     return VLC_SUCCESS;
549 }