]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
More cleanup
[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 int  MouseEvent     ( vlc_object_t *, char const *,
70                              vlc_value_t, vlc_value_t, void * );
71
72 /* Exported functions */
73 static void RunIntf        ( intf_thread_t *p_intf );
74
75 /*****************************************************************************
76  * Module descriptor
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." )
81
82 #define BUTTON_TEXT N_( "Trigger button" )
83 #define BUTTON_LONGTEXT N_( \
84     "Trigger button for mouse gestures." )
85
86 static const char *button_list[] = { "left", "middle", "right" };
87 static const char *button_list_text[] =
88                                    { N_("Left"), N_("Middle"), N_("Right") };
89
90 vlc_module_begin();
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") );
100
101     set_capability( "interface", 0 );
102     set_callbacks( E_(Open), E_(Close) );
103 vlc_module_end();
104
105 /*****************************************************************************
106  * OpenIntf: initialize interface
107  *****************************************************************************/
108 int E_(Open) ( vlc_object_t *p_this )
109 {
110     intf_thread_t *p_intf = (intf_thread_t *)p_this;
111
112     /* Allocate instance and initialize some members */
113     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
114     if( p_intf->p_sys == NULL )
115     {
116         return( 1 );
117     };
118
119     p_intf->pf_run = RunIntf;
120
121     return( 0 );
122 }
123
124 /*****************************************************************************
125  * gesture: return a subpattern within a pattern
126  *****************************************************************************/
127 static int gesture( int i_pattern, int i_num )
128 {
129     return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
130 }
131
132 /*****************************************************************************
133  * CloseIntf: destroy dummy interface
134  *****************************************************************************/
135 void E_(Close) ( vlc_object_t *p_this )
136 {
137     intf_thread_t *p_intf = (intf_thread_t *)p_this;
138
139     /* Destroy structure */
140     free( p_intf->p_sys );
141 }
142
143
144 /*****************************************************************************
145  * RunIntf: main loop
146  *****************************************************************************/
147 static void RunIntf( intf_thread_t *p_intf )
148 {
149     playlist_t * p_playlist = NULL;
150     p_intf->p_sys->p_vout = NULL;
151
152     if( InitThread( p_intf ) < 0 )
153     {
154         msg_Err( p_intf, "can't initialize interface thread" );
155         return;
156     }
157     msg_Dbg( p_intf, "interface thread initialized" );
158
159     /* Main loop */
160     while( !intf_ShouldDie( p_intf ) )
161     {
162         vlc_mutex_lock( &p_intf->change_lock );
163
164         /*
165          * mouse cursor
166          */
167         if( p_intf->p_sys->b_got_gesture )
168         {
169             /* Do something */
170             switch( p_intf->p_sys->i_pattern )
171             {
172             case LEFT:
173                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
174                                               FIND_ANYWHERE );
175                 if( p_playlist == NULL )
176                 {
177                     break;
178                 }
179
180                 playlist_Prev( p_playlist );
181                 vlc_object_release( p_playlist );
182                 break;
183             case RIGHT:
184                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
185                                               FIND_ANYWHERE );
186                 if( p_playlist == NULL )
187                 {
188                     break;
189                 }
190
191                 playlist_Next( p_playlist );
192                 vlc_object_release( p_playlist );
193                 break;
194             case GESTURE(UP,RIGHT,NONE,NONE):
195                 if (p_intf->p_sys->p_vout )
196                 {
197                     ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
198                         VOUT_FULLSCREEN_CHANGE;
199                 }
200                 break;
201             case GESTURE(DOWN,RIGHT,NONE,NONE):
202                 /* FIXME: Should close the vout!"*/
203                 p_intf->p_libvlc->b_die = VLC_TRUE;
204                 break;
205             case GESTURE(DOWN,LEFT,UP,RIGHT):
206                 msg_Dbg(p_intf, "a square was drawn!" );
207                 break;
208             default:
209                 break;
210             }
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;
214         }
215
216
217         vlc_mutex_unlock( &p_intf->change_lock );
218
219         /*
220          * video output
221          */
222         if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
223         {
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;
230         }
231
232         if( p_intf->p_sys->p_vout == NULL )
233         {
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 )
237             {
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 );
242             }
243         }
244
245         /* Wait a bit */
246         msleep( INTF_IDLE_SLEEP );
247     }
248
249     if( p_intf->p_sys->p_vout )
250     {
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 );
256     }
257 }
258
259 /*****************************************************************************
260  * InitThread:
261  *****************************************************************************/
262 static int InitThread( intf_thread_t * p_intf )
263 {
264     char *psz_button;
265     /* we might need some locking here */
266     if( !intf_ShouldDie( p_intf ) )
267     {
268         input_thread_t * p_input;
269
270         p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
271
272         vlc_mutex_lock( &p_intf->change_lock );
273
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" ) )
281         {
282             p_intf->p_sys->i_button_mask = 1;
283         }
284         else if ( !strcmp( psz_button, "middle" ) )
285         {
286             p_intf->p_sys->i_button_mask = 2;
287         }
288         else if ( !strcmp( psz_button, "right" ) )
289         {
290             p_intf->p_sys->i_button_mask = 4;
291         }
292
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 );
296
297         return 0;
298     }
299     else
300     {
301         return -1;
302     }
303 }
304
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 )
310 {
311     vlc_value_t val;
312     int pattern = 0;
313
314     signed int i_horizontal, i_vertical;
315     intf_thread_t *p_intf = (intf_thread_t *)p_data;
316
317     /* don't process new gestures before the last events are processed */
318     if( p_intf->p_sys->b_got_gesture )
319     {
320         return VLC_SUCCESS;
321     }
322
323     vlc_mutex_lock( &p_intf->change_lock );
324     if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
325     {
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;
336
337         if( i_horizontal < 0 )
338         {
339             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
340             pattern = LEFT;
341         }
342         else if( i_horizontal > 0 )
343         {
344             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
345             pattern = RIGHT;
346         }
347         if( i_vertical < 0 )
348         {
349             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
350             pattern = UP;
351         }
352         else if( i_vertical > 0 )
353         {
354             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
355             pattern = DOWN;
356         }
357         if( pattern )
358         {
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 )
363             {
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++;
367             }
368         }
369
370     }
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 )
374     {
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;
380     }
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 )
384     {
385         p_intf->p_sys->b_button_pressed = VLC_FALSE;
386         p_intf->p_sys->b_got_gesture = VLC_TRUE;
387     }
388
389     vlc_mutex_unlock( &p_intf->change_lock );
390
391     return VLC_SUCCESS;
392 }