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