]> git.sesse.net Git - vlc/blob - modules/control/gestures.c
Remove stdlib.h
[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 <string.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc_interface.h>
31 #include <vlc_vout.h>
32 #include <vlc_aout.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     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 void EndThread      ( 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 const char *button_list[] = { "left", "middle", "right" };
87 static const char *button_list_text[] =
88                                    { N_("Left"), N_("Middle"), N_("Right") };
89
90 vlc_module_begin();
91     set_shortname( _("Gestures"));
92     set_category( CAT_INTERFACE );
93     set_subcategory( SUBCAT_INTERFACE_CONTROL );
94     add_integer( "gestures-threshold", 30, NULL,
95                  THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
96     add_string( "gestures-button", "right", NULL,
97                 BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
98         change_string_list( button_list, button_list_text, 0 );
99     set_description( _("Mouse gestures control interface") );
100
101     set_capability( "interface", 0 );
102     set_callbacks( E_(Open), E_(Close) );
103 vlc_module_end();
104
105 /*****************************************************************************
106  * OpenIntf: initialize interface
107  *****************************************************************************/
108 int E_(Open) ( vlc_object_t *p_this )
109 {
110     intf_thread_t *p_intf = (intf_thread_t *)p_this;
111
112     /* Allocate instance and initialize some members */
113     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
114     if( p_intf->p_sys == NULL )
115     {
116         return( 1 );
117     };
118
119     p_intf->pf_run = RunIntf;
120
121     return( 0 );
122 }
123
124 /*****************************************************************************
125  * gesture: return a subpattern within a pattern
126  *****************************************************************************/
127 static int gesture( int i_pattern, int i_num )
128 {
129     return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
130 }
131
132 /*****************************************************************************
133  * input_from_playlist: don't forget to release the return value
134  *  Also this function should really be available from core.
135  *****************************************************************************/
136 static input_thread_t * input_from_playlist ( playlist_t *p_playlist )
137 {
138     input_thread_t * p_input;
139
140     PL_LOCK; 
141     p_input = p_playlist->p_input; 
142     if( p_input ) 
143         vlc_object_yield( p_input ); 
144     PL_UNLOCK;
145
146     return p_input;
147 }
148
149 /*****************************************************************************
150  * CloseIntf: destroy dummy interface
151  *****************************************************************************/
152 void E_(Close) ( vlc_object_t *p_this )
153 {
154     intf_thread_t *p_intf = (intf_thread_t *)p_this;
155
156     /* Destroy structure */
157     free( p_intf->p_sys );
158 }
159
160
161 /*****************************************************************************
162  * RunIntf: main loop
163  *****************************************************************************/
164 static void RunIntf( intf_thread_t *p_intf )
165 {
166     playlist_t * p_playlist = NULL;
167
168     vlc_mutex_lock( &p_intf->change_lock );
169     p_intf->p_sys->p_vout = NULL;
170     vlc_mutex_unlock( &p_intf->change_lock );
171
172     if( InitThread( p_intf ) < 0 )
173     {
174         msg_Err( p_intf, "can't initialize interface thread" );
175         return;
176     }
177     msg_Dbg( p_intf, "interface thread initialized" );
178
179     /* Main loop */
180     while( !intf_ShouldDie( p_intf ) )
181     {
182         vlc_mutex_lock( &p_intf->change_lock );
183
184         /*
185          * mouse cursor
186          */
187         if( p_intf->p_sys->b_got_gesture )
188         {
189             vlc_value_t val;
190             int i_interval = 0;
191             /* Do something */
192             /* If you modify this, please try to follow this convention:
193                Start with LEFT, RIGHT for playback related commands
194                and UP, DOWN, for other commands */
195             switch( p_intf->p_sys->i_pattern )
196             {
197             case LEFT:
198                 i_interval = config_GetInt( p_intf , "short-jump-size" );
199                 if ( i_interval > 0 ) {
200                     val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
201                     var_Set( p_intf, "time-offset", val );
202                 }
203                 msg_Dbg(p_intf, "Go backward in the movie!");
204                 break;
205             case RIGHT:
206                 i_interval = config_GetInt( p_intf , "short-jump-size" );
207                 if ( i_interval > 0 ) {
208                     val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
209                     var_Set( p_intf, "time-offset", val );
210                 }
211                 msg_Dbg(p_intf, "Go forward in the movie!");
212                 break;
213             case GESTURE(LEFT,UP,NONE,NONE):
214                 /*FIXME BF*/
215                 msg_Dbg(p_intf, "Going slower.");
216                 break;
217             case GESTURE(RIGHT,UP,NONE,NONE):
218                 /*FIXME FF*/
219                 msg_Dbg(p_intf, "Going faster.");
220                 break;
221             case GESTURE(LEFT,RIGHT,NONE,NONE):
222             case GESTURE(RIGHT,LEFT,NONE,NONE):
223                 {
224                     input_thread_t * p_input;
225                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
226                                               FIND_ANYWHERE );
227
228                    if( !p_playlist )
229                         break;
230
231                     p_input = input_from_playlist( p_playlist );
232                     vlc_object_release( p_playlist );
233                     
234                     if( !p_input )
235                         break;
236                     
237                     val.i_int = PLAYING_S;
238                     if( p_input )
239                     {
240                         var_Get( p_input, "state", &val);
241                         if( val.i_int == PAUSE_S )
242                         {
243                             val.i_int = PLAYING_S;
244                         }
245                         else
246                         {
247                             val.i_int = PAUSE_S;
248                         }
249                         var_Set( p_input, "state", val);
250                     }
251                     msg_Dbg(p_intf, "Play/Pause");
252                     vlc_object_release( p_input );
253                 }
254                 break;
255             case GESTURE(LEFT,DOWN,NONE,NONE):
256                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
257                                               FIND_ANYWHERE );
258                 if( p_playlist == NULL )
259                 {
260                     break;
261                 }
262
263                 playlist_Prev( p_playlist );
264                 vlc_object_release( p_playlist );
265                 break;
266             case GESTURE(RIGHT,DOWN,NONE,NONE):
267                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
268                                               FIND_ANYWHERE );
269                 if( p_playlist == NULL )
270                 {
271                     break;
272                 }
273
274                 playlist_Next( p_playlist );
275                 vlc_object_release( p_playlist );
276                 break;
277             case UP:
278                 {
279                     audio_volume_t i_newvol;
280                     aout_VolumeUp( p_intf, 1, &i_newvol );
281                     msg_Dbg(p_intf, "Louder");
282                 }
283                 break;
284             case DOWN:
285                 {
286                     audio_volume_t i_newvol;
287                     aout_VolumeDown( p_intf, 1, &i_newvol );
288                     msg_Dbg(p_intf, "Quieter");
289                 }
290                 break;
291             case GESTURE(UP,DOWN,NONE,NONE):
292             case GESTURE(DOWN,UP,NONE,NONE):
293                 {
294                     audio_volume_t i_newvol = -1;
295                     aout_VolumeMute( p_intf, &i_newvol );
296                     msg_Dbg(p_intf, "Mute sound");
297                 }
298                 break;
299             case GESTURE(UP,RIGHT,NONE,NONE):
300                 {
301                    input_thread_t * p_input;
302                    vlc_value_t val, list, list2;
303                    int i_count, i;
304
305                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
306                                               FIND_ANYWHERE );
307
308                    if( !p_playlist )
309                         break;
310
311                     p_input = input_from_playlist( p_playlist );
312
313                     vlc_object_release( p_playlist );
314
315                     if( !p_input )
316                         break;
317
318                    var_Get( p_input, "audio-es", &val );
319                    var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
320                                &list, &list2 );
321                    i_count = list.p_list->i_count;
322                    if( i_count <= 1 )
323                    {
324                        vlc_object_release( p_input );
325                        break;
326                    }
327                    for( i = 0; i < i_count; i++ )
328                    {
329                        if( val.i_int == list.p_list->p_values[i].i_int )
330                        {
331                            break;
332                        }
333                    }
334                    /* value of audio-es was not in choices list */
335                    if( i == i_count )
336                    {
337                        msg_Warn( p_input,
338                                "invalid current audio track, selecting 0" );
339                        var_Set( p_input, "audio-es",
340                                list.p_list->p_values[0] );
341                        i = 0;
342                    }
343                    else if( i == i_count - 1 )
344                    {
345                        var_Set( p_input, "audio-es",
346                                list.p_list->p_values[1] );
347                        i = 1;
348                    }
349                    else
350                    {
351                        var_Set( p_input, "audio-es",
352                                list.p_list->p_values[i+1] );
353                        i++;
354                    }
355                    vlc_object_release( p_input );
356                 }
357                 break;
358             case GESTURE(DOWN,RIGHT,NONE,NONE):
359                 {
360                     input_thread_t * p_input;
361                     vlc_value_t val, list, list2;
362                     int i_count, i;
363
364                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
365                                               FIND_ANYWHERE );
366
367                     if( !p_playlist )
368                         break;
369
370                     p_input = input_from_playlist( p_playlist );
371                     vlc_object_release( p_playlist );
372
373                     if( !p_input )
374                         break;
375
376                     var_Get( p_input, "spu-es", &val );
377
378                     var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
379                             &list, &list2 );
380                     i_count = list.p_list->i_count;
381                     if( i_count <= 1 )
382                     {
383                         vlc_object_release( p_input );
384                         break;
385                     }
386                     for( i = 0; i < i_count; i++ )
387                     {
388                         if( val.i_int == list.p_list->p_values[i].i_int )
389                         {
390                             break;
391                         }
392                     }
393                     /* value of spu-es was not in choices list */
394                     if( i == i_count )
395                     {
396                         msg_Warn( p_input,
397                                 "invalid current subtitle track, selecting 0" );
398                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
399                         i = 0;
400                     }
401                     else if( i == i_count - 1 )
402                     {
403                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
404                         i = 0;
405                     }
406                     else
407                     {
408                         var_Set( p_input, "spu-es", 
409                                 list.p_list->p_values[i+1] );
410                         i = i + 1;
411                     }
412                     vlc_object_release( p_input );
413                 }
414                 break;
415             case GESTURE(UP,LEFT,NONE,NONE):
416                 if (p_intf->p_sys->p_vout )
417                 {
418                     ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
419                         VOUT_FULLSCREEN_CHANGE;
420                 }
421                 break;
422             case GESTURE(DOWN,LEFT,NONE,NONE):
423                 /* FIXME: Should close the vout!"*/
424                 vlc_object_kill( p_intf->p_libvlc );
425                 break;
426             case GESTURE(DOWN,LEFT,UP,RIGHT):
427             case GESTURE(UP,RIGHT,DOWN,LEFT):
428                 msg_Dbg(p_intf, "a square was drawn!" );
429                 break;
430             default:
431                 break;
432             }
433             p_intf->p_sys->i_num_gestures = 0;
434             p_intf->p_sys->i_pattern = 0;
435             p_intf->p_sys->b_got_gesture = VLC_FALSE;
436         }
437
438         /*
439          * video output
440          */
441         if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
442         {
443             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
444                              MouseEvent, p_intf );
445             var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
446                              MouseEvent, p_intf );
447             vlc_object_release( p_intf->p_sys->p_vout );
448             p_intf->p_sys->p_vout = NULL;
449         }
450
451         if( p_intf->p_sys->p_vout == NULL )
452         {
453             p_intf->p_sys->p_vout = vlc_object_find( p_intf,
454                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
455             if( p_intf->p_sys->p_vout )
456             {
457                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
458                                  MouseEvent, p_intf );
459                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
460                                  MouseEvent, p_intf );
461             }
462         }
463
464         vlc_mutex_unlock( &p_intf->change_lock );
465
466         /* Wait a bit */
467         msleep( INTF_IDLE_SLEEP );
468     }
469
470     EndThread( p_intf );
471 }
472
473 /*****************************************************************************
474  * InitThread:
475  *****************************************************************************/
476 static int InitThread( intf_thread_t * p_intf )
477 {
478     char *psz_button;
479     /* we might need some locking here */
480     if( !intf_ShouldDie( p_intf ) )
481     {
482         /* p_intf->change_lock locking strategy:
483          * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
484          * - make sure there won't be  cross increment/decrement ref count
485          *   of p_intf->p_sys members p_intf->change_lock should be locked
486          *   during those operations */
487         vlc_mutex_lock( &p_intf->change_lock );
488
489         p_intf->p_sys->b_got_gesture = VLC_FALSE;
490         p_intf->p_sys->b_button_pressed = VLC_FALSE;
491         p_intf->p_sys->i_threshold =
492                      config_GetInt( p_intf, "gestures-threshold" );
493         psz_button = config_GetPsz( p_intf, "gestures-button" );
494         if ( !strcmp( psz_button, "left" ) )
495         {
496             p_intf->p_sys->i_button_mask = 1;
497         }
498         else if ( !strcmp( psz_button, "middle" ) )
499         {
500             p_intf->p_sys->i_button_mask = 2;
501         }
502         else if ( !strcmp( psz_button, "right" ) )
503         {
504             p_intf->p_sys->i_button_mask = 4;
505         }
506
507         p_intf->p_sys->i_pattern = 0;
508         p_intf->p_sys->i_num_gestures = 0;
509         vlc_mutex_unlock( &p_intf->change_lock );
510
511         return 0;
512     }
513     else
514     {
515         return -1;
516     }
517 }
518
519 /*****************************************************************************
520  * EndThread:
521  *****************************************************************************/
522 static void EndThread( intf_thread_t * p_intf )
523 {
524     vlc_mutex_lock( &p_intf->change_lock );
525
526     if( p_intf->p_sys->p_vout )
527     {
528         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
529                          MouseEvent, p_intf );
530         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
531                          MouseEvent, p_intf );
532         vlc_object_release( p_intf->p_sys->p_vout );
533     }
534
535     vlc_mutex_unlock( &p_intf->change_lock );
536 }
537
538 /*****************************************************************************
539  * MouseEvent: callback for mouse events
540  *****************************************************************************/
541 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
542                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
543 {
544     vlc_value_t val;
545     int pattern = 0;
546
547     signed int i_horizontal, i_vertical;
548     intf_thread_t *p_intf = (intf_thread_t *)p_data;
549
550     vlc_mutex_lock( &p_intf->change_lock );
551
552     /* don't process new gestures before the last events are processed */
553     if( p_intf->p_sys->b_got_gesture )
554     {
555         vlc_mutex_unlock( &p_intf->change_lock );
556         return VLC_SUCCESS;
557     }
558
559     if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
560     {
561         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
562         p_intf->p_sys->i_mouse_x = val.i_int;
563         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
564         p_intf->p_sys->i_mouse_y = val.i_int;
565         i_horizontal = p_intf->p_sys->i_mouse_x -
566             p_intf->p_sys->i_last_x;
567         i_horizontal = i_horizontal / p_intf->p_sys->i_threshold;
568         i_vertical = p_intf->p_sys->i_mouse_y
569             - p_intf->p_sys->i_last_y;
570         i_vertical = i_vertical / p_intf->p_sys->i_threshold;
571
572         if( i_horizontal < 0 )
573         {
574             msg_Dbg( p_intf, "left gesture (%d)", i_horizontal );
575             pattern = LEFT;
576         }
577         else if( i_horizontal > 0 )
578         {
579             msg_Dbg( p_intf, "right gesture (%d)", i_horizontal );
580             pattern = RIGHT;
581         }
582         if( i_vertical < 0 )
583         {
584             msg_Dbg( p_intf, "up gesture (%d)", i_vertical );
585             pattern = UP;
586         }
587         else if( i_vertical > 0 )
588         {
589             msg_Dbg( p_intf, "down gesture (%d)", i_vertical );
590             pattern = DOWN;
591         }
592         if( pattern )
593         {
594             p_intf->p_sys->i_last_y = p_intf->p_sys->i_mouse_y;
595             p_intf->p_sys->i_last_x = p_intf->p_sys->i_mouse_x;
596             if( gesture( p_intf->p_sys->i_pattern,
597                          p_intf->p_sys->i_num_gestures - 1 ) != pattern )
598             {
599                 p_intf->p_sys->i_pattern |=
600                     pattern << ( p_intf->p_sys->i_num_gestures * 4 );
601                 p_intf->p_sys->i_num_gestures++;
602             }
603         }
604
605     }
606     if( !strcmp( psz_var, "mouse-button-down" )
607         && newval.i_int & p_intf->p_sys->i_button_mask
608         && !p_intf->p_sys->b_button_pressed )
609     {
610         p_intf->p_sys->b_button_pressed = VLC_TRUE;
611         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
612         p_intf->p_sys->i_last_x = val.i_int;
613         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
614         p_intf->p_sys->i_last_y = val.i_int;
615     }
616     if( !strcmp( psz_var, "mouse-button-down" )
617         && !( newval.i_int & p_intf->p_sys->i_button_mask )
618         && p_intf->p_sys->b_button_pressed )
619     {
620         p_intf->p_sys->b_button_pressed = VLC_FALSE;
621         p_intf->p_sys->b_got_gesture = VLC_TRUE;
622     }
623
624     vlc_mutex_unlock( &p_intf->change_lock );
625
626     return VLC_SUCCESS;
627 }