1 /*****************************************************************************
2 * gestures.c: control vlc with mouse gestures
3 *****************************************************************************
4 * Copyright (C) 2004-2009 the VideoLAN team
6 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
36 #include <vlc_playlist_legacy.h>
37 #include <vlc_input.h>
40 /*****************************************************************************
41 * intf_sys_t: description and status of interface
42 *****************************************************************************/
46 input_thread_t
*p_input
;
47 vout_thread_t
*p_vout
;
48 bool b_button_pressed
;
49 int i_last_x
, i_last_y
;
50 unsigned int i_pattern
;
51 unsigned int i_num_gestures
;
56 /*****************************************************************************
58 *****************************************************************************/
64 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
66 static int Open ( vlc_object_t
* );
67 static void Close ( vlc_object_t
* );
69 /*****************************************************************************
71 *****************************************************************************/
72 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
73 #define THRESHOLD_LONGTEXT N_( \
74 "Amount of movement required for a mouse gesture to be recorded." )
76 #define BUTTON_TEXT N_( "Trigger button" )
77 #define BUTTON_LONGTEXT N_( \
78 "Trigger button for mouse gestures." )
80 #define BUTTON_DEFAULT "left"
82 static const char *const button_list
[] = { "left", "middle", "right" };
83 static const char *const button_list_text
[] =
84 { N_("Left"), N_("Middle"), N_("Right") };
87 set_shortname( N_("Gestures"))
88 set_category( CAT_INTERFACE
)
89 set_subcategory( SUBCAT_INTERFACE_CONTROL
)
90 add_integer( "gestures-threshold", 30,
91 THRESHOLD_TEXT
, THRESHOLD_LONGTEXT
, true )
92 add_string( "gestures-button", BUTTON_DEFAULT
,
93 BUTTON_TEXT
, BUTTON_LONGTEXT
, false )
94 change_string_list( button_list
, button_list_text
)
95 set_description( N_("Mouse gestures control interface") )
97 set_capability( "interface", 0 )
98 set_callbacks( Open
, Close
)
101 static int PlaylistEvent( vlc_object_t
*, char const *,
102 vlc_value_t
, vlc_value_t
, void * );
103 static int InputEvent( vlc_object_t
*, char const *,
104 vlc_value_t
, vlc_value_t
, void * );
105 static int MovedEvent( vlc_object_t
*, char const *,
106 vlc_value_t
, vlc_value_t
, void * );
107 static int ButtonEvent( vlc_object_t
*, char const *,
108 vlc_value_t
, vlc_value_t
, void * );
110 /*****************************************************************************
111 * OpenIntf: initialize interface
112 *****************************************************************************/
113 static int Open ( vlc_object_t
*p_this
)
115 intf_thread_t
*p_intf
= (intf_thread_t
*)p_this
;
117 /* Allocate instance and initialize some members */
118 intf_sys_t
*p_sys
= p_intf
->p_sys
= malloc( sizeof( intf_sys_t
) );
119 if( unlikely(p_sys
== NULL
) )
122 // Configure the module
123 vlc_mutex_init( &p_sys
->lock
);
124 p_sys
->p_input
= NULL
;
125 p_sys
->p_vout
= NULL
;
126 p_sys
->b_button_pressed
= false;
127 p_sys
->i_threshold
= var_InheritInteger( p_intf
, "gestures-threshold" );
129 // Choose the tight button to use
130 char *psz_button
= var_InheritString( p_intf
, "gestures-button" );
131 if( psz_button
&& !strcmp( psz_button
, "left" ) )
132 p_sys
->i_button_mask
= 1;
133 else if( psz_button
&& !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;
142 var_AddCallback( pl_Get(p_intf
), "input-current", PlaylistEvent
, p_intf
);
147 /*****************************************************************************
148 * gesture: return a subpattern within a pattern
149 *****************************************************************************/
150 static inline unsigned gesture( unsigned i_pattern
, unsigned i_num
)
152 return ( i_pattern
>> ( i_num
* 4 ) ) & 0xF;
155 /*****************************************************************************
156 * CloseIntf: destroy dummy interface
157 *****************************************************************************/
158 static void Close ( vlc_object_t
*p_this
)
160 intf_thread_t
*p_intf
= (intf_thread_t
*)p_this
;
161 intf_sys_t
*p_sys
= p_intf
->p_sys
;
163 /* Destroy the callbacks (the order matters!) */
164 var_DelCallback( pl_Get(p_intf
), "input-current", PlaylistEvent
, p_intf
);
166 if( p_sys
->p_input
!= NULL
)
167 var_DelCallback( p_sys
->p_input
, "intf-event", InputEvent
, p_intf
);
171 var_DelCallback( p_sys
->p_vout
, "mouse-moved", MovedEvent
, p_intf
);
172 var_DelCallback( p_sys
->p_vout
, "mouse-button-down",
173 ButtonEvent
, p_intf
);
174 vlc_object_release( p_sys
->p_vout
);
177 /* Destroy structure */
178 vlc_mutex_destroy( &p_sys
->lock
);
182 static void ProcessGesture( intf_thread_t
*p_intf
)
184 intf_sys_t
*p_sys
= p_intf
->p_sys
;
185 playlist_t
*p_playlist
= pl_Get( p_intf
);
188 /* If you modify this, please try to follow this convention:
189 Start with LEFT, RIGHT for playback related commands
190 and UP, DOWN, for other commands */
191 switch( p_sys
->i_pattern
)
195 msg_Dbg( p_intf
, "Go backward in the movie!" );
197 input_thread_t
*p_input
= playlist_CurrentInput( p_playlist
);
198 if( p_input
== NULL
)
201 int it
= var_InheritInteger( p_intf
, "short-jump-size" );
203 var_SetInteger( p_input
, "time-offset", vlc_tick_from_sec( -it
) );
204 vlc_object_release( p_input
);
210 msg_Dbg( p_intf
, "Go forward in the movie!" );
212 input_thread_t
*p_input
= playlist_CurrentInput( p_playlist
);
213 if( p_input
== NULL
)
216 int it
= var_InheritInteger( p_intf
, "short-jump-size" );
218 var_SetInteger( p_input
, "time-offset", vlc_tick_from_sec( it
) );
219 vlc_object_release( p_input
);
223 case GESTURE(LEFT
,UP
,NONE
,NONE
):
224 msg_Dbg( p_intf
, "Going slower." );
225 var_TriggerCallback( p_playlist
, "rate-slower" );
228 case GESTURE(RIGHT
,UP
,NONE
,NONE
):
229 msg_Dbg( p_intf
, "Going faster." );
230 var_TriggerCallback( p_playlist
, "rate-faster" );
233 case GESTURE(LEFT
,RIGHT
,NONE
,NONE
):
234 case GESTURE(RIGHT
,LEFT
,NONE
,NONE
):
236 msg_Dbg( p_intf
, "Play/Pause" );
238 input_thread_t
*p_input
= playlist_CurrentInput( p_playlist
);
239 if( p_input
== NULL
)
242 int i_state
= var_GetInteger( p_input
, "state" );
243 i_state
= (i_state
== PLAYING_S
) ? PAUSE_S
: PLAYING_S
;
244 var_SetInteger( p_input
, "state", i_state
);
245 vlc_object_release( p_input
);
249 case GESTURE(LEFT
,DOWN
,NONE
,NONE
):
250 playlist_Prev( p_playlist
);
253 case GESTURE(RIGHT
,DOWN
,NONE
,NONE
):
254 playlist_Next( p_playlist
);
258 msg_Dbg(p_intf
, "Louder");
259 playlist_VolumeUp( p_playlist
, 1, NULL
);
263 msg_Dbg(p_intf
, "Quieter");
264 playlist_VolumeDown( p_playlist
, 1, NULL
);
267 case GESTURE(UP
,DOWN
,NONE
,NONE
):
268 case GESTURE(DOWN
,UP
,NONE
,NONE
):
269 msg_Dbg( p_intf
, "Mute sound" );
270 playlist_MuteToggle( p_playlist
);
273 case GESTURE(UP
,RIGHT
,NONE
,NONE
):
275 input_thread_t
*p_input
= playlist_CurrentInput( p_playlist
);
276 if( p_input
== NULL
)
282 var_Change( p_input
, "audio-es", VLC_VAR_GETCHOICES
,
283 &count
, &list
, (char ***)NULL
);
287 int i_audio_es
= var_GetInteger( p_input
, "audio-es" );
290 for( i
= 0; i
< count
; i
++ )
291 if( i_audio_es
== list
[i
].i_int
)
293 /* value of audio-es was not in choices list */
297 "invalid current audio track, selecting 0" );
300 else if( i
== count
- 1 )
304 var_SetInteger( p_input
, "audio-es", list
[i
].i_int
);
307 vlc_object_release( p_input
);
311 case GESTURE(DOWN
,RIGHT
,NONE
,NONE
):
313 input_thread_t
*p_input
= playlist_CurrentInput( p_playlist
);
314 if( p_input
== NULL
)
320 var_Change( p_input
, "spu-es", VLC_VAR_GETCHOICES
,
321 &count
, &list
, (char ***)NULL
);
325 int i_audio_es
= var_GetInteger( p_input
, "spu-es" );
328 for( i
= 0; i
< count
; i
++ )
329 if( i_audio_es
== list
[i
].i_int
)
331 /* value of audio-es was not in choices list */
335 "invalid current subtitle track, selecting 0" );
338 else if( i
== count
- 1 )
342 var_SetInteger( p_input
, "audio-es", list
[i
].i_int
);
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
->obj
.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
;
385 unsigned int pattern
= 0;
387 i_horizontal
/= p_sys
->i_threshold
;
388 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( p_sys
->i_num_gestures
> 0
416 && gesture( p_sys
->i_pattern
, p_sys
->i_num_gestures
- 1 )
419 p_sys
->i_pattern
|= pattern
<< ( p_sys
->i_num_gestures
* 4 );
420 p_sys
->i_num_gestures
++;
422 else if( p_sys
->i_num_gestures
== 0 )
424 p_sys
->i_pattern
= pattern
;
425 p_sys
->i_num_gestures
++;
430 vlc_mutex_unlock( &p_sys
->lock
);
435 static int ButtonEvent( vlc_object_t
*p_this
, char const *psz_var
,
436 vlc_value_t oldval
, vlc_value_t val
, void *p_data
)
438 intf_thread_t
*p_intf
= p_data
;
439 intf_sys_t
*p_sys
= p_intf
->p_sys
;
441 (void) psz_var
; (void) oldval
;
443 vlc_mutex_lock( &p_sys
->lock
);
444 if( val
.i_int
& p_sys
->i_button_mask
)
446 if( !p_sys
->b_button_pressed
)
448 p_sys
->b_button_pressed
= true;
449 var_GetCoords( p_this
, "mouse-moved",
450 &p_sys
->i_last_x
, &p_sys
->i_last_y
);
455 if( p_sys
->b_button_pressed
)
457 p_sys
->b_button_pressed
= false;
458 ProcessGesture( p_intf
);
461 vlc_mutex_unlock( &p_sys
->lock
);
466 static int InputEvent( vlc_object_t
*p_this
, char const *psz_var
,
467 vlc_value_t oldval
, vlc_value_t val
, void *p_data
)
469 input_thread_t
*p_input
= (input_thread_t
*)p_this
;
470 intf_thread_t
*p_intf
= p_data
;
471 intf_sys_t
*p_sys
= p_intf
->p_sys
;
473 (void) psz_var
; (void) oldval
;
477 case INPUT_EVENT_VOUT
:
478 /* intf-event is serialized against itself and is the sole user of
479 * p_sys->p_vout. So there is no need to acquire the lock currently. */
480 if( p_sys
->p_vout
!= NULL
)
481 { /* /!\ Beware of lock inversion with var_DelCallback() /!\ */
482 var_DelCallback( p_sys
->p_vout
, "mouse-moved", MovedEvent
,
484 var_DelCallback( p_sys
->p_vout
, "mouse-button-down", ButtonEvent
,
486 vlc_object_release( p_sys
->p_vout
);
489 p_sys
->p_vout
= input_GetVout( p_input
);
490 if( p_sys
->p_vout
!= NULL
)
492 var_AddCallback( p_sys
->p_vout
, "mouse-moved", MovedEvent
,
494 var_AddCallback( p_sys
->p_vout
, "mouse-button-down", ButtonEvent
,
502 static int PlaylistEvent( vlc_object_t
*p_this
, char const *psz_var
,
503 vlc_value_t oldval
, vlc_value_t val
, void *p_data
)
505 intf_thread_t
*p_intf
= p_data
;
506 intf_sys_t
*p_sys
= p_intf
->p_sys
;
507 input_thread_t
*p_input
= val
.p_address
;
509 (void) p_this
; (void) psz_var
;
511 if( p_sys
->p_input
!= NULL
)
513 assert( p_sys
->p_input
== oldval
.p_address
);
514 var_DelCallback( p_sys
->p_input
, "intf-event", InputEvent
, p_intf
);
517 p_sys
->p_input
= p_input
;
519 if( p_input
!= NULL
)
520 var_AddCallback( p_input
, "intf-event", InputEvent
, p_intf
);