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