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 vout_thread_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 #if defined (HAVE_MAEMO)
90 # define BUTTON_DEFAULT "left"
92 # define BUTTON_DEFAULT "right"
95 static const char *const button_list
[] = { "left", "middle", "right" };
96 static const char *const button_list_text
[] =
97 { N_("Left"), N_("Middle"), N_("Right") };
100 set_shortname( N_("Gestures"))
101 set_category( CAT_INTERFACE
)
102 set_subcategory( SUBCAT_INTERFACE_CONTROL
)
103 add_integer( "gestures-threshold", 30, NULL
,
104 THRESHOLD_TEXT
, THRESHOLD_LONGTEXT
, true )
105 add_string( "gestures-button", BUTTON_DEFAULT
, NULL
,
106 BUTTON_TEXT
, BUTTON_LONGTEXT
, false )
107 change_string_list( button_list
, button_list_text
, 0 )
108 set_description( N_("Mouse gestures control interface") )
110 set_capability( "interface", 0 )
111 set_callbacks( Open
, Close
)
114 /*****************************************************************************
115 * OpenIntf: initialize interface
116 *****************************************************************************/
117 int Open ( vlc_object_t
*p_this
)
119 intf_thread_t
*p_intf
= (intf_thread_t
*)p_this
;
121 /* Allocate instance and initialize some members */
122 intf_sys_t
*p_sys
= p_intf
->p_sys
= malloc( sizeof( intf_sys_t
) );
123 if( p_intf
->p_sys
== NULL
)
126 // Configure the module
127 p_intf
->pf_run
= RunIntf
;
129 vlc_mutex_init( &p_sys
->lock
);
130 p_sys
->p_vout
= NULL
;
131 p_sys
->b_got_gesture
= false;
132 p_sys
->b_button_pressed
= false;
133 p_sys
->i_threshold
= var_InheritInteger( p_intf
, "gestures-threshold" );
135 // Choose the tight button to use
136 char *psz_button
= var_InheritString( p_intf
, "gestures-button" );
137 if( psz_button
&& !strcmp( psz_button
, "left" ) )
138 p_sys
->i_button_mask
= 1;
139 else if( psz_button
&& !strcmp( psz_button
, "middle" ) )
140 p_sys
->i_button_mask
= 2;
141 else // psz_button == "right"
142 p_sys
->i_button_mask
= 4;
145 p_sys
->i_pattern
= 0;
146 p_sys
->i_num_gestures
= 0;
151 /*****************************************************************************
152 * gesture: return a subpattern within a pattern
153 *****************************************************************************/
154 static int gesture( int i_pattern
, int i_num
)
156 return ( i_pattern
>> ( i_num
* 4 ) ) & 0xF;
159 /*****************************************************************************
160 * CloseIntf: destroy dummy interface
161 *****************************************************************************/
162 void Close ( vlc_object_t
*p_this
)
164 intf_thread_t
*p_intf
= (intf_thread_t
*)p_this
;
166 // Destroy the callbacks
167 if( p_intf
->p_sys
->p_vout
)
169 var_DelCallback( p_intf
->p_sys
->p_vout
, "mouse-moved",
170 MouseEvent
, p_intf
);
171 var_DelCallback( p_intf
->p_sys
->p_vout
, "mouse-button-down",
172 MouseEvent
, p_intf
);
173 vlc_object_release( p_intf
->p_sys
->p_vout
);
176 /* Destroy structure */
177 vlc_mutex_destroy( &p_intf
->p_sys
->lock
);
178 free( p_intf
->p_sys
);
182 /*****************************************************************************
184 *****************************************************************************/
185 static void RunIntf( intf_thread_t
*p_intf
)
187 intf_sys_t
*p_sys
= p_intf
->p_sys
;
188 int canc
= vlc_savecancel();
189 input_thread_t
*p_input
;
192 while( vlc_object_alive( p_intf
) )
194 vlc_mutex_lock( &p_sys
->lock
);
199 if( p_sys
->b_got_gesture
)
203 /* If you modify this, please try to follow this convention:
204 Start with LEFT, RIGHT for playback related commands
205 and UP, DOWN, for other commands */
206 playlist_t
* p_playlist
= pl_Get( p_intf
);
207 switch( p_sys
->i_pattern
)
210 msg_Dbg( p_intf
, "Go backward in the movie!" );
211 p_input
= playlist_CurrentInput( p_playlist
);
214 i_interval
= var_InheritInteger( p_intf
, "short-jump-size" );
215 if ( i_interval
> 0 )
217 mtime_t i_time
= ( (mtime_t
)( -i_interval
) * 1000000L);
218 var_SetTime( p_input
, "time-offset", i_time
);
220 vlc_object_release( p_input
);
225 msg_Dbg( p_intf
, "Go forward in the movie!" );
226 p_input
= playlist_CurrentInput( p_playlist
);
229 i_interval
= var_InheritInteger( p_intf
, "short-jump-size" );
230 if ( i_interval
> 0 )
232 mtime_t i_time
= ( (mtime_t
)( i_interval
) * 1000000L);
233 var_SetTime( p_input
, "time-offset", i_time
);
235 vlc_object_release( p_input
);
239 case GESTURE(LEFT
,UP
,NONE
,NONE
):
240 msg_Dbg( p_intf
, "Going slower." );
241 p_input
= playlist_CurrentInput( p_playlist
);
244 var_TriggerCallback( p_input
, "rate-slower" );
245 vlc_object_release( p_input
);
249 case GESTURE(RIGHT
,UP
,NONE
,NONE
):
250 msg_Dbg( p_intf
, "Going faster." );
251 p_input
= playlist_CurrentInput( p_playlist
);
254 var_TriggerCallback( p_input
, "rate-faster" );
255 vlc_object_release( p_input
);
259 case GESTURE(LEFT
,RIGHT
,NONE
,NONE
):
260 case GESTURE(RIGHT
,LEFT
,NONE
,NONE
):
261 msg_Dbg( p_intf
, "Play/Pause" );
262 p_input
= playlist_CurrentInput( p_playlist
);
266 int i_state
= var_GetInteger( p_input
, "state" );
267 var_SetInteger( p_input
, "state", ( i_state
!= PLAYING_S
)
268 ? PLAYING_S
: PAUSE_S
);
269 vlc_object_release( p_input
);
273 case GESTURE(LEFT
,DOWN
,NONE
,NONE
):
274 playlist_Prev( p_playlist
);
277 case GESTURE(RIGHT
,DOWN
,NONE
,NONE
):
278 playlist_Next( p_playlist
);
282 msg_Dbg(p_intf
, "Louder");
283 aout_VolumeUp( p_playlist
, 1, NULL
);
287 msg_Dbg(p_intf
, "Quieter");
288 aout_VolumeDown( p_playlist
, 1, NULL
);
291 case GESTURE(UP
,DOWN
,NONE
,NONE
):
292 case GESTURE(DOWN
,UP
,NONE
,NONE
):
293 msg_Dbg( p_intf
, "Mute sound" );
294 aout_ToggleMute( p_playlist
, NULL
);
297 case GESTURE(UP
,RIGHT
,NONE
,NONE
):
299 vlc_value_t list
, list2
;
300 int i_count
, i
, i_audio_es
;
302 p_input
= playlist_CurrentInput( p_playlist
);
306 i_audio_es
= var_GetInteger( p_input
, "audio-es" );
307 var_Change( p_input
, "audio-es", VLC_VAR_GETCHOICES
,
309 i_count
= list
.p_list
->i_count
;
312 var_FreeList( &list
, &list2
);
313 vlc_object_release( p_input
);
316 for( i
= 0; i
< i_count
; i
++ )
318 if( i_audio_es
== list
.p_list
->p_values
[i
].i_int
)
321 /* value of audio-es was not in choices list */
325 "invalid current audio track, selecting 0" );
328 else if( i
== i_count
- 1 )
332 var_SetInteger( p_input
, "audio-es", list
.p_list
->p_values
[i
].i_int
);
333 var_FreeList( &list
, &list2
);
334 vlc_object_release( p_input
);
337 case GESTURE(DOWN
,RIGHT
,NONE
,NONE
):
339 vlc_value_t list
, list2
;
340 int i_count
, i
, i_spu_es
;
342 p_input
= playlist_CurrentInput( p_playlist
);
346 i_spu_es
= var_GetInteger( p_input
, "spu-es" );
348 var_Change( p_input
, "spu-es", VLC_VAR_GETCHOICES
,
350 i_count
= list
.p_list
->i_count
;
353 vlc_object_release( p_input
);
354 var_FreeList( &list
, &list2
);
357 for( i
= 0; i
< i_count
; i
++ )
359 if( i_spu_es
== list
.p_list
->p_values
[i
].i_int
)
364 /* value of spu-es was not in choices list */
368 "invalid current subtitle track, selecting 0" );
371 else if( i
== i_count
- 1 )
375 var_SetInteger( p_input
, "spu-es", list
.p_list
->p_values
[i
].i_int
);
376 var_FreeList( &list
, &list2
);
377 vlc_object_release( p_input
);
381 case GESTURE(UP
,LEFT
,NONE
,NONE
):
383 bool val
= var_ToggleBool( pl_Get( p_intf
), "fullscreen" );
385 var_SetBool( p_sys
->p_vout
, "fullscreen", val
);
389 case GESTURE(DOWN
,LEFT
,NONE
,NONE
):
390 /* FIXME: Should close the vout!"*/
391 libvlc_Quit( p_intf
->p_libvlc
);
393 case GESTURE(DOWN
,LEFT
,UP
,RIGHT
):
394 case GESTURE(UP
,RIGHT
,DOWN
,LEFT
):
395 msg_Dbg( p_intf
, "a square was drawn!" );
400 p_sys
->i_num_gestures
= 0;
401 p_sys
->i_pattern
= 0;
402 p_sys
->b_got_gesture
= false;
408 if( p_sys
->p_vout
&& !vlc_object_alive( p_sys
->p_vout
) )
410 var_DelCallback( p_sys
->p_vout
, "mouse-moved",
411 MouseEvent
, p_intf
);
412 var_DelCallback( p_sys
->p_vout
, "mouse-button-down",
413 MouseEvent
, p_intf
);
414 vlc_object_release( p_sys
->p_vout
);
415 p_sys
->p_vout
= NULL
;
418 if( p_sys
->p_vout
== NULL
)
420 p_input
= playlist_CurrentInput( pl_Get( p_intf
) );
423 p_sys
->p_vout
= input_GetVout( p_input
);
424 vlc_object_release( p_input
);
428 var_AddCallback( p_sys
->p_vout
, "mouse-moved",
429 MouseEvent
, p_intf
);
430 var_AddCallback( p_sys
->p_vout
, "mouse-button-down",
431 MouseEvent
, p_intf
);
435 vlc_mutex_unlock( &p_sys
->lock
);
438 msleep( INTF_IDLE_SLEEP
);
441 vlc_restorecancel( canc
);
444 /*****************************************************************************
445 * MouseEvent: callback for mouse events
446 *****************************************************************************/
447 static int MouseEvent( vlc_object_t
*p_this
, char const *psz_var
,
448 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
450 VLC_UNUSED(p_this
); VLC_UNUSED(oldval
);
453 signed int i_horizontal
, i_vertical
;
454 intf_thread_t
*p_intf
= (intf_thread_t
*)p_data
;
455 intf_sys_t
*p_sys
= p_intf
->p_sys
;
457 vlc_mutex_lock( &p_sys
->lock
);
459 /* don't process new gestures before the last events are processed */
460 if( p_sys
->b_got_gesture
)
462 vlc_mutex_unlock( &p_sys
->lock
);
466 if( !strcmp( psz_var
, "mouse-moved" ) && p_sys
->b_button_pressed
)
468 p_sys
->i_mouse_x
= newval
.coords
.x
;
469 p_sys
->i_mouse_y
= newval
.coords
.y
;
470 i_horizontal
= p_sys
->i_mouse_x
- p_sys
->i_last_x
;
471 i_horizontal
= i_horizontal
/ p_sys
->i_threshold
;
472 i_vertical
= p_sys
->i_mouse_y
- p_sys
->i_last_y
;
473 i_vertical
= i_vertical
/ p_sys
->i_threshold
;
475 if( i_horizontal
< 0 )
477 msg_Dbg( p_intf
, "left gesture (%d)", i_horizontal
);
480 else if( i_horizontal
> 0 )
482 msg_Dbg( p_intf
, "right gesture (%d)", i_horizontal
);
487 msg_Dbg( p_intf
, "up gesture (%d)", i_vertical
);
490 else if( i_vertical
> 0 )
492 msg_Dbg( p_intf
, "down gesture (%d)", i_vertical
);
497 p_sys
->i_last_y
= p_sys
->i_mouse_y
;
498 p_sys
->i_last_x
= p_sys
->i_mouse_x
;
499 if( gesture( p_sys
->i_pattern
, p_sys
->i_num_gestures
- 1 )
502 p_sys
->i_pattern
|= pattern
<< ( p_sys
->i_num_gestures
* 4 );
503 p_sys
->i_num_gestures
++;
508 else if( !strcmp( psz_var
, "mouse-button-down" ) )
510 if( (newval
.i_int
& p_sys
->i_button_mask
) && !p_sys
->b_button_pressed
)
512 p_sys
->b_button_pressed
= true;
513 var_GetCoords( p_sys
->p_vout
, "mouse-moved",
514 &p_sys
->i_last_x
, &p_sys
->i_last_y
);
516 else if( !( newval
.i_int
& p_sys
->i_button_mask
) && p_sys
->b_button_pressed
)
518 p_sys
->b_button_pressed
= false;
519 p_sys
->b_got_gesture
= true;
523 vlc_mutex_unlock( &p_sys
->lock
);