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