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