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 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_interface.h>
37 #include <vlc_playlist.h>
38 #include <vlc_input.h>
41 /*****************************************************************************
42 * intf_sys_t: description and status of interface
43 *****************************************************************************/
47 input_thread_t
*p_input
;
48 vout_thread_t
*p_vout
;
49 bool b_button_pressed
;
50 int i_last_x
, i_last_y
;
51 unsigned int i_pattern
;
52 unsigned int i_num_gestures
;
57 /*****************************************************************************
59 *****************************************************************************/
65 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
67 static int Open ( vlc_object_t
* );
68 static void Close ( vlc_object_t
* );
70 /*****************************************************************************
72 *****************************************************************************/
73 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
74 #define THRESHOLD_LONGTEXT N_( \
75 "Amount of movement required for a mouse gesture to be recorded." )
77 #define BUTTON_TEXT N_( "Trigger button" )
78 #define BUTTON_LONGTEXT N_( \
79 "Trigger button for mouse gestures." )
81 #define BUTTON_DEFAULT "left"
83 static const char *const button_list
[] = { "left", "middle", "right" };
84 static const char *const button_list_text
[] =
85 { N_("Left"), N_("Middle"), N_("Right") };
88 set_shortname( N_("Gestures"))
89 set_category( CAT_INTERFACE
)
90 set_subcategory( SUBCAT_INTERFACE_CONTROL
)
91 add_integer( "gestures-threshold", 30,
92 THRESHOLD_TEXT
, THRESHOLD_LONGTEXT
, true )
93 add_string( "gestures-button", BUTTON_DEFAULT
,
94 BUTTON_TEXT
, BUTTON_LONGTEXT
, false )
95 change_string_list( button_list
, button_list_text
)
96 set_description( N_("Mouse gestures control interface") )
98 set_capability( "interface", 0 )
99 set_callbacks( Open
, Close
)
102 static int PlaylistEvent( vlc_object_t
*, char const *,
103 vlc_value_t
, vlc_value_t
, void * );
104 static int InputEvent( vlc_object_t
*, char const *,
105 vlc_value_t
, vlc_value_t
, void * );
106 static int MovedEvent( vlc_object_t
*, char const *,
107 vlc_value_t
, vlc_value_t
, void * );
108 static int ButtonEvent( vlc_object_t
*, char const *,
109 vlc_value_t
, vlc_value_t
, void * );
111 /*****************************************************************************
112 * OpenIntf: initialize interface
113 *****************************************************************************/
114 static int Open ( vlc_object_t
*p_this
)
116 intf_thread_t
*p_intf
= (intf_thread_t
*)p_this
;
118 /* Allocate instance and initialize some members */
119 intf_sys_t
*p_sys
= p_intf
->p_sys
= malloc( sizeof( intf_sys_t
) );
120 if( unlikely(p_sys
== NULL
) )
123 // Configure the module
124 vlc_mutex_init( &p_sys
->lock
);
125 p_sys
->p_input
= NULL
;
126 p_sys
->p_vout
= NULL
;
127 p_sys
->b_button_pressed
= false;
128 p_sys
->i_threshold
= var_InheritInteger( p_intf
, "gestures-threshold" );
130 // Choose the tight button to use
131 char *psz_button
= var_InheritString( p_intf
, "gestures-button" );
132 if( psz_button
&& !strcmp( psz_button
, "left" ) )
133 p_sys
->i_button_mask
= 1;
134 else if( psz_button
&& !strcmp( psz_button
, "middle" ) )
135 p_sys
->i_button_mask
= 2;
136 else // psz_button == "right"
137 p_sys
->i_button_mask
= 4;
140 p_sys
->i_pattern
= 0;
141 p_sys
->i_num_gestures
= 0;
143 var_AddCallback( pl_Get(p_intf
), "input-current", PlaylistEvent
, p_intf
);
148 /*****************************************************************************
149 * gesture: return a subpattern within a pattern
150 *****************************************************************************/
151 static inline unsigned gesture( unsigned i_pattern
, unsigned i_num
)
153 return ( i_pattern
>> ( i_num
* 4 ) ) & 0xF;
156 /*****************************************************************************
157 * CloseIntf: destroy dummy interface
158 *****************************************************************************/
159 static void Close ( vlc_object_t
*p_this
)
161 intf_thread_t
*p_intf
= (intf_thread_t
*)p_this
;
162 intf_sys_t
*p_sys
= p_intf
->p_sys
;
164 /* Destroy the callbacks (the order matters!) */
165 var_DelCallback( pl_Get(p_intf
), "input-current", PlaylistEvent
, p_intf
);
167 if( p_sys
->p_input
!= NULL
)
168 var_DelCallback( p_sys
->p_input
, "intf-event", InputEvent
, p_intf
);
172 var_DelCallback( p_sys
->p_vout
, "mouse-moved", MovedEvent
, p_intf
);
173 var_DelCallback( p_sys
->p_vout
, "mouse-button-down",
174 ButtonEvent
, p_intf
);
175 vlc_object_release( p_sys
->p_vout
);
178 /* Destroy structure */
179 vlc_mutex_destroy( &p_sys
->lock
);
183 static void ProcessGesture( intf_thread_t
*p_intf
)
185 intf_sys_t
*p_sys
= p_intf
->p_sys
;
186 playlist_t
*p_playlist
= pl_Get( p_intf
);
189 /* If you modify this, please try to follow this convention:
190 Start with LEFT, RIGHT for playback related commands
191 and UP, DOWN, for other commands */
192 switch( p_sys
->i_pattern
)
196 msg_Dbg( p_intf
, "Go backward in the movie!" );
198 input_thread_t
*p_input
= playlist_CurrentInput( p_playlist
);
199 if( p_input
== NULL
)
202 int it
= var_InheritInteger( p_intf
, "short-jump-size" );
204 var_SetInteger( p_input
, "time-offset", -CLOCK_FREQ
* it
);
205 vlc_object_release( p_input
);
211 msg_Dbg( p_intf
, "Go forward in the movie!" );
213 input_thread_t
*p_input
= playlist_CurrentInput( p_playlist
);
214 if( p_input
== NULL
)
217 int it
= var_InheritInteger( p_intf
, "short-jump-size" );
219 var_SetInteger( p_input
, "time-offset", CLOCK_FREQ
* it
);
220 vlc_object_release( p_input
);
224 case GESTURE(LEFT
,UP
,NONE
,NONE
):
225 msg_Dbg( p_intf
, "Going slower." );
226 var_TriggerCallback( p_playlist
, "rate-slower" );
229 case GESTURE(RIGHT
,UP
,NONE
,NONE
):
230 msg_Dbg( p_intf
, "Going faster." );
231 var_TriggerCallback( p_playlist
, "rate-faster" );
234 case GESTURE(LEFT
,RIGHT
,NONE
,NONE
):
235 case GESTURE(RIGHT
,LEFT
,NONE
,NONE
):
237 msg_Dbg( p_intf
, "Play/Pause" );
239 input_thread_t
*p_input
= playlist_CurrentInput( p_playlist
);
240 if( p_input
== NULL
)
243 int i_state
= var_GetInteger( p_input
, "state" );
244 i_state
= (i_state
== PLAYING_S
) ? PAUSE_S
: PLAYING_S
;
245 var_SetInteger( p_input
, "state", i_state
);
246 vlc_object_release( p_input
);
250 case GESTURE(LEFT
,DOWN
,NONE
,NONE
):
251 playlist_Prev( p_playlist
);
254 case GESTURE(RIGHT
,DOWN
,NONE
,NONE
):
255 playlist_Next( p_playlist
);
259 msg_Dbg(p_intf
, "Louder");
260 playlist_VolumeUp( p_playlist
, 1, NULL
);
264 msg_Dbg(p_intf
, "Quieter");
265 playlist_VolumeDown( p_playlist
, 1, NULL
);
268 case GESTURE(UP
,DOWN
,NONE
,NONE
):
269 case GESTURE(DOWN
,UP
,NONE
,NONE
):
270 msg_Dbg( p_intf
, "Mute sound" );
271 playlist_MuteToggle( p_playlist
);
274 case GESTURE(UP
,RIGHT
,NONE
,NONE
):
276 input_thread_t
*p_input
= playlist_CurrentInput( p_playlist
);
277 if( p_input
== NULL
)
280 vlc_value_t list
, list2
;
281 var_Change( p_input
, "audio-es", VLC_VAR_GETCHOICES
,
284 if( list
.p_list
->i_count
> 1 )
286 int i_audio_es
= var_GetInteger( p_input
, "audio-es" );
289 for( i
= 0; i
< list
.p_list
->i_count
; i
++ )
290 if( i_audio_es
== list
.p_list
->p_values
[i
].i_int
)
292 /* value of audio-es was not in choices list */
293 if( i
== list
.p_list
->i_count
)
296 "invalid current audio track, selecting 0" );
299 else if( i
== list
.p_list
->i_count
- 1 )
303 var_SetInteger( p_input
, "audio-es",
304 list
.p_list
->p_values
[i
].i_int
);
306 var_FreeList( &list
, &list2
);
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
)
317 vlc_value_t list
, list2
;
318 var_Change( p_input
, "spu-es", VLC_VAR_GETCHOICES
,
321 if( list
.p_list
->i_count
> 1 )
323 int i_audio_es
= var_GetInteger( p_input
, "spu-es" );
326 for( i
= 0; i
< list
.p_list
->i_count
; i
++ )
327 if( i_audio_es
== list
.p_list
->p_values
[i
].i_int
)
329 /* value of audio-es was not in choices list */
330 if( i
== list
.p_list
->i_count
)
333 "invalid current subtitle track, selecting 0" );
336 else if( i
== list
.p_list
->i_count
- 1 )
340 var_SetInteger( p_input
, "audio-es",
341 list
.p_list
->p_values
[i
].i_int
);
343 var_FreeList( &list
, &list2
);
344 vlc_object_release( p_input
);
348 case GESTURE(UP
,LEFT
,NONE
,NONE
):
350 bool val
= var_ToggleBool( pl_Get( p_intf
), "fullscreen" );
352 var_SetBool( p_sys
->p_vout
, "fullscreen", val
);
356 case GESTURE(DOWN
,LEFT
,NONE
,NONE
):
357 /* FIXME: Should close the vout!"*/
358 libvlc_Quit( p_intf
->obj
.libvlc
);
361 case GESTURE(DOWN
,LEFT
,UP
,RIGHT
):
362 case GESTURE(UP
,RIGHT
,DOWN
,LEFT
):
363 msg_Dbg( p_intf
, "a square was drawn!" );
367 p_sys
->i_num_gestures
= 0;
368 p_sys
->i_pattern
= 0;
371 static int MovedEvent( vlc_object_t
*p_this
, char const *psz_var
,
372 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
374 intf_thread_t
*p_intf
= (intf_thread_t
*)p_data
;
375 intf_sys_t
*p_sys
= p_intf
->p_sys
;
377 (void) p_this
; (void) psz_var
; (void) oldval
;
379 vlc_mutex_lock( &p_sys
->lock
);
380 if( p_sys
->b_button_pressed
)
382 int i_horizontal
= newval
.coords
.x
- p_sys
->i_last_x
;
383 int i_vertical
= newval
.coords
.y
- p_sys
->i_last_y
;
384 unsigned int pattern
= 0;
386 i_horizontal
/= p_sys
->i_threshold
;
387 i_vertical
/= p_sys
->i_threshold
;
389 if( i_horizontal
< 0 )
391 msg_Dbg( p_intf
, "left gesture (%d)", i_horizontal
);
394 else if( i_horizontal
> 0 )
396 msg_Dbg( p_intf
, "right gesture (%d)", i_horizontal
);
401 msg_Dbg( p_intf
, "up gesture (%d)", i_vertical
);
404 else if( i_vertical
> 0 )
406 msg_Dbg( p_intf
, "down gesture (%d)", i_vertical
);
412 p_sys
->i_last_x
= newval
.coords
.x
;
413 p_sys
->i_last_y
= newval
.coords
.y
;
414 if( p_sys
->i_num_gestures
> 0
415 && 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
++;
421 else if( p_sys
->i_num_gestures
== 0 )
423 p_sys
->i_pattern
= pattern
;
424 p_sys
->i_num_gestures
++;
429 vlc_mutex_unlock( &p_sys
->lock
);
434 static int ButtonEvent( vlc_object_t
*p_this
, char const *psz_var
,
435 vlc_value_t oldval
, vlc_value_t val
, void *p_data
)
437 intf_thread_t
*p_intf
= p_data
;
438 intf_sys_t
*p_sys
= p_intf
->p_sys
;
440 (void) psz_var
; (void) oldval
;
442 vlc_mutex_lock( &p_sys
->lock
);
443 if( val
.i_int
& p_sys
->i_button_mask
)
445 if( !p_sys
->b_button_pressed
)
447 p_sys
->b_button_pressed
= true;
448 var_GetCoords( p_this
, "mouse-moved",
449 &p_sys
->i_last_x
, &p_sys
->i_last_y
);
454 if( p_sys
->b_button_pressed
)
456 p_sys
->b_button_pressed
= false;
457 ProcessGesture( p_intf
);
460 vlc_mutex_unlock( &p_sys
->lock
);
465 static int InputEvent( vlc_object_t
*p_this
, char const *psz_var
,
466 vlc_value_t oldval
, vlc_value_t val
, void *p_data
)
468 input_thread_t
*p_input
= (input_thread_t
*)p_this
;
469 intf_thread_t
*p_intf
= p_data
;
470 intf_sys_t
*p_sys
= p_intf
->p_sys
;
472 (void) psz_var
; (void) oldval
;
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
;
510 if( p_sys
->p_input
!= NULL
)
512 assert( p_sys
->p_input
== oldval
.p_address
);
513 var_DelCallback( p_sys
->p_input
, "intf-event", InputEvent
, p_intf
);
516 p_sys
->p_input
= p_input
;
518 if( p_input
!= NULL
)
519 var_AddCallback( p_input
, "intf-event", InputEvent
, p_intf
);