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