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 *****************************************************************************/
33 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
42 /*****************************************************************************
43 * intf_sys_t: description and status of interface
44 *****************************************************************************/
47 vlc_object_t * p_vout;
48 vlc_bool_t b_got_gesture;
49 vlc_bool_t b_button_pressed;
50 int i_mouse_x, i_mouse_y;
51 int i_last_x, i_last_y;
52 unsigned int i_pattern;
58 /*****************************************************************************
60 *****************************************************************************/
66 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
68 int E_(Open) ( vlc_object_t * );
69 void E_(Close) ( vlc_object_t * );
70 static int InitThread ( intf_thread_t *p_intf );
71 static void EndThread ( intf_thread_t *p_intf );
72 static int MouseEvent ( vlc_object_t *, char const *,
73 vlc_value_t, vlc_value_t, void * );
75 /* Exported functions */
76 static void RunIntf ( intf_thread_t *p_intf );
78 /*****************************************************************************
80 *****************************************************************************/
81 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
82 #define THRESHOLD_LONGTEXT N_( \
83 "Amount of movement required for a mouse gesture to be recorded." )
85 #define BUTTON_TEXT N_( "Trigger button" )
86 #define BUTTON_LONGTEXT N_( \
87 "Trigger button for mouse gestures." )
89 static const char *button_list[] = { "left", "middle", "right" };
90 static const char *button_list_text[] =
91 { N_("Left"), N_("Middle"), N_("Right") };
94 set_shortname( _("Gestures"));
95 set_category( CAT_INTERFACE );
96 set_subcategory( SUBCAT_INTERFACE_CONTROL );
97 add_integer( "gestures-threshold", 30, NULL,
98 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
99 add_string( "gestures-button", "right", NULL,
100 BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
101 change_string_list( button_list, button_list_text, 0 );
102 set_description( _("Mouse gestures control interface") );
104 set_capability( "interface", 0 );
105 set_callbacks( E_(Open), E_(Close) );
108 /*****************************************************************************
109 * OpenIntf: initialize interface
110 *****************************************************************************/
111 int E_(Open) ( vlc_object_t *p_this )
113 intf_thread_t *p_intf = (intf_thread_t *)p_this;
115 /* Allocate instance and initialize some members */
116 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
117 if( p_intf->p_sys == NULL )
122 p_intf->pf_run = RunIntf;
127 /*****************************************************************************
128 * gesture: return a subpattern within a pattern
129 *****************************************************************************/
130 static int gesture( int i_pattern, int i_num )
132 return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
135 /*****************************************************************************
136 * input_from_playlist: don't forget to release the return value
137 * Also this function should really be available from core.
138 *****************************************************************************/
139 static input_thread_t * input_from_playlist ( playlist_t *p_playlist )
141 input_thread_t * p_input;
144 p_input = p_playlist->p_input;
146 vlc_object_yield( p_input );
152 /*****************************************************************************
153 * CloseIntf: destroy dummy interface
154 *****************************************************************************/
155 void E_(Close) ( vlc_object_t *p_this )
157 intf_thread_t *p_intf = (intf_thread_t *)p_this;
159 /* Destroy structure */
160 free( p_intf->p_sys );
164 /*****************************************************************************
166 *****************************************************************************/
167 static void RunIntf( intf_thread_t *p_intf )
169 playlist_t * p_playlist = NULL;
171 vlc_mutex_lock( &p_intf->change_lock );
172 p_intf->p_sys->p_vout = NULL;
173 vlc_mutex_unlock( &p_intf->change_lock );
175 if( InitThread( p_intf ) < 0 )
177 msg_Err( p_intf, "can't initialize interface thread" );
180 msg_Dbg( p_intf, "interface thread initialized" );
183 while( !intf_ShouldDie( p_intf ) )
185 vlc_mutex_lock( &p_intf->change_lock );
190 if( p_intf->p_sys->b_got_gesture )
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 )
201 i_interval = config_GetInt( p_intf , "short-jump-size" );
202 if ( i_interval > 0 ) {
203 val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
204 var_Set( p_intf, "time-offset", val );
206 msg_Dbg(p_intf, "Go backward in the movie!");
209 i_interval = config_GetInt( p_intf , "short-jump-size" );
210 if ( i_interval > 0 ) {
211 val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
212 var_Set( p_intf, "time-offset", val );
214 msg_Dbg(p_intf, "Go forward in the movie!");
216 case GESTURE(LEFT,UP,NONE,NONE):
218 msg_Dbg(p_intf, "Going slower.");
220 case GESTURE(RIGHT,UP,NONE,NONE):
222 msg_Dbg(p_intf, "Going faster.");
224 case GESTURE(LEFT,RIGHT,NONE,NONE):
225 case GESTURE(RIGHT,LEFT,NONE,NONE):
227 input_thread_t * p_input;
228 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
234 p_input = input_from_playlist( p_playlist );
235 vlc_object_release( p_playlist );
240 val.i_int = PLAYING_S;
243 var_Get( p_input, "state", &val);
244 if( val.i_int == PAUSE_S )
246 val.i_int = PLAYING_S;
252 var_Set( p_input, "state", val);
254 msg_Dbg(p_intf, "Play/Pause");
255 vlc_object_release( p_input );
258 case GESTURE(LEFT,DOWN,NONE,NONE):
259 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
261 if( p_playlist == NULL )
266 playlist_Prev( p_playlist );
267 vlc_object_release( p_playlist );
269 case GESTURE(RIGHT,DOWN,NONE,NONE):
270 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
272 if( p_playlist == NULL )
277 playlist_Next( p_playlist );
278 vlc_object_release( p_playlist );
282 audio_volume_t i_newvol;
283 aout_VolumeUp( p_intf, 1, &i_newvol );
284 msg_Dbg(p_intf, "Louder");
289 audio_volume_t i_newvol;
290 aout_VolumeDown( p_intf, 1, &i_newvol );
291 msg_Dbg(p_intf, "Quieter");
294 case GESTURE(UP,DOWN,NONE,NONE):
295 case GESTURE(DOWN,UP,NONE,NONE):
297 audio_volume_t i_newvol = -1;
298 aout_VolumeMute( p_intf, &i_newvol );
299 msg_Dbg(p_intf, "Mute sound");
302 case GESTURE(UP,RIGHT,NONE,NONE):
304 input_thread_t * p_input;
305 vlc_value_t val, list, list2;
308 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
314 p_input = input_from_playlist( p_playlist );
316 vlc_object_release( p_playlist );
321 var_Get( p_input, "audio-es", &val );
322 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
324 i_count = list.p_list->i_count;
327 vlc_object_release( p_input );
330 for( i = 0; i < i_count; i++ )
332 if( val.i_int == list.p_list->p_values[i].i_int )
337 /* value of audio-es was not in choices list */
341 "invalid current audio track, selecting 0" );
342 var_Set( p_input, "audio-es",
343 list.p_list->p_values[0] );
346 else if( i == i_count - 1 )
348 var_Set( p_input, "audio-es",
349 list.p_list->p_values[1] );
354 var_Set( p_input, "audio-es",
355 list.p_list->p_values[i+1] );
358 vlc_object_release( p_input );
361 case GESTURE(DOWN,RIGHT,NONE,NONE):
363 input_thread_t * p_input;
364 vlc_value_t val, list, list2;
367 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
373 p_input = input_from_playlist( p_playlist );
374 vlc_object_release( p_playlist );
379 var_Get( p_input, "spu-es", &val );
381 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
383 i_count = list.p_list->i_count;
386 vlc_object_release( p_input );
389 for( i = 0; i < i_count; i++ )
391 if( val.i_int == list.p_list->p_values[i].i_int )
396 /* value of spu-es was not in choices list */
400 "invalid current subtitle track, selecting 0" );
401 var_Set( p_input, "spu-es", list.p_list->p_values[0] );
404 else if( i == i_count - 1 )
406 var_Set( p_input, "spu-es", list.p_list->p_values[0] );
411 var_Set( p_input, "spu-es",
412 list.p_list->p_values[i+1] );
415 vlc_object_release( p_input );
418 case GESTURE(UP,LEFT,NONE,NONE):
419 if (p_intf->p_sys->p_vout )
421 ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
422 VOUT_FULLSCREEN_CHANGE;
425 case GESTURE(DOWN,LEFT,NONE,NONE):
426 /* FIXME: Should close the vout!"*/
427 vlc_object_kill( p_intf->p_libvlc );
429 case GESTURE(DOWN,LEFT,UP,RIGHT):
430 case GESTURE(UP,RIGHT,DOWN,LEFT):
431 msg_Dbg(p_intf, "a square was drawn!" );
436 p_intf->p_sys->i_num_gestures = 0;
437 p_intf->p_sys->i_pattern = 0;
438 p_intf->p_sys->b_got_gesture = VLC_FALSE;
444 if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
446 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
447 MouseEvent, p_intf );
448 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
449 MouseEvent, p_intf );
450 vlc_object_release( p_intf->p_sys->p_vout );
451 p_intf->p_sys->p_vout = NULL;
454 if( p_intf->p_sys->p_vout == NULL )
456 p_intf->p_sys->p_vout = vlc_object_find( p_intf,
457 VLC_OBJECT_VOUT, FIND_ANYWHERE );
458 if( p_intf->p_sys->p_vout )
460 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
461 MouseEvent, p_intf );
462 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
463 MouseEvent, p_intf );
467 vlc_mutex_unlock( &p_intf->change_lock );
470 msleep( INTF_IDLE_SLEEP );
476 /*****************************************************************************
478 *****************************************************************************/
479 static int InitThread( intf_thread_t * p_intf )
482 /* we might need some locking here */
483 if( !intf_ShouldDie( p_intf ) )
485 /* p_intf->change_lock locking strategy:
486 * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
487 * - make sure there won't be cross increment/decrement ref count
488 * of p_intf->p_sys members p_intf->change_lock should be locked
489 * during those operations */
490 vlc_mutex_lock( &p_intf->change_lock );
492 p_intf->p_sys->b_got_gesture = VLC_FALSE;
493 p_intf->p_sys->b_button_pressed = VLC_FALSE;
494 p_intf->p_sys->i_threshold =
495 config_GetInt( p_intf, "gestures-threshold" );
496 psz_button = config_GetPsz( p_intf, "gestures-button" );
497 if ( !strcmp( psz_button, "left" ) )
499 p_intf->p_sys->i_button_mask = 1;
501 else if ( !strcmp( psz_button, "middle" ) )
503 p_intf->p_sys->i_button_mask = 2;
505 else if ( !strcmp( psz_button, "right" ) )
507 p_intf->p_sys->i_button_mask = 4;
510 p_intf->p_sys->i_pattern = 0;
511 p_intf->p_sys->i_num_gestures = 0;
512 vlc_mutex_unlock( &p_intf->change_lock );
522 /*****************************************************************************
524 *****************************************************************************/
525 static void EndThread( intf_thread_t * p_intf )
527 vlc_mutex_lock( &p_intf->change_lock );
529 if( p_intf->p_sys->p_vout )
531 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
532 MouseEvent, p_intf );
533 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
534 MouseEvent, p_intf );
535 vlc_object_release( p_intf->p_sys->p_vout );
538 vlc_mutex_unlock( &p_intf->change_lock );
541 /*****************************************************************************
542 * MouseEvent: callback for mouse events
543 *****************************************************************************/
544 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
545 vlc_value_t oldval, vlc_value_t newval, void *p_data )
550 signed int i_horizontal, i_vertical;
551 intf_thread_t *p_intf = (intf_thread_t *)p_data;
553 vlc_mutex_lock( &p_intf->change_lock );
555 /* don't process new gestures before the last events are processed */
556 if( p_intf->p_sys->b_got_gesture )
558 vlc_mutex_unlock( &p_intf->change_lock );
562 if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
564 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
565 p_intf->p_sys->i_mouse_x = val.i_int;
566 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
567 p_intf->p_sys->i_mouse_y = val.i_int;
568 i_horizontal = p_intf->p_sys->i_mouse_x -
569 p_intf->p_sys->i_last_x;
570 i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
571 i_vertical = p_intf->p_sys->i_mouse_y
572 - p_intf->p_sys->i_last_y;
573 i_vertical = i_vertical / p_intf->p_sys->i_threshold;
575 if( i_horizontal < 0 )
577 msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
580 else if( i_horizontal > 0 )
582 msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
587 msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
590 else if( i_vertical > 0 )
592 msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
597 p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
598 p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
599 if( gesture( p_intf->p_sys->i_pattern,
600 p_intf->p_sys->i_num_gestures - 1 ) != pattern )
602 p_intf->p_sys->i_pattern |=
603 pattern << ( p_intf->p_sys->i_num_gestures * 4 );
604 p_intf->p_sys->i_num_gestures++;
609 if( !strcmp( psz_var, "mouse-button-down" )
610 && newval.i_int & p_intf->p_sys->i_button_mask
611 && !p_intf->p_sys->b_button_pressed )
613 p_intf->p_sys->b_button_pressed = VLC_TRUE;
614 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
615 p_intf->p_sys->i_last_x = val.i_int;
616 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
617 p_intf->p_sys->i_last_y = val.i_int;
619 if( !strcmp( psz_var, "mouse-button-down" )
620 && !( newval.i_int & p_intf->p_sys->i_button_mask )
621 && p_intf->p_sys->b_button_pressed )
623 p_intf->p_sys->b_button_pressed = VLC_FALSE;
624 p_intf->p_sys->b_got_gesture = VLC_TRUE;
627 vlc_mutex_unlock( &p_intf->change_lock );