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>
36 #include <vlc_playlist.h>
39 /*****************************************************************************
40 * intf_sys_t: description and status of interface
41 *****************************************************************************/
45 input_thread_t *p_input;
46 vout_thread_t *p_vout;
47 bool b_button_pressed;
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 static int Open ( vlc_object_t * );
66 static void Close ( vlc_object_t * );
68 /*****************************************************************************
70 *****************************************************************************/
71 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
72 #define THRESHOLD_LONGTEXT N_( \
73 "Amount of movement required for a mouse gesture to be recorded." )
75 #define BUTTON_TEXT N_( "Trigger button" )
76 #define BUTTON_LONGTEXT N_( \
77 "Trigger button for mouse gestures." )
79 #if defined (HAVE_MAEMO)
80 # define BUTTON_DEFAULT "left"
82 # define BUTTON_DEFAULT "right"
85 static const char *const button_list[] = { "left", "middle", "right" };
86 static const char *const button_list_text[] =
87 { N_("Left"), N_("Middle"), N_("Right") };
90 set_shortname( N_("Gestures"))
91 set_category( CAT_INTERFACE )
92 set_subcategory( SUBCAT_INTERFACE_CONTROL )
93 add_integer( "gestures-threshold", 30,
94 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, true )
95 add_string( "gestures-button", BUTTON_DEFAULT,
96 BUTTON_TEXT, BUTTON_LONGTEXT, false )
97 change_string_list( button_list, button_list_text )
98 set_description( N_("Mouse gestures control interface") )
100 set_capability( "interface", 0 )
101 set_callbacks( Open, Close )
104 static int PlaylistEvent( vlc_object_t *, char const *,
105 vlc_value_t, vlc_value_t, void * );
106 static int InputEvent( vlc_object_t *, char const *,
107 vlc_value_t, vlc_value_t, void * );
108 static int MovedEvent( vlc_object_t *, char const *,
109 vlc_value_t, vlc_value_t, void * );
110 static int ButtonEvent( vlc_object_t *, char const *,
111 vlc_value_t, vlc_value_t, void * );
113 /*****************************************************************************
114 * OpenIntf: initialize interface
115 *****************************************************************************/
116 static int Open ( vlc_object_t *p_this )
118 intf_thread_t *p_intf = (intf_thread_t *)p_this;
120 /* Allocate instance and initialize some members */
121 intf_sys_t *p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
122 if( unlikely(p_sys == NULL) )
125 // Configure the module
126 vlc_mutex_init( &p_sys->lock );
127 p_sys->p_input = NULL;
128 p_sys->p_vout = NULL;
129 p_sys->b_button_pressed = false;
130 p_sys->i_threshold = var_InheritInteger( p_intf, "gestures-threshold" );
132 // Choose the tight button to use
133 char *psz_button = var_InheritString( p_intf, "gestures-button" );
134 if( psz_button && !strcmp( psz_button, "left" ) )
135 p_sys->i_button_mask = 1;
136 else if( psz_button && !strcmp( psz_button, "middle" ) )
137 p_sys->i_button_mask = 2;
138 else // psz_button == "right"
139 p_sys->i_button_mask = 4;
142 p_sys->i_pattern = 0;
143 p_sys->i_num_gestures = 0;
145 var_AddCallback( pl_Get(p_intf), "input-current", PlaylistEvent, p_intf );
150 /*****************************************************************************
151 * gesture: return a subpattern within a pattern
152 *****************************************************************************/
153 static int gesture( int i_pattern, int i_num )
155 return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
158 /*****************************************************************************
159 * CloseIntf: destroy dummy interface
160 *****************************************************************************/
161 static void Close ( vlc_object_t *p_this )
163 intf_thread_t *p_intf = (intf_thread_t *)p_this;
164 intf_sys_t *p_sys = p_intf->p_sys;
166 /* Destroy the callbacks (the order matters!) */
167 var_DelCallback( pl_Get(p_intf), "input-current", PlaylistEvent, p_intf );
171 var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
172 vlc_object_release( p_sys->p_input );
177 var_DelCallback( p_sys->p_vout, "mouse-moved", MovedEvent, p_intf );
178 var_DelCallback( p_sys->p_vout, "mouse-button-down",
179 ButtonEvent, p_intf );
180 vlc_object_release( p_sys->p_vout );
183 /* Destroy structure */
184 vlc_mutex_destroy( &p_sys->lock );
188 static void ProcessGesture( intf_thread_t *p_intf )
190 intf_sys_t *p_sys = p_intf->p_sys;
191 playlist_t *p_playlist = pl_Get( p_intf );
194 /* If you modify this, please try to follow this convention:
195 Start with LEFT, RIGHT for playback related commands
196 and UP, DOWN, for other commands */
197 switch( p_sys->i_pattern )
201 msg_Dbg( p_intf, "Go backward in the movie!" );
203 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
204 if( p_input == NULL )
207 int it = var_InheritInteger( p_intf , "short-jump-size" );
209 var_SetTime( p_input, "time-offset", -CLOCK_FREQ * it );
210 vlc_object_release( p_input );
216 msg_Dbg( p_intf, "Go forward in the movie!" );
218 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
219 if( p_input == NULL )
222 int it = var_InheritInteger( p_intf , "short-jump-size" );
224 var_SetTime( p_input, "time-offset", CLOCK_FREQ * it );
225 vlc_object_release( p_input );
229 case GESTURE(LEFT,UP,NONE,NONE):
230 msg_Dbg( p_intf, "Going slower." );
231 var_TriggerCallback( p_playlist, "rate-slower" );
234 case GESTURE(RIGHT,UP,NONE,NONE):
235 msg_Dbg( p_intf, "Going faster." );
236 var_TriggerCallback( p_playlist, "rate-faster" );
239 case GESTURE(LEFT,RIGHT,NONE,NONE):
240 case GESTURE(RIGHT,LEFT,NONE,NONE):
242 msg_Dbg( p_intf, "Play/Pause" );
244 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
245 if( p_input == NULL )
248 int i_state = var_GetInteger( p_input, "state" );
249 i_state = (i_state == PLAYING_S) ? PAUSE_S : PLAYING_S;
250 var_SetInteger( p_input, "state", i_state );
251 vlc_object_release( p_input );
255 case GESTURE(LEFT,DOWN,NONE,NONE):
256 playlist_Prev( p_playlist );
259 case GESTURE(RIGHT,DOWN,NONE,NONE):
260 playlist_Next( p_playlist );
264 msg_Dbg(p_intf, "Louder");
265 playlist_VolumeUp( p_playlist, 1, NULL );
269 msg_Dbg(p_intf, "Quieter");
270 playlist_VolumeDown( p_playlist, 1, NULL );
273 case GESTURE(UP,DOWN,NONE,NONE):
274 case GESTURE(DOWN,UP,NONE,NONE):
275 msg_Dbg( p_intf, "Mute sound" );
276 playlist_MuteToggle( p_playlist );
279 case GESTURE(UP,RIGHT,NONE,NONE):
281 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
282 if( p_input == NULL )
285 vlc_value_t list, list2;
286 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
289 if( list.p_list->i_count > 1 )
291 int i_audio_es = var_GetInteger( p_input, "audio-es" );
294 for( i = 0; i < list.p_list->i_count; i++ )
295 if( i_audio_es == list.p_list->p_values[i].i_int )
297 /* value of audio-es was not in choices list */
298 if( i == list.p_list->i_count )
301 "invalid current audio track, selecting 0" );
304 else if( i == list.p_list->i_count - 1 )
308 var_SetInteger( p_input, "audio-es",
309 list.p_list->p_values[i].i_int );
311 var_FreeList( &list, &list2 );
312 vlc_object_release( p_input );
316 case GESTURE(DOWN,RIGHT,NONE,NONE):
318 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
319 if( p_input == NULL )
322 vlc_value_t list, list2;
323 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
326 if( list.p_list->i_count > 1 )
328 int i_audio_es = var_GetInteger( p_input, "spu-es" );
331 for( i = 0; i < list.p_list->i_count; i++ )
332 if( i_audio_es == list.p_list->p_values[i].i_int )
334 /* value of audio-es was not in choices list */
335 if( i == list.p_list->i_count )
338 "invalid current subtitle track, selecting 0" );
341 else if( i == list.p_list->i_count - 1 )
345 var_SetInteger( p_input, "audio-es",
346 list.p_list->p_values[i].i_int );
348 var_FreeList( &list, &list2 );
349 vlc_object_release( p_input );
353 case GESTURE(UP,LEFT,NONE,NONE):
355 bool val = var_ToggleBool( pl_Get( p_intf ), "fullscreen" );
357 var_SetBool( p_sys->p_vout, "fullscreen", val );
361 case GESTURE(DOWN,LEFT,NONE,NONE):
362 /* FIXME: Should close the vout!"*/
363 libvlc_Quit( p_intf->p_libvlc );
366 case GESTURE(DOWN,LEFT,UP,RIGHT):
367 case GESTURE(UP,RIGHT,DOWN,LEFT):
368 msg_Dbg( p_intf, "a square was drawn!" );
372 p_sys->i_num_gestures = 0;
373 p_sys->i_pattern = 0;
376 static int MovedEvent( vlc_object_t *p_this, char const *psz_var,
377 vlc_value_t oldval, vlc_value_t newval, void *p_data )
379 intf_thread_t *p_intf = (intf_thread_t *)p_data;
380 intf_sys_t *p_sys = p_intf->p_sys;
382 (void) p_this; (void) psz_var; (void) oldval;
384 vlc_mutex_lock( &p_sys->lock );
385 if( p_sys->b_button_pressed )
387 int i_horizontal = newval.coords.x - p_sys->i_last_x;
388 int i_vertical = newval.coords.y - p_sys->i_last_y;
391 i_horizontal = i_horizontal / p_sys->i_threshold;
392 i_vertical = i_vertical / p_sys->i_threshold;
394 if( i_horizontal < 0 )
396 msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
399 else if( i_horizontal > 0 )
401 msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
406 msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
409 else if( i_vertical > 0 )
411 msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
417 p_sys->i_last_x = newval.coords.x;
418 p_sys->i_last_y = newval.coords.y;
419 if( gesture( p_sys->i_pattern, p_sys->i_num_gestures - 1 )
422 p_sys->i_pattern |= pattern << ( p_sys->i_num_gestures * 4 );
423 p_sys->i_num_gestures++;
428 vlc_mutex_unlock( &p_sys->lock );
433 static int ButtonEvent( vlc_object_t *p_this, char const *psz_var,
434 vlc_value_t oldval, vlc_value_t val, void *p_data )
436 intf_thread_t *p_intf = p_data;
437 intf_sys_t *p_sys = p_intf->p_sys;
439 (void) psz_var; (void) oldval;
441 vlc_mutex_lock( &p_sys->lock );
442 if( val.i_int & p_sys->i_button_mask )
444 if( !p_sys->b_button_pressed )
446 p_sys->b_button_pressed = true;
447 var_GetCoords( p_this, "mouse-moved",
448 &p_sys->i_last_x, &p_sys->i_last_y );
453 if( p_sys->b_button_pressed )
455 p_sys->b_button_pressed = false;
456 ProcessGesture( p_intf );
459 vlc_mutex_unlock( &p_sys->lock );
464 static int InputEvent( vlc_object_t *p_this, char const *psz_var,
465 vlc_value_t oldval, vlc_value_t val, void *p_data )
467 input_thread_t *p_input = (input_thread_t *)p_this;
468 intf_thread_t *p_intf = p_data;
469 intf_sys_t *p_sys = p_intf->p_sys;
471 (void) psz_var; (void) oldval;
475 case INPUT_EVENT_DEAD:
476 vlc_object_release( p_input );
477 p_sys->p_input = NULL; /* FIXME: locking!! */
480 case INPUT_EVENT_VOUT:
481 /* intf-event is serialized against itself and is the sole user of
482 * p_sys->p_vout. So there is no need to acquire the lock currently. */
483 if( p_sys->p_vout != NULL )
484 { /* /!\ Beware of lock inversion with var_DelCallback() /!\ */
485 var_DelCallback( p_sys->p_vout, "mouse-moved", MovedEvent,
487 var_DelCallback( p_sys->p_vout, "mouse-button-down", ButtonEvent,
489 vlc_object_release( p_sys->p_vout );
492 p_sys->p_vout = input_GetVout( p_input );
493 if( p_sys->p_vout != NULL )
495 var_AddCallback( p_sys->p_vout, "mouse-moved", MovedEvent,
497 var_AddCallback( p_sys->p_vout, "mouse-button-down", ButtonEvent,
505 static int PlaylistEvent( vlc_object_t *p_this, char const *psz_var,
506 vlc_value_t oldval, vlc_value_t val, void *p_data )
508 intf_thread_t *p_intf = p_data;
509 intf_sys_t *p_sys = p_intf->p_sys;
510 input_thread_t *p_input = val.p_address;
512 (void) p_this; (void) psz_var; (void) oldval;
514 var_AddCallback( p_input, "intf-event", InputEvent, p_intf );
515 assert( p_sys->p_input == NULL );
516 p_sys->p_input = vlc_object_hold( p_input );