1 /*****************************************************************************
2 * gestures.c: control vlc with mouse gestures
3 *****************************************************************************
4 * Copyright (C) 2004 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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc_interface.h>
33 #include <vlc_playlist.h>
39 /*****************************************************************************
40 * intf_sys_t: description and status of interface
41 *****************************************************************************/
44 vlc_object_t * p_vout;
45 input_thread_t * p_input;
46 vlc_bool_t b_got_gesture;
47 vlc_bool_t b_button_pressed;
48 int i_mouse_x, i_mouse_y;
49 int i_last_x, i_last_y;
50 unsigned int i_pattern;
56 /*****************************************************************************
58 *****************************************************************************/
64 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
66 int E_(Open) ( vlc_object_t * );
67 void E_(Close) ( vlc_object_t * );
68 static int InitThread ( intf_thread_t *p_intf );
69 static int MouseEvent ( vlc_object_t *, char const *,
70 vlc_value_t, vlc_value_t, void * );
72 /* Exported functions */
73 static void RunIntf ( intf_thread_t *p_intf );
75 /*****************************************************************************
77 *****************************************************************************/
78 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
79 #define THRESHOLD_LONGTEXT N_( \
80 "Amount of movement required for a mouse gesture to be recorded." )
82 #define BUTTON_TEXT N_( "Trigger button" )
83 #define BUTTON_LONGTEXT N_( \
84 "Trigger button for mouse gestures." )
86 static const char *button_list[] = { "left", "middle", "right" };
87 static const char *button_list_text[] =
88 { N_("Left"), N_("Middle"), N_("Right") };
91 set_shortname( _("Gestures"));
92 set_category( CAT_INTERFACE );
93 set_subcategory( SUBCAT_INTERFACE_CONTROL );
94 add_integer( "gestures-threshold", 30, NULL,
95 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
96 add_string( "gestures-button", "right", NULL,
97 BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
98 change_string_list( button_list, button_list_text, 0 );
99 set_description( _("Mouse gestures control interface") );
101 set_capability( "interface", 0 );
102 set_callbacks( E_(Open), E_(Close) );
105 /*****************************************************************************
106 * OpenIntf: initialize interface
107 *****************************************************************************/
108 int E_(Open) ( vlc_object_t *p_this )
110 intf_thread_t *p_intf = (intf_thread_t *)p_this;
112 /* Allocate instance and initialize some members */
113 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
114 if( p_intf->p_sys == NULL )
119 p_intf->pf_run = RunIntf;
124 /*****************************************************************************
125 * gesture: return a subpattern within a pattern
126 *****************************************************************************/
127 static int gesture( int i_pattern, int i_num )
129 return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
132 /*****************************************************************************
133 * CloseIntf: destroy dummy interface
134 *****************************************************************************/
135 void E_(Close) ( vlc_object_t *p_this )
137 intf_thread_t *p_intf = (intf_thread_t *)p_this;
139 /* Destroy structure */
140 free( p_intf->p_sys );
144 /*****************************************************************************
146 *****************************************************************************/
147 static void RunIntf( intf_thread_t *p_intf )
149 playlist_t * p_playlist = NULL;
150 p_intf->p_sys->p_vout = NULL;
152 if( InitThread( p_intf ) < 0 )
154 msg_Err( p_intf, "can't initialize interface thread" );
157 msg_Dbg( p_intf, "interface thread initialized" );
160 while( !intf_ShouldDie( p_intf ) )
162 vlc_mutex_lock( &p_intf->change_lock );
167 if( p_intf->p_sys->b_got_gesture )
170 switch( p_intf->p_sys->i_pattern )
173 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
175 if( p_playlist == NULL )
180 playlist_Prev( p_playlist );
181 vlc_object_release( p_playlist );
184 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
186 if( p_playlist == NULL )
191 playlist_Next( p_playlist );
192 vlc_object_release( p_playlist );
194 case GESTURE(UP,RIGHT,NONE,NONE):
195 if (p_intf->p_sys->p_vout )
197 ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
198 VOUT_FULLSCREEN_CHANGE;
201 case GESTURE(DOWN,RIGHT,NONE,NONE):
202 /* FIXME: Should close the vout!"*/
203 p_intf->p_libvlc->b_die = VLC_TRUE;
205 case GESTURE(DOWN,LEFT,UP,RIGHT):
206 msg_Dbg(p_intf, "a square was drawn!" );
211 p_intf->p_sys->i_num_gestures = 0;
212 p_intf->p_sys->i_pattern = 0;
213 p_intf->p_sys->b_got_gesture = VLC_FALSE;
217 vlc_mutex_unlock( &p_intf->change_lock );
222 if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
224 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
225 MouseEvent, p_intf );
226 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
227 MouseEvent, p_intf );
228 vlc_object_release( p_intf->p_sys->p_vout );
229 p_intf->p_sys->p_vout = NULL;
232 if( p_intf->p_sys->p_vout == NULL )
234 p_intf->p_sys->p_vout = vlc_object_find( p_intf,
235 VLC_OBJECT_VOUT, FIND_ANYWHERE );
236 if( p_intf->p_sys->p_vout )
238 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
239 MouseEvent, p_intf );
240 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
241 MouseEvent, p_intf );
246 msleep( INTF_IDLE_SLEEP );
249 if( p_intf->p_sys->p_vout )
251 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
252 MouseEvent, p_intf );
253 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
254 MouseEvent, p_intf );
255 vlc_object_release( p_intf->p_sys->p_vout );
259 /*****************************************************************************
261 *****************************************************************************/
262 static int InitThread( intf_thread_t * p_intf )
265 /* we might need some locking here */
266 if( !intf_ShouldDie( p_intf ) )
268 input_thread_t * p_input;
270 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
272 vlc_mutex_lock( &p_intf->change_lock );
274 p_intf->p_sys->p_input = p_input;
275 p_intf->p_sys->b_got_gesture = VLC_FALSE;
276 p_intf->p_sys->b_button_pressed = VLC_FALSE;
277 p_intf->p_sys->i_threshold =
278 config_GetInt( p_intf, "gestures-threshold" );
279 psz_button = config_GetPsz( p_intf, "gestures-button" );
280 if ( !strcmp( psz_button, "left" ) )
282 p_intf->p_sys->i_button_mask = 1;
284 else if ( !strcmp( psz_button, "middle" ) )
286 p_intf->p_sys->i_button_mask = 2;
288 else if ( !strcmp( psz_button, "right" ) )
290 p_intf->p_sys->i_button_mask = 4;
293 p_intf->p_sys->i_pattern = 0;
294 p_intf->p_sys->i_num_gestures = 0;
295 vlc_mutex_unlock( &p_intf->change_lock );
305 /*****************************************************************************
306 * MouseEvent: callback for mouse events
307 *****************************************************************************/
308 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
309 vlc_value_t oldval, vlc_value_t newval, void *p_data )
314 signed int i_horizontal, i_vertical;
315 intf_thread_t *p_intf = (intf_thread_t *)p_data;
317 /* don't process new gestures before the last events are processed */
318 if( p_intf->p_sys->b_got_gesture )
323 vlc_mutex_lock( &p_intf->change_lock );
324 if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
326 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
327 p_intf->p_sys->i_mouse_x = val.i_int;
328 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
329 p_intf->p_sys->i_mouse_y = val.i_int;
330 i_horizontal = p_intf->p_sys->i_mouse_x -
331 p_intf->p_sys->i_last_x;
332 i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
333 i_vertical = p_intf->p_sys->i_mouse_y
334 - p_intf->p_sys->i_last_y;
335 i_vertical = i_vertical / p_intf->p_sys->i_threshold;
337 if( i_horizontal < 0 )
339 msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
342 else if( i_horizontal > 0 )
344 msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
349 msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
352 else if( i_vertical > 0 )
354 msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
359 p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
360 p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
361 if( gesture( p_intf->p_sys->i_pattern,
362 p_intf->p_sys->i_num_gestures - 1 ) != pattern )
364 p_intf->p_sys->i_pattern |=
365 pattern << ( p_intf->p_sys->i_num_gestures * 4 );
366 p_intf->p_sys->i_num_gestures++;
371 if( !strcmp( psz_var, "mouse-button-down" )
372 && newval.i_int & p_intf->p_sys->i_button_mask
373 && !p_intf->p_sys->b_button_pressed )
375 p_intf->p_sys->b_button_pressed = VLC_TRUE;
376 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
377 p_intf->p_sys->i_last_x = val.i_int;
378 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
379 p_intf->p_sys->i_last_y = val.i_int;
381 if( !strcmp( psz_var, "mouse-button-down" )
382 && !( newval.i_int & p_intf->p_sys->i_button_mask )
383 && p_intf->p_sys->b_button_pressed )
385 p_intf->p_sys->b_button_pressed = VLC_FALSE;
386 p_intf->p_sys->b_got_gesture = VLC_TRUE;
389 vlc_mutex_unlock( &p_intf->change_lock );