]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
* include/configuration.h: some small re-work of the config declaration macros.
[vlc] / modules / control / gestures.c
1 /*****************************************************************************
2  * geatures.c: control vlc with mouse gestures
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: gestures.c,v 1.6 2003/11/05 00:39:16 gbazin Exp $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/intf.h>
33 #include <vlc/vout.h>
34
35 #include "stream_control.h"
36 #include "input_ext-intf.h"
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" )
78 #define THRESHOLD_LONGTEXT N_( \
79     "the amount of movement required for a mouse" \
80     " gesture to be recorded" )
81
82 #define BUTTON_TEXT N_( "Mouse button" )
83 #define BUTTON_LONGTEXT N_( \
84     "the mouse button to be held down during mouse gestures" )
85
86 static char *button_list[] = { "left", "middle", "right" };
87 static char *button_list_text[] = { N_("Left"), N_("Middle"), N_("Right") };
88
89 vlc_module_begin();
90     add_category_hint( N_( "Gestures" ), NULL, VLC_FALSE );
91     add_integer( "gestures-threshold", 30, NULL, THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
92     add_string( "gestures-button", "right", NULL,
93                 BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
94         change_string_list( button_list, button_list_text, 0 );
95     set_description( _("mouse gestures control interface") );
96
97     set_capability( "interface", 0 );
98     set_callbacks( E_(Open), E_(Close) );
99 vlc_module_end();
100
101 /*****************************************************************************
102  * OpenIntf: initialize interface
103  *****************************************************************************/
104 int E_(Open) ( vlc_object_t *p_this )
105 {
106     intf_thread_t *p_intf = (intf_thread_t *)p_this;
107
108     /* Allocate instance and initialize some members */
109     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
110     if( p_intf->p_sys == NULL )
111     {
112         return( 1 );
113     };
114
115     p_intf->pf_run = RunIntf;
116
117     return( 0 );
118 }
119
120 /*****************************************************************************
121  * gesture: return a subpattern within a pattern
122  *****************************************************************************/
123 static int gesture( int i_pattern, int i_num )
124 {
125     return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
126 }
127         
128 /*****************************************************************************
129  * CloseIntf: destroy dummy interface
130  *****************************************************************************/
131 void E_(Close) ( vlc_object_t *p_this )
132 {
133     intf_thread_t *p_intf = (intf_thread_t *)p_this;
134
135     /* Destroy structure */
136     free( p_intf->p_sys );
137 }
138
139
140 /*****************************************************************************
141  * RunIntf: main loop
142  *****************************************************************************/
143 static void RunIntf( intf_thread_t *p_intf )
144 {
145     playlist_t * p_playlist = NULL;
146     p_intf->p_sys->p_vout = NULL;
147
148     if( InitThread( p_intf ) < 0 )
149     {
150         msg_Err( p_intf, "can't initialize intf" );
151         return;
152     }
153     msg_Dbg( p_intf, "intf initialized" );
154
155     /* Main loop */
156     while( !p_intf->b_die )
157     {
158         vlc_mutex_lock( &p_intf->change_lock );
159
160         /* 
161          * mouse cursor
162          */
163         if( p_intf->p_sys->b_got_gesture )
164         {
165             /* Do something */
166             switch( p_intf->p_sys->i_pattern )
167             {
168             case LEFT:
169                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
170                                               FIND_ANYWHERE );
171                 if( p_playlist == NULL )
172                 {
173                     break;
174                 }
175                 
176                 playlist_Prev( p_playlist );
177                 vlc_object_release( p_playlist );
178                 break;
179             case RIGHT:
180                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
181                                               FIND_ANYWHERE );
182                 if( p_playlist == NULL )
183                 {
184                     break;
185                 }
186                 
187                 playlist_Next( p_playlist );
188                 vlc_object_release( p_playlist );
189                 break;
190             case GESTURE(UP,RIGHT,NONE,NONE):
191                 if (p_intf->p_sys->p_vout )
192                 {
193                     ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
194                         VOUT_FULLSCREEN_CHANGE;
195                 }
196                 break;
197             case GESTURE(DOWN,RIGHT,NONE,NONE):
198                 /* FIXME: Should close the vout!"*/
199                 p_intf->p_vlc->b_die = VLC_TRUE;
200                 break;
201             case GESTURE(DOWN,LEFT,UP,RIGHT):
202                 msg_Dbg(p_intf, "A square!" );
203                 break;
204             default:
205                 break;
206             }
207             p_intf->p_sys->i_num_gestures = 0;
208             p_intf->p_sys->i_pattern = 0;
209             p_intf->p_sys->b_got_gesture = VLC_FALSE;
210         }
211                 
212
213         vlc_mutex_unlock( &p_intf->change_lock );
214
215         /* 
216          * video output
217          */
218         if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
219         {
220             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
221                              MouseEvent, p_intf );
222             var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
223                              MouseEvent, p_intf );
224             vlc_object_release( p_intf->p_sys->p_vout );
225             p_intf->p_sys->p_vout = NULL;
226         }
227
228         if( p_intf->p_sys->p_vout == NULL )
229         {
230             p_intf->p_sys->p_vout = vlc_object_find( p_intf,
231                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
232             if( p_intf->p_sys->p_vout )
233             {
234                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
235                                  MouseEvent, p_intf );
236                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
237                                  MouseEvent, p_intf );
238             }
239         }
240
241         /* Wait a bit */
242         msleep( INTF_IDLE_SLEEP );
243     }
244
245     if( p_intf->p_sys->p_vout )
246     {
247         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
248                          MouseEvent, p_intf );
249         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
250                          MouseEvent, p_intf );
251         vlc_object_release( p_intf->p_sys->p_vout );
252     }
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( !p_intf->b_die )
263     {
264         input_thread_t * p_input;
265
266         p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
267
268         vlc_mutex_lock( &p_intf->change_lock );
269
270         p_intf->p_sys->p_input = p_input;
271         p_intf->p_sys->b_got_gesture = VLC_FALSE;
272         p_intf->p_sys->b_button_pressed = VLC_FALSE;
273         p_intf->p_sys->i_threshold = config_GetInt( p_intf, "gestures-threshold" );
274         psz_button = config_GetPsz( p_intf, "gestures-button" );
275         if ( !strcmp( psz_button, "left" ) )
276         {
277             p_intf->p_sys->i_button_mask = 1;
278         }
279         else if ( !strcmp( psz_button, "middle" ) )
280         {
281             p_intf->p_sys->i_button_mask = 2;
282         }
283         else if ( !strcmp( psz_button, "right" ) )
284         {
285             p_intf->p_sys->i_button_mask = 4;
286         }
287
288         p_intf->p_sys->i_pattern = 0;
289         p_intf->p_sys->i_num_gestures = 0;
290         vlc_mutex_unlock( &p_intf->change_lock );
291
292         return 0;
293     }
294     else
295     {
296         return -1;
297     }
298 }
299
300 /*****************************************************************************
301  * MouseEvent: callback for mouse events
302  *****************************************************************************/
303 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
304                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
305 {
306     vlc_value_t val;
307     int pattern = 0;
308     
309     signed int i_horizontal, i_vertical;
310     intf_thread_t *p_intf = (intf_thread_t *)p_data;
311
312     /* don't process new gestures before the last events are processed */
313     if( p_intf->p_sys->b_got_gesture ) {
314         return VLC_SUCCESS;
315     }
316
317     vlc_mutex_lock( &p_intf->change_lock );
318     if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed ) {
319         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
320         p_intf->p_sys->i_mouse_x = val.i_int;
321         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
322         p_intf->p_sys->i_mouse_y = val.i_int;
323         i_horizontal = p_intf->p_sys->i_mouse_x -
324             p_intf->p_sys->i_last_x;
325         i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
326         i_vertical = p_intf->p_sys->i_mouse_y
327             - p_intf->p_sys->i_last_y;
328         i_vertical = i_vertical / p_intf->p_sys->i_threshold;
329         
330         if ( i_horizontal < 0 )
331         {
332             msg_Dbg( p_intf, "left gesture(%d)", i_horizontal );
333             pattern = LEFT;
334         }
335         else if ( i_horizontal > 0 )
336         {
337             msg_Dbg( p_intf, "right gesture(%d)", i_horizontal );
338             pattern = RIGHT;
339         }
340         if ( i_vertical < 0 )
341         {
342             msg_Dbg( p_intf, "up gesture(%d)", i_vertical );
343             pattern = UP;
344         }
345         else if ( i_vertical > 0 )
346         {
347             msg_Dbg( p_intf, "down gesture(%d)", i_vertical );
348             pattern = DOWN;
349         }
350         if (pattern )
351         {
352             p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
353             p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
354             if( gesture(p_intf->p_sys->i_pattern, p_intf->p_sys->i_num_gestures - 1 ) != pattern )
355             {
356                 p_intf->p_sys->i_pattern |= pattern << ( p_intf->p_sys->i_num_gestures * 4 );
357                 p_intf->p_sys->i_num_gestures++;
358             }
359         }
360
361     }
362     if( !strcmp( psz_var, "mouse-button-down" ) && newval.i_int & p_intf->p_sys->i_button_mask
363         && !p_intf->p_sys->b_button_pressed )
364     {
365         p_intf->p_sys->b_button_pressed = VLC_TRUE;
366         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
367         p_intf->p_sys->i_last_x = val.i_int;
368         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
369         p_intf->p_sys->i_last_y = val.i_int;
370     }
371     if( !strcmp( psz_var, "mouse-button-down" ) && !( newval.i_int & p_intf->p_sys->i_button_mask )
372         && p_intf->p_sys->b_button_pressed )
373     {
374         p_intf->p_sys->b_button_pressed = VLC_FALSE;
375         p_intf->p_sys->b_got_gesture = VLC_TRUE;
376     }
377
378     vlc_mutex_unlock( &p_intf->change_lock );
379
380     return VLC_SUCCESS;
381 }