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