1 /*****************************************************************************
2 * gestures.c: control vlc with mouse gestures
3 *****************************************************************************
4 * Copyright (C) 2004-2009 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 *****************************************************************************/
32 #include <vlc_common.h>
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 *****************************************************************************/
49 vlc_object_t * p_vout;
51 bool b_button_pressed;
52 int i_mouse_x, i_mouse_y;
53 int i_last_x, i_last_y;
54 unsigned int i_pattern;
60 /*****************************************************************************
62 *****************************************************************************/
68 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
70 int Open ( vlc_object_t * );
71 void Close ( vlc_object_t * );
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 *const button_list[] = { "left", "middle", "right" };
90 static const char *const button_list_text[] =
91 { N_("Left"), N_("Middle"), N_("Right") };
94 set_shortname( N_("Gestures"))
95 set_category( CAT_INTERFACE )
96 set_subcategory( SUBCAT_INTERFACE_CONTROL )
97 add_integer( "gestures-threshold", 30, NULL,
98 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, true )
99 add_string( "gestures-button", "right", NULL,
100 BUTTON_TEXT, BUTTON_LONGTEXT, false )
101 change_string_list( button_list, button_list_text, 0 )
102 set_description( N_("Mouse gestures control interface") )
104 set_capability( "interface", 0 )
105 set_callbacks( Open, Close )
108 /*****************************************************************************
109 * OpenIntf: initialize interface
110 *****************************************************************************/
111 int 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 intf_sys_t *p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
117 if( p_intf->p_sys == NULL )
120 // Configure the module
121 p_intf->pf_run = RunIntf;
123 vlc_mutex_init( &p_sys->lock );
124 p_sys->p_vout = NULL;
125 p_sys->b_got_gesture = false;
126 p_sys->b_button_pressed = false;
127 p_sys->i_threshold = config_GetInt( p_intf, "gestures-threshold" );
129 // Choose the tight button to use
130 char *psz_button = config_GetPsz( p_intf, "gestures-button" );
131 if( !strcmp( psz_button, "left" ) )
132 p_sys->i_button_mask = 1;
133 else if( !strcmp( psz_button, "middle" ) )
134 p_sys->i_button_mask = 2;
135 else // psz_button == "right"
136 p_sys->i_button_mask = 4;
139 p_sys->i_pattern = 0;
140 p_sys->i_num_gestures = 0;
145 /*****************************************************************************
146 * gesture: return a subpattern within a pattern
147 *****************************************************************************/
148 static int gesture( int i_pattern, int i_num )
150 return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
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 the callbacks
161 if( p_intf->p_sys->p_vout )
163 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
164 MouseEvent, p_intf );
165 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
166 MouseEvent, p_intf );
167 vlc_object_release( p_intf->p_sys->p_vout );
170 /* Destroy structure */
171 vlc_mutex_destroy( &p_intf->p_sys->lock );
172 free( p_intf->p_sys );
176 /*****************************************************************************
178 *****************************************************************************/
179 static void RunIntf( intf_thread_t *p_intf )
181 playlist_t * p_playlist = NULL;
182 int canc = vlc_savecancel();
183 input_thread_t *p_input;
186 while( vlc_object_alive( p_intf ) )
188 vlc_mutex_lock( &p_intf->p_sys->lock );
193 if( p_intf->p_sys->b_got_gesture )
198 /* If you modify this, please try to follow this convention:
199 Start with LEFT, RIGHT for playback related commands
200 and UP, DOWN, for other commands */
201 switch( p_intf->p_sys->i_pattern )
204 msg_Dbg( p_intf, "Go backward in the movie!" );
205 p_playlist = pl_Hold( p_intf );
206 p_input = playlist_CurrentInput( p_playlist );
207 pl_Release( p_intf );
210 i_interval = config_GetInt( p_intf , "short-jump-size" );
211 if ( i_interval > 0 )
213 val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
214 var_Set( p_input, "time-offset", val );
216 vlc_object_release( p_input );
221 msg_Dbg( p_intf, "Go forward in the movie!" );
222 p_playlist = pl_Hold( p_intf );
223 p_input = playlist_CurrentInput( p_playlist );
224 pl_Release( p_intf );
228 i_interval = config_GetInt( p_intf , "short-jump-size" );
229 if ( i_interval > 0 )
231 val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
232 var_Set( p_input, "time-offset", val );
234 vlc_object_release( p_input );
238 case GESTURE(LEFT,UP,NONE,NONE):
239 msg_Dbg( p_intf, "Going slower." );
240 p_playlist = pl_Hold( p_intf );
241 p_input = playlist_CurrentInput( p_playlist );
242 pl_Release( p_intf );
245 var_SetVoid( p_input, "rate-slower" );
246 vlc_object_release( p_input );
250 case GESTURE(RIGHT,UP,NONE,NONE):
251 msg_Dbg( p_intf, "Going faster." );
252 p_playlist = pl_Hold( p_intf );
253 p_input = playlist_CurrentInput( p_playlist );
254 pl_Release( p_intf );
257 var_SetVoid( p_input, "rate-faster" );
258 vlc_object_release( p_input );
262 case GESTURE(LEFT,RIGHT,NONE,NONE):
263 case GESTURE(RIGHT,LEFT,NONE,NONE):
264 msg_Dbg( p_intf, "Play/Pause" );
265 p_playlist = pl_Hold( p_intf );
266 p_input = playlist_CurrentInput( p_playlist );
267 pl_Release( p_intf );
271 var_Get( p_input, "state", &val);
272 val.i_int = ( val.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
273 var_Set( p_input, "state", val);
274 vlc_object_release( p_input );
278 case GESTURE(LEFT,DOWN,NONE,NONE):
279 p_playlist = pl_Hold( p_intf );
280 playlist_Prev( p_playlist );
281 pl_Release( p_intf );
284 case GESTURE(RIGHT,DOWN,NONE,NONE):
285 p_playlist = pl_Hold( p_intf );
286 playlist_Next( p_playlist );
287 pl_Release( p_intf );
291 msg_Dbg(p_intf, "Louder");
292 aout_VolumeUp( p_intf, 1, NULL );
296 msg_Dbg(p_intf, "Quieter");
297 aout_VolumeDown( p_intf, 1, NULL );
300 case GESTURE(UP,DOWN,NONE,NONE):
301 case GESTURE(DOWN,UP,NONE,NONE):
302 msg_Dbg(p_intf, "Mute sound");
303 aout_VolumeMute( p_intf, NULL );
306 case GESTURE(UP,RIGHT,NONE,NONE):
308 vlc_value_t val, list, list2;
311 p_playlist = pl_Hold( p_intf );
312 p_input = playlist_CurrentInput( p_playlist );
313 pl_Release( p_intf );
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 )
332 /* value of audio-es was not in choices list */
336 "invalid current audio track, selecting 0" );
339 else if( i == i_count - 1 )
343 var_Set( p_input, "audio-es", list.p_list->p_values[i] );
344 vlc_object_release( p_input );
347 case GESTURE(DOWN,RIGHT,NONE,NONE):
349 vlc_value_t val, list, list2;
352 p_playlist = pl_Hold( p_intf );
353 p_input = playlist_CurrentInput( p_playlist );
354 pl_Release( p_intf );
359 var_Get( p_input, "spu-es", &val );
361 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
363 i_count = list.p_list->i_count;
366 vlc_object_release( p_input );
369 for( i = 0; i < i_count; i++ )
371 if( val.i_int == list.p_list->p_values[i].i_int )
376 /* value of spu-es was not in choices list */
380 "invalid current subtitle track, selecting 0" );
383 else if( i == i_count - 1 )
387 var_Set( p_input, "spu-es", list.p_list->p_values[i] );
388 vlc_object_release( p_input );
392 case GESTURE(UP,LEFT,NONE,NONE):
393 if (p_intf->p_sys->p_vout )
395 var_Get( p_intf->p_sys->p_vout, "fullscreen", &val );
396 val.b_bool = !val.b_bool;
397 var_Set( p_intf->p_sys->p_vout, "fullscreen", val );
401 case GESTURE(DOWN,LEFT,NONE,NONE):
402 /* FIXME: Should close the vout!"*/
403 libvlc_Quit( p_intf->p_libvlc );
405 case GESTURE(DOWN,LEFT,UP,RIGHT):
406 case GESTURE(UP,RIGHT,DOWN,LEFT):
407 msg_Dbg(p_intf, "a square was drawn!" );
412 p_intf->p_sys->i_num_gestures = 0;
413 p_intf->p_sys->i_pattern = 0;
414 p_intf->p_sys->b_got_gesture = false;
420 if( p_intf->p_sys->p_vout && !vlc_object_alive (p_intf->p_sys->p_vout) )
422 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
423 MouseEvent, p_intf );
424 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
425 MouseEvent, p_intf );
426 vlc_object_release( p_intf->p_sys->p_vout );
427 p_intf->p_sys->p_vout = NULL;
430 if( p_intf->p_sys->p_vout == NULL )
432 p_playlist = pl_Hold( p_intf );
433 p_input = playlist_CurrentInput( p_playlist );
434 pl_Release( p_intf );
437 p_intf->p_sys->p_vout = input_GetVout( p_input );
438 vlc_object_release( p_input );
440 if( p_intf->p_sys->p_vout )
442 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
443 MouseEvent, p_intf );
444 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
445 MouseEvent, p_intf );
449 vlc_mutex_unlock( &p_intf->p_sys->lock );
452 msleep( INTF_IDLE_SLEEP );
455 vlc_restorecancel( canc );
458 /*****************************************************************************
459 * MouseEvent: callback for mouse events
460 *****************************************************************************/
461 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
462 vlc_value_t oldval, vlc_value_t newval, void *p_data )
464 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
468 signed int i_horizontal, i_vertical;
469 intf_thread_t *p_intf = (intf_thread_t *)p_data;
471 vlc_mutex_lock( &p_intf->p_sys->lock );
473 /* don't process new gestures before the last events are processed */
474 if( p_intf->p_sys->b_got_gesture )
476 vlc_mutex_unlock( &p_intf->p_sys->lock );
480 if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
482 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
483 p_intf->p_sys->i_mouse_x = val.i_int;
484 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
485 p_intf->p_sys->i_mouse_y = val.i_int;
486 i_horizontal = p_intf->p_sys->i_mouse_x -
487 p_intf->p_sys->i_last_x;
488 i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
489 i_vertical = p_intf->p_sys->i_mouse_y
490 - p_intf->p_sys->i_last_y;
491 i_vertical = i_vertical / p_intf->p_sys->i_threshold;
493 if( i_horizontal < 0 )
495 msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
498 else if( i_horizontal > 0 )
500 msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
505 msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
508 else if( i_vertical > 0 )
510 msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
515 p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
516 p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
517 if( gesture( p_intf->p_sys->i_pattern,
518 p_intf->p_sys->i_num_gestures - 1 ) != pattern )
520 p_intf->p_sys->i_pattern |=
521 pattern << ( p_intf->p_sys->i_num_gestures * 4 );
522 p_intf->p_sys->i_num_gestures++;
527 if( !strcmp( psz_var, "mouse-button-down" )
528 && newval.i_int & p_intf->p_sys->i_button_mask
529 && !p_intf->p_sys->b_button_pressed )
531 p_intf->p_sys->b_button_pressed = true;
532 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
533 p_intf->p_sys->i_last_x = val.i_int;
534 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
535 p_intf->p_sys->i_last_y = val.i_int;
537 if( !strcmp( psz_var, "mouse-button-down" )
538 && !( newval.i_int & p_intf->p_sys->i_button_mask )
539 && p_intf->p_sys->b_button_pressed )
541 p_intf->p_sys->b_button_pressed = false;
542 p_intf->p_sys->b_got_gesture = true;
545 vlc_mutex_unlock( &p_intf->p_sys->lock );