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 *****************************************************************************/
30 #include <vlc_interface.h>
33 #include <vlc_playlist.h>
39 /*****************************************************************************
40 * intf_sys_t: description and status of interface
41 *****************************************************************************/
44 vlc_object_t * p_vout;
45 vlc_bool_t b_got_gesture;
46 vlc_bool_t b_button_pressed;
47 int i_mouse_x, i_mouse_y;
48 int i_last_x, i_last_y;
49 unsigned int i_pattern;
55 /*****************************************************************************
57 *****************************************************************************/
63 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
65 int E_(Open) ( vlc_object_t * );
66 void E_(Close) ( vlc_object_t * );
67 static int InitThread ( intf_thread_t *p_intf );
68 static void EndThread ( intf_thread_t *p_intf );
69 static int MouseEvent ( vlc_object_t *, char const *,
70 vlc_value_t, vlc_value_t, void * );
72 /* Exported functions */
73 static void RunIntf ( intf_thread_t *p_intf );
75 /*****************************************************************************
77 *****************************************************************************/
78 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
79 #define THRESHOLD_LONGTEXT N_( \
80 "Amount of movement required for a mouse gesture to be recorded." )
82 #define BUTTON_TEXT N_( "Trigger button" )
83 #define BUTTON_LONGTEXT N_( \
84 "Trigger button for mouse gestures." )
86 static const char *button_list[] = { "left", "middle", "right" };
87 static const char *button_list_text[] =
88 { N_("Left"), N_("Middle"), N_("Right") };
91 set_shortname( _("Gestures"));
92 set_category( CAT_INTERFACE );
93 set_subcategory( SUBCAT_INTERFACE_CONTROL );
94 add_integer( "gestures-threshold", 30, NULL,
95 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
96 add_string( "gestures-button", "right", NULL,
97 BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
98 change_string_list( button_list, button_list_text, 0 );
99 set_description( _("Mouse gestures control interface") );
101 set_capability( "interface", 0 );
102 set_callbacks( E_(Open), E_(Close) );
105 /*****************************************************************************
106 * OpenIntf: initialize interface
107 *****************************************************************************/
108 int E_(Open) ( vlc_object_t *p_this )
110 intf_thread_t *p_intf = (intf_thread_t *)p_this;
112 /* Allocate instance and initialize some members */
113 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
114 if( p_intf->p_sys == NULL )
119 p_intf->pf_run = RunIntf;
124 /*****************************************************************************
125 * gesture: return a subpattern within a pattern
126 *****************************************************************************/
127 static int gesture( int i_pattern, int i_num )
129 return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
132 /*****************************************************************************
133 * input_from_playlist: don't forget to release the return value
134 * Also this function should really be available from core.
135 *****************************************************************************/
136 static input_thread_t * input_from_playlist ( playlist_t *p_playlist )
138 input_thread_t * p_input;
141 p_input = p_playlist->p_input;
143 vlc_object_yield( p_input );
149 /*****************************************************************************
150 * CloseIntf: destroy dummy interface
151 *****************************************************************************/
152 void E_(Close) ( vlc_object_t *p_this )
154 intf_thread_t *p_intf = (intf_thread_t *)p_this;
156 /* Destroy structure */
157 free( p_intf->p_sys );
161 /*****************************************************************************
163 *****************************************************************************/
164 static void RunIntf( intf_thread_t *p_intf )
166 playlist_t * p_playlist = NULL;
168 vlc_mutex_lock( &p_intf->change_lock );
169 p_intf->p_sys->p_vout = NULL;
170 vlc_mutex_unlock( &p_intf->change_lock );
172 if( InitThread( p_intf ) < 0 )
174 msg_Err( p_intf, "can't initialize interface thread" );
177 msg_Dbg( p_intf, "interface thread initialized" );
180 while( !intf_ShouldDie( p_intf ) )
182 vlc_mutex_lock( &p_intf->change_lock );
187 if( p_intf->p_sys->b_got_gesture )
192 /* If you modify this, please try to follow this convention:
193 Start with LEFT, RIGHT for playback related commands
194 and UP, DOWN, for other commands */
195 switch( p_intf->p_sys->i_pattern )
198 i_interval = config_GetInt( p_intf , "short-jump-size" );
199 if ( i_interval > 0 ) {
200 val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
201 var_Set( p_intf, "time-offset", val );
203 msg_Dbg(p_intf, "Go backward in the movie!");
206 i_interval = config_GetInt( p_intf , "short-jump-size" );
207 if ( i_interval > 0 ) {
208 val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
209 var_Set( p_intf, "time-offset", val );
211 msg_Dbg(p_intf, "Go forward in the movie!");
213 case GESTURE(LEFT,UP,NONE,NONE):
215 msg_Dbg(p_intf, "Going slower.");
217 case GESTURE(RIGHT,UP,NONE,NONE):
219 msg_Dbg(p_intf, "Going faster.");
221 case GESTURE(LEFT,RIGHT,NONE,NONE):
222 case GESTURE(RIGHT,LEFT,NONE,NONE):
224 input_thread_t * p_input;
225 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
231 p_input = input_from_playlist( p_playlist );
232 vlc_object_release( p_playlist );
237 val.i_int = PLAYING_S;
240 var_Get( p_input, "state", &val);
241 if( val.i_int == PAUSE_S )
243 val.i_int = PLAYING_S;
249 var_Set( p_input, "state", val);
251 msg_Dbg(p_intf, "Play/Pause");
252 vlc_object_release( p_input );
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 );
313 vlc_object_release( p_playlist );
318 var_Get( p_input, "audio-es", &val );
319 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
321 i_count = list.p_list->i_count;
324 vlc_object_release( p_input );
327 for( i = 0; i < i_count; i++ )
329 if( val.i_int == list.p_list->p_values[i].i_int )
334 /* value of audio-es was not in choices list */
338 "invalid current audio track, selecting 0" );
339 var_Set( p_input, "audio-es",
340 list.p_list->p_values[0] );
343 else if( i == i_count - 1 )
345 var_Set( p_input, "audio-es",
346 list.p_list->p_values[1] );
351 var_Set( p_input, "audio-es",
352 list.p_list->p_values[i+1] );
355 vlc_object_release( p_input );
358 case GESTURE(DOWN,RIGHT,NONE,NONE):
360 input_thread_t * p_input;
361 vlc_value_t val, list, list2;
364 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
370 p_input = input_from_playlist( p_playlist );
371 vlc_object_release( p_playlist );
376 var_Get( p_input, "spu-es", &val );
378 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
380 i_count = list.p_list->i_count;
383 vlc_object_release( p_input );
386 for( i = 0; i < i_count; i++ )
388 if( val.i_int == list.p_list->p_values[i].i_int )
393 /* value of spu-es was not in choices list */
397 "invalid current subtitle track, selecting 0" );
398 var_Set( p_input, "spu-es", list.p_list->p_values[0] );
401 else if( i == i_count - 1 )
403 var_Set( p_input, "spu-es", list.p_list->p_values[0] );
408 var_Set( p_input, "spu-es",
409 list.p_list->p_values[i+1] );
412 vlc_object_release( p_input );
415 case GESTURE(UP,LEFT,NONE,NONE):
416 if (p_intf->p_sys->p_vout )
418 ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
419 VOUT_FULLSCREEN_CHANGE;
422 case GESTURE(DOWN,LEFT,NONE,NONE):
423 /* FIXME: Should close the vout!"*/
424 vlc_object_kill( p_intf->p_libvlc );
426 case GESTURE(DOWN,LEFT,UP,RIGHT):
427 case GESTURE(UP,RIGHT,DOWN,LEFT):
428 msg_Dbg(p_intf, "a square was drawn!" );
433 p_intf->p_sys->i_num_gestures = 0;
434 p_intf->p_sys->i_pattern = 0;
435 p_intf->p_sys->b_got_gesture = VLC_FALSE;
441 if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
443 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
444 MouseEvent, p_intf );
445 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
446 MouseEvent, p_intf );
447 vlc_object_release( p_intf->p_sys->p_vout );
448 p_intf->p_sys->p_vout = NULL;
451 if( p_intf->p_sys->p_vout == NULL )
453 p_intf->p_sys->p_vout = vlc_object_find( p_intf,
454 VLC_OBJECT_VOUT, FIND_ANYWHERE );
455 if( p_intf->p_sys->p_vout )
457 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
458 MouseEvent, p_intf );
459 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
460 MouseEvent, p_intf );
464 vlc_mutex_unlock( &p_intf->change_lock );
467 msleep( INTF_IDLE_SLEEP );
473 /*****************************************************************************
475 *****************************************************************************/
476 static int InitThread( intf_thread_t * p_intf )
479 /* we might need some locking here */
480 if( !intf_ShouldDie( p_intf ) )
482 /* p_intf->change_lock locking strategy:
483 * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
484 * - make sure there won't be cross increment/decrement ref count
485 * of p_intf->p_sys members p_intf->change_lock should be locked
486 * during those operations */
487 vlc_mutex_lock( &p_intf->change_lock );
489 p_intf->p_sys->b_got_gesture = VLC_FALSE;
490 p_intf->p_sys->b_button_pressed = VLC_FALSE;
491 p_intf->p_sys->i_threshold =
492 config_GetInt( p_intf, "gestures-threshold" );
493 psz_button = config_GetPsz( p_intf, "gestures-button" );
494 if ( !strcmp( psz_button, "left" ) )
496 p_intf->p_sys->i_button_mask = 1;
498 else if ( !strcmp( psz_button, "middle" ) )
500 p_intf->p_sys->i_button_mask = 2;
502 else if ( !strcmp( psz_button, "right" ) )
504 p_intf->p_sys->i_button_mask = 4;
507 p_intf->p_sys->i_pattern = 0;
508 p_intf->p_sys->i_num_gestures = 0;
509 vlc_mutex_unlock( &p_intf->change_lock );
519 /*****************************************************************************
521 *****************************************************************************/
522 static void EndThread( intf_thread_t * p_intf )
524 vlc_mutex_lock( &p_intf->change_lock );
526 if( p_intf->p_sys->p_vout )
528 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
529 MouseEvent, p_intf );
530 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
531 MouseEvent, p_intf );
532 vlc_object_release( p_intf->p_sys->p_vout );
535 vlc_mutex_unlock( &p_intf->change_lock );
538 /*****************************************************************************
539 * MouseEvent: callback for mouse events
540 *****************************************************************************/
541 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
542 vlc_value_t oldval, vlc_value_t newval, void *p_data )
547 signed int i_horizontal, i_vertical;
548 intf_thread_t *p_intf = (intf_thread_t *)p_data;
550 vlc_mutex_lock( &p_intf->change_lock );
552 /* don't process new gestures before the last events are processed */
553 if( p_intf->p_sys->b_got_gesture )
555 vlc_mutex_unlock( &p_intf->change_lock );
559 if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
561 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
562 p_intf->p_sys->i_mouse_x = val.i_int;
563 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
564 p_intf->p_sys->i_mouse_y = val.i_int;
565 i_horizontal = p_intf->p_sys->i_mouse_x -
566 p_intf->p_sys->i_last_x;
567 i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
568 i_vertical = p_intf->p_sys->i_mouse_y
569 - p_intf->p_sys->i_last_y;
570 i_vertical = i_vertical / p_intf->p_sys->i_threshold;
572 if( i_horizontal < 0 )
574 msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
577 else if( i_horizontal > 0 )
579 msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
584 msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
587 else if( i_vertical > 0 )
589 msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
594 p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
595 p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
596 if( gesture( p_intf->p_sys->i_pattern,
597 p_intf->p_sys->i_num_gestures - 1 ) != pattern )
599 p_intf->p_sys->i_pattern |=
600 pattern << ( p_intf->p_sys->i_num_gestures * 4 );
601 p_intf->p_sys->i_num_gestures++;
606 if( !strcmp( psz_var, "mouse-button-down" )
607 && newval.i_int & p_intf->p_sys->i_button_mask
608 && !p_intf->p_sys->b_button_pressed )
610 p_intf->p_sys->b_button_pressed = VLC_TRUE;
611 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
612 p_intf->p_sys->i_last_x = val.i_int;
613 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
614 p_intf->p_sys->i_last_y = val.i_int;
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_FALSE;
621 p_intf->p_sys->b_got_gesture = VLC_TRUE;
624 vlc_mutex_unlock( &p_intf->change_lock );