Converted globalhotkeys to XCB.
[vlc.git] / modules / control / hotkeys.c
1 /*****************************************************************************
2  * hotkeys.c: Hotkey handling for vlc
3  *****************************************************************************
4  * Copyright (C) 2005-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *          Jean-Paul Saman <jpsaman #_at_# m2x.nl>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_interface.h>
36 #include <vlc_input.h>
37 #include <vlc_vout.h>
38 #include <vlc_aout.h>
39 #include <vlc_osd.h>
40 #include <vlc_playlist.h>
41 #include "vlc_keys.h"
42 #include "math.h"
43
44 #define BUFFER_SIZE 10
45
46 #define CHANNELS_NUMBER 4
47 #define VOLUME_TEXT_CHAN     p_intf->p_sys->p_channels[ 0 ]
48 #define VOLUME_WIDGET_CHAN   p_intf->p_sys->p_channels[ 1 ]
49 #define POSITION_TEXT_CHAN   p_intf->p_sys->p_channels[ 2 ]
50 #define POSITION_WIDGET_CHAN p_intf->p_sys->p_channels[ 3 ]
51
52 /*****************************************************************************
53  * intf_sys_t: description and status of FB interface
54  *****************************************************************************/
55 struct intf_sys_t
56 {
57     int                 p_actions[ BUFFER_SIZE ]; /* buffer that contains
58                                                    * action events */
59     int                 i_size;        /* number of events in buffer */
60     int                 p_channels[ CHANNELS_NUMBER ]; /* contains registered
61                                                         * channel IDs */
62     input_thread_t *    p_input;       /* pointer to input */
63     vout_thread_t *     p_vout;        /* pointer to vout object */
64     vlc_mutex_t         lock; /* callback lock */
65     vlc_cond_t          wait; /* callback event */
66     int                 i_mousewheel_mode;
67 };
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 static int  Open    ( vlc_object_t * );
73 static void Close   ( vlc_object_t * );
74 static void Run     ( intf_thread_t * );
75 static int  GetAction( intf_thread_t *);
76 static int  ActionEvent( vlc_object_t *, char const *,
77                          vlc_value_t, vlc_value_t, void * );
78 static int  SpecialKeyEvent( vlc_object_t *, char const *,
79                              vlc_value_t, vlc_value_t, void * );
80 static void PlayBookmark( intf_thread_t *, int );
81 static void SetBookmark ( intf_thread_t *, int );
82 static void DisplayPosition( intf_thread_t *, vout_thread_t *, input_thread_t * );
83 static void DisplayVolume  ( intf_thread_t *, vout_thread_t *, audio_volume_t );
84 static void ClearChannels  ( intf_thread_t *, vout_thread_t * );
85
86 /*****************************************************************************
87  * Module descriptor
88  *****************************************************************************/
89
90 enum{
91     MOUSEWHEEL_VOLUME,
92     MOUSEWHEEL_POSITION,
93     NO_MOUSEWHEEL,
94 };
95
96 static const int i_mode_list[] =
97     { MOUSEWHEEL_VOLUME, MOUSEWHEEL_POSITION, NO_MOUSEWHEEL };
98
99 static const char *const psz_mode_list_text[] =
100     { N_("Volume Control"), N_("Position Control"), N_("Ignore") };
101
102 vlc_module_begin ()
103     set_shortname( N_("Hotkeys") )
104     set_description( N_("Hotkeys management interface") )
105     set_capability( "interface", 0 )
106     set_callbacks( Open, Close )
107
108     add_integer( "hotkeys-mousewheel-mode", MOUSEWHEEL_VOLUME, NULL,
109                  N_("MouseWheel x-axis Control"),
110                  N_("MouseWheel x-axis can control volume, position or "
111                     "mousewheel event can be ignored"), false )
112             change_integer_list( i_mode_list, psz_mode_list_text, NULL )
113
114 vlc_module_end ()
115
116 /*****************************************************************************
117  * Open: initialize interface
118  *****************************************************************************/
119 static int Open( vlc_object_t *p_this )
120 {
121     intf_thread_t *p_intf = (intf_thread_t *)p_this;
122     intf_sys_t *p_sys;
123     p_sys = malloc( sizeof( intf_sys_t ) );
124     if( !p_sys )
125         return VLC_ENOMEM;
126
127     p_intf->p_sys = p_sys;
128     p_intf->pf_run = Run;
129
130     p_sys->i_size = 0;
131     vlc_mutex_init( &p_sys->lock );
132     vlc_cond_init( &p_sys->wait );
133     p_intf->p_sys->i_mousewheel_mode =
134         config_GetInt( p_intf, "hotkeys-mousewheel-mode" );
135
136     var_AddCallback( p_intf->p_libvlc, "key-pressed", SpecialKeyEvent, p_intf );
137     var_AddCallback( p_intf->p_libvlc, "key-action", ActionEvent, p_intf );
138     return VLC_SUCCESS;
139 }
140
141 /*****************************************************************************
142  * Close: destroy interface
143  *****************************************************************************/
144 static void Close( vlc_object_t *p_this )
145 {
146     intf_thread_t *p_intf = (intf_thread_t *)p_this;
147     intf_sys_t *p_sys = p_intf->p_sys;
148
149     var_DelCallback( p_intf->p_libvlc, "key-action", ActionEvent, p_intf );
150     var_DelCallback( p_intf->p_libvlc, "key-pressed", SpecialKeyEvent, p_intf );
151
152     vlc_cond_destroy( &p_sys->wait );
153     vlc_mutex_destroy( &p_sys->lock );
154
155     /* Destroy structure */
156     free( p_intf->p_sys );
157 }
158
159 /*****************************************************************************
160  * Run: main loop
161  *****************************************************************************/
162 static void Run( intf_thread_t *p_intf )
163 {
164     vout_thread_t *p_vout = NULL;
165     vlc_value_t val;
166     playlist_t *p_playlist = pl_Hold( p_intf );
167     int canc = vlc_savecancel();
168
169     vlc_cleanup_push( __pl_Release, p_intf );
170
171     /* Initialize hotkey structure */
172     for( struct hotkey *p_hotkey = p_intf->p_libvlc->p_hotkeys;
173          p_hotkey->psz_action != NULL;
174          p_hotkey++ )
175     {
176         p_hotkey->i_key = config_GetInt( p_intf, p_hotkey->psz_action );
177     }
178
179     for( ;; )
180     {
181         input_thread_t *p_input;
182         vout_thread_t *p_last_vout;
183         int i_action;
184
185         vlc_restorecancel( canc );
186         i_action = GetAction( p_intf );
187
188         canc = vlc_savecancel();
189
190         /* Update the input */
191         p_input = playlist_CurrentInput( p_playlist );
192
193         /* Update the vout */
194         p_last_vout = p_vout;
195         p_vout = p_input ? input_GetVout( p_input ) : NULL;
196
197         /* Register OSD channels */
198         if( p_vout && p_vout != p_last_vout )
199         {
200             int i;
201             for( i = 0; i < CHANNELS_NUMBER; i++ )
202             {
203                 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
204                              &p_intf->p_sys->p_channels[ i ] );
205             }
206         }
207
208         /* Quit */
209         if( i_action == ACTIONID_QUIT )
210         {
211             libvlc_Quit( p_intf->p_libvlc );
212
213             ClearChannels( p_intf, p_vout );
214             vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Quit" ) );
215             if( p_vout )
216                 vlc_object_release( p_vout );
217             if( p_input )
218                 vlc_object_release( p_input );
219             continue;
220         }
221         /* Volume and audio actions */
222         else if( i_action == ACTIONID_VOL_UP )
223         {
224             audio_volume_t i_newvol;
225             aout_VolumeUp( p_intf, 1, &i_newvol );
226             DisplayVolume( p_intf, p_vout, i_newvol );
227         }
228         else if( i_action == ACTIONID_VOL_DOWN )
229         {
230             audio_volume_t i_newvol;
231             aout_VolumeDown( p_intf, 1, &i_newvol );
232             DisplayVolume( p_intf, p_vout, i_newvol );
233         }
234         else if( i_action == ACTIONID_VOL_MUTE )
235         {
236             audio_volume_t i_newvol = -1;
237             aout_VolumeMute( p_intf, &i_newvol );
238             if( p_vout )
239             {
240                 if( i_newvol == 0 )
241                 {
242                     ClearChannels( p_intf, p_vout );
243                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
244                                   OSD_MUTE_ICON );
245                 }
246                 else
247                 {
248                     DisplayVolume( p_intf, p_vout, i_newvol );
249                 }
250             }
251         }
252         /* Interface showing */
253         else if( i_action == ACTIONID_INTF_SHOW )
254             var_SetBool( p_intf->p_libvlc, "intf-show", true );
255         else if( i_action == ACTIONID_INTF_HIDE )
256             var_SetBool( p_intf->p_libvlc, "intf-show", false );
257         /* Video Output actions */
258         else if( i_action == ACTIONID_SNAPSHOT )
259         {
260             if( p_vout ) var_TriggerCallback( p_vout, "video-snapshot" );
261         }
262         else if( i_action == ACTIONID_TOGGLE_FULLSCREEN )
263         {
264             if( p_vout )
265             {
266                 var_Get( p_vout, "fullscreen", &val );
267                 val.b_bool = !val.b_bool;
268                 var_Set( p_vout, "fullscreen", val );
269             }
270             else
271             {
272                 var_Get( p_playlist, "fullscreen", &val );
273                 val.b_bool = !val.b_bool;
274                 var_Set( p_playlist, "fullscreen", val );
275             }
276         }
277         else if( i_action == ACTIONID_LEAVE_FULLSCREEN )
278         {
279             if( p_vout && var_GetBool( p_vout, "fullscreen" ) )
280             {
281                 var_SetBool( p_vout, "fullscreen", false );
282             }
283         }
284         else if( i_action == ACTIONID_ZOOM_QUARTER ||
285                  i_action == ACTIONID_ZOOM_HALF ||
286                  i_action == ACTIONID_ZOOM_ORIGINAL ||
287                  i_action == ACTIONID_ZOOM_DOUBLE )
288         {
289             if( p_vout )
290             {
291                 if( i_action == ACTIONID_ZOOM_QUARTER )
292                     val.f_float = 0.25;
293                 if( i_action == ACTIONID_ZOOM_HALF )
294                     val.f_float = 0.5;
295                 if( i_action == ACTIONID_ZOOM_ORIGINAL )
296                     val.f_float = 1;
297                 if( i_action == ACTIONID_ZOOM_DOUBLE )
298                     val.f_float = 2;
299                 var_Set( p_vout, "zoom", val );
300             }
301         }
302         else if( i_action == ACTIONID_WALLPAPER )
303         {
304             if( p_vout )
305             {
306                 var_Get( p_vout, "directx-wallpaper", &val );
307                 val.b_bool = !val.b_bool;
308                 var_Set( p_vout, "directx-wallpaper", val );
309             }
310             else
311             {
312                 var_Get( p_playlist, "directx-wallpaper", &val );
313                 val.b_bool = !val.b_bool;
314                 var_Set( p_playlist, "directx-wallpaper", val );
315             }
316         }
317         /* Playlist actions */
318         else if( i_action == ACTIONID_LOOP )
319         {
320             /* Toggle Normal -> Loop -> Repeat -> Normal ... */
321             vlc_value_t val2;
322             var_Get( p_playlist, "loop", &val );
323             var_Get( p_playlist, "repeat", &val2 );
324             if( val2.b_bool == true )
325             {
326                 val.b_bool = false;
327                 val2.b_bool = false;
328             }
329             else if( val.b_bool == true )
330             {
331                 val.b_bool = false;
332                 val2.b_bool = true;
333             }
334             else
335             {
336                 val.b_bool = true;
337             }
338             var_Set( p_playlist, "loop", val );
339             var_Set( p_playlist, "repeat", val2 );
340         }
341         else if( i_action == ACTIONID_RANDOM )
342         {
343             var_Get( p_playlist, "random", &val );
344             val.b_bool = !val.b_bool;
345             var_Set( p_playlist, "random", val );
346         }
347         else if( i_action == ACTIONID_PLAY_PAUSE )
348         {
349             val.i_int = PLAYING_S;
350             if( p_input )
351             {
352                 ClearChannels( p_intf, p_vout );
353
354                 var_Get( p_input, "state", &val );
355                 if( val.i_int != PAUSE_S )
356                 {
357                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
358                                   OSD_PAUSE_ICON );
359                     val.i_int = PAUSE_S;
360                 }
361                 else
362                 {
363                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
364                                   OSD_PLAY_ICON );
365                     val.i_int = PLAYING_S;
366                 }
367                 var_Set( p_input, "state", val );
368             }
369             else
370             {
371                 playlist_Play( p_playlist );
372             }
373         }
374         else if( i_action == ACTIONID_AUDIODEVICE_CYCLE )
375         {
376             vlc_value_t val, list, list2;
377             int i_count, i;
378
379             aout_instance_t *p_aout =
380                 vlc_object_find( p_intf, VLC_OBJECT_AOUT, FIND_ANYWHERE );
381             var_Get( p_aout, "audio-device", &val );
382             var_Change( p_aout, "audio-device", VLC_VAR_GETCHOICES,
383                     &list, &list2 );
384             i_count = list.p_list->i_count;
385
386             /* Not enough device to switch between */
387             if( i_count <= 1 )
388                 continue;
389
390             for( i = 0; i < i_count; i++ )
391             {
392                 if( val.i_int == list.p_list->p_values[i].i_int )
393                 {
394                     break;
395                 }
396             }
397             if( i == i_count )
398             {
399                 msg_Warn( p_aout,
400                         "invalid current audio device, selecting 0" );
401                 var_Set( p_aout, "audio-device",
402                         list.p_list->p_values[0] );
403                 i = 0;
404             }
405             else if( i == i_count -1 )
406             {
407                 var_Set( p_aout, "audio-device",
408                         list.p_list->p_values[0] );
409                 i = 0;
410             }
411             else
412             {
413                 var_Set( p_aout, "audio-device",
414                         list.p_list->p_values[i+1] );
415                 i++;
416             }
417             vout_OSDMessage( p_intf, DEFAULT_CHAN,
418                     _("Audio Device: %s"),
419                     list2.p_list->p_values[i].psz_string);
420             vlc_object_release( p_aout );
421         }
422         /* Input options */
423         else if( p_input )
424         {
425             bool b_seekable = var_GetBool( p_input, "can-seek" );
426             int i_interval =0;
427
428             if( i_action == ACTIONID_PAUSE )
429             {
430                 var_Get( p_input, "state", &val );
431                 if( val.i_int != PAUSE_S )
432                 {
433                     ClearChannels( p_intf, p_vout );
434                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
435                                   OSD_PAUSE_ICON );
436                     val.i_int = PAUSE_S;
437                     var_Set( p_input, "state", val );
438                 }
439             }
440             else if( i_action == ACTIONID_JUMP_BACKWARD_EXTRASHORT
441                      && b_seekable )
442             {
443 #define SET_TIME( a, b ) \
444     i_interval = config_GetInt( p_input, a "-jump-size" ); \
445     if( i_interval > 0 ) { \
446         val.i_time = (mtime_t)(i_interval * b) * 1000000L; \
447         var_Set( p_input, "time-offset", val ); \
448         DisplayPosition( p_intf, p_vout, p_input ); \
449     }
450                 SET_TIME( "extrashort", -1 );
451             }
452             else if( i_action == ACTIONID_JUMP_FORWARD_EXTRASHORT && b_seekable )
453             {
454                 SET_TIME( "extrashort", 1 );
455             }
456             else if( i_action == ACTIONID_JUMP_BACKWARD_SHORT && b_seekable )
457             {
458                 SET_TIME( "short", -1 );
459             }
460             else if( i_action == ACTIONID_JUMP_FORWARD_SHORT && b_seekable )
461             {
462                 SET_TIME( "short", 1 );
463             }
464             else if( i_action == ACTIONID_JUMP_BACKWARD_MEDIUM && b_seekable )
465             {
466                 SET_TIME( "medium", -1 );
467             }
468             else if( i_action == ACTIONID_JUMP_FORWARD_MEDIUM && b_seekable )
469             {
470                 SET_TIME( "medium", 1 );
471             }
472             else if( i_action == ACTIONID_JUMP_BACKWARD_LONG && b_seekable )
473             {
474                 SET_TIME( "long", -1 );
475             }
476             else if( i_action == ACTIONID_JUMP_FORWARD_LONG && b_seekable )
477             {
478                 SET_TIME( "long", 1 );
479 #undef SET_TIME
480             }
481             else if( i_action == ACTIONID_AUDIO_TRACK )
482             {
483                 vlc_value_t val, list, list2;
484                 int i_count, i;
485                 var_Get( p_input, "audio-es", &val );
486                 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
487                             &list, &list2 );
488                 i_count = list.p_list->i_count;
489                 if( i_count <= 1 )
490                 {
491                     continue;
492                 }
493                 for( i = 0; i < i_count; i++ )
494                 {
495                     if( val.i_int == list.p_list->p_values[i].i_int )
496                     {
497                         break;
498                     }
499                 }
500                 /* value of audio-es was not in choices list */
501                 if( i == i_count )
502                 {
503                     msg_Warn( p_input,
504                               "invalid current audio track, selecting 0" );
505                     i = 0;
506                 }
507                 else if( i == i_count - 1 )
508                     i = 1;
509                 else
510                     i++;
511                 var_Set( p_input, "audio-es", list.p_list->p_values[i] );
512                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
513                                  _("Audio track: %s"),
514                                  list2.p_list->p_values[i].psz_string );
515             }
516             else if( i_action == ACTIONID_SUBTITLE_TRACK )
517             {
518                 vlc_value_t val, list, list2;
519                 int i_count, i;
520                 var_Get( p_input, "spu-es", &val );
521
522                 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
523                             &list, &list2 );
524                 i_count = list.p_list->i_count;
525                 if( i_count <= 1 )
526                 {
527                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
528                                      _("Subtitle track: %s"), _("N/A") );
529                     continue;
530                 }
531                 for( i = 0; i < i_count; i++ )
532                 {
533                     if( val.i_int == list.p_list->p_values[i].i_int )
534                     {
535                         break;
536                     }
537                 }
538                 /* value of spu-es was not in choices list */
539                 if( i == i_count )
540                 {
541                     msg_Warn( p_input,
542                               "invalid current subtitle track, selecting 0" );
543                     i = 0;
544                 }
545                 else if( i == i_count - 1 )
546                     i = 0;
547                 else
548                     i++;
549                 var_Set( p_input, "spu-es", list.p_list->p_values[i] );
550                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
551                                  _("Subtitle track: %s"),
552                                  list2.p_list->p_values[i].psz_string );
553             }
554             else if( i_action == ACTIONID_ASPECT_RATIO && p_vout )
555             {
556                 vlc_value_t val={0}, val_list, text_list;
557                 var_Get( p_vout, "aspect-ratio", &val );
558                 if( var_Change( p_vout, "aspect-ratio", VLC_VAR_GETLIST,
559                                 &val_list, &text_list ) >= 0 )
560                 {
561                     int i;
562                     for( i = 0; i < val_list.p_list->i_count; i++ )
563                     {
564                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
565                                      val.psz_string ) )
566                         {
567                             i++;
568                             break;
569                         }
570                     }
571                     if( i == val_list.p_list->i_count ) i = 0;
572                     var_SetString( p_vout, "aspect-ratio",
573                                    val_list.p_list->p_values[i].psz_string );
574                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
575                                      _("Aspect ratio: %s"),
576                                      text_list.p_list->p_values[i].psz_string );
577
578                     var_Change( p_vout, "aspect-ratio", VLC_VAR_FREELIST, &val_list, &text_list );
579                 }
580                 free( val.psz_string );
581             }
582             else if( i_action == ACTIONID_CROP && p_vout )
583             {
584                 vlc_value_t val={0}, val_list, text_list;
585                 var_Get( p_vout, "crop", &val );
586                 if( var_Change( p_vout, "crop", VLC_VAR_GETLIST,
587                                 &val_list, &text_list ) >= 0 )
588                 {
589                     int i;
590                     for( i = 0; i < val_list.p_list->i_count; i++ )
591                     {
592                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
593                                      val.psz_string ) )
594                         {
595                             i++;
596                             break;
597                         }
598                     }
599                     if( i == val_list.p_list->i_count ) i = 0;
600                     var_SetString( p_vout, "crop",
601                                    val_list.p_list->p_values[i].psz_string );
602                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
603                                      _("Crop: %s"),
604                                      text_list.p_list->p_values[i].psz_string );
605
606                     var_Change( p_vout, "crop", VLC_VAR_FREELIST, &val_list, &text_list );
607                 }
608                 free( val.psz_string );
609             }
610             else if( i_action == ACTIONID_TOGGLE_AUTOSCALE && p_vout )
611             {
612                 float f_scalefactor = var_GetFloat( p_vout, "scale" );
613                 if ( f_scalefactor != 1.0 )
614                 {
615                     var_SetFloat( p_vout, "scale", 1.0 );
616                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
617                                          _("Zooming reset") );
618                 }
619                 else
620                 {
621                     bool b_autoscale = !var_GetBool( p_vout, "autoscale" );
622                     var_SetBool( p_vout, "autoscale", b_autoscale );
623                     if( b_autoscale )
624                         vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
625                                          _("Scaled to screen") );
626                     else
627                         vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
628                                          _("Original Size") );
629                 }
630             }
631             else if( i_action == ACTIONID_SCALE_UP && p_vout )
632             {
633                float f_scalefactor;
634
635                f_scalefactor = var_GetFloat( p_vout, "scale" );
636                if( f_scalefactor < 10. )
637                    f_scalefactor += .1;
638                var_SetFloat( p_vout, "scale", f_scalefactor );
639             }
640             else if( i_action == ACTIONID_SCALE_DOWN && p_vout )
641             {
642                float f_scalefactor;
643
644                f_scalefactor = var_GetFloat( p_vout, "scale" );
645                if( f_scalefactor > .3 )
646                    f_scalefactor -= .1;
647                var_SetFloat( p_vout, "scale", f_scalefactor );
648             }
649             else if( i_action == ACTIONID_DEINTERLACE && p_vout )
650             {
651                 vlc_value_t val={0}, val_list, text_list;
652                 var_Get( p_vout, "deinterlace", &val );
653                 if( var_Change( p_vout, "deinterlace", VLC_VAR_GETLIST,
654                                 &val_list, &text_list ) >= 0 )
655                 {
656                     int i;
657                     for( i = 0; i < val_list.p_list->i_count; i++ )
658                     {
659                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
660                                      val.psz_string ) )
661                         {
662                             i++;
663                             break;
664                         }
665                     }
666                     if( i == val_list.p_list->i_count ) i = 0;
667                     var_SetString( p_vout, "deinterlace",
668                                    val_list.p_list->p_values[i].psz_string );
669                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
670                                      _("Deinterlace mode: %s"),
671                                      text_list.p_list->p_values[i].psz_string );
672
673                     var_Change( p_vout, "deinterlace", VLC_VAR_FREELIST, &val_list, &text_list );
674                 }
675                 free( val.psz_string );
676             }
677             else if( ( i_action == ACTIONID_ZOOM || i_action == ACTIONID_UNZOOM ) && p_vout )
678             {
679                 vlc_value_t val={0}, val_list, text_list;
680                 var_Get( p_vout, "zoom", &val );
681                 if( var_Change( p_vout, "zoom", VLC_VAR_GETLIST,
682                                 &val_list, &text_list ) >= 0 )
683                 {
684                     int i;
685                     for( i = 0; i < val_list.p_list->i_count; i++ )
686                     {
687                         if( val_list.p_list->p_values[i].f_float
688                            == val.f_float )
689                         {
690                             if( i_action == ACTIONID_ZOOM )
691                                 i++;
692                             else /* ACTIONID_UNZOOM */
693                                 i--;
694                             break;
695                         }
696                     }
697                     if( i == val_list.p_list->i_count ) i = 0;
698                     if( i == -1 ) i = val_list.p_list->i_count-1;
699                     var_SetFloat( p_vout, "zoom",
700                                   val_list.p_list->p_values[i].f_float );
701                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
702                                      _("Zoom mode: %s"),
703                                 text_list.p_list->p_values[i].var.psz_name );
704
705                     var_Change( p_vout, "zoom", VLC_VAR_FREELIST, &val_list, &text_list );
706                 }
707             }
708             else if( i_action == ACTIONID_CROP_TOP && p_vout )
709                 var_IncInteger( p_vout, "crop-top" );
710             else if( i_action == ACTIONID_UNCROP_TOP && p_vout )
711                 var_DecInteger( p_vout, "crop-top" );
712             else if( i_action == ACTIONID_CROP_BOTTOM && p_vout )
713                 var_IncInteger( p_vout, "crop-bottom" );
714             else if( i_action == ACTIONID_UNCROP_BOTTOM && p_vout )
715                  var_DecInteger( p_vout, "crop-bottom" );
716             else if( i_action == ACTIONID_CROP_LEFT && p_vout )
717                  var_IncInteger( p_vout, "crop-left" );
718             else if( i_action == ACTIONID_UNCROP_LEFT && p_vout )
719                  var_DecInteger( p_vout, "crop-left" );
720             else if( i_action == ACTIONID_CROP_RIGHT && p_vout )
721                  var_IncInteger( p_vout, "crop-right" );
722             else if( i_action == ACTIONID_UNCROP_RIGHT && p_vout )
723                  var_DecInteger( p_vout, "crop-right" );
724
725             else if( i_action == ACTIONID_NEXT )
726             {
727                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN, _("Next") );
728                 playlist_Next( p_playlist );
729             }
730             else if( i_action == ACTIONID_PREV )
731             {
732                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
733                                  _("Previous") );
734                 playlist_Prev( p_playlist );
735             }
736             else if( i_action == ACTIONID_STOP )
737             {
738                 playlist_Stop( p_playlist );
739             }
740             else if( i_action == ACTIONID_FRAME_NEXT )
741             {
742                 var_SetVoid( p_input, "frame-next" );
743                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
744                                  _("Next frame") );
745             }
746             else if( i_action == ACTIONID_FASTER )
747             {
748                 var_SetVoid( p_input, "rate-faster" );
749                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
750                                  _("Faster") );
751             }
752             else if( i_action == ACTIONID_SLOWER )
753             {
754                 var_SetVoid( p_input, "rate-slower" );
755                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
756                                  _("Slower") );
757             }
758             else if( i_action == ACTIONID_RATE_NORMAL )
759             {
760                 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
761                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
762                                  _("1.00x") );
763             }
764             else if( i_action == ACTIONID_RATE_FASTER_FINE ||
765                      i_action == ACTIONID_RATE_SLOWER_FINE )
766             {
767                 /* The playback rate is defined by INPUT_RATE_DEFAULT / "rate"
768                  * and we want to increase/decrease it by 0.1 while making sure
769                  * that the resulting playback rate is a multiple of 0.1
770                  */
771                 int i_rate = var_GetInteger( p_input, "rate" );
772                 if( i_rate == 0 )
773                     i_rate = INPUT_RATE_MIN;
774                 int i_sign = i_rate < 0 ? -1 : 1;
775                 const int i_dir = i_action == ACTIONID_RATE_FASTER_FINE ? 1 : -1;
776
777                 const double f_speed = floor( ( (double)INPUT_RATE_DEFAULT / abs(i_rate) + 0.05 ) / 0.1 + i_dir ) * 0.1;
778                 if( f_speed <= (double)INPUT_RATE_DEFAULT / INPUT_RATE_MAX ) /* Needed to avoid infinity */
779                     i_rate = INPUT_RATE_MAX;
780                 else
781                     i_rate = INPUT_RATE_DEFAULT / f_speed + 0.5;
782
783                 i_rate = i_sign * __MIN( __MAX( i_rate, INPUT_RATE_MIN ), INPUT_RATE_MAX );
784
785                 var_SetInteger( p_input, "rate", i_rate );
786
787                 char psz_msg[7+1];
788                 snprintf( psz_msg, sizeof(psz_msg), _("%.2fx"), (double)INPUT_RATE_DEFAULT / i_rate );
789                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN, psz_msg );
790             }
791             else if( i_action == ACTIONID_POSITION && b_seekable )
792             {
793                 DisplayPosition( p_intf, p_vout, p_input );
794             }
795             else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
796                      i_action <= ACTIONID_PLAY_BOOKMARK10 )
797             {
798                 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
799             }
800             else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
801                      i_action <= ACTIONID_SET_BOOKMARK10 )
802             {
803                 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
804             }
805             /* Only makes sense with DVD */
806             else if( i_action == ACTIONID_TITLE_PREV )
807                 var_SetVoid( p_input, "prev-title" );
808             else if( i_action == ACTIONID_TITLE_NEXT )
809                 var_SetVoid( p_input, "next-title" );
810             else if( i_action == ACTIONID_CHAPTER_PREV )
811                 var_SetVoid( p_input, "prev-chapter" );
812             else if( i_action == ACTIONID_CHAPTER_NEXT )
813                 var_SetVoid( p_input, "next-chapter" );
814             else if( i_action == ACTIONID_DISC_MENU )
815                 var_SetInteger( p_input, "title  0", 2 );
816
817             else if( i_action == ACTIONID_SUBDELAY_DOWN )
818             {
819                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
820                 i_delay -= 50000;    /* 50 ms */
821                 var_SetTime( p_input, "spu-delay", i_delay );
822                 ClearChannels( p_intf, p_vout );
823                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
824                                  _( "Subtitle delay %i ms" ),
825                                  (int)(i_delay/1000) );
826             }
827             else if( i_action == ACTIONID_SUBDELAY_UP )
828             {
829                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
830                 i_delay += 50000;    /* 50 ms */
831                 var_SetTime( p_input, "spu-delay", i_delay );
832                 ClearChannels( p_intf, p_vout );
833                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
834                                 _( "Subtitle delay %i ms" ),
835                                  (int)(i_delay/1000) );
836             }
837             else if( i_action == ACTIONID_AUDIODELAY_DOWN )
838             {
839                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
840                 i_delay -= 50000;    /* 50 ms */
841                 var_SetTime( p_input, "audio-delay", i_delay );
842                 ClearChannels( p_intf, p_vout );
843                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
844                                 _( "Audio delay %i ms" ),
845                                  (int)(i_delay/1000) );
846             }
847             else if( i_action == ACTIONID_AUDIODELAY_UP )
848             {
849                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
850                 i_delay += 50000;    /* 50 ms */
851                 var_SetTime( p_input, "audio-delay", i_delay );
852                 ClearChannels( p_intf, p_vout );
853                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
854                                 _( "Audio delay %i ms" ),
855                                  (int)(i_delay/1000) );
856             }
857             else if( i_action == ACTIONID_PLAY )
858             {
859                 var_Get( p_input, "rate", &val );
860                 if( val.i_int != INPUT_RATE_DEFAULT )
861                 {
862                     /* Return to normal speed */
863                     var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
864                 }
865                 else
866                 {
867                     ClearChannels( p_intf, p_vout );
868                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
869                                   OSD_PLAY_ICON );
870                     playlist_Play( p_playlist );
871                 }
872             }
873             else if( i_action == ACTIONID_MENU_ON )
874             {
875                 osd_MenuShow( VLC_OBJECT(p_intf) );
876             }
877             else if( i_action == ACTIONID_MENU_OFF )
878             {
879                 osd_MenuHide( VLC_OBJECT(p_intf) );
880             }
881             else if( i_action == ACTIONID_MENU_LEFT )
882             {
883                 osd_MenuPrev( VLC_OBJECT(p_intf) );
884             }
885             else if( i_action == ACTIONID_MENU_RIGHT )
886             {
887                 osd_MenuNext( VLC_OBJECT(p_intf) );
888             }
889             else if( i_action == ACTIONID_MENU_UP )
890             {
891                 osd_MenuUp( VLC_OBJECT(p_intf) );
892             }
893             else if( i_action == ACTIONID_MENU_DOWN )
894             {
895                 osd_MenuDown( VLC_OBJECT(p_intf) );
896             }
897             else if( i_action == ACTIONID_MENU_SELECT )
898             {
899                 osd_MenuActivate( VLC_OBJECT(p_intf) );
900             }
901             else if( i_action == ACTIONID_RECORD )
902             {
903                 if( var_GetBool( p_input, "can-record" ) )
904                 {
905                     const bool b_record = !var_GetBool( p_input, "record" );
906
907                     if( b_record )
908                         vout_OSDMessage( p_intf, DEFAULT_CHAN, _("Recording") );
909                     else
910                         vout_OSDMessage( p_intf, DEFAULT_CHAN, _("Recording done") );
911                     var_SetBool( p_input, "record", b_record );
912                 }
913             }
914         }
915         if( p_vout )
916             vlc_object_release( p_vout );
917         if( p_input )
918             vlc_object_release( p_input );
919     }
920
921     /* dead code */
922     abort();
923     vlc_cleanup_pop();
924 }
925
926 static int GetAction( intf_thread_t *p_intf )
927 {
928     intf_sys_t *p_sys = p_intf->p_sys;
929     int i_ret;
930
931     vlc_mutex_lock( &p_sys->lock );
932     mutex_cleanup_push( &p_sys->lock );
933
934     while( p_sys->i_size == 0 )
935         vlc_cond_wait( &p_sys->wait, &p_sys->lock );
936
937     i_ret = p_sys->p_actions[ 0 ];
938     p_sys->i_size--;
939     for( int i = 0; i < p_sys->i_size; i++ )
940         p_sys->p_actions[i] = p_sys->p_actions[i + 1];
941
942     vlc_cleanup_run();
943     return i_ret;
944 }
945
946 static int PutAction( intf_thread_t *p_intf, int i_action )
947 {
948     intf_sys_t *p_sys = p_intf->p_sys;
949     int i_ret = VLC_EGENERIC;
950
951     vlc_mutex_lock( &p_sys->lock );
952     if ( p_sys->i_size >= BUFFER_SIZE )
953         msg_Warn( p_intf, "event buffer full, dropping key actions" );
954     else
955         p_sys->p_actions[p_sys->i_size++] = i_action;
956
957     vlc_cond_signal( &p_sys->wait );
958     vlc_mutex_unlock( &p_sys->lock );
959     return i_ret;
960 }
961
962 /*****************************************************************************
963  * SpecialKeyEvent: callback for mouse events
964  *****************************************************************************/
965 static int SpecialKeyEvent( vlc_object_t *libvlc, char const *psz_var,
966                             vlc_value_t oldval, vlc_value_t newval,
967                             void *p_data )
968 {
969     intf_thread_t *p_intf = (intf_thread_t *)p_data;
970     int i_action = 0;
971
972     (void)psz_var;
973     (void)oldval;
974
975     int i_mode = p_intf->p_sys->i_mousewheel_mode;
976
977     /* Special action for mouse event */
978     /* FIXME: rework hotkeys handling to allow more than 1 event
979      * to trigger one same action */
980     switch (newval.i_int & KEY_SPECIAL)
981     {
982         case KEY_MOUSEWHEELUP:
983             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ? ACTIONID_VOL_UP
984                                  : ACTIONID_JUMP_FORWARD_EXTRASHORT;
985             break;
986         case KEY_MOUSEWHEELDOWN:
987             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ? ACTIONID_VOL_DOWN
988                                 : ACTIONID_JUMP_BACKWARD_EXTRASHORT;
989             break;
990         case KEY_MOUSEWHEELLEFT:
991             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ?
992                         ACTIONID_JUMP_BACKWARD_EXTRASHORT : ACTIONID_VOL_DOWN;
993             break;
994         case KEY_MOUSEWHEELRIGHT:
995             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ?
996                         ACTIONID_JUMP_FORWARD_EXTRASHORT : ACTIONID_VOL_UP;
997             break;
998         case KEY_MENU:
999             var_SetBool( libvlc, "intf-popupmenu", true );
1000             break;
1001         default:
1002           return VLC_SUCCESS;
1003     }
1004
1005     if( i_mode == NO_MOUSEWHEEL ) return VLC_SUCCESS;
1006
1007     if( i_action )
1008         return PutAction( p_intf, i_action );
1009     return VLC_SUCCESS;
1010 }
1011
1012 /*****************************************************************************
1013  * ActionEvent: callback for hotkey actions
1014  *****************************************************************************/
1015 static int ActionEvent( vlc_object_t *libvlc, char const *psz_var,
1016                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1017 {
1018     intf_thread_t *p_intf = (intf_thread_t *)p_data;
1019
1020     (void)libvlc;
1021     (void)psz_var;
1022     (void)oldval;
1023
1024     return PutAction( p_intf, newval.i_int );
1025 }
1026
1027 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
1028 {
1029     char *psz_bookmark_name;
1030     if( asprintf( &psz_bookmark_name, "bookmark%i", i_num ) == -1 )
1031         return;
1032
1033     playlist_t *p_playlist = pl_Hold( p_intf );
1034     char *psz_bookmark = var_CreateGetString( p_intf, psz_bookmark_name );
1035
1036     PL_LOCK;
1037     FOREACH_ARRAY( playlist_item_t *p_item, p_playlist->items )
1038         char *psz_uri = input_item_GetURI( p_item->p_input );
1039         if( !strcmp( psz_bookmark, psz_uri ) )
1040         {
1041             free( psz_uri );
1042             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
1043                               NULL, p_item );
1044             break;
1045         }
1046         else
1047             free( psz_uri );
1048     FOREACH_END();
1049     PL_UNLOCK;
1050
1051     free( psz_bookmark );
1052     free( psz_bookmark_name );
1053     pl_Release( p_intf );
1054 }
1055
1056 static void SetBookmark( intf_thread_t *p_intf, int i_num )
1057 {
1058     char *psz_bookmark_name;
1059     if( asprintf( &psz_bookmark_name, "bookmark%i", i_num ) == -1 )
1060         return;
1061
1062     playlist_t *p_playlist = pl_Hold( p_intf );
1063     var_Create( p_intf, psz_bookmark_name,
1064                 VLC_VAR_STRING|VLC_VAR_DOINHERIT );
1065     playlist_item_t * p_item = playlist_CurrentPlayingItem( p_playlist );
1066     if( p_item )
1067     {
1068         char *psz_uri = input_item_GetURI( p_item->p_input );
1069         config_PutPsz( p_intf, psz_bookmark_name, psz_uri);
1070         msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num, psz_uri);
1071         free( psz_uri );
1072         config_SaveConfigFile( p_intf, "hotkeys" );
1073     }
1074
1075     pl_Release( p_intf );
1076     free( psz_bookmark_name );
1077 }
1078
1079 static void DisplayPosition( intf_thread_t *p_intf, vout_thread_t *p_vout,
1080                              input_thread_t *p_input )
1081 {
1082     char psz_duration[MSTRTIME_MAX_SIZE];
1083     char psz_time[MSTRTIME_MAX_SIZE];
1084     vlc_value_t time, pos;
1085     mtime_t i_seconds;
1086
1087     if( p_vout == NULL ) return;
1088
1089     ClearChannels( p_intf, p_vout );
1090
1091     var_Get( p_input, "time", &time );
1092     i_seconds = time.i_time / 1000000;
1093     secstotimestr ( psz_time, i_seconds );
1094
1095     var_Get( p_input, "length", &time );
1096     if( time.i_time > 0 )
1097     {
1098         secstotimestr( psz_duration, time.i_time / 1000000 );
1099         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, "%s / %s",
1100                          psz_time, psz_duration );
1101     }
1102     else if( i_seconds > 0 )
1103     {
1104         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, "%s", psz_time );
1105     }
1106
1107     if( p_vout->b_fullscreen )
1108     {
1109         var_Get( p_input, "position", &pos );
1110         vout_OSDSlider( VLC_OBJECT( p_input ), POSITION_WIDGET_CHAN,
1111                         pos.f_float * 100, OSD_HOR_SLIDER );
1112     }
1113 }
1114
1115 static void DisplayVolume( intf_thread_t *p_intf, vout_thread_t *p_vout,
1116                            audio_volume_t i_vol )
1117 {
1118     if( p_vout == NULL )
1119     {
1120         return;
1121     }
1122     ClearChannels( p_intf, p_vout );
1123
1124     if( p_vout->b_fullscreen )
1125     {
1126         vout_OSDSlider( VLC_OBJECT( p_vout ), VOLUME_WIDGET_CHAN,
1127             i_vol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
1128     }
1129     else
1130     {
1131         vout_OSDMessage( p_vout, VOLUME_TEXT_CHAN, _( "Volume %d%%" ),
1132                          i_vol*400/AOUT_VOLUME_MAX );
1133     }
1134 }
1135
1136 static void ClearChannels( intf_thread_t *p_intf, vout_thread_t *p_vout )
1137 {
1138     int i;
1139
1140     if( p_vout )
1141     {
1142         spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
1143         for( i = 0; i < CHANNELS_NUMBER; i++ )
1144         {
1145             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1146                          p_intf->p_sys->p_channels[ i ] );
1147         }
1148     }
1149 }