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