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