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_plugin.h>
34 #include <vlc_interface.h>
37 #include <vlc_playlist.h>
43 /*****************************************************************************
44 * intf_sys_t: description and status of interface
45 *****************************************************************************/
48 vlc_object_t * p_vout;
50 bool b_button_pressed;
51 int i_mouse_x, i_mouse_y;
52 int i_last_x, i_last_y;
53 unsigned int i_pattern;
59 /*****************************************************************************
61 *****************************************************************************/
67 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
69 int Open ( vlc_object_t * );
70 void Close ( vlc_object_t * );
71 static int InitThread ( intf_thread_t *p_intf );
72 static void EndThread ( intf_thread_t *p_intf );
73 static int MouseEvent ( vlc_object_t *, char const *,
74 vlc_value_t, vlc_value_t, void * );
76 /* Exported functions */
77 static void RunIntf ( intf_thread_t *p_intf );
79 /*****************************************************************************
81 *****************************************************************************/
82 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
83 #define THRESHOLD_LONGTEXT N_( \
84 "Amount of movement required for a mouse gesture to be recorded." )
86 #define BUTTON_TEXT N_( "Trigger button" )
87 #define BUTTON_LONGTEXT N_( \
88 "Trigger button for mouse gestures." )
90 static const char *const button_list[] = { "left", "middle", "right" };
91 static const char *const button_list_text[] =
92 { N_("Left"), N_("Middle"), N_("Right") };
95 set_shortname( N_("Gestures"));
96 set_category( CAT_INTERFACE );
97 set_subcategory( SUBCAT_INTERFACE_CONTROL );
98 add_integer( "gestures-threshold", 30, NULL,
99 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, true );
100 add_string( "gestures-button", "right", NULL,
101 BUTTON_TEXT, BUTTON_LONGTEXT, false );
102 change_string_list( button_list, button_list_text, 0 );
103 set_description( N_("Mouse gestures control interface") );
105 set_capability( "interface", 0 );
106 set_callbacks( Open, Close );
109 /*****************************************************************************
110 * OpenIntf: initialize interface
111 *****************************************************************************/
112 int Open ( vlc_object_t *p_this )
114 intf_thread_t *p_intf = (intf_thread_t *)p_this;
116 /* Allocate instance and initialize some members */
117 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
118 if( p_intf->p_sys == NULL )
123 p_intf->pf_run = RunIntf;
128 /*****************************************************************************
129 * gesture: return a subpattern within a pattern
130 *****************************************************************************/
131 static int gesture( int i_pattern, int i_num )
133 return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
136 /*****************************************************************************
137 * input_from_playlist: don't forget to release the return value
138 * Also this function should really be available from core.
139 *****************************************************************************/
140 static input_thread_t * input_from_playlist ( playlist_t *p_playlist )
142 input_thread_t * p_input;
145 p_input = p_playlist->p_input;
147 vlc_object_yield( p_input );
153 /*****************************************************************************
154 * CloseIntf: destroy dummy interface
155 *****************************************************************************/
156 void Close ( vlc_object_t *p_this )
158 intf_thread_t *p_intf = (intf_thread_t *)p_this;
160 /* Destroy structure */
161 free( p_intf->p_sys );
165 /*****************************************************************************
167 *****************************************************************************/
168 static void RunIntf( intf_thread_t *p_intf )
170 playlist_t * p_playlist = NULL;
172 vlc_mutex_lock( &p_intf->change_lock );
173 p_intf->p_sys->p_vout = NULL;
174 vlc_mutex_unlock( &p_intf->change_lock );
176 if( InitThread( p_intf ) < 0 )
178 msg_Err( p_intf, "can't initialize interface thread" );
181 msg_Dbg( p_intf, "interface thread initialized" );
184 while( !intf_ShouldDie( p_intf ) )
186 vlc_mutex_lock( &p_intf->change_lock );
191 if( p_intf->p_sys->b_got_gesture )
196 /* If you modify this, please try to follow this convention:
197 Start with LEFT, RIGHT for playback related commands
198 and UP, DOWN, for other commands */
199 switch( p_intf->p_sys->i_pattern )
202 i_interval = config_GetInt( p_intf , "short-jump-size" );
203 if ( i_interval > 0 ) {
204 val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
205 var_Set( p_intf, "time-offset", val );
207 msg_Dbg(p_intf, "Go backward in the movie!");
210 i_interval = config_GetInt( p_intf , "short-jump-size" );
211 if ( i_interval > 0 ) {
212 val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
213 var_Set( p_intf, "time-offset", val );
215 msg_Dbg(p_intf, "Go forward in the movie!");
217 case GESTURE(LEFT,UP,NONE,NONE):
219 msg_Dbg(p_intf, "Going slower.");
221 case GESTURE(RIGHT,UP,NONE,NONE):
223 msg_Dbg(p_intf, "Going faster.");
225 case GESTURE(LEFT,RIGHT,NONE,NONE):
226 case GESTURE(RIGHT,LEFT,NONE,NONE):
228 input_thread_t * p_input;
229 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
235 p_input = input_from_playlist( p_playlist );
236 vlc_object_release( p_playlist );
241 val.i_int = PLAYING_S;
244 var_Get( p_input, "state", &val);
245 if( val.i_int == PAUSE_S )
247 val.i_int = PLAYING_S;
253 var_Set( p_input, "state", val);
255 msg_Dbg(p_intf, "Play/Pause");
256 vlc_object_release( p_input );
259 case GESTURE(LEFT,DOWN,NONE,NONE):
260 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
262 if( p_playlist == NULL )
267 playlist_Prev( p_playlist );
268 vlc_object_release( p_playlist );
270 case GESTURE(RIGHT,DOWN,NONE,NONE):
271 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
273 if( p_playlist == NULL )
278 playlist_Next( p_playlist );
279 vlc_object_release( p_playlist );
283 audio_volume_t i_newvol;
284 aout_VolumeUp( p_intf, 1, &i_newvol );
285 msg_Dbg(p_intf, "Louder");
290 audio_volume_t i_newvol;
291 aout_VolumeDown( p_intf, 1, &i_newvol );
292 msg_Dbg(p_intf, "Quieter");
295 case GESTURE(UP,DOWN,NONE,NONE):
296 case GESTURE(DOWN,UP,NONE,NONE):
298 audio_volume_t i_newvol = -1;
299 aout_VolumeMute( p_intf, &i_newvol );
300 msg_Dbg(p_intf, "Mute sound");
303 case GESTURE(UP,RIGHT,NONE,NONE):
305 input_thread_t * p_input;
306 vlc_value_t val, list, list2;
309 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
315 p_input = input_from_playlist( p_playlist );
317 vlc_object_release( p_playlist );
322 var_Get( p_input, "audio-es", &val );
323 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
325 i_count = list.p_list->i_count;
328 vlc_object_release( p_input );
331 for( i = 0; i < i_count; i++ )
333 if( val.i_int == list.p_list->p_values[i].i_int )
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 );
362 case GESTURE(DOWN,RIGHT,NONE,NONE):
364 input_thread_t * p_input;
365 vlc_value_t val, list, list2;
368 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
374 p_input = input_from_playlist( p_playlist );
375 vlc_object_release( p_playlist );
380 var_Get( p_input, "spu-es", &val );
382 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
384 i_count = list.p_list->i_count;
387 vlc_object_release( p_input );
390 for( i = 0; i < i_count; i++ )
392 if( val.i_int == list.p_list->p_values[i].i_int )
397 /* value of spu-es was not in choices list */
401 "invalid current subtitle track, selecting 0" );
402 var_Set( p_input, "spu-es", list.p_list->p_values[0] );
405 else if( i == i_count - 1 )
407 var_Set( p_input, "spu-es", list.p_list->p_values[0] );
412 var_Set( p_input, "spu-es",
413 list.p_list->p_values[i+1] );
416 vlc_object_release( p_input );
419 case GESTURE(UP,LEFT,NONE,NONE):
420 if (p_intf->p_sys->p_vout )
422 ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
423 VOUT_FULLSCREEN_CHANGE;
426 case GESTURE(DOWN,LEFT,NONE,NONE):
427 /* FIXME: Should close the vout!"*/
428 vlc_object_kill( p_intf->p_libvlc );
430 case GESTURE(DOWN,LEFT,UP,RIGHT):
431 case GESTURE(UP,RIGHT,DOWN,LEFT):
432 msg_Dbg(p_intf, "a square was drawn!" );
437 p_intf->p_sys->i_num_gestures = 0;
438 p_intf->p_sys->i_pattern = 0;
439 p_intf->p_sys->b_got_gesture = false;
445 if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
447 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
448 MouseEvent, p_intf );
449 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
450 MouseEvent, p_intf );
451 vlc_object_release( p_intf->p_sys->p_vout );
452 p_intf->p_sys->p_vout = NULL;
455 if( p_intf->p_sys->p_vout == NULL )
457 p_intf->p_sys->p_vout = vlc_object_find( p_intf,
458 VLC_OBJECT_VOUT, FIND_ANYWHERE );
459 if( p_intf->p_sys->p_vout )
461 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
462 MouseEvent, p_intf );
463 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
464 MouseEvent, p_intf );
468 vlc_mutex_unlock( &p_intf->change_lock );
471 msleep( INTF_IDLE_SLEEP );
477 /*****************************************************************************
479 *****************************************************************************/
480 static int InitThread( intf_thread_t * p_intf )
483 /* we might need some locking here */
484 if( !intf_ShouldDie( p_intf ) )
486 /* p_intf->change_lock locking strategy:
487 * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
488 * - make sure there won't be cross increment/decrement ref count
489 * of p_intf->p_sys members p_intf->change_lock should be locked
490 * during those operations */
491 vlc_mutex_lock( &p_intf->change_lock );
493 p_intf->p_sys->b_got_gesture = false;
494 p_intf->p_sys->b_button_pressed = false;
495 p_intf->p_sys->i_threshold =
496 config_GetInt( p_intf, "gestures-threshold" );
497 psz_button = config_GetPsz( p_intf, "gestures-button" );
498 if ( !strcmp( psz_button, "left" ) )
500 p_intf->p_sys->i_button_mask = 1;
502 else if ( !strcmp( psz_button, "middle" ) )
504 p_intf->p_sys->i_button_mask = 2;
506 else if ( !strcmp( psz_button, "right" ) )
508 p_intf->p_sys->i_button_mask = 4;
511 p_intf->p_sys->i_pattern = 0;
512 p_intf->p_sys->i_num_gestures = 0;
513 vlc_mutex_unlock( &p_intf->change_lock );
523 /*****************************************************************************
525 *****************************************************************************/
526 static void EndThread( intf_thread_t * p_intf )
528 vlc_mutex_lock( &p_intf->change_lock );
530 if( p_intf->p_sys->p_vout )
532 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
533 MouseEvent, p_intf );
534 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
535 MouseEvent, p_intf );
536 vlc_object_release( p_intf->p_sys->p_vout );
539 vlc_mutex_unlock( &p_intf->change_lock );
542 /*****************************************************************************
543 * MouseEvent: callback for mouse events
544 *****************************************************************************/
545 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
546 vlc_value_t oldval, vlc_value_t newval, void *p_data )
548 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
552 signed int i_horizontal, i_vertical;
553 intf_thread_t *p_intf = (intf_thread_t *)p_data;
555 vlc_mutex_lock( &p_intf->change_lock );
557 /* don't process new gestures before the last events are processed */
558 if( p_intf->p_sys->b_got_gesture )
560 vlc_mutex_unlock( &p_intf->change_lock );
564 if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
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;
577 if( i_horizontal < 0 )
579 msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
582 else if( i_horizontal > 0 )
584 msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
589 msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
592 else if( i_vertical > 0 )
594 msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
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 )
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++;
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 )
615 p_intf->p_sys->b_button_pressed = 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;
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 )
625 p_intf->p_sys->b_button_pressed = false;
626 p_intf->p_sys->b_got_gesture = true;
629 vlc_mutex_unlock( &p_intf->change_lock );