]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
New gestures actions.
[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 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc_interface.h>
32 #include <vlc_vout.h>
33 #include <vlc_aout.h>
34 #include <vlc_playlist.h>
35
36 #ifdef HAVE_UNISTD_H
37 #    include <unistd.h>
38 #endif
39
40 /*****************************************************************************
41  * intf_sys_t: description and status of interface
42  *****************************************************************************/
43 struct intf_sys_t
44 {
45     vlc_object_t *      p_vout;
46     input_thread_t *    p_input;
47     vlc_bool_t          b_got_gesture;
48     vlc_bool_t          b_button_pressed;
49     int                 i_mouse_x, i_mouse_y;
50     int                 i_last_x, i_last_y;
51     unsigned int        i_pattern;
52     int                 i_num_gestures;
53     int                 i_threshold;
54     int                 i_button_mask;
55 };
56
57 /*****************************************************************************
58  * Local prototypes.
59  *****************************************************************************/
60 #define UP 1
61 #define DOWN 2
62 #define LEFT 3
63 #define RIGHT 4
64 #define NONE 0
65 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
66
67 int  E_(Open)   ( vlc_object_t * );
68 void E_(Close)  ( vlc_object_t * );
69 static int  InitThread     ( intf_thread_t *p_intf );
70 static void EndThread      ( intf_thread_t *p_intf );
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 *button_list[] = { "left", "middle", "right" };
89 static const char *button_list_text[] =
90                                    { N_("Left"), N_("Middle"), N_("Right") };
91
92 vlc_module_begin();
93     set_shortname( _("Gestures"));
94     set_category( CAT_INTERFACE );
95     set_subcategory( SUBCAT_INTERFACE_CONTROL );
96     add_integer( "gestures-threshold", 30, NULL,
97                  THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
98     add_string( "gestures-button", "right", NULL,
99                 BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
100         change_string_list( button_list, button_list_text, 0 );
101     set_description( _("Mouse gestures control interface") );
102
103     set_capability( "interface", 0 );
104     set_callbacks( E_(Open), E_(Close) );
105 vlc_module_end();
106
107 /*****************************************************************************
108  * OpenIntf: initialize interface
109  *****************************************************************************/
110 int E_(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     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
116     if( p_intf->p_sys == NULL )
117     {
118         return( 1 );
119     };
120
121     p_intf->pf_run = RunIntf;
122
123     return( 0 );
124 }
125
126 /*****************************************************************************
127  * gesture: return a subpattern within a pattern
128  *****************************************************************************/
129 static int gesture( int i_pattern, int i_num )
130 {
131     return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
132 }
133
134 /*****************************************************************************
135  * CloseIntf: destroy dummy interface
136  *****************************************************************************/
137 void E_(Close) ( vlc_object_t *p_this )
138 {
139     intf_thread_t *p_intf = (intf_thread_t *)p_this;
140
141     /* Destroy structure */
142     free( p_intf->p_sys );
143 }
144
145
146 /*****************************************************************************
147  * RunIntf: main loop
148  *****************************************************************************/
149 static void RunIntf( intf_thread_t *p_intf )
150 {
151     playlist_t * p_playlist = NULL;
152
153     vlc_mutex_lock( &p_intf->change_lock );
154     p_intf->p_sys->p_vout = NULL;
155     vlc_mutex_unlock( &p_intf->change_lock );
156
157     input_thread_t * p_input = p_intf->p_sys->p_input;
158
159     if( InitThread( p_intf ) < 0 )
160     {
161         msg_Err( p_intf, "can't initialize interface thread" );
162         return;
163     }
164     msg_Dbg( p_intf, "interface thread initialized" );
165
166     /* Main loop */
167     while( !intf_ShouldDie( p_intf ) )
168     {
169         vlc_mutex_lock( &p_intf->change_lock );
170
171         /*
172          * mouse cursor
173          */
174         if( p_intf->p_sys->b_got_gesture )
175         {
176             vlc_value_t val;
177             int i_interval = 0;
178             /* Do something */
179             /* If you modify this, please try to follow this convention:
180                Start with LEFT, RIGHT for playback related commands
181                and UP, DOWN, for other commands */
182             switch( p_intf->p_sys->i_pattern )
183             {
184             case LEFT:
185                 i_interval = config_GetInt( p_intf , "short-jump-size" );
186                 if ( i_interval > 0 ) {
187                     val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
188                     var_Set( p_intf, "time-offset", val );
189                 }
190                 msg_Dbg(p_intf, "Go backward in the movie!");
191                 break;
192             case RIGHT:
193                 i_interval = config_GetInt( p_intf , "short-jump-size" );
194                 if ( i_interval > 0 ) {
195                     val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
196                     var_Set( p_intf, "time-offset", val );
197                 }
198                 msg_Dbg(p_intf, "Go forward in the movie!");
199                 break;
200             case GESTURE(LEFT,UP,NONE,NONE):
201                 /*FIXME BF*/
202                 msg_Dbg(p_intf, "Going slower.");
203                 break;
204             case GESTURE(RIGHT,UP,NONE,NONE):
205                 /*FIXME FF*/
206                 msg_Dbg(p_intf, "Going faster.");
207                 break;
208             case GESTURE(LEFT,RIGHT,NONE,NONE):
209             case GESTURE(RIGHT,LEFT,NONE,NONE):
210                 val.i_int = PLAYING_S;
211                 if( p_input )
212                 {
213                     var_Get( p_input, "state", &val);
214                     if( val.i_int == PAUSE_S )
215                     {
216                         val.i_int = PLAYING_S;
217                     }
218                     else
219                     {
220                         val.i_int = PAUSE_S;
221                     }
222                     var_Set( p_input, "state", val);
223                 }
224                 msg_Dbg(p_intf, "Play/Pause");
225                 break;
226             case GESTURE(LEFT,DOWN,NONE,NONE):
227                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
228                                               FIND_ANYWHERE );
229                 if( p_playlist == NULL )
230                 {
231                     break;
232                 }
233
234                 playlist_Prev( p_playlist );
235                 vlc_object_release( p_playlist );
236                 break;
237             case GESTURE(RIGHT,DOWN,NONE,NONE):
238                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
239                                               FIND_ANYWHERE );
240                 if( p_playlist == NULL )
241                 {
242                     break;
243                 }
244
245                 playlist_Next( p_playlist );
246                 vlc_object_release( p_playlist );
247                 break;
248             case UP:
249                 {
250                     audio_volume_t i_newvol;
251                     aout_VolumeUp( p_intf, 1, &i_newvol );
252                     msg_Dbg(p_intf, "Louder");
253                 }
254                 break;
255             case DOWN:
256                 {
257                     audio_volume_t i_newvol;
258                     aout_VolumeDown( p_intf, 1, &i_newvol );
259                     msg_Dbg(p_intf, "Quieter");
260                 }
261                 break;
262             case GESTURE(UP,DOWN,NONE,NONE):
263             case GESTURE(DOWN,UP,NONE,NONE):
264                 {
265                     audio_volume_t i_newvol = -1;
266                     aout_VolumeMute( p_intf, &i_newvol );
267                     msg_Dbg(p_intf, "Mute sound");
268                 }
269                 break;
270             case GESTURE(UP,RIGHT,NONE,NONE):
271                 {
272                    vlc_value_t val, list, list2;
273                    int i_count, i;
274                    var_Get( p_input, "audio-es", &val );
275                    var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
276                                &list, &list2 );
277                    i_count = list.p_list->i_count;
278                    if( i_count <= 1 )
279                    {
280                        continue;
281                    }
282                    for( i = 0; i < i_count; i++ )
283                    {
284                        if( val.i_int == list.p_list->p_values[i].i_int )
285                        {
286                            break;
287                        }
288                    }
289                    /* value of audio-es was not in choices list */
290                    if( i == i_count )
291                    {
292                        msg_Warn( p_input,
293                                "invalid current audio track, selecting 0" );
294                        var_Set( p_input, "audio-es",
295                                list.p_list->p_values[0] );
296                        i = 0;
297                    }
298                    else if( i == i_count - 1 )
299                    {
300                        var_Set( p_input, "audio-es",
301                                list.p_list->p_values[1] );
302                        i = 1;
303                    }
304                    else
305                    {
306                        var_Set( p_input, "audio-es",
307                                list.p_list->p_values[i+1] );
308                        i++;
309                    }
310                 }
311                 break;
312             case GESTURE(DOWN,RIGHT,NONE,NONE):
313                 {
314                     vlc_value_t val, list, list2;
315                     int i_count, i;
316                     var_Get( p_input, "spu-es", &val );
317
318                     var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
319                             &list, &list2 );
320                     i_count = list.p_list->i_count;
321                     if( i_count <= 1 )
322                     {
323                         continue;
324                     }
325                     for( i = 0; i < i_count; i++ )
326                     {
327                         if( val.i_int == list.p_list->p_values[i].i_int )
328                         {
329                             break;
330                         }
331                     }
332                     /* value of spu-es was not in choices list */
333                     if( i == i_count )
334                     {
335                         msg_Warn( p_input,
336                                 "invalid current subtitle track, selecting 0" );
337                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
338                         i = 0;
339                     }
340                     else if( i == i_count - 1 )
341                     {
342                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
343                         i = 0;
344                     }
345                     else
346                     {
347                         var_Set( p_input, "spu-es", 
348                                 list.p_list->p_values[i+1] );
349                         i = i + 1;
350                     }
351                 }
352                 break;
353             case GESTURE(UP,LEFT,NONE,NONE):
354                 if (p_intf->p_sys->p_vout )
355                 {
356                     ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
357                         VOUT_FULLSCREEN_CHANGE;
358                 }
359                 break;
360             case GESTURE(DOWN,LEFT,NONE,NONE):
361                 /* FIXME: Should close the vout!"*/
362                 p_intf->p_libvlc->b_die = VLC_TRUE;
363                 break;
364             case GESTURE(DOWN,LEFT,UP,RIGHT):
365             case GESTURE(UP,RIGHT,DOWN,LEFT):
366                 msg_Dbg(p_intf, "a square was drawn!" );
367                 break;
368             default:
369                 break;
370             }
371             p_intf->p_sys->i_num_gestures = 0;
372             p_intf->p_sys->i_pattern = 0;
373             p_intf->p_sys->b_got_gesture = VLC_FALSE;
374         }
375
376         /*
377          * video output
378          */
379         if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
380         {
381             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
382                              MouseEvent, p_intf );
383             var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
384                              MouseEvent, p_intf );
385             vlc_object_release( p_intf->p_sys->p_vout );
386             p_intf->p_sys->p_vout = NULL;
387         }
388
389         if( p_intf->p_sys->p_vout == NULL )
390         {
391             p_intf->p_sys->p_vout = vlc_object_find( p_intf,
392                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
393             if( p_intf->p_sys->p_vout )
394             {
395                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
396                                  MouseEvent, p_intf );
397                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
398                                  MouseEvent, p_intf );
399             }
400         }
401
402         vlc_mutex_unlock( &p_intf->change_lock );
403
404         /* Wait a bit */
405         msleep( INTF_IDLE_SLEEP );
406     }
407
408     EndThread( p_intf );
409 }
410
411 /*****************************************************************************
412  * InitThread:
413  *****************************************************************************/
414 static int InitThread( intf_thread_t * p_intf )
415 {
416     char *psz_button;
417     /* we might need some locking here */
418     if( !intf_ShouldDie( p_intf ) )
419     {
420         /* p_intf->change_lock locking strategy:
421          * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
422          * - make sure there won't be  cross increment/decrement ref count
423          *   of p_intf->p_sys members p_intf->change_lock should be locked
424          *   during those operations */
425         vlc_mutex_lock( &p_intf->change_lock );
426
427         /* p_intf->p_sys->p_input references counting strategy:
428          * - InitThread is responsible for only one ref count retaining
429          * - EndThread is responsible for the corresponding release */
430         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
431                                                   FIND_PARENT );
432
433         p_intf->p_sys->b_got_gesture = VLC_FALSE;
434         p_intf->p_sys->b_button_pressed = VLC_FALSE;
435         p_intf->p_sys->i_threshold =
436                      config_GetInt( p_intf, "gestures-threshold" );
437         psz_button = config_GetPsz( p_intf, "gestures-button" );
438         if ( !strcmp( psz_button, "left" ) )
439         {
440             p_intf->p_sys->i_button_mask = 1;
441         }
442         else if ( !strcmp( psz_button, "middle" ) )
443         {
444             p_intf->p_sys->i_button_mask = 2;
445         }
446         else if ( !strcmp( psz_button, "right" ) )
447         {
448             p_intf->p_sys->i_button_mask = 4;
449         }
450
451         p_intf->p_sys->i_pattern = 0;
452         p_intf->p_sys->i_num_gestures = 0;
453         vlc_mutex_unlock( &p_intf->change_lock );
454
455         return 0;
456     }
457     else
458     {
459         return -1;
460     }
461 }
462
463 /*****************************************************************************
464  * EndThread:
465  *****************************************************************************/
466 static void EndThread( intf_thread_t * p_intf )
467 {
468     vlc_mutex_lock( &p_intf->change_lock );
469
470     if( p_intf->p_sys->p_vout )
471     {
472         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
473                          MouseEvent, p_intf );
474         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
475                          MouseEvent, p_intf );
476         vlc_object_release( p_intf->p_sys->p_vout );
477     }
478
479     if( p_intf->p_sys->p_input )
480     {
481         vlc_object_release(p_intf->p_sys->p_input);
482     }
483
484     vlc_mutex_unlock( &p_intf->change_lock );
485 }
486
487 /*****************************************************************************
488  * MouseEvent: callback for mouse events
489  *****************************************************************************/
490 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
491                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
492 {
493     vlc_value_t val;
494     int pattern = 0;
495
496     signed int i_horizontal, i_vertical;
497     intf_thread_t *p_intf = (intf_thread_t *)p_data;
498
499     vlc_mutex_lock( &p_intf->change_lock );
500
501     /* don't process new gestures before the last events are processed */
502     if( p_intf->p_sys->b_got_gesture )
503     {
504         vlc_mutex_unlock( &p_intf->change_lock );
505         return VLC_SUCCESS;
506     }
507
508     if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
509     {
510         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
511         p_intf->p_sys->i_mouse_x = val.i_int;
512         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
513         p_intf->p_sys->i_mouse_y = val.i_int;
514         i_horizontal = p_intf->p_sys->i_mouse_x -
515             p_intf->p_sys->i_last_x;
516         i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
517         i_vertical = p_intf->p_sys->i_mouse_y
518             - p_intf->p_sys->i_last_y;
519         i_vertical = i_vertical / p_intf->p_sys->i_threshold;
520
521         if( i_horizontal < 0 )
522         {
523             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
524             pattern = LEFT;
525         }
526         else if( i_horizontal > 0 )
527         {
528             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
529             pattern = RIGHT;
530         }
531         if( i_vertical < 0 )
532         {
533             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
534             pattern = UP;
535         }
536         else if( i_vertical > 0 )
537         {
538             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
539             pattern = DOWN;
540         }
541         if( pattern )
542         {
543             p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
544             p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
545             if( gesture( p_intf->p_sys->i_pattern,
546                          p_intf->p_sys->i_num_gestures - 1 ) != pattern )
547             {
548                 p_intf->p_sys->i_pattern |=
549                     pattern << ( p_intf->p_sys->i_num_gestures * 4 );
550                 p_intf->p_sys->i_num_gestures++;
551             }
552         }
553
554     }
555     if( !strcmp( psz_var, "mouse-button-down" )
556         && newval.i_int & p_intf->p_sys->i_button_mask
557         && !p_intf->p_sys->b_button_pressed )
558     {
559         p_intf->p_sys->b_button_pressed = VLC_TRUE;
560         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
561         p_intf->p_sys->i_last_x = val.i_int;
562         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
563         p_intf->p_sys->i_last_y = val.i_int;
564     }
565     if( !strcmp( psz_var, "mouse-button-down" )
566         && !( newval.i_int & p_intf->p_sys->i_button_mask )
567         && p_intf->p_sys->b_button_pressed )
568     {
569         p_intf->p_sys->b_button_pressed = VLC_FALSE;
570         p_intf->p_sys->b_got_gesture = VLC_TRUE;
571     }
572
573     vlc_mutex_unlock( &p_intf->change_lock );
574
575     return VLC_SUCCESS;
576 }