1 /*****************************************************************************
2 * gestures.c: control vlc with mouse gestures
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc_interface.h>
34 #include <vlc_playlist.h>
40 /*****************************************************************************
41 * intf_sys_t: description and status of interface
42 *****************************************************************************/
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;
56 /*****************************************************************************
58 *****************************************************************************/
64 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
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 * );
73 /* Exported functions */
74 static void RunIntf ( intf_thread_t *p_intf );
76 /*****************************************************************************
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." )
83 #define BUTTON_TEXT N_( "Trigger button" )
84 #define BUTTON_LONGTEXT N_( \
85 "Trigger button for mouse gestures." )
87 static const char *button_list[] = { "left", "middle", "right" };
88 static const char *button_list_text[] =
89 { N_("Left"), N_("Middle"), N_("Right") };
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") );
102 set_capability( "interface", 0 );
103 set_callbacks( E_(Open), E_(Close) );
106 /*****************************************************************************
107 * OpenIntf: initialize interface
108 *****************************************************************************/
109 int E_(Open) ( vlc_object_t *p_this )
111 intf_thread_t *p_intf = (intf_thread_t *)p_this;
113 /* Allocate instance and initialize some members */
114 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
115 if( p_intf->p_sys == NULL )
120 p_intf->pf_run = RunIntf;
125 /*****************************************************************************
126 * gesture: return a subpattern within a pattern
127 *****************************************************************************/
128 static int gesture( int i_pattern, int i_num )
130 return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
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 )
139 input_thread_t * p_input;
142 p_input = p_playlist->p_input;
144 vlc_object_yield( p_input );
150 /*****************************************************************************
151 * CloseIntf: destroy dummy interface
152 *****************************************************************************/
153 void E_(Close) ( vlc_object_t *p_this )
155 intf_thread_t *p_intf = (intf_thread_t *)p_this;
157 /* Destroy structure */
158 free( p_intf->p_sys );
162 /*****************************************************************************
164 *****************************************************************************/
165 static void RunIntf( intf_thread_t *p_intf )
167 playlist_t * p_playlist = NULL;
169 if( InitThread( p_intf ) < 0 )
171 msg_Err( p_intf, "can't initialize interface thread" );
174 msg_Dbg( p_intf, "interface thread initialized" );
177 while( !intf_ShouldDie( p_intf ) )
179 vlc_mutex_lock( &p_intf->change_lock );
184 if( p_intf->p_sys->b_got_gesture )
189 /* If you modify this, please try to follow this convention:
190 Start with LEFT, RIGHT for playback related commands
191 and UP, DOWN, for other commands */
192 switch( p_intf->p_sys->i_pattern )
195 i_interval = config_GetInt( p_intf , "short-jump-size" );
196 if ( i_interval > 0 ) {
197 val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
198 var_Set( p_intf, "time-offset", val );
200 msg_Dbg(p_intf, "Go backward in the movie!");
203 i_interval = config_GetInt( p_intf , "short-jump-size" );
204 if ( i_interval > 0 ) {
205 val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
206 var_Set( p_intf, "time-offset", val );
208 msg_Dbg(p_intf, "Go forward in the movie!");
210 case GESTURE(LEFT,UP,NONE,NONE):
212 msg_Dbg(p_intf, "Going slower.");
214 case GESTURE(RIGHT,UP,NONE,NONE):
216 msg_Dbg(p_intf, "Going faster.");
218 case GESTURE(LEFT,RIGHT,NONE,NONE):
219 case GESTURE(RIGHT,LEFT,NONE,NONE):
221 input_thread_t * p_input;
222 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
228 p_input = input_from_playlist( p_playlist );
232 vlc_object_release( p_playlist );
236 val.i_int = PLAYING_S;
239 var_Get( p_input, "state", &val);
240 if( val.i_int == PAUSE_S )
242 val.i_int = PLAYING_S;
248 var_Set( p_input, "state", val);
250 msg_Dbg(p_intf, "Play/Pause");
251 vlc_object_release( p_input );
252 vlc_object_release( p_playlist );
255 case GESTURE(LEFT,DOWN,NONE,NONE):
256 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
258 if( p_playlist == NULL )
263 playlist_Prev( p_playlist );
264 vlc_object_release( p_playlist );
266 case GESTURE(RIGHT,DOWN,NONE,NONE):
267 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
269 if( p_playlist == NULL )
274 playlist_Next( p_playlist );
275 vlc_object_release( p_playlist );
279 audio_volume_t i_newvol;
280 aout_VolumeUp( p_intf, 1, &i_newvol );
281 msg_Dbg(p_intf, "Louder");
286 audio_volume_t i_newvol;
287 aout_VolumeDown( p_intf, 1, &i_newvol );
288 msg_Dbg(p_intf, "Quieter");
291 case GESTURE(UP,DOWN,NONE,NONE):
292 case GESTURE(DOWN,UP,NONE,NONE):
294 audio_volume_t i_newvol = -1;
295 aout_VolumeMute( p_intf, &i_newvol );
296 msg_Dbg(p_intf, "Mute sound");
299 case GESTURE(UP,RIGHT,NONE,NONE):
301 input_thread_t * p_input;
302 vlc_value_t val, list, list2;
305 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
311 p_input = input_from_playlist( p_playlist );
315 vlc_object_release( p_playlist );
319 var_Get( p_input, "audio-es", &val );
320 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
322 i_count = list.p_list->i_count;
325 vlc_object_release( p_input );
326 vlc_object_release( p_playlist );
329 for( i = 0; i < i_count; i++ )
331 if( val.i_int == list.p_list->p_values[i].i_int )
333 vlc_object_release( p_input );
334 vlc_object_release( p_playlist );
338 /* value of audio-es was not in choices list */
342 "invalid current audio track, selecting 0" );
343 var_Set( p_input, "audio-es",
344 list.p_list->p_values[0] );
347 else if( i == i_count - 1 )
349 var_Set( p_input, "audio-es",
350 list.p_list->p_values[1] );
355 var_Set( p_input, "audio-es",
356 list.p_list->p_values[i+1] );
359 vlc_object_release( p_input );
360 vlc_object_release( p_playlist );
363 case GESTURE(DOWN,RIGHT,NONE,NONE):
365 input_thread_t * p_input;
366 vlc_value_t val, list, list2;
369 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
375 p_input = input_from_playlist( p_playlist );
379 vlc_object_release( p_playlist );
382 var_Get( p_input, "spu-es", &val );
384 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
386 i_count = list.p_list->i_count;
389 vlc_object_release( p_input );
390 vlc_object_release( p_playlist );
393 for( i = 0; i < i_count; i++ )
395 if( val.i_int == list.p_list->p_values[i].i_int )
397 vlc_object_release( p_input );
398 vlc_object_release( p_playlist );
402 /* value of spu-es was not in choices list */
406 "invalid current subtitle track, selecting 0" );
407 var_Set( p_input, "spu-es", list.p_list->p_values[0] );
410 else if( i == i_count - 1 )
412 var_Set( p_input, "spu-es", list.p_list->p_values[0] );
417 var_Set( p_input, "spu-es",
418 list.p_list->p_values[i+1] );
421 vlc_object_release( p_input );
422 vlc_object_release( p_playlist );
425 case GESTURE(UP,LEFT,NONE,NONE):
426 if (p_intf->p_sys->p_vout )
428 ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
429 VOUT_FULLSCREEN_CHANGE;
432 case GESTURE(DOWN,LEFT,NONE,NONE):
433 /* FIXME: Should close the vout!"*/
434 p_intf->p_libvlc->b_die = VLC_TRUE;
436 case GESTURE(DOWN,LEFT,UP,RIGHT):
437 case GESTURE(UP,RIGHT,DOWN,LEFT):
438 msg_Dbg(p_intf, "a square was drawn!" );
443 p_intf->p_sys->i_num_gestures = 0;
444 p_intf->p_sys->i_pattern = 0;
445 p_intf->p_sys->b_got_gesture = VLC_FALSE;
451 if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
453 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
454 MouseEvent, p_intf );
455 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
456 MouseEvent, p_intf );
457 vlc_object_release( p_intf->p_sys->p_vout );
458 p_intf->p_sys->p_vout = NULL;
461 if( p_intf->p_sys->p_vout == NULL )
463 p_intf->p_sys->p_vout = vlc_object_find( p_intf,
464 VLC_OBJECT_VOUT, FIND_ANYWHERE );
465 if( p_intf->p_sys->p_vout )
467 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
468 MouseEvent, p_intf );
469 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
470 MouseEvent, p_intf );
474 vlc_mutex_unlock( &p_intf->change_lock );
477 msleep( INTF_IDLE_SLEEP );
483 /*****************************************************************************
485 *****************************************************************************/
486 static int InitThread( intf_thread_t * p_intf )
489 /* we might need some locking here */
490 if( !intf_ShouldDie( p_intf ) )
492 /* p_intf->change_lock locking strategy:
493 * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
494 * - make sure there won't be cross increment/decrement ref count
495 * of p_intf->p_sys members p_intf->change_lock should be locked
496 * during those operations */
497 vlc_mutex_lock( &p_intf->change_lock );
499 p_intf->p_sys->b_got_gesture = VLC_FALSE;
500 p_intf->p_sys->b_button_pressed = VLC_FALSE;
501 p_intf->p_sys->i_threshold =
502 config_GetInt( p_intf, "gestures-threshold" );
503 psz_button = config_GetPsz( p_intf, "gestures-button" );
504 if ( !strcmp( psz_button, "left" ) )
506 p_intf->p_sys->i_button_mask = 1;
508 else if ( !strcmp( psz_button, "middle" ) )
510 p_intf->p_sys->i_button_mask = 2;
512 else if ( !strcmp( psz_button, "right" ) )
514 p_intf->p_sys->i_button_mask = 4;
517 p_intf->p_sys->i_pattern = 0;
518 p_intf->p_sys->i_num_gestures = 0;
519 vlc_mutex_unlock( &p_intf->change_lock );
529 /*****************************************************************************
531 *****************************************************************************/
532 static void EndThread( intf_thread_t * p_intf )
534 vlc_mutex_lock( &p_intf->change_lock );
536 if( p_intf->p_sys->p_vout )
538 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
539 MouseEvent, p_intf );
540 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
541 MouseEvent, p_intf );
542 vlc_object_release( p_intf->p_sys->p_vout );
545 vlc_mutex_unlock( &p_intf->change_lock );
548 /*****************************************************************************
549 * MouseEvent: callback for mouse events
550 *****************************************************************************/
551 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
552 vlc_value_t oldval, vlc_value_t newval, void *p_data )
557 signed int i_horizontal, i_vertical;
558 intf_thread_t *p_intf = (intf_thread_t *)p_data;
560 vlc_mutex_lock( &p_intf->change_lock );
562 /* don't process new gestures before the last events are processed */
563 if( p_intf->p_sys->b_got_gesture )
565 vlc_mutex_unlock( &p_intf->change_lock );
569 if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
571 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
572 p_intf->p_sys->i_mouse_x = val.i_int;
573 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
574 p_intf->p_sys->i_mouse_y = val.i_int;
575 i_horizontal = p_intf->p_sys->i_mouse_x -
576 p_intf->p_sys->i_last_x;
577 i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
578 i_vertical = p_intf->p_sys->i_mouse_y
579 - p_intf->p_sys->i_last_y;
580 i_vertical = i_vertical / p_intf->p_sys->i_threshold;
582 if( i_horizontal < 0 )
584 msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
587 else if( i_horizontal > 0 )
589 msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
594 msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
597 else if( i_vertical > 0 )
599 msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
604 p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
605 p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
606 if( gesture( p_intf->p_sys->i_pattern,
607 p_intf->p_sys->i_num_gestures - 1 ) != pattern )
609 p_intf->p_sys->i_pattern |=
610 pattern << ( p_intf->p_sys->i_num_gestures * 4 );
611 p_intf->p_sys->i_num_gestures++;
616 if( !strcmp( psz_var, "mouse-button-down" )
617 && newval.i_int & p_intf->p_sys->i_button_mask
618 && !p_intf->p_sys->b_button_pressed )
620 p_intf->p_sys->b_button_pressed = VLC_TRUE;
621 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
622 p_intf->p_sys->i_last_x = val.i_int;
623 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
624 p_intf->p_sys->i_last_y = val.i_int;
626 if( !strcmp( psz_var, "mouse-button-down" )
627 && !( newval.i_int & p_intf->p_sys->i_button_mask )
628 && p_intf->p_sys->b_button_pressed )
630 p_intf->p_sys->b_button_pressed = VLC_FALSE;
631 p_intf->p_sys->b_got_gesture = VLC_TRUE;
634 vlc_mutex_unlock( &p_intf->change_lock );