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 void EndThread ( intf_thread_t *p_intf );
70 static int MouseEvent ( vlc_object_t *, char const *,
71 vlc_value_t, vlc_value_t, void * );
73 /* Exported functions */
74 static void RunIntf ( intf_thread_t *p_intf );
76 /*****************************************************************************
78 *****************************************************************************/
79 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
80 #define THRESHOLD_LONGTEXT N_( \
81 "Amount of movement required for a mouse gesture to be recorded." )
83 #define BUTTON_TEXT N_( "Trigger button" )
84 #define BUTTON_LONGTEXT N_( \
85 "Trigger button for mouse gestures." )
87 static const char *button_list[] = { "left", "middle", "right" };
88 static const char *button_list_text[] =
89 { N_("Left"), N_("Middle"), N_("Right") };
92 set_shortname( _("Gestures"));
93 set_category( CAT_INTERFACE );
94 set_subcategory( SUBCAT_INTERFACE_CONTROL );
95 add_integer( "gestures-threshold", 30, NULL,
96 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
97 add_string( "gestures-button", "right", NULL,
98 BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
99 change_string_list( button_list, button_list_text, 0 );
100 set_description( _("Mouse gestures control interface") );
102 set_capability( "interface", 0 );
103 set_callbacks( E_(Open), E_(Close) );
106 /*****************************************************************************
107 * OpenIntf: initialize interface
108 *****************************************************************************/
109 int E_(Open) ( vlc_object_t *p_this )
111 intf_thread_t *p_intf = (intf_thread_t *)p_this;
113 /* Allocate instance and initialize some members */
114 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
115 if( p_intf->p_sys == NULL )
120 p_intf->pf_run = RunIntf;
125 /*****************************************************************************
126 * gesture: return a subpattern within a pattern
127 *****************************************************************************/
128 static int gesture( int i_pattern, int i_num )
130 return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
133 /*****************************************************************************
134 * CloseIntf: destroy dummy interface
135 *****************************************************************************/
136 void E_(Close) ( vlc_object_t *p_this )
138 intf_thread_t *p_intf = (intf_thread_t *)p_this;
140 /* Destroy structure */
141 free( p_intf->p_sys );
145 /*****************************************************************************
147 *****************************************************************************/
148 static void RunIntf( intf_thread_t *p_intf )
150 playlist_t * p_playlist = NULL;
151 p_intf->p_sys->p_vout = NULL;
153 if( InitThread( p_intf ) < 0 )
155 msg_Err( p_intf, "can't initialize interface thread" );
158 msg_Dbg( p_intf, "interface thread initialized" );
161 while( !intf_ShouldDie( p_intf ) )
163 vlc_mutex_lock( &p_intf->change_lock );
168 if( p_intf->p_sys->b_got_gesture )
171 switch( p_intf->p_sys->i_pattern )
174 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
176 if( p_playlist == NULL )
181 playlist_Prev( p_playlist );
182 vlc_object_release( p_playlist );
185 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
187 if( p_playlist == NULL )
192 playlist_Next( p_playlist );
193 vlc_object_release( p_playlist );
195 case GESTURE(UP,RIGHT,NONE,NONE):
196 if (p_intf->p_sys->p_vout )
198 ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
199 VOUT_FULLSCREEN_CHANGE;
202 case GESTURE(DOWN,RIGHT,NONE,NONE):
203 /* FIXME: Should close the vout!"*/
204 p_intf->p_libvlc->b_die = VLC_TRUE;
206 case GESTURE(DOWN,LEFT,UP,RIGHT):
207 msg_Dbg(p_intf, "a square was drawn!" );
212 p_intf->p_sys->i_num_gestures = 0;
213 p_intf->p_sys->i_pattern = 0;
214 p_intf->p_sys->b_got_gesture = VLC_FALSE;
220 if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
222 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
223 MouseEvent, p_intf );
224 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
225 MouseEvent, p_intf );
226 vlc_object_release( p_intf->p_sys->p_vout );
227 p_intf->p_sys->p_vout = NULL;
230 if( p_intf->p_sys->p_vout == NULL )
232 p_intf->p_sys->p_vout = vlc_object_find( p_intf,
233 VLC_OBJECT_VOUT, FIND_ANYWHERE );
234 if( p_intf->p_sys->p_vout )
236 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
237 MouseEvent, p_intf );
238 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
239 MouseEvent, p_intf );
243 vlc_mutex_unlock( &p_intf->change_lock );
246 msleep( INTF_IDLE_SLEEP );
252 /*****************************************************************************
254 *****************************************************************************/
255 static int InitThread( intf_thread_t * p_intf )
258 /* we might need some locking here */
259 if( !intf_ShouldDie( p_intf ) )
261 /* p_intf->change_lock locking strategy:
262 * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
263 * - make sure there won't be cross increment/decrement ref count
264 * of p_intf->p_sys members p_intf->change_lock should be locked
265 * during those operations */
266 vlc_mutex_lock( &p_intf->change_lock );
268 /* p_intf->p_sys->p_input references counting strategy:
269 * - InitThread is responsible for only one ref count retaining
270 * - EndThread is responsible for the corresponding release */
271 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_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 /*****************************************************************************
306 *****************************************************************************/
307 static void EndThread( intf_thread_t * p_intf )
309 vlc_mutex_lock( &p_intf->change_lock );
311 if( p_intf->p_sys->p_vout )
313 var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
314 MouseEvent, p_intf );
315 var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
316 MouseEvent, p_intf );
317 vlc_object_release( p_intf->p_sys->p_vout );
320 if( p_intf->p_sys->p_input )
322 vlc_object_release(p_intf->p_sys->p_input);
325 vlc_mutex_unlock( &p_intf->change_lock );
328 /*****************************************************************************
329 * MouseEvent: callback for mouse events
330 *****************************************************************************/
331 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
332 vlc_value_t oldval, vlc_value_t newval, void *p_data )
337 signed int i_horizontal, i_vertical;
338 intf_thread_t *p_intf = (intf_thread_t *)p_data;
340 vlc_mutex_lock( &p_intf->change_lock );
342 /* don't process new gestures before the last events are processed */
343 if( p_intf->p_sys->b_got_gesture )
345 vlc_mutex_unlock( &p_intf->change_lock );
349 if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
351 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
352 p_intf->p_sys->i_mouse_x = val.i_int;
353 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
354 p_intf->p_sys->i_mouse_y = val.i_int;
355 i_horizontal = p_intf->p_sys->i_mouse_x -
356 p_intf->p_sys->i_last_x;
357 i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
358 i_vertical = p_intf->p_sys->i_mouse_y
359 - p_intf->p_sys->i_last_y;
360 i_vertical = i_vertical / p_intf->p_sys->i_threshold;
362 if( i_horizontal < 0 )
364 msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
367 else if( i_horizontal > 0 )
369 msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
374 msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
377 else if( i_vertical > 0 )
379 msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
384 p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
385 p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
386 if( gesture( p_intf->p_sys->i_pattern,
387 p_intf->p_sys->i_num_gestures - 1 ) != pattern )
389 p_intf->p_sys->i_pattern |=
390 pattern << ( p_intf->p_sys->i_num_gestures * 4 );
391 p_intf->p_sys->i_num_gestures++;
396 if( !strcmp( psz_var, "mouse-button-down" )
397 && newval.i_int & p_intf->p_sys->i_button_mask
398 && !p_intf->p_sys->b_button_pressed )
400 p_intf->p_sys->b_button_pressed = VLC_TRUE;
401 var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
402 p_intf->p_sys->i_last_x = val.i_int;
403 var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
404 p_intf->p_sys->i_last_y = val.i_int;
406 if( !strcmp( psz_var, "mouse-button-down" )
407 && !( newval.i_int & p_intf->p_sys->i_button_mask )
408 && p_intf->p_sys->b_button_pressed )
410 p_intf->p_sys->b_button_pressed = VLC_FALSE;
411 p_intf->p_sys->b_got_gesture = VLC_TRUE;
414 vlc_mutex_unlock( &p_intf->change_lock );