]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
e7003c9abd601b0a7c5622fd05544118045cdaab
[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
172     vlc_mutex_lock( &p_intf->change_lock );
173     p_intf->p_sys->p_vout = NULL;
174     vlc_mutex_unlock( &p_intf->change_lock );
175
176     if( InitThread( p_intf ) < 0 )
177     {
178         msg_Err( p_intf, "can't initialize interface thread" );
179         return;
180     }
181     msg_Dbg( p_intf, "interface thread initialized" );
182
183     /* Main loop */
184     while( !intf_ShouldDie( p_intf ) )
185     {
186         vlc_mutex_lock( &p_intf->change_lock );
187
188         /*
189          * mouse cursor
190          */
191         if( p_intf->p_sys->b_got_gesture )
192         {
193             vlc_value_t val;
194             int i_interval = 0;
195             /* Do something */
196             /* If you modify this, please try to follow this convention:
197                Start with LEFT, RIGHT for playback related commands
198                and UP, DOWN, for other commands */
199             switch( p_intf->p_sys->i_pattern )
200             {
201             case LEFT:
202                 i_interval = config_GetInt( p_intf , "short-jump-size" );
203                 if ( i_interval > 0 ) {
204                     val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
205                     var_Set( p_intf, "time-offset", val );
206                 }
207                 msg_Dbg(p_intf, "Go backward in the movie!");
208                 break;
209             case RIGHT:
210                 i_interval = config_GetInt( p_intf , "short-jump-size" );
211                 if ( i_interval > 0 ) {
212                     val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
213                     var_Set( p_intf, "time-offset", val );
214                 }
215                 msg_Dbg(p_intf, "Go forward in the movie!");
216                 break;
217             case GESTURE(LEFT,UP,NONE,NONE):
218                 /*FIXME BF*/
219                 msg_Dbg(p_intf, "Going slower.");
220                 break;
221             case GESTURE(RIGHT,UP,NONE,NONE):
222                 /*FIXME FF*/
223                 msg_Dbg(p_intf, "Going faster.");
224                 break;
225             case GESTURE(LEFT,RIGHT,NONE,NONE):
226             case GESTURE(RIGHT,LEFT,NONE,NONE):
227                 {
228                     input_thread_t * p_input;
229                     p_playlist = pl_Yield( p_intf );
230
231                     p_input = input_from_playlist( p_playlist );
232                     vlc_object_release( p_playlist );
233  
234                     if( !p_input )
235                         break;
236  
237                     val.i_int = PLAYING_S;
238                     if( p_input )
239                     {
240                         var_Get( p_input, "state", &val);
241                         if( val.i_int == PAUSE_S )
242                         {
243                             val.i_int = PLAYING_S;
244                         }
245                         else
246                         {
247                             val.i_int = PAUSE_S;
248                         }
249                         var_Set( p_input, "state", val);
250                     }
251                     msg_Dbg(p_intf, "Play/Pause");
252                     vlc_object_release( p_input );
253                 }
254                 break;
255             case GESTURE(LEFT,DOWN,NONE,NONE):
256                 p_playlist = pl_Yield( p_intf );
257
258                 playlist_Prev( p_playlist );
259                 vlc_object_release( p_playlist );
260                 break;
261             case GESTURE(RIGHT,DOWN,NONE,NONE):
262                 p_playlist = pl_Yield( p_intf );
263
264                 playlist_Next( p_playlist );
265                 vlc_object_release( p_playlist );
266                 break;
267             case UP:
268                 {
269                     audio_volume_t i_newvol;
270                     aout_VolumeUp( p_intf, 1, &i_newvol );
271                     msg_Dbg(p_intf, "Louder");
272                 }
273                 break;
274             case DOWN:
275                 {
276                     audio_volume_t i_newvol;
277                     aout_VolumeDown( p_intf, 1, &i_newvol );
278                     msg_Dbg(p_intf, "Quieter");
279                 }
280                 break;
281             case GESTURE(UP,DOWN,NONE,NONE):
282             case GESTURE(DOWN,UP,NONE,NONE):
283                 {
284                     audio_volume_t i_newvol = -1;
285                     aout_VolumeMute( p_intf, &i_newvol );
286                     msg_Dbg(p_intf, "Mute sound");
287                 }
288                 break;
289             case GESTURE(UP,RIGHT,NONE,NONE):
290                 {
291                    input_thread_t * p_input;
292                    vlc_value_t val, list, list2;
293                    int i_count, i;
294
295                     p_playlist = pl_Yield( p_intf );
296
297                     p_input = input_from_playlist( p_playlist );
298
299                     vlc_object_release( p_playlist );
300
301                     if( !p_input )
302                         break;
303
304                    var_Get( p_input, "audio-es", &val );
305                    var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
306                                &list, &list2 );
307                    i_count = list.p_list->i_count;
308                    if( i_count <= 1 )
309                    {
310                        vlc_object_release( p_input );
311                        break;
312                    }
313                    for( i = 0; i < i_count; i++ )
314                    {
315                        if( val.i_int == list.p_list->p_values[i].i_int )
316                        {
317                            break;
318                        }
319                    }
320                    /* value of audio-es was not in choices list */
321                    if( i == i_count )
322                    {
323                        msg_Warn( p_input,
324                                "invalid current audio track, selecting 0" );
325                        var_Set( p_input, "audio-es",
326                                list.p_list->p_values[0] );
327                        i = 0;
328                    }
329                    else if( i == i_count - 1 )
330                    {
331                        var_Set( p_input, "audio-es",
332                                list.p_list->p_values[1] );
333                        i = 1;
334                    }
335                    else
336                    {
337                        var_Set( p_input, "audio-es",
338                                list.p_list->p_values[i+1] );
339                        i++;
340                    }
341                    vlc_object_release( p_input );
342                 }
343                 break;
344             case GESTURE(DOWN,RIGHT,NONE,NONE):
345                 {
346                     input_thread_t * p_input;
347                     vlc_value_t val, list, list2;
348                     int i_count, i;
349
350                     p_playlist = pl_Yield( p_intf );
351
352                     p_input = input_from_playlist( p_playlist );
353                     vlc_object_release( p_playlist );
354
355                     if( !p_input )
356                         break;
357
358                     var_Get( p_input, "spu-es", &val );
359
360                     var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
361                             &list, &list2 );
362                     i_count = list.p_list->i_count;
363                     if( i_count <= 1 )
364                     {
365                         vlc_object_release( p_input );
366                         break;
367                     }
368                     for( i = 0; i < i_count; i++ )
369                     {
370                         if( val.i_int == list.p_list->p_values[i].i_int )
371                         {
372                             break;
373                         }
374                     }
375                     /* value of spu-es was not in choices list */
376                     if( i == i_count )
377                     {
378                         msg_Warn( p_input,
379                                 "invalid current subtitle track, selecting 0" );
380                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
381                         i = 0;
382                     }
383                     else if( i == i_count - 1 )
384                     {
385                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
386                         i = 0;
387                     }
388                     else
389                     {
390                         var_Set( p_input, "spu-es",
391                                 list.p_list->p_values[i+1] );
392                         i = i + 1;
393                     }
394                     vlc_object_release( p_input );
395                 }
396                 break;
397             case GESTURE(UP,LEFT,NONE,NONE):
398                 if (p_intf->p_sys->p_vout )
399                 {
400                     ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
401                         VOUT_FULLSCREEN_CHANGE;
402                 }
403                 break;
404             case GESTURE(DOWN,LEFT,NONE,NONE):
405                 /* FIXME: Should close the vout!"*/
406                 vlc_object_kill( p_intf->p_libvlc );
407                 break;
408             case GESTURE(DOWN,LEFT,UP,RIGHT):
409             case GESTURE(UP,RIGHT,DOWN,LEFT):
410                 msg_Dbg(p_intf, "a square was drawn!" );
411                 break;
412             default:
413                 break;
414             }
415             p_intf->p_sys->i_num_gestures = 0;
416             p_intf->p_sys->i_pattern = 0;
417             p_intf->p_sys->b_got_gesture = false;
418         }
419
420         /*
421          * video output
422          */
423         if( p_intf->p_sys->p_vout && !vlc_object_alive (p_intf->p_sys->p_vout) )
424         {
425             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
426                              MouseEvent, p_intf );
427             var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
428                              MouseEvent, p_intf );
429             vlc_object_release( p_intf->p_sys->p_vout );
430             p_intf->p_sys->p_vout = NULL;
431         }
432
433         if( p_intf->p_sys->p_vout == NULL )
434         {
435             p_intf->p_sys->p_vout = vlc_object_find( p_intf,
436                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
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     EndThread( p_intf );
453 }
454
455 /*****************************************************************************
456  * InitThread:
457  *****************************************************************************/
458 static int InitThread( intf_thread_t * p_intf )
459 {
460     char *psz_button;
461     /* we might need some locking here */
462     if( !intf_ShouldDie( p_intf ) )
463     {
464         /* p_intf->change_lock locking strategy:
465          * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
466          * - make sure there won't be  cross increment/decrement ref count
467          *   of p_intf->p_sys members p_intf->change_lock should be locked
468          *   during those operations */
469         vlc_mutex_lock( &p_intf->change_lock );
470
471         p_intf->p_sys->b_got_gesture = false;
472         p_intf->p_sys->b_button_pressed = false;
473         p_intf->p_sys->i_threshold =
474                      config_GetInt( p_intf, "gestures-threshold" );
475         psz_button = config_GetPsz( p_intf, "gestures-button" );
476         if ( !strcmp( psz_button, "left" ) )
477         {
478             p_intf->p_sys->i_button_mask = 1;
479         }
480         else if ( !strcmp( psz_button, "middle" ) )
481         {
482             p_intf->p_sys->i_button_mask = 2;
483         }
484         else if ( !strcmp( psz_button, "right" ) )
485         {
486             p_intf->p_sys->i_button_mask = 4;
487         }
488
489         p_intf->p_sys->i_pattern = 0;
490         p_intf->p_sys->i_num_gestures = 0;
491         vlc_mutex_unlock( &p_intf->change_lock );
492
493         return 0;
494     }
495     else
496     {
497         return -1;
498     }
499 }
500
501 /*****************************************************************************
502  * EndThread:
503  *****************************************************************************/
504 static void EndThread( intf_thread_t * p_intf )
505 {
506     vlc_mutex_lock( &p_intf->change_lock );
507
508     if( p_intf->p_sys->p_vout )
509     {
510         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
511                          MouseEvent, p_intf );
512         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
513                          MouseEvent, p_intf );
514         vlc_object_release( p_intf->p_sys->p_vout );
515     }
516
517     vlc_mutex_unlock( &p_intf->change_lock );
518 }
519
520 /*****************************************************************************
521  * MouseEvent: callback for mouse events
522  *****************************************************************************/
523 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
524                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
525 {
526     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
527     vlc_value_t val;
528     int pattern = 0;
529
530     signed int i_horizontal, i_vertical;
531     intf_thread_t *p_intf = (intf_thread_t *)p_data;
532
533     vlc_mutex_lock( &p_intf->change_lock );
534
535     /* don't process new gestures before the last events are processed */
536     if( p_intf->p_sys->b_got_gesture )
537     {
538         vlc_mutex_unlock( &p_intf->change_lock );
539         return VLC_SUCCESS;
540     }
541
542     if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
543     {
544         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
545         p_intf->p_sys->i_mouse_x = val.i_int;
546         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
547         p_intf->p_sys->i_mouse_y = val.i_int;
548         i_horizontal = p_intf->p_sys->i_mouse_x -
549             p_intf->p_sys->i_last_x;
550         i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
551         i_vertical = p_intf->p_sys->i_mouse_y
552             - p_intf->p_sys->i_last_y;
553         i_vertical = i_vertical / p_intf->p_sys->i_threshold;
554
555         if( i_horizontal < 0 )
556         {
557             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
558             pattern = LEFT;
559         }
560         else if( i_horizontal > 0 )
561         {
562             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
563             pattern = RIGHT;
564         }
565         if( i_vertical < 0 )
566         {
567             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
568             pattern = UP;
569         }
570         else if( i_vertical > 0 )
571         {
572             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
573             pattern = DOWN;
574         }
575         if( pattern )
576         {
577             p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
578             p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
579             if( gesture( p_intf->p_sys->i_pattern,
580                          p_intf->p_sys->i_num_gestures - 1 ) != pattern )
581             {
582                 p_intf->p_sys->i_pattern |=
583                     pattern << ( p_intf->p_sys->i_num_gestures * 4 );
584                 p_intf->p_sys->i_num_gestures++;
585             }
586         }
587
588     }
589     if( !strcmp( psz_var, "mouse-button-down" )
590         && newval.i_int & p_intf->p_sys->i_button_mask
591         && !p_intf->p_sys->b_button_pressed )
592     {
593         p_intf->p_sys->b_button_pressed = true;
594         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
595         p_intf->p_sys->i_last_x = val.i_int;
596         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
597         p_intf->p_sys->i_last_y = val.i_int;
598     }
599     if( !strcmp( psz_var, "mouse-button-down" )
600         && !( newval.i_int & p_intf->p_sys->i_button_mask )
601         && p_intf->p_sys->b_button_pressed )
602     {
603         p_intf->p_sys->b_button_pressed = false;
604         p_intf->p_sys->b_got_gesture = true;
605     }
606
607     vlc_mutex_unlock( &p_intf->change_lock );
608
609     return VLC_SUCCESS;
610 }