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