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 #define BUTTON_DEFAULT "left"
81 static const char *const button_list[] = { "left", "middle", "right" };
82 static const char *const button_list_text[] =
83 { N_("Left"), N_("Middle"), N_("Right") };
86 set_shortname( N_("Gestures"))
87 set_category( CAT_INTERFACE )
88 set_subcategory( SUBCAT_INTERFACE_CONTROL )
89 add_integer( "gestures-threshold", 30,
90 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, true )
91 add_string( "gestures-button", BUTTON_DEFAULT,
92 BUTTON_TEXT, BUTTON_LONGTEXT, false )
93 change_string_list( button_list, button_list_text )
94 set_description( N_("Mouse gestures control interface") )
96 set_capability( "interface", 0 )
97 set_callbacks( Open, Close )
100 static int PlaylistEvent( vlc_object_t *, char const *,
101 vlc_value_t, vlc_value_t, void * );
102 static int InputEvent( vlc_object_t *, char const *,
103 vlc_value_t, vlc_value_t, void * );
104 static int MovedEvent( vlc_object_t *, char const *,
105 vlc_value_t, vlc_value_t, void * );
106 static int ButtonEvent( vlc_object_t *, char const *,
107 vlc_value_t, vlc_value_t, void * );
109 /*****************************************************************************
110 * OpenIntf: initialize interface
111 *****************************************************************************/
112 static 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 intf_sys_t *p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
118 if( unlikely(p_sys == NULL) )
121 // Configure the module
122 vlc_mutex_init( &p_sys->lock );
123 p_sys->p_input = NULL;
124 p_sys->p_vout = NULL;
125 p_sys->b_button_pressed = false;
126 p_sys->i_threshold = var_InheritInteger( p_intf, "gestures-threshold" );
128 // Choose the tight button to use
129 char *psz_button = var_InheritString( p_intf, "gestures-button" );
130 if( psz_button && !strcmp( psz_button, "left" ) )
131 p_sys->i_button_mask = 1;
132 else if( psz_button && !strcmp( psz_button, "middle" ) )
133 p_sys->i_button_mask = 2;
134 else // psz_button == "right"
135 p_sys->i_button_mask = 4;
138 p_sys->i_pattern = 0;
139 p_sys->i_num_gestures = 0;
141 var_AddCallback( pl_Get(p_intf), "input-current", PlaylistEvent, p_intf );
146 /*****************************************************************************
147 * gesture: return a subpattern within a pattern
148 *****************************************************************************/
149 static int gesture( int i_pattern, int i_num )
151 return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
154 /*****************************************************************************
155 * CloseIntf: destroy dummy interface
156 *****************************************************************************/
157 static void Close ( vlc_object_t *p_this )
159 intf_thread_t *p_intf = (intf_thread_t *)p_this;
160 intf_sys_t *p_sys = p_intf->p_sys;
162 /* Destroy the callbacks (the order matters!) */
163 var_DelCallback( pl_Get(p_intf), "input-current", PlaylistEvent, p_intf );
167 var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
168 vlc_object_release( p_sys->p_input );
173 var_DelCallback( p_sys->p_vout, "mouse-moved", MovedEvent, p_intf );
174 var_DelCallback( p_sys->p_vout, "mouse-button-down",
175 ButtonEvent, p_intf );
176 vlc_object_release( p_sys->p_vout );
179 /* Destroy structure */
180 vlc_mutex_destroy( &p_sys->lock );
184 static void ProcessGesture( intf_thread_t *p_intf )
186 intf_sys_t *p_sys = p_intf->p_sys;
187 playlist_t *p_playlist = pl_Get( p_intf );
190 /* If you modify this, please try to follow this convention:
191 Start with LEFT, RIGHT for playback related commands
192 and UP, DOWN, for other commands */
193 switch( p_sys->i_pattern )
197 msg_Dbg( p_intf, "Go backward in the movie!" );
199 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
200 if( p_input == NULL )
203 int it = var_InheritInteger( p_intf , "short-jump-size" );
205 var_SetTime( p_input, "time-offset", -CLOCK_FREQ * it );
206 vlc_object_release( p_input );
212 msg_Dbg( p_intf, "Go forward in the movie!" );
214 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
215 if( p_input == NULL )
218 int it = var_InheritInteger( p_intf , "short-jump-size" );
220 var_SetTime( p_input, "time-offset", CLOCK_FREQ * it );
221 vlc_object_release( p_input );
225 case GESTURE(LEFT,UP,NONE,NONE):
226 msg_Dbg( p_intf, "Going slower." );
227 var_TriggerCallback( p_playlist, "rate-slower" );
230 case GESTURE(RIGHT,UP,NONE,NONE):
231 msg_Dbg( p_intf, "Going faster." );
232 var_TriggerCallback( p_playlist, "rate-faster" );
235 case GESTURE(LEFT,RIGHT,NONE,NONE):
236 case GESTURE(RIGHT,LEFT,NONE,NONE):
238 msg_Dbg( p_intf, "Play/Pause" );
240 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
241 if( p_input == NULL )
244 int i_state = var_GetInteger( p_input, "state" );
245 i_state = (i_state == PLAYING_S) ? PAUSE_S : PLAYING_S;
246 var_SetInteger( p_input, "state", i_state );
247 vlc_object_release( p_input );
251 case GESTURE(LEFT,DOWN,NONE,NONE):
252 playlist_Prev( p_playlist );
255 case GESTURE(RIGHT,DOWN,NONE,NONE):
256 playlist_Next( p_playlist );
260 msg_Dbg(p_intf, "Louder");
261 playlist_VolumeUp( p_playlist, 1, NULL );
265 msg_Dbg(p_intf, "Quieter");
266 playlist_VolumeDown( p_playlist, 1, NULL );
269 case GESTURE(UP,DOWN,NONE,NONE):
270 case GESTURE(DOWN,UP,NONE,NONE):
271 msg_Dbg( p_intf, "Mute sound" );
272 playlist_MuteToggle( p_playlist );
275 case GESTURE(UP,RIGHT,NONE,NONE):
277 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
278 if( p_input == NULL )
281 vlc_value_t list, list2;
282 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
285 if( list.p_list->i_count > 1 )
287 int i_audio_es = var_GetInteger( p_input, "audio-es" );
290 for( i = 0; i < list.p_list->i_count; i++ )
291 if( i_audio_es == list.p_list->p_values[i].i_int )
293 /* value of audio-es was not in choices list */
294 if( i == list.p_list->i_count )
297 "invalid current audio track, selecting 0" );
300 else if( i == list.p_list->i_count - 1 )
304 var_SetInteger( p_input, "audio-es",
305 list.p_list->p_values[i].i_int );
307 var_FreeList( &list, &list2 );
308 vlc_object_release( p_input );
312 case GESTURE(DOWN,RIGHT,NONE,NONE):
314 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
315 if( p_input == NULL )
318 vlc_value_t list, list2;
319 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
322 if( list.p_list->i_count > 1 )
324 int i_audio_es = var_GetInteger( p_input, "spu-es" );
327 for( i = 0; i < list.p_list->i_count; i++ )
328 if( i_audio_es == list.p_list->p_values[i].i_int )
330 /* value of audio-es was not in choices list */
331 if( i == list.p_list->i_count )
334 "invalid current subtitle track, selecting 0" );
337 else if( i == list.p_list->i_count - 1 )
341 var_SetInteger( p_input, "audio-es",
342 list.p_list->p_values[i].i_int );
344 var_FreeList( &list, &list2 );
345 vlc_object_release( p_input );
349 case GESTURE(UP,LEFT,NONE,NONE):
351 bool val = var_ToggleBool( pl_Get( p_intf ), "fullscreen" );
353 var_SetBool( p_sys->p_vout, "fullscreen", val );
357 case GESTURE(DOWN,LEFT,NONE,NONE):
358 /* FIXME: Should close the vout!"*/
359 libvlc_Quit( p_intf->p_libvlc );
362 case GESTURE(DOWN,LEFT,UP,RIGHT):
363 case GESTURE(UP,RIGHT,DOWN,LEFT):
364 msg_Dbg( p_intf, "a square was drawn!" );
368 p_sys->i_num_gestures = 0;
369 p_sys->i_pattern = 0;
372 static int MovedEvent( vlc_object_t *p_this, char const *psz_var,
373 vlc_value_t oldval, vlc_value_t newval, void *p_data )
375 intf_thread_t *p_intf = (intf_thread_t *)p_data;
376 intf_sys_t *p_sys = p_intf->p_sys;
378 (void) p_this; (void) psz_var; (void) oldval;
380 vlc_mutex_lock( &p_sys->lock );
381 if( p_sys->b_button_pressed )
383 int i_horizontal = newval.coords.x - p_sys->i_last_x;
384 int i_vertical = newval.coords.y - p_sys->i_last_y;
387 i_horizontal = i_horizontal / p_sys->i_threshold;
388 i_vertical = i_vertical / p_sys->i_threshold;
390 if( i_horizontal < 0 )
392 msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
395 else if( i_horizontal > 0 )
397 msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
402 msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
405 else if( i_vertical > 0 )
407 msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
413 p_sys->i_last_x = newval.coords.x;
414 p_sys->i_last_y = newval.coords.y;
415 if( gesture( p_sys->i_pattern, p_sys->i_num_gestures - 1 )
418 p_sys->i_pattern |= pattern << ( p_sys->i_num_gestures * 4 );
419 p_sys->i_num_gestures++;
424 vlc_mutex_unlock( &p_sys->lock );
429 static int ButtonEvent( vlc_object_t *p_this, char const *psz_var,
430 vlc_value_t oldval, vlc_value_t val, void *p_data )
432 intf_thread_t *p_intf = p_data;
433 intf_sys_t *p_sys = p_intf->p_sys;
435 (void) psz_var; (void) oldval;
437 vlc_mutex_lock( &p_sys->lock );
438 if( val.i_int & p_sys->i_button_mask )
440 if( !p_sys->b_button_pressed )
442 p_sys->b_button_pressed = true;
443 var_GetCoords( p_this, "mouse-moved",
444 &p_sys->i_last_x, &p_sys->i_last_y );
449 if( p_sys->b_button_pressed )
451 p_sys->b_button_pressed = false;
452 ProcessGesture( p_intf );
455 vlc_mutex_unlock( &p_sys->lock );
460 static int InputEvent( vlc_object_t *p_this, char const *psz_var,
461 vlc_value_t oldval, vlc_value_t val, void *p_data )
463 input_thread_t *p_input = (input_thread_t *)p_this;
464 intf_thread_t *p_intf = p_data;
465 intf_sys_t *p_sys = p_intf->p_sys;
467 (void) psz_var; (void) oldval;
471 case INPUT_EVENT_DEAD:
472 vlc_object_release( p_input );
473 p_sys->p_input = NULL; /* FIXME: locking!! */
476 case INPUT_EVENT_VOUT:
477 /* intf-event is serialized against itself and is the sole user of
478 * p_sys->p_vout. So there is no need to acquire the lock currently. */
479 if( p_sys->p_vout != NULL )
480 { /* /!\ Beware of lock inversion with var_DelCallback() /!\ */
481 var_DelCallback( p_sys->p_vout, "mouse-moved", MovedEvent,
483 var_DelCallback( p_sys->p_vout, "mouse-button-down", ButtonEvent,
485 vlc_object_release( p_sys->p_vout );
488 p_sys->p_vout = input_GetVout( p_input );
489 if( p_sys->p_vout != NULL )
491 var_AddCallback( p_sys->p_vout, "mouse-moved", MovedEvent,
493 var_AddCallback( p_sys->p_vout, "mouse-button-down", ButtonEvent,
501 static int PlaylistEvent( vlc_object_t *p_this, char const *psz_var,
502 vlc_value_t oldval, vlc_value_t val, void *p_data )
504 intf_thread_t *p_intf = p_data;
505 intf_sys_t *p_sys = p_intf->p_sys;
506 input_thread_t *p_input = val.p_address;
508 (void) p_this; (void) psz_var; (void) oldval;
510 var_AddCallback( p_input, "intf-event", InputEvent, p_intf );
511 assert( p_sys->p_input == NULL );
512 p_sys->p_input = vlc_object_hold( p_input );