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 char *button_list[] = { "left", "middle", "right" };
87 static char *button_list_text[] = { N_("Left"), N_("Middle"), N_("Right") };
90 set_shortname( _("Gestures"));
91 set_category( CAT_INTERFACE );
92 set_subcategory( SUBCAT_INTERFACE_CONTROL );
93 add_integer( "gestures-threshold", 30, NULL,
94 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
95 add_string( "gestures-button", "right", NULL,
96 BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
97 change_string_list( button_list, button_list_text, 0 );
98 set_description( _("Mouse gestures control interface") );
100 set_capability( "interface", 0 );
101 set_callbacks( E_(Open), E_(Close) );
104 /*****************************************************************************
105 * OpenIntf: initialize interface
106 *****************************************************************************/
107 int E_(Open) ( vlc_object_t *p_this )
109 intf_thread_t *p_intf = (intf_thread_t *)p_this;
111 /* Allocate instance and initialize some members */
112 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
113 if( p_intf->p_sys == NULL )
118 p_intf->pf_run = RunIntf;
123 /*****************************************************************************
124 * gesture: return a subpattern within a pattern
125 *****************************************************************************/
126 static int gesture( int i_pattern, int i_num )
128 return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
131 /*****************************************************************************
132 * CloseIntf: destroy dummy interface
133 *****************************************************************************/
134 void E_(Close) ( vlc_object_t *p_this )
136 intf_thread_t *p_intf = (intf_thread_t *)p_this;
138 /* Destroy structure */
139 free( p_intf->p_sys );
143 /*****************************************************************************
145 *****************************************************************************/
146 static void RunIntf( intf_thread_t *p_intf )
148 playlist_t * p_playlist = NULL;
149 p_intf->p_sys->p_vout = NULL;
151 if( InitThread( p_intf ) < 0 )
153 msg_Err( p_intf, "can't initialize interface thread" );
156 msg_Dbg( p_intf, "interface thread initialized" );
159 while( !intf_ShouldDie( p_intf ) )
161 vlc_mutex_lock( &p_intf->change_lock );
166 if( p_intf->p_sys->b_got_gesture )
169 switch( p_intf->p_sys->i_pattern )
172 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
174 if( p_playlist == NULL )
179 playlist_Prev( p_playlist );
180 vlc_object_release( p_playlist );
183 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
185 if( p_playlist == NULL )
190 playlist_Next( p_playlist );
191 vlc_object_release( p_playlist );
193 case GESTURE(UP,RIGHT,NONE,NONE):
194 if (p_intf->p_sys->p_vout )
196 ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
197 VOUT_FULLSCREEN_CHANGE;
200 case GESTURE(DOWN,RIGHT,NONE,NONE):
201 /* FIXME: Should close the vout!"*/
202 p_intf->p_libvlc->b_die = VLC_TRUE;
204 case GESTURE(DOWN,LEFT,UP,RIGHT):
205 msg_Dbg(p_intf, "a square was drawn!" );
210 p_intf->p_sys->i_num_gestures = 0;
211 p_intf->p_sys->i_pattern = 0;
212 p_intf->p_sys->b_got_gesture = VLC_FALSE;
216 vlc_mutex_unlock( &p_intf->change_lock );
221 if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
223 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
224 MouseEvent, p_intf );
225 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
226 MouseEvent, p_intf );
227 vlc_object_release( p_intf->p_sys->p_vout );
228 p_intf->p_sys->p_vout = NULL;
231 if( p_intf->p_sys->p_vout == NULL )
233 p_intf->p_sys->p_vout = vlc_object_find( p_intf,
234 VLC_OBJECT_VOUT, FIND_ANYWHERE );
235 if( p_intf->p_sys->p_vout )
237 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
238 MouseEvent, p_intf );
239 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
240 MouseEvent, p_intf );
245 msleep( INTF_IDLE_SLEEP );
248 if( p_intf->p_sys->p_vout )
250 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
251 MouseEvent, p_intf );
252 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
253 MouseEvent, p_intf );
254 vlc_object_release( p_intf->p_sys->p_vout );
258 /*****************************************************************************
260 *****************************************************************************/
261 static int InitThread( intf_thread_t * p_intf )
264 /* we might need some locking here */
265 if( !intf_ShouldDie( p_intf ) )
267 input_thread_t * p_input;
269 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
271 vlc_mutex_lock( &p_intf->change_lock );
273 p_intf->p_sys->p_input = p_input;
274 p_intf->p_sys->b_got_gesture = VLC_FALSE;
275 p_intf->p_sys->b_button_pressed = VLC_FALSE;
276 p_intf->p_sys->i_threshold =
277 config_GetInt( p_intf, "gestures-threshold" );
278 psz_button = config_GetPsz( p_intf, "gestures-button" );
279 if ( !strcmp( psz_button, "left" ) )
281 p_intf->p_sys->i_button_mask = 1;
283 else if ( !strcmp( psz_button, "middle" ) )
285 p_intf->p_sys->i_button_mask = 2;
287 else if ( !strcmp( psz_button, "right" ) )
289 p_intf->p_sys->i_button_mask = 4;
292 p_intf->p_sys->i_pattern = 0;
293 p_intf->p_sys->i_num_gestures = 0;
294 vlc_mutex_unlock( &p_intf->change_lock );
304 /*****************************************************************************
305 * MouseEvent: callback for mouse events
306 *****************************************************************************/
307 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
308 vlc_value_t oldval, vlc_value_t newval, void *p_data )
313 signed int i_horizontal, i_vertical;
314 intf_thread_t *p_intf = (intf_thread_t *)p_data;
316 /* don't process new gestures before the last events are processed */
317 if( p_intf->p_sys->b_got_gesture )
322 vlc_mutex_lock( &p_intf->change_lock );
323 if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
325 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
326 p_intf->p_sys->i_mouse_x = val.i_int;
327 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
328 p_intf->p_sys->i_mouse_y = val.i_int;
329 i_horizontal = p_intf->p_sys->i_mouse_x -
330 p_intf->p_sys->i_last_x;
331 i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
332 i_vertical = p_intf->p_sys->i_mouse_y
333 - p_intf->p_sys->i_last_y;
334 i_vertical = i_vertical / p_intf->p_sys->i_threshold;
336 if( i_horizontal < 0 )
338 msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
341 else if( i_horizontal > 0 )
343 msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
348 msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
351 else if( i_vertical > 0 )
353 msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
358 p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
359 p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
360 if( gesture( p_intf->p_sys->i_pattern,
361 p_intf->p_sys->i_num_gestures - 1 ) != pattern )
363 p_intf->p_sys->i_pattern |=
364 pattern << ( p_intf->p_sys->i_num_gestures * 4 );
365 p_intf->p_sys->i_num_gestures++;
370 if( !strcmp( psz_var, "mouse-button-down" )
371 && newval.i_int & p_intf->p_sys->i_button_mask
372 && !p_intf->p_sys->b_button_pressed )
374 p_intf->p_sys->b_button_pressed = VLC_TRUE;
375 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
376 p_intf->p_sys->i_last_x = val.i_int;
377 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
378 p_intf->p_sys->i_last_y = val.i_int;
380 if( !strcmp( psz_var, "mouse-button-down" )
381 && !( newval.i_int & p_intf->p_sys->i_button_mask )
382 && p_intf->p_sys->b_button_pressed )
384 p_intf->p_sys->b_button_pressed = VLC_FALSE;
385 p_intf->p_sys->b_got_gesture = VLC_TRUE;
388 vlc_mutex_unlock( &p_intf->change_lock );