]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
Gestures: Make sure we follow the locking strategy established in [19436].
[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_interface.h>
32 #include <vlc_vout.h>
33 #include <vlc_playlist.h>
34
35 #ifdef HAVE_UNISTD_H
36 #    include <unistd.h>
37 #endif
38
39 /*****************************************************************************
40  * intf_sys_t: description and status of interface
41  *****************************************************************************/
42 struct intf_sys_t
43 {
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;
51     int                 i_num_gestures;
52     int                 i_threshold;
53     int                 i_button_mask;
54 };
55
56 /*****************************************************************************
57  * Local prototypes.
58  *****************************************************************************/
59 #define UP 1
60 #define DOWN 2
61 #define LEFT 3
62 #define RIGHT 4
63 #define NONE 0
64 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
65
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 * );
72
73 /* Exported functions */
74 static void RunIntf        ( intf_thread_t *p_intf );
75
76 /*****************************************************************************
77  * Module descriptor
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." )
82
83 #define BUTTON_TEXT N_( "Trigger button" )
84 #define BUTTON_LONGTEXT N_( \
85     "Trigger button for mouse gestures." )
86
87 static const char *button_list[] = { "left", "middle", "right" };
88 static const char *button_list_text[] =
89                                    { N_("Left"), N_("Middle"), N_("Right") };
90
91 vlc_module_begin();
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") );
101
102     set_capability( "interface", 0 );
103     set_callbacks( E_(Open), E_(Close) );
104 vlc_module_end();
105
106 /*****************************************************************************
107  * OpenIntf: initialize interface
108  *****************************************************************************/
109 int E_(Open) ( vlc_object_t *p_this )
110 {
111     intf_thread_t *p_intf = (intf_thread_t *)p_this;
112
113     /* Allocate instance and initialize some members */
114     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
115     if( p_intf->p_sys == NULL )
116     {
117         return( 1 );
118     };
119
120     p_intf->pf_run = RunIntf;
121
122     return( 0 );
123 }
124
125 /*****************************************************************************
126  * gesture: return a subpattern within a pattern
127  *****************************************************************************/
128 static int gesture( int i_pattern, int i_num )
129 {
130     return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
131 }
132
133 /*****************************************************************************
134  * CloseIntf: destroy dummy interface
135  *****************************************************************************/
136 void E_(Close) ( vlc_object_t *p_this )
137 {
138     intf_thread_t *p_intf = (intf_thread_t *)p_this;
139
140     /* Destroy structure */
141     free( p_intf->p_sys );
142 }
143
144
145 /*****************************************************************************
146  * RunIntf: main loop
147  *****************************************************************************/
148 static void RunIntf( intf_thread_t *p_intf )
149 {
150     playlist_t * p_playlist = NULL;
151     
152     vlc_mutex_lock( &p_intf->change_lock );
153     p_intf->p_sys->p_vout = NULL;
154     vlc_mutex_unlock( &p_intf->change_lock );
155
156     if( InitThread( p_intf ) < 0 )
157     {
158         msg_Err( p_intf, "can't initialize interface thread" );
159         return;
160     }
161     msg_Dbg( p_intf, "interface thread initialized" );
162
163     /* Main loop */
164     while( !intf_ShouldDie( p_intf ) )
165     {
166         vlc_mutex_lock( &p_intf->change_lock );
167
168         /*
169          * mouse cursor
170          */
171         if( p_intf->p_sys->b_got_gesture )
172         {
173             /* Do something */
174             switch( p_intf->p_sys->i_pattern )
175             {
176             case LEFT:
177                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
178                                               FIND_ANYWHERE );
179                 if( p_playlist == NULL )
180                 {
181                     break;
182                 }
183
184                 playlist_Prev( p_playlist );
185                 vlc_object_release( p_playlist );
186                 break;
187             case RIGHT:
188                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
189                                               FIND_ANYWHERE );
190                 if( p_playlist == NULL )
191                 {
192                     break;
193                 }
194
195                 playlist_Next( p_playlist );
196                 vlc_object_release( p_playlist );
197                 break;
198             case GESTURE(UP,RIGHT,NONE,NONE):
199                 if (p_intf->p_sys->p_vout )
200                 {
201                     ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
202                         VOUT_FULLSCREEN_CHANGE;
203                 }
204                 break;
205             case GESTURE(DOWN,RIGHT,NONE,NONE):
206                 /* FIXME: Should close the vout!"*/
207                 p_intf->p_libvlc->b_die = VLC_TRUE;
208                 break;
209             case GESTURE(DOWN,LEFT,UP,RIGHT):
210                 msg_Dbg(p_intf, "a square was drawn!" );
211                 break;
212             default:
213                 break;
214             }
215             p_intf->p_sys->i_num_gestures = 0;
216             p_intf->p_sys->i_pattern = 0;
217             p_intf->p_sys->b_got_gesture = VLC_FALSE;
218         }
219
220         /*
221          * video output
222          */
223         if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
224         {
225             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
226                              MouseEvent, p_intf );
227             var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
228                              MouseEvent, p_intf );
229             vlc_object_release( p_intf->p_sys->p_vout );
230             p_intf->p_sys->p_vout = NULL;
231         }
232
233         if( p_intf->p_sys->p_vout == NULL )
234         {
235             p_intf->p_sys->p_vout = vlc_object_find( p_intf,
236                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
237             if( p_intf->p_sys->p_vout )
238             {
239                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
240                                  MouseEvent, p_intf );
241                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
242                                  MouseEvent, p_intf );
243             }
244         }
245
246         vlc_mutex_unlock( &p_intf->change_lock );
247
248         /* Wait a bit */
249         msleep( INTF_IDLE_SLEEP );
250     }
251
252     EndThread( p_intf );
253 }
254
255 /*****************************************************************************
256  * InitThread:
257  *****************************************************************************/
258 static int InitThread( intf_thread_t * p_intf )
259 {
260     char *psz_button;
261     /* we might need some locking here */
262     if( !intf_ShouldDie( p_intf ) )
263     {
264         /* p_intf->change_lock locking strategy:
265          * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
266          * - make sure there won't be  cross increment/decrement ref count
267          *   of p_intf->p_sys members p_intf->change_lock should be locked
268          *   during those operations */
269         vlc_mutex_lock( &p_intf->change_lock );
270
271         /* p_intf->p_sys->p_input references counting strategy:
272          * - InitThread is responsible for only one ref count retaining
273          * - EndThread is responsible for the corresponding release */
274         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
275                                                   FIND_PARENT );
276
277         p_intf->p_sys->b_got_gesture = VLC_FALSE;
278         p_intf->p_sys->b_button_pressed = VLC_FALSE;
279         p_intf->p_sys->i_threshold =
280                      config_GetInt( p_intf, "gestures-threshold" );
281         psz_button = config_GetPsz( p_intf, "gestures-button" );
282         if ( !strcmp( psz_button, "left" ) )
283         {
284             p_intf->p_sys->i_button_mask = 1;
285         }
286         else if ( !strcmp( psz_button, "middle" ) )
287         {
288             p_intf->p_sys->i_button_mask = 2;
289         }
290         else if ( !strcmp( psz_button, "right" ) )
291         {
292             p_intf->p_sys->i_button_mask = 4;
293         }
294
295         p_intf->p_sys->i_pattern = 0;
296         p_intf->p_sys->i_num_gestures = 0;
297         vlc_mutex_unlock( &p_intf->change_lock );
298
299         return 0;
300     }
301     else
302     {
303         return -1;
304     }
305 }
306
307 /*****************************************************************************
308  * EndThread:
309  *****************************************************************************/
310 static void EndThread( intf_thread_t * p_intf )
311 {
312     vlc_mutex_lock( &p_intf->change_lock );
313
314     if( p_intf->p_sys->p_vout )
315     {
316         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
317                          MouseEvent, p_intf );
318         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
319                          MouseEvent, p_intf );
320         vlc_object_release( p_intf->p_sys->p_vout );
321     }
322
323     if( p_intf->p_sys->p_input )
324     {
325         vlc_object_release(p_intf->p_sys->p_input);
326     }
327
328     vlc_mutex_unlock( &p_intf->change_lock );
329 }
330
331 /*****************************************************************************
332  * MouseEvent: callback for mouse events
333  *****************************************************************************/
334 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
335                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
336 {
337     vlc_value_t val;
338     int pattern = 0;
339
340     signed int i_horizontal, i_vertical;
341     intf_thread_t *p_intf = (intf_thread_t *)p_data;
342
343     vlc_mutex_lock( &p_intf->change_lock );
344
345     /* don't process new gestures before the last events are processed */
346     if( p_intf->p_sys->b_got_gesture )
347     {
348         vlc_mutex_unlock( &p_intf->change_lock );
349         return VLC_SUCCESS;
350     }
351
352     if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
353     {
354         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
355         p_intf->p_sys->i_mouse_x = val.i_int;
356         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
357         p_intf->p_sys->i_mouse_y = val.i_int;
358         i_horizontal = p_intf->p_sys->i_mouse_x -
359             p_intf->p_sys->i_last_x;
360         i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
361         i_vertical = p_intf->p_sys->i_mouse_y
362             - p_intf->p_sys->i_last_y;
363         i_vertical = i_vertical / p_intf->p_sys->i_threshold;
364
365         if( i_horizontal < 0 )
366         {
367             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
368             pattern = LEFT;
369         }
370         else if( i_horizontal > 0 )
371         {
372             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
373             pattern = RIGHT;
374         }
375         if( i_vertical < 0 )
376         {
377             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
378             pattern = UP;
379         }
380         else if( i_vertical > 0 )
381         {
382             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
383             pattern = DOWN;
384         }
385         if( pattern )
386         {
387             p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
388             p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
389             if( gesture( p_intf->p_sys->i_pattern,
390                          p_intf->p_sys->i_num_gestures - 1 ) != pattern )
391             {
392                 p_intf->p_sys->i_pattern |=
393                     pattern << ( p_intf->p_sys->i_num_gestures * 4 );
394                 p_intf->p_sys->i_num_gestures++;
395             }
396         }
397
398     }
399     if( !strcmp( psz_var, "mouse-button-down" )
400         && newval.i_int & p_intf->p_sys->i_button_mask
401         && !p_intf->p_sys->b_button_pressed )
402     {
403         p_intf->p_sys->b_button_pressed = VLC_TRUE;
404         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
405         p_intf->p_sys->i_last_x = val.i_int;
406         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
407         p_intf->p_sys->i_last_y = val.i_int;
408     }
409     if( !strcmp( psz_var, "mouse-button-down" )
410         && !( newval.i_int & p_intf->p_sys->i_button_mask )
411         && p_intf->p_sys->b_button_pressed )
412     {
413         p_intf->p_sys->b_button_pressed = VLC_FALSE;
414         p_intf->p_sys->b_got_gesture = VLC_TRUE;
415     }
416
417     vlc_mutex_unlock( &p_intf->change_lock );
418
419     return VLC_SUCCESS;
420 }