]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
81e778b56d8eeba343a275ee9a6a32698e8dc663
[vlc] / modules / control / gestures.c
1 /*****************************************************************************
2  * gestures.c: control vlc with mouse gestures
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29 #include <vlc_interface.h>
30 #include <vlc_vout.h>
31 #include <vlc_aout.h>
32 #include <vlc_playlist.h>
33
34 #ifdef HAVE_UNISTD_H
35 #    include <unistd.h>
36 #endif
37
38 /*****************************************************************************
39  * intf_sys_t: description and status of interface
40  *****************************************************************************/
41 struct intf_sys_t
42 {
43     vlc_object_t *      p_vout;
44     vlc_bool_t          b_got_gesture;
45     vlc_bool_t          b_button_pressed;
46     int                 i_mouse_x, i_mouse_y;
47     int                 i_last_x, i_last_y;
48     unsigned int        i_pattern;
49     int                 i_num_gestures;
50     int                 i_threshold;
51     int                 i_button_mask;
52 };
53
54 /*****************************************************************************
55  * Local prototypes.
56  *****************************************************************************/
57 #define UP 1
58 #define DOWN 2
59 #define LEFT 3
60 #define RIGHT 4
61 #define NONE 0
62 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
63
64 int  E_(Open)   ( vlc_object_t * );
65 void E_(Close)  ( vlc_object_t * );
66 static int  InitThread     ( intf_thread_t *p_intf );
67 static void EndThread      ( intf_thread_t *p_intf );
68 static int  MouseEvent     ( vlc_object_t *, char const *,
69                              vlc_value_t, vlc_value_t, void * );
70
71 /* Exported functions */
72 static void RunIntf        ( intf_thread_t *p_intf );
73
74 /*****************************************************************************
75  * Module descriptor
76  *****************************************************************************/
77 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
78 #define THRESHOLD_LONGTEXT N_( \
79     "Amount of movement required for a mouse gesture to be recorded." )
80
81 #define BUTTON_TEXT N_( "Trigger button" )
82 #define BUTTON_LONGTEXT N_( \
83     "Trigger button for mouse gestures." )
84
85 static const char *button_list[] = { "left", "middle", "right" };
86 static const char *button_list_text[] =
87                                    { N_("Left"), N_("Middle"), N_("Right") };
88
89 vlc_module_begin();
90     set_shortname( _("Gestures"));
91     set_category( CAT_INTERFACE );
92     set_subcategory( SUBCAT_INTERFACE_CONTROL );
93     add_integer( "gestures-threshold", 30, NULL,
94                  THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
95     add_string( "gestures-button", "right", NULL,
96                 BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
97         change_string_list( button_list, button_list_text, 0 );
98     set_description( _("Mouse gestures control interface") );
99
100     set_capability( "interface", 0 );
101     set_callbacks( E_(Open), E_(Close) );
102 vlc_module_end();
103
104 /*****************************************************************************
105  * OpenIntf: initialize interface
106  *****************************************************************************/
107 int E_(Open) ( vlc_object_t *p_this )
108 {
109     intf_thread_t *p_intf = (intf_thread_t *)p_this;
110
111     /* Allocate instance and initialize some members */
112     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
113     if( p_intf->p_sys == NULL )
114     {
115         return( 1 );
116     };
117
118     p_intf->pf_run = RunIntf;
119
120     return( 0 );
121 }
122
123 /*****************************************************************************
124  * gesture: return a subpattern within a pattern
125  *****************************************************************************/
126 static int gesture( int i_pattern, int i_num )
127 {
128     return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
129 }
130
131 /*****************************************************************************
132  * input_from_playlist: don't forget to release the return value
133  *  Also this function should really be available from core.
134  *****************************************************************************/
135 static input_thread_t * input_from_playlist ( playlist_t *p_playlist )
136 {
137     input_thread_t * p_input;
138
139     PL_LOCK; 
140     p_input = p_playlist->p_input; 
141     if( p_input ) 
142         vlc_object_yield( p_input ); 
143     PL_UNLOCK;
144
145     return p_input;
146 }
147
148 /*****************************************************************************
149  * CloseIntf: destroy dummy interface
150  *****************************************************************************/
151 void E_(Close) ( vlc_object_t *p_this )
152 {
153     intf_thread_t *p_intf = (intf_thread_t *)p_this;
154
155     /* Destroy structure */
156     free( p_intf->p_sys );
157 }
158
159
160 /*****************************************************************************
161  * RunIntf: main loop
162  *****************************************************************************/
163 static void RunIntf( intf_thread_t *p_intf )
164 {
165     playlist_t * p_playlist = NULL;
166
167     vlc_mutex_lock( &p_intf->change_lock );
168     p_intf->p_sys->p_vout = NULL;
169     vlc_mutex_unlock( &p_intf->change_lock );
170
171     if( InitThread( p_intf ) < 0 )
172     {
173         msg_Err( p_intf, "can't initialize interface thread" );
174         return;
175     }
176     msg_Dbg( p_intf, "interface thread initialized" );
177
178     /* Main loop */
179     while( !intf_ShouldDie( p_intf ) )
180     {
181         vlc_mutex_lock( &p_intf->change_lock );
182
183         /*
184          * mouse cursor
185          */
186         if( p_intf->p_sys->b_got_gesture )
187         {
188             vlc_value_t val;
189             int i_interval = 0;
190             /* Do something */
191             /* If you modify this, please try to follow this convention:
192                Start with LEFT, RIGHT for playback related commands
193                and UP, DOWN, for other commands */
194             switch( p_intf->p_sys->i_pattern )
195             {
196             case LEFT:
197                 i_interval = config_GetInt( p_intf , "short-jump-size" );
198                 if ( i_interval > 0 ) {
199                     val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
200                     var_Set( p_intf, "time-offset", val );
201                 }
202                 msg_Dbg(p_intf, "Go backward in the movie!");
203                 break;
204             case RIGHT:
205                 i_interval = config_GetInt( p_intf , "short-jump-size" );
206                 if ( i_interval > 0 ) {
207                     val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
208                     var_Set( p_intf, "time-offset", val );
209                 }
210                 msg_Dbg(p_intf, "Go forward in the movie!");
211                 break;
212             case GESTURE(LEFT,UP,NONE,NONE):
213                 /*FIXME BF*/
214                 msg_Dbg(p_intf, "Going slower.");
215                 break;
216             case GESTURE(RIGHT,UP,NONE,NONE):
217                 /*FIXME FF*/
218                 msg_Dbg(p_intf, "Going faster.");
219                 break;
220             case GESTURE(LEFT,RIGHT,NONE,NONE):
221             case GESTURE(RIGHT,LEFT,NONE,NONE):
222                 {
223                     input_thread_t * p_input;
224                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
225                                               FIND_ANYWHERE );
226
227                    if( !p_playlist )
228                         break;
229
230                     p_input = input_from_playlist( p_playlist );
231                     vlc_object_release( p_playlist );
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 = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
256                                               FIND_ANYWHERE );
257                 if( p_playlist == NULL )
258                 {
259                     break;
260                 }
261
262                 playlist_Prev( p_playlist );
263                 vlc_object_release( p_playlist );
264                 break;
265             case GESTURE(RIGHT,DOWN,NONE,NONE):
266                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
267                                               FIND_ANYWHERE );
268                 if( p_playlist == NULL )
269                 {
270                     break;
271                 }
272
273                 playlist_Next( p_playlist );
274                 vlc_object_release( p_playlist );
275                 break;
276             case UP:
277                 {
278                     audio_volume_t i_newvol;
279                     aout_VolumeUp( p_intf, 1, &i_newvol );
280                     msg_Dbg(p_intf, "Louder");
281                 }
282                 break;
283             case DOWN:
284                 {
285                     audio_volume_t i_newvol;
286                     aout_VolumeDown( p_intf, 1, &i_newvol );
287                     msg_Dbg(p_intf, "Quieter");
288                 }
289                 break;
290             case GESTURE(UP,DOWN,NONE,NONE):
291             case GESTURE(DOWN,UP,NONE,NONE):
292                 {
293                     audio_volume_t i_newvol = -1;
294                     aout_VolumeMute( p_intf, &i_newvol );
295                     msg_Dbg(p_intf, "Mute sound");
296                 }
297                 break;
298             case GESTURE(UP,RIGHT,NONE,NONE):
299                 {
300                    input_thread_t * p_input;
301                    vlc_value_t val, list, list2;
302                    int i_count, i;
303
304                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
305                                               FIND_ANYWHERE );
306
307                    if( !p_playlist )
308                         break;
309
310                     p_input = input_from_playlist( p_playlist );
311
312                     vlc_object_release( p_playlist );
313
314                     if( !p_input )
315                         break;
316
317                    var_Get( p_input, "audio-es", &val );
318                    var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
319                                &list, &list2 );
320                    i_count = list.p_list->i_count;
321                    if( i_count <= 1 )
322                    {
323                        vlc_object_release( p_input );
324                        break;
325                    }
326                    for( i = 0; i < i_count; i++ )
327                    {
328                        if( val.i_int == list.p_list->p_values[i].i_int )
329                        {
330                            break;
331                        }
332                    }
333                    /* value of audio-es was not in choices list */
334                    if( i == i_count )
335                    {
336                        msg_Warn( p_input,
337                                "invalid current audio track, selecting 0" );
338                        var_Set( p_input, "audio-es",
339                                list.p_list->p_values[0] );
340                        i = 0;
341                    }
342                    else if( i == i_count - 1 )
343                    {
344                        var_Set( p_input, "audio-es",
345                                list.p_list->p_values[1] );
346                        i = 1;
347                    }
348                    else
349                    {
350                        var_Set( p_input, "audio-es",
351                                list.p_list->p_values[i+1] );
352                        i++;
353                    }
354                    vlc_object_release( p_input );
355                 }
356                 break;
357             case GESTURE(DOWN,RIGHT,NONE,NONE):
358                 {
359                     input_thread_t * p_input;
360                     vlc_value_t val, list, list2;
361                     int i_count, i;
362
363                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
364                                               FIND_ANYWHERE );
365
366                     if( !p_playlist )
367                         break;
368
369                     p_input = input_from_playlist( p_playlist );
370                     vlc_object_release( p_playlist );
371
372                     if( !p_input )
373                         break;
374
375                     var_Get( p_input, "spu-es", &val );
376
377                     var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
378                             &list, &list2 );
379                     i_count = list.p_list->i_count;
380                     if( i_count <= 1 )
381                     {
382                         vlc_object_release( p_input );
383                         break;
384                     }
385                     for( i = 0; i < i_count; i++ )
386                     {
387                         if( val.i_int == list.p_list->p_values[i].i_int )
388                         {
389                             break;
390                         }
391                     }
392                     /* value of spu-es was not in choices list */
393                     if( i == i_count )
394                     {
395                         msg_Warn( p_input,
396                                 "invalid current subtitle track, selecting 0" );
397                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
398                         i = 0;
399                     }
400                     else if( i == i_count - 1 )
401                     {
402                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
403                         i = 0;
404                     }
405                     else
406                     {
407                         var_Set( p_input, "spu-es", 
408                                 list.p_list->p_values[i+1] );
409                         i = i + 1;
410                     }
411                     vlc_object_release( p_input );
412                 }
413                 break;
414             case GESTURE(UP,LEFT,NONE,NONE):
415                 if (p_intf->p_sys->p_vout )
416                 {
417                     ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
418                         VOUT_FULLSCREEN_CHANGE;
419                 }
420                 break;
421             case GESTURE(DOWN,LEFT,NONE,NONE):
422                 /* FIXME: Should close the vout!"*/
423                 vlc_object_kill( p_intf->p_libvlc );
424                 break;
425             case GESTURE(DOWN,LEFT,UP,RIGHT):
426             case GESTURE(UP,RIGHT,DOWN,LEFT):
427                 msg_Dbg(p_intf, "a square was drawn!" );
428                 break;
429             default:
430                 break;
431             }
432             p_intf->p_sys->i_num_gestures = 0;
433             p_intf->p_sys->i_pattern = 0;
434             p_intf->p_sys->b_got_gesture = VLC_FALSE;
435         }
436
437         /*
438          * video output
439          */
440         if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
441         {
442             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
443                              MouseEvent, p_intf );
444             var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
445                              MouseEvent, p_intf );
446             vlc_object_release( p_intf->p_sys->p_vout );
447             p_intf->p_sys->p_vout = NULL;
448         }
449
450         if( p_intf->p_sys->p_vout == NULL )
451         {
452             p_intf->p_sys->p_vout = vlc_object_find( p_intf,
453                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
454             if( p_intf->p_sys->p_vout )
455             {
456                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
457                                  MouseEvent, p_intf );
458                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
459                                  MouseEvent, p_intf );
460             }
461         }
462
463         vlc_mutex_unlock( &p_intf->change_lock );
464
465         /* Wait a bit */
466         msleep( INTF_IDLE_SLEEP );
467     }
468
469     EndThread( p_intf );
470 }
471
472 /*****************************************************************************
473  * InitThread:
474  *****************************************************************************/
475 static int InitThread( intf_thread_t * p_intf )
476 {
477     char *psz_button;
478     /* we might need some locking here */
479     if( !intf_ShouldDie( p_intf ) )
480     {
481         /* p_intf->change_lock locking strategy:
482          * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
483          * - make sure there won't be  cross increment/decrement ref count
484          *   of p_intf->p_sys members p_intf->change_lock should be locked
485          *   during those operations */
486         vlc_mutex_lock( &p_intf->change_lock );
487
488         p_intf->p_sys->b_got_gesture = VLC_FALSE;
489         p_intf->p_sys->b_button_pressed = VLC_FALSE;
490         p_intf->p_sys->i_threshold =
491                      config_GetInt( p_intf, "gestures-threshold" );
492         psz_button = config_GetPsz( p_intf, "gestures-button" );
493         if ( !strcmp( psz_button, "left" ) )
494         {
495             p_intf->p_sys->i_button_mask = 1;
496         }
497         else if ( !strcmp( psz_button, "middle" ) )
498         {
499             p_intf->p_sys->i_button_mask = 2;
500         }
501         else if ( !strcmp( psz_button, "right" ) )
502         {
503             p_intf->p_sys->i_button_mask = 4;
504         }
505
506         p_intf->p_sys->i_pattern = 0;
507         p_intf->p_sys->i_num_gestures = 0;
508         vlc_mutex_unlock( &p_intf->change_lock );
509
510         return 0;
511     }
512     else
513     {
514         return -1;
515     }
516 }
517
518 /*****************************************************************************
519  * EndThread:
520  *****************************************************************************/
521 static void EndThread( intf_thread_t * p_intf )
522 {
523     vlc_mutex_lock( &p_intf->change_lock );
524
525     if( p_intf->p_sys->p_vout )
526     {
527         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
528                          MouseEvent, p_intf );
529         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
530                          MouseEvent, p_intf );
531         vlc_object_release( p_intf->p_sys->p_vout );
532     }
533
534     vlc_mutex_unlock( &p_intf->change_lock );
535 }
536
537 /*****************************************************************************
538  * MouseEvent: callback for mouse events
539  *****************************************************************************/
540 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
541                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
542 {
543     vlc_value_t val;
544     int pattern = 0;
545
546     signed int i_horizontal, i_vertical;
547     intf_thread_t *p_intf = (intf_thread_t *)p_data;
548
549     vlc_mutex_lock( &p_intf->change_lock );
550
551     /* don't process new gestures before the last events are processed */
552     if( p_intf->p_sys->b_got_gesture )
553     {
554         vlc_mutex_unlock( &p_intf->change_lock );
555         return VLC_SUCCESS;
556     }
557
558     if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
559     {
560         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
561         p_intf->p_sys->i_mouse_x = val.i_int;
562         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
563         p_intf->p_sys->i_mouse_y = val.i_int;
564         i_horizontal = p_intf->p_sys->i_mouse_x -
565             p_intf->p_sys->i_last_x;
566         i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
567         i_vertical = p_intf->p_sys->i_mouse_y
568             - p_intf->p_sys->i_last_y;
569         i_vertical = i_vertical / p_intf->p_sys->i_threshold;
570
571         if( i_horizontal < 0 )
572         {
573             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
574             pattern = LEFT;
575         }
576         else if( i_horizontal > 0 )
577         {
578             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
579             pattern = RIGHT;
580         }
581         if( i_vertical < 0 )
582         {
583             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
584             pattern = UP;
585         }
586         else if( i_vertical > 0 )
587         {
588             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
589             pattern = DOWN;
590         }
591         if( pattern )
592         {
593             p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
594             p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
595             if( gesture( p_intf->p_sys->i_pattern,
596                          p_intf->p_sys->i_num_gestures - 1 ) != pattern )
597             {
598                 p_intf->p_sys->i_pattern |=
599                     pattern << ( p_intf->p_sys->i_num_gestures * 4 );
600                 p_intf->p_sys->i_num_gestures++;
601             }
602         }
603
604     }
605     if( !strcmp( psz_var, "mouse-button-down" )
606         && newval.i_int & p_intf->p_sys->i_button_mask
607         && !p_intf->p_sys->b_button_pressed )
608     {
609         p_intf->p_sys->b_button_pressed = VLC_TRUE;
610         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
611         p_intf->p_sys->i_last_x = val.i_int;
612         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
613         p_intf->p_sys->i_last_y = val.i_int;
614     }
615     if( !strcmp( psz_var, "mouse-button-down" )
616         && !( newval.i_int & p_intf->p_sys->i_button_mask )
617         && p_intf->p_sys->b_button_pressed )
618     {
619         p_intf->p_sys->b_button_pressed = VLC_FALSE;
620         p_intf->p_sys->b_got_gesture = VLC_TRUE;
621     }
622
623     vlc_mutex_unlock( &p_intf->change_lock );
624
625     return VLC_SUCCESS;
626 }