]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
* Get rid of the Manager thread by making blocking interfaces listen to
[vlc] / modules / control / gestures.c
1 /*****************************************************************************
2  * gestures.c: control vlc with mouse gestures
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32 #include <vlc/vout.h>
33
34 #ifdef HAVE_UNISTD_H
35 #    include <unistd.h>
36 #endif
37
38 /*****************************************************************************
39  * intf_sys_t: description and status of interface
40  *****************************************************************************/
41 struct intf_sys_t
42 {
43     vlc_object_t *      p_vout;
44     input_thread_t *    p_input;
45     vlc_bool_t          b_got_gesture;
46     vlc_bool_t          b_button_pressed;
47     int                 i_mouse_x, i_mouse_y;
48     int                 i_last_x, i_last_y;
49     unsigned int        i_pattern;
50     int                 i_num_gestures;
51     int                 i_threshold;
52     int                 i_button_mask;
53 };
54
55 /*****************************************************************************
56  * Local prototypes.
57  *****************************************************************************/
58 #define UP 1
59 #define DOWN 2
60 #define LEFT 3
61 #define RIGHT 4
62 #define NONE 0
63 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
64
65 int  E_(Open)   ( vlc_object_t * );
66 void E_(Close)  ( vlc_object_t * );
67 static int  InitThread     ( intf_thread_t *p_intf );
68 static int  MouseEvent     ( vlc_object_t *, char const *,
69                              vlc_value_t, vlc_value_t, void * );
70
71 /* Exported functions */
72 static void RunIntf        ( intf_thread_t *p_intf );
73
74 /*****************************************************************************
75  * Module descriptor
76  *****************************************************************************/
77 #define THRESHOLD_TEXT N_( "Motion threshold (10-100)" )
78 #define THRESHOLD_LONGTEXT N_( \
79     "Amount of movement required for a mouse gesture to be recorded." )
80
81 #define BUTTON_TEXT N_( "Trigger button" )
82 #define BUTTON_LONGTEXT N_( \
83     "Trigger button for mouse gestures." )
84
85 static char *button_list[] = { "left", "middle", "right" };
86 static char *button_list_text[] = { N_("Left"), N_("Middle"), N_("Right") };
87
88 vlc_module_begin();
89     set_shortname( _("Gestures"));
90     set_category( CAT_INTERFACE );
91     set_subcategory( SUBCAT_INTERFACE_CONTROL );
92     add_integer( "gestures-threshold", 30, NULL,
93                  THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
94     add_string( "gestures-button", "right", NULL,
95                 BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
96         change_string_list( button_list, button_list_text, 0 );
97     set_description( _("Mouse gestures control interface") );
98
99     set_capability( "interface", 0 );
100     set_callbacks( E_(Open), E_(Close) );
101 vlc_module_end();
102
103 /*****************************************************************************
104  * OpenIntf: initialize interface
105  *****************************************************************************/
106 int E_(Open) ( vlc_object_t *p_this )
107 {
108     intf_thread_t *p_intf = (intf_thread_t *)p_this;
109
110     /* Allocate instance and initialize some members */
111     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
112     if( p_intf->p_sys == NULL )
113     {
114         return( 1 );
115     };
116
117     p_intf->pf_run = RunIntf;
118
119     return( 0 );
120 }
121
122 /*****************************************************************************
123  * gesture: return a subpattern within a pattern
124  *****************************************************************************/
125 static int gesture( int i_pattern, int i_num )
126 {
127     return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
128 }
129
130 /*****************************************************************************
131  * CloseIntf: destroy dummy interface
132  *****************************************************************************/
133 void E_(Close) ( vlc_object_t *p_this )
134 {
135     intf_thread_t *p_intf = (intf_thread_t *)p_this;
136
137     /* Destroy structure */
138     free( p_intf->p_sys );
139 }
140
141
142 /*****************************************************************************
143  * RunIntf: main loop
144  *****************************************************************************/
145 static void RunIntf( intf_thread_t *p_intf )
146 {
147     playlist_t * p_playlist = NULL;
148     p_intf->p_sys->p_vout = NULL;
149
150     if( InitThread( p_intf ) < 0 )
151     {
152         msg_Err( p_intf, "can't initialize interface thread" );
153         return;
154     }
155     msg_Dbg( p_intf, "interface thread initialized" );
156
157     /* Main loop */
158     while( !intf_ShouldDie( p_intf ) )
159     {
160         vlc_mutex_lock( &p_intf->change_lock );
161
162         /*
163          * mouse cursor
164          */
165         if( p_intf->p_sys->b_got_gesture )
166         {
167             /* Do something */
168             switch( p_intf->p_sys->i_pattern )
169             {
170             case LEFT:
171                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
172                                               FIND_ANYWHERE );
173                 if( p_playlist == NULL )
174                 {
175                     break;
176                 }
177
178                 playlist_Prev( p_playlist );
179                 vlc_object_release( p_playlist );
180                 break;
181             case RIGHT:
182                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
183                                               FIND_ANYWHERE );
184                 if( p_playlist == NULL )
185                 {
186                     break;
187                 }
188
189                 playlist_Next( p_playlist );
190                 vlc_object_release( p_playlist );
191                 break;
192             case GESTURE(UP,RIGHT,NONE,NONE):
193                 if (p_intf->p_sys->p_vout )
194                 {
195                     ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
196                         VOUT_FULLSCREEN_CHANGE;
197                 }
198                 break;
199             case GESTURE(DOWN,RIGHT,NONE,NONE):
200                 /* FIXME: Should close the vout!"*/
201                 p_intf->p_libvlc->b_die = VLC_TRUE;
202                 break;
203             case GESTURE(DOWN,LEFT,UP,RIGHT):
204                 msg_Dbg(p_intf, "a square was drawn!" );
205                 break;
206             default:
207                 break;
208             }
209             p_intf->p_sys->i_num_gestures = 0;
210             p_intf->p_sys->i_pattern = 0;
211             p_intf->p_sys->b_got_gesture = VLC_FALSE;
212         }
213
214
215         vlc_mutex_unlock( &p_intf->change_lock );
216
217         /*
218          * video output
219          */
220         if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
221         {
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;
228         }
229
230         if( p_intf->p_sys->p_vout == NULL )
231         {
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 )
235             {
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 );
240             }
241         }
242
243         /* Wait a bit */
244         msleep( INTF_IDLE_SLEEP );
245     }
246
247     if( p_intf->p_sys->p_vout )
248     {
249         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
250                          MouseEvent, p_intf );
251         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
252                          MouseEvent, p_intf );
253         vlc_object_release( p_intf->p_sys->p_vout );
254     }
255 }
256
257 /*****************************************************************************
258  * InitThread:
259  *****************************************************************************/
260 static int InitThread( intf_thread_t * p_intf )
261 {
262     char *psz_button;
263     /* we might need some locking here */
264     if( !intf_ShouldDie( p_intf ) )
265     {
266         input_thread_t * p_input;
267
268         p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
269
270         vlc_mutex_lock( &p_intf->change_lock );
271
272         p_intf->p_sys->p_input = p_input;
273         p_intf->p_sys->b_got_gesture = VLC_FALSE;
274         p_intf->p_sys->b_button_pressed = VLC_FALSE;
275         p_intf->p_sys->i_threshold =
276                      config_GetInt( p_intf, "gestures-threshold" );
277         psz_button = config_GetPsz( p_intf, "gestures-button" );
278         if ( !strcmp( psz_button, "left" ) )
279         {
280             p_intf->p_sys->i_button_mask = 1;
281         }
282         else if ( !strcmp( psz_button, "middle" ) )
283         {
284             p_intf->p_sys->i_button_mask = 2;
285         }
286         else if ( !strcmp( psz_button, "right" ) )
287         {
288             p_intf->p_sys->i_button_mask = 4;
289         }
290
291         p_intf->p_sys->i_pattern = 0;
292         p_intf->p_sys->i_num_gestures = 0;
293         vlc_mutex_unlock( &p_intf->change_lock );
294
295         return 0;
296     }
297     else
298     {
299         return -1;
300     }
301 }
302
303 /*****************************************************************************
304  * MouseEvent: callback for mouse events
305  *****************************************************************************/
306 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
307                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
308 {
309     vlc_value_t val;
310     int pattern = 0;
311
312     signed int i_horizontal, i_vertical;
313     intf_thread_t *p_intf = (intf_thread_t *)p_data;
314
315     /* don't process new gestures before the last events are processed */
316     if( p_intf->p_sys->b_got_gesture )
317     {
318         return VLC_SUCCESS;
319     }
320
321     vlc_mutex_lock( &p_intf->change_lock );
322     if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
323     {
324         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
325         p_intf->p_sys->i_mouse_x = val.i_int;
326         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
327         p_intf->p_sys->i_mouse_y = val.i_int;
328         i_horizontal = p_intf->p_sys->i_mouse_x -
329             p_intf->p_sys->i_last_x;
330         i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
331         i_vertical = p_intf->p_sys->i_mouse_y
332             - p_intf->p_sys->i_last_y;
333         i_vertical = i_vertical / p_intf->p_sys->i_threshold;
334
335         if( i_horizontal < 0 )
336         {
337             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
338             pattern = LEFT;
339         }
340         else if( i_horizontal > 0 )
341         {
342             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
343             pattern = RIGHT;
344         }
345         if( i_vertical < 0 )
346         {
347             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
348             pattern = UP;
349         }
350         else if( i_vertical > 0 )
351         {
352             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
353             pattern = DOWN;
354         }
355         if( pattern )
356         {
357             p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
358             p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
359             if( gesture( p_intf->p_sys->i_pattern,
360                          p_intf->p_sys->i_num_gestures - 1 ) != pattern )
361             {
362                 p_intf->p_sys->i_pattern |=
363                     pattern << ( p_intf->p_sys->i_num_gestures * 4 );
364                 p_intf->p_sys->i_num_gestures++;
365             }
366         }
367
368     }
369     if( !strcmp( psz_var, "mouse-button-down" )
370         && newval.i_int & p_intf->p_sys->i_button_mask
371         && !p_intf->p_sys->b_button_pressed )
372     {
373         p_intf->p_sys->b_button_pressed = VLC_TRUE;
374         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
375         p_intf->p_sys->i_last_x = val.i_int;
376         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
377         p_intf->p_sys->i_last_y = val.i_int;
378     }
379     if( !strcmp( psz_var, "mouse-button-down" )
380         && !( newval.i_int & p_intf->p_sys->i_button_mask )
381         && p_intf->p_sys->b_button_pressed )
382     {
383         p_intf->p_sys->b_button_pressed = VLC_FALSE;
384         p_intf->p_sys->b_got_gesture = VLC_TRUE;
385     }
386
387     vlc_mutex_unlock( &p_intf->change_lock );
388
389     return VLC_SUCCESS;
390 }