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