]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
d0bb0291328b25332c1df3f6236b7a899c8697df
[vlc] / modules / control / gestures.c
1 /*****************************************************************************
2  * gestures.c: control vlc with mouse gestures
3  *****************************************************************************
4  * Copyright (C) 2004 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  InitThread     ( intf_thread_t *p_intf );
72 static void EndThread      ( intf_thread_t *p_intf );
73 static int  MouseEvent     ( vlc_object_t *, char const *,
74                              vlc_value_t, vlc_value_t, void * );
75
76 /* Exported functions */
77 static void RunIntf        ( intf_thread_t *p_intf );
78
79 /*****************************************************************************
80  * Module descriptor
81  *****************************************************************************/
82 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
83 #define THRESHOLD_LONGTEXT N_( \
84     "Amount of movement required for a mouse gesture to be recorded." )
85
86 #define BUTTON_TEXT N_( "Trigger button" )
87 #define BUTTON_LONGTEXT N_( \
88     "Trigger button for mouse gestures." )
89
90 static const char *const button_list[] = { "left", "middle", "right" };
91 static const char *const button_list_text[] =
92                                    { N_("Left"), N_("Middle"), N_("Right") };
93
94 vlc_module_begin();
95     set_shortname( N_("Gestures"));
96     set_category( CAT_INTERFACE );
97     set_subcategory( SUBCAT_INTERFACE_CONTROL );
98     add_integer( "gestures-threshold", 30, NULL,
99                  THRESHOLD_TEXT, THRESHOLD_LONGTEXT, true );
100     add_string( "gestures-button", "right", NULL,
101                 BUTTON_TEXT, BUTTON_LONGTEXT, false );
102         change_string_list( button_list, button_list_text, 0 );
103     set_description( N_("Mouse gestures control interface") );
104
105     set_capability( "interface", 0 );
106     set_callbacks( Open, Close );
107 vlc_module_end();
108
109 /*****************************************************************************
110  * OpenIntf: initialize interface
111  *****************************************************************************/
112 int Open ( vlc_object_t *p_this )
113 {
114     intf_thread_t *p_intf = (intf_thread_t *)p_this;
115
116     /* Allocate instance and initialize some members */
117     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
118     if( p_intf->p_sys == NULL )
119     {
120         return( 1 );
121     };
122
123     p_intf->pf_run = RunIntf;
124
125     return( 0 );
126 }
127
128 /*****************************************************************************
129  * gesture: return a subpattern within a pattern
130  *****************************************************************************/
131 static int gesture( int i_pattern, int i_num )
132 {
133     return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
134 }
135
136 /*****************************************************************************
137  * input_from_playlist: don't forget to release the return value
138  *  Also this function should really be available from core.
139  *****************************************************************************/
140 static input_thread_t * input_from_playlist ( playlist_t *p_playlist )
141 {
142     input_thread_t * p_input;
143
144     PL_LOCK;
145     p_input = p_playlist->p_input;
146     if( p_input )
147         vlc_object_yield( p_input );
148     PL_UNLOCK;
149
150     return p_input;
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 structure */
161     free( p_intf->p_sys );
162 }
163
164
165 /*****************************************************************************
166  * RunIntf: main loop
167  *****************************************************************************/
168 static void RunIntf( intf_thread_t *p_intf )
169 {
170     playlist_t * p_playlist = NULL;
171     int canc = vlc_savecancel();
172
173     vlc_mutex_lock( &p_intf->change_lock );
174     p_intf->p_sys->p_vout = NULL;
175     vlc_mutex_unlock( &p_intf->change_lock );
176
177     if( InitThread( p_intf ) < 0 )
178     {
179         msg_Err( p_intf, "can't initialize interface thread" );
180         return;
181     }
182     msg_Dbg( p_intf, "interface thread initialized" );
183
184     /* Main loop */
185     while( vlc_object_alive( p_intf ) )
186     {
187         vlc_mutex_lock( &p_intf->change_lock );
188
189         /*
190          * mouse cursor
191          */
192         if( p_intf->p_sys->b_got_gesture )
193         {
194             vlc_value_t val;
195             int i_interval = 0;
196             /* Do something */
197             /* If you modify this, please try to follow this convention:
198                Start with LEFT, RIGHT for playback related commands
199                and UP, DOWN, for other commands */
200             switch( p_intf->p_sys->i_pattern )
201             {
202             case LEFT:
203                 i_interval = config_GetInt( p_intf , "short-jump-size" );
204                 if ( i_interval > 0 ) {
205                     val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
206                     var_Set( p_intf, "time-offset", val );
207                 }
208                 msg_Dbg(p_intf, "Go backward in the movie!");
209                 break;
210             case RIGHT:
211                 i_interval = config_GetInt( p_intf , "short-jump-size" );
212                 if ( i_interval > 0 ) {
213                     val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
214                     var_Set( p_intf, "time-offset", val );
215                 }
216                 msg_Dbg(p_intf, "Go forward in the movie!");
217                 break;
218             case GESTURE(LEFT,UP,NONE,NONE):
219                 /*FIXME BF*/
220                 msg_Dbg(p_intf, "Going slower.");
221                 break;
222             case GESTURE(RIGHT,UP,NONE,NONE):
223                 /*FIXME FF*/
224                 msg_Dbg(p_intf, "Going faster.");
225                 break;
226             case GESTURE(LEFT,RIGHT,NONE,NONE):
227             case GESTURE(RIGHT,LEFT,NONE,NONE):
228                 {
229                     input_thread_t * p_input;
230                     p_playlist = pl_Yield( p_intf );
231
232                     p_input = input_from_playlist( p_playlist );
233                     vlc_object_release( p_playlist );
234  
235                     if( !p_input )
236                         break;
237  
238                     val.i_int = PLAYING_S;
239                     if( p_input )
240                     {
241                         var_Get( p_input, "state", &val);
242                         if( val.i_int == PAUSE_S )
243                         {
244                             val.i_int = PLAYING_S;
245                         }
246                         else
247                         {
248                             val.i_int = PAUSE_S;
249                         }
250                         var_Set( p_input, "state", val);
251                     }
252                     msg_Dbg(p_intf, "Play/Pause");
253                     vlc_object_release( p_input );
254                 }
255                 break;
256             case GESTURE(LEFT,DOWN,NONE,NONE):
257                 p_playlist = pl_Yield( p_intf );
258
259                 playlist_Prev( p_playlist );
260                 vlc_object_release( p_playlist );
261                 break;
262             case GESTURE(RIGHT,DOWN,NONE,NONE):
263                 p_playlist = pl_Yield( p_intf );
264
265                 playlist_Next( p_playlist );
266                 vlc_object_release( p_playlist );
267                 break;
268             case UP:
269                 {
270                     audio_volume_t i_newvol;
271                     aout_VolumeUp( p_intf, 1, &i_newvol );
272                     msg_Dbg(p_intf, "Louder");
273                 }
274                 break;
275             case DOWN:
276                 {
277                     audio_volume_t i_newvol;
278                     aout_VolumeDown( p_intf, 1, &i_newvol );
279                     msg_Dbg(p_intf, "Quieter");
280                 }
281                 break;
282             case GESTURE(UP,DOWN,NONE,NONE):
283             case GESTURE(DOWN,UP,NONE,NONE):
284                 {
285                     audio_volume_t i_newvol = -1;
286                     aout_VolumeMute( p_intf, &i_newvol );
287                     msg_Dbg(p_intf, "Mute sound");
288                 }
289                 break;
290             case GESTURE(UP,RIGHT,NONE,NONE):
291                 {
292                    input_thread_t * p_input;
293                    vlc_value_t val, list, list2;
294                    int i_count, i;
295
296                     p_playlist = pl_Yield( p_intf );
297
298                     p_input = input_from_playlist( p_playlist );
299
300                     vlc_object_release( p_playlist );
301
302                     if( !p_input )
303                         break;
304
305                    var_Get( p_input, "audio-es", &val );
306                    var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
307                                &list, &list2 );
308                    i_count = list.p_list->i_count;
309                    if( i_count <= 1 )
310                    {
311                        vlc_object_release( p_input );
312                        break;
313                    }
314                    for( i = 0; i < i_count; i++ )
315                    {
316                        if( val.i_int == list.p_list->p_values[i].i_int )
317                        {
318                            break;
319                        }
320                    }
321                    /* value of audio-es was not in choices list */
322                    if( i == i_count )
323                    {
324                        msg_Warn( p_input,
325                                "invalid current audio track, selecting 0" );
326                        var_Set( p_input, "audio-es",
327                                list.p_list->p_values[0] );
328                        i = 0;
329                    }
330                    else if( i == i_count - 1 )
331                    {
332                        var_Set( p_input, "audio-es",
333                                list.p_list->p_values[1] );
334                        i = 1;
335                    }
336                    else
337                    {
338                        var_Set( p_input, "audio-es",
339                                list.p_list->p_values[i+1] );
340                        i++;
341                    }
342                    vlc_object_release( p_input );
343                 }
344                 break;
345             case GESTURE(DOWN,RIGHT,NONE,NONE):
346                 {
347                     input_thread_t * p_input;
348                     vlc_value_t val, list, list2;
349                     int i_count, i;
350
351                     p_playlist = pl_Yield( p_intf );
352
353                     p_input = input_from_playlist( p_playlist );
354                     vlc_object_release( p_playlist );
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                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
382                         i = 0;
383                     }
384                     else if( i == i_count - 1 )
385                     {
386                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
387                         i = 0;
388                     }
389                     else
390                     {
391                         var_Set( p_input, "spu-es",
392                                 list.p_list->p_values[i+1] );
393                         i = i + 1;
394                     }
395                     vlc_object_release( p_input );
396                 }
397                 break;
398             case GESTURE(UP,LEFT,NONE,NONE):
399                 if (p_intf->p_sys->p_vout )
400                 {
401                     ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
402                         VOUT_FULLSCREEN_CHANGE;
403                 }
404                 break;
405             case GESTURE(DOWN,LEFT,NONE,NONE):
406                 /* FIXME: Should close the vout!"*/
407                 vlc_object_kill( p_intf->p_libvlc );
408                 break;
409             case GESTURE(DOWN,LEFT,UP,RIGHT):
410             case GESTURE(UP,RIGHT,DOWN,LEFT):
411                 msg_Dbg(p_intf, "a square was drawn!" );
412                 break;
413             default:
414                 break;
415             }
416             p_intf->p_sys->i_num_gestures = 0;
417             p_intf->p_sys->i_pattern = 0;
418             p_intf->p_sys->b_got_gesture = false;
419         }
420
421         /*
422          * video output
423          */
424         if( p_intf->p_sys->p_vout && !vlc_object_alive (p_intf->p_sys->p_vout) )
425         {
426             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
427                              MouseEvent, p_intf );
428             var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
429                              MouseEvent, p_intf );
430             vlc_object_release( p_intf->p_sys->p_vout );
431             p_intf->p_sys->p_vout = NULL;
432         }
433
434         if( p_intf->p_sys->p_vout == NULL )
435         {
436             p_intf->p_sys->p_vout = vlc_object_find( p_intf,
437                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
438             if( p_intf->p_sys->p_vout )
439             {
440                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
441                                  MouseEvent, p_intf );
442                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
443                                  MouseEvent, p_intf );
444             }
445         }
446
447         vlc_mutex_unlock( &p_intf->change_lock );
448
449         /* Wait a bit */
450         msleep( INTF_IDLE_SLEEP );
451     }
452
453     EndThread( p_intf );
454     vlc_restorecancel( canc );
455 }
456
457 /*****************************************************************************
458  * InitThread:
459  *****************************************************************************/
460 static int InitThread( intf_thread_t * p_intf )
461 {
462     char *psz_button;
463     /* we might need some locking here */
464     if( vlc_object_alive( p_intf ) )
465     {
466         /* p_intf->change_lock locking strategy:
467          * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
468          * - make sure there won't be  cross increment/decrement ref count
469          *   of p_intf->p_sys members p_intf->change_lock should be locked
470          *   during those operations */
471         vlc_mutex_lock( &p_intf->change_lock );
472
473         p_intf->p_sys->b_got_gesture = false;
474         p_intf->p_sys->b_button_pressed = false;
475         p_intf->p_sys->i_threshold =
476                      config_GetInt( p_intf, "gestures-threshold" );
477         psz_button = config_GetPsz( p_intf, "gestures-button" );
478         if ( !strcmp( psz_button, "left" ) )
479         {
480             p_intf->p_sys->i_button_mask = 1;
481         }
482         else if ( !strcmp( psz_button, "middle" ) )
483         {
484             p_intf->p_sys->i_button_mask = 2;
485         }
486         else if ( !strcmp( psz_button, "right" ) )
487         {
488             p_intf->p_sys->i_button_mask = 4;
489         }
490         free( psz_button );
491
492         p_intf->p_sys->i_pattern = 0;
493         p_intf->p_sys->i_num_gestures = 0;
494         vlc_mutex_unlock( &p_intf->change_lock );
495
496         return 0;
497     }
498     else
499     {
500         return -1;
501     }
502 }
503
504 /*****************************************************************************
505  * EndThread:
506  *****************************************************************************/
507 static void EndThread( intf_thread_t * p_intf )
508 {
509     vlc_mutex_lock( &p_intf->change_lock );
510
511     if( p_intf->p_sys->p_vout )
512     {
513         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
514                          MouseEvent, p_intf );
515         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
516                          MouseEvent, p_intf );
517         vlc_object_release( p_intf->p_sys->p_vout );
518     }
519
520     vlc_mutex_unlock( &p_intf->change_lock );
521 }
522
523 /*****************************************************************************
524  * MouseEvent: callback for mouse events
525  *****************************************************************************/
526 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
527                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
528 {
529     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
530     vlc_value_t val;
531     int pattern = 0;
532
533     signed int i_horizontal, i_vertical;
534     intf_thread_t *p_intf = (intf_thread_t *)p_data;
535
536     vlc_mutex_lock( &p_intf->change_lock );
537
538     /* don't process new gestures before the last events are processed */
539     if( p_intf->p_sys->b_got_gesture )
540     {
541         vlc_mutex_unlock( &p_intf->change_lock );
542         return VLC_SUCCESS;
543     }
544
545     if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
546     {
547         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
548         p_intf->p_sys->i_mouse_x = val.i_int;
549         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
550         p_intf->p_sys->i_mouse_y = val.i_int;
551         i_horizontal = p_intf->p_sys->i_mouse_x -
552             p_intf->p_sys->i_last_x;
553         i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
554         i_vertical = p_intf->p_sys->i_mouse_y
555             - p_intf->p_sys->i_last_y;
556         i_vertical = i_vertical / p_intf->p_sys->i_threshold;
557
558         if( i_horizontal < 0 )
559         {
560             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
561             pattern = LEFT;
562         }
563         else if( i_horizontal > 0 )
564         {
565             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
566             pattern = RIGHT;
567         }
568         if( i_vertical < 0 )
569         {
570             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
571             pattern = UP;
572         }
573         else if( i_vertical > 0 )
574         {
575             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
576             pattern = DOWN;
577         }
578         if( pattern )
579         {
580             p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
581             p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
582             if( gesture( p_intf->p_sys->i_pattern,
583                          p_intf->p_sys->i_num_gestures - 1 ) != pattern )
584             {
585                 p_intf->p_sys->i_pattern |=
586                     pattern << ( p_intf->p_sys->i_num_gestures * 4 );
587                 p_intf->p_sys->i_num_gestures++;
588             }
589         }
590
591     }
592     if( !strcmp( psz_var, "mouse-button-down" )
593         && newval.i_int & p_intf->p_sys->i_button_mask
594         && !p_intf->p_sys->b_button_pressed )
595     {
596         p_intf->p_sys->b_button_pressed = true;
597         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
598         p_intf->p_sys->i_last_x = val.i_int;
599         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
600         p_intf->p_sys->i_last_y = val.i_int;
601     }
602     if( !strcmp( psz_var, "mouse-button-down" )
603         && !( newval.i_int & p_intf->p_sys->i_button_mask )
604         && p_intf->p_sys->b_button_pressed )
605     {
606         p_intf->p_sys->b_button_pressed = false;
607         p_intf->p_sys->b_got_gesture = true;
608     }
609
610     vlc_mutex_unlock( &p_intf->change_lock );
611
612     return VLC_SUCCESS;
613 }