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