Added hotkeys for fine playback rate control.
[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     int i;
167     playlist_t *p_playlist = pl_Hold( p_intf );
168     int canc = vlc_savecancel();
169
170     vlc_cleanup_push( __pl_Release, p_intf );
171
172     /* Initialize hotkey structure */
173     for( struct hotkey *p_hotkey = p_intf->p_libvlc->p_hotkeys;
174          p_hotkey->psz_action != NULL;
175          p_hotkey++ )
176     {
177         p_hotkey->i_key = config_GetInt( p_intf, p_hotkey->psz_action );
178     }
179
180     for( ;; )
181     {
182         input_thread_t *p_input;
183         vout_thread_t *p_last_vout;
184         int i_action;
185
186         vlc_restorecancel( canc );
187         i_action = GetAction( p_intf );
188
189         canc = vlc_savecancel();
190
191         /* Update the input */
192         p_input = playlist_CurrentInput( p_playlist );
193
194         /* Update the vout */
195         p_last_vout = p_vout;
196         p_vout = p_input ? input_GetVout( p_input ) : NULL;
197
198         /* Register OSD channels */
199         if( p_vout && p_vout != p_last_vout )
200         {
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                     var_Set( p_input, "audio-es",
506                              list.p_list->p_values[0] );
507                     i = 0;
508                 }
509                 else if( i == i_count - 1 )
510                 {
511                     var_Set( p_input, "audio-es",
512                              list.p_list->p_values[1] );
513                     i = 1;
514                 }
515                 else
516                 {
517                     var_Set( p_input, "audio-es",
518                              list.p_list->p_values[i+1] );
519                     i++;
520                 }
521                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
522                                  _("Audio track: %s"),
523                                  list2.p_list->p_values[i].psz_string );
524             }
525             else if( i_action == ACTIONID_SUBTITLE_TRACK )
526             {
527                 vlc_value_t val, list, list2;
528                 int i_count, i;
529                 var_Get( p_input, "spu-es", &val );
530
531                 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
532                             &list, &list2 );
533                 i_count = list.p_list->i_count;
534                 if( i_count <= 1 )
535                 {
536                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
537                                      _("Subtitle track: %s"), _("N/A") );
538                     continue;
539                 }
540                 for( i = 0; i < i_count; i++ )
541                 {
542                     if( val.i_int == list.p_list->p_values[i].i_int )
543                     {
544                         break;
545                     }
546                 }
547                 /* value of spu-es was not in choices list */
548                 if( i == i_count )
549                 {
550                     msg_Warn( p_input,
551                               "invalid current subtitle track, selecting 0" );
552                     var_Set( p_input, "spu-es", list.p_list->p_values[0] );
553                     i = 0;
554                 }
555                 else if( i == i_count - 1 )
556                 {
557                     var_Set( p_input, "spu-es", list.p_list->p_values[0] );
558                     i = 0;
559                 }
560                 else
561                 {
562                     var_Set( p_input, "spu-es", list.p_list->p_values[i+1] );
563                     i = i + 1;
564                 }
565                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
566                                  _("Subtitle track: %s"),
567                                  list2.p_list->p_values[i].psz_string );
568             }
569             else if( i_action == ACTIONID_ASPECT_RATIO && p_vout )
570             {
571                 vlc_value_t val={0}, val_list, text_list;
572                 var_Get( p_vout, "aspect-ratio", &val );
573                 if( var_Change( p_vout, "aspect-ratio", VLC_VAR_GETLIST,
574                                 &val_list, &text_list ) >= 0 )
575                 {
576                     int i;
577                     for( i = 0; i < val_list.p_list->i_count; i++ )
578                     {
579                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
580                                      val.psz_string ) )
581                         {
582                             i++;
583                             break;
584                         }
585                     }
586                     if( i == val_list.p_list->i_count ) i = 0;
587                     var_SetString( p_vout, "aspect-ratio",
588                                    val_list.p_list->p_values[i].psz_string );
589                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
590                                      _("Aspect ratio: %s"),
591                                      text_list.p_list->p_values[i].psz_string );
592
593                     var_Change( p_vout, "aspect-ratio", VLC_VAR_FREELIST, &val_list, &text_list );
594                 }
595                 free( val.psz_string );
596             }
597             else if( i_action == ACTIONID_CROP && p_vout )
598             {
599                 vlc_value_t val={0}, val_list, text_list;
600                 var_Get( p_vout, "crop", &val );
601                 if( var_Change( p_vout, "crop", VLC_VAR_GETLIST,
602                                 &val_list, &text_list ) >= 0 )
603                 {
604                     int i;
605                     for( i = 0; i < val_list.p_list->i_count; i++ )
606                     {
607                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
608                                      val.psz_string ) )
609                         {
610                             i++;
611                             break;
612                         }
613                     }
614                     if( i == val_list.p_list->i_count ) i = 0;
615                     var_SetString( p_vout, "crop",
616                                    val_list.p_list->p_values[i].psz_string );
617                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
618                                      _("Crop: %s"),
619                                      text_list.p_list->p_values[i].psz_string );
620
621                     var_Change( p_vout, "crop", VLC_VAR_FREELIST, &val_list, &text_list );
622                 }
623                 free( val.psz_string );
624             }
625             else if( i_action == ACTIONID_TOGGLE_AUTOSCALE && p_vout )
626             {
627                 float f_scalefactor = var_GetFloat( p_vout, "scale" );
628                 if ( f_scalefactor != 1.0 )
629                 {
630                     var_SetFloat( p_vout, "scale", 1.0 );
631                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
632                                          _("Zooming reset") );
633                 }
634                 else
635                 {
636                     bool b_autoscale = !var_GetBool( p_vout, "autoscale" );
637                     var_SetBool( p_vout, "autoscale", b_autoscale );
638                     if( b_autoscale )
639                         vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
640                                          _("Scaled to screen") );
641                     else
642                         vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
643                                          _("Original Size") );
644                 }
645             }
646             else if( i_action == ACTIONID_SCALE_UP && p_vout )
647             {
648                float f_scalefactor;
649
650                f_scalefactor = var_GetFloat( p_vout, "scale" );
651                if( f_scalefactor < 10. )
652                    f_scalefactor += .1;
653                var_SetFloat( p_vout, "scale", f_scalefactor );
654             }
655             else if( i_action == ACTIONID_SCALE_DOWN && p_vout )
656             {
657                float f_scalefactor;
658
659                f_scalefactor = var_GetFloat( p_vout, "scale" );
660                if( f_scalefactor > .3 )
661                    f_scalefactor -= .1;
662                var_SetFloat( p_vout, "scale", f_scalefactor );
663             }
664             else if( i_action == ACTIONID_DEINTERLACE && p_vout )
665             {
666                 vlc_value_t val={0}, val_list, text_list;
667                 var_Get( p_vout, "deinterlace", &val );
668                 if( var_Change( p_vout, "deinterlace", VLC_VAR_GETLIST,
669                                 &val_list, &text_list ) >= 0 )
670                 {
671                     int i;
672                     for( i = 0; i < val_list.p_list->i_count; i++ )
673                     {
674                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
675                                      val.psz_string ) )
676                         {
677                             i++;
678                             break;
679                         }
680                     }
681                     if( i == val_list.p_list->i_count ) i = 0;
682                     var_SetString( p_vout, "deinterlace",
683                                    val_list.p_list->p_values[i].psz_string );
684                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
685                                      _("Deinterlace mode: %s"),
686                                      text_list.p_list->p_values[i].psz_string );
687
688                     var_Change( p_vout, "deinterlace", VLC_VAR_FREELIST, &val_list, &text_list );
689                 }
690                 free( val.psz_string );
691             }
692             else if( ( i_action == ACTIONID_ZOOM || i_action == ACTIONID_UNZOOM ) && p_vout )
693             {
694                 vlc_value_t val={0}, val_list, text_list;
695                 var_Get( p_vout, "zoom", &val );
696                 if( var_Change( p_vout, "zoom", VLC_VAR_GETLIST,
697                                 &val_list, &text_list ) >= 0 )
698                 {
699                     int i;
700                     for( i = 0; i < val_list.p_list->i_count; i++ )
701                     {
702                         if( val_list.p_list->p_values[i].f_float
703                            == val.f_float )
704                         {
705                             if( i_action == ACTIONID_ZOOM )
706                                 i++;
707                             else /* ACTIONID_UNZOOM */
708                                 i--;
709                             break;
710                         }
711                     }
712                     if( i == val_list.p_list->i_count ) i = 0;
713                     if( i == -1 ) i = val_list.p_list->i_count-1;
714                     var_SetFloat( p_vout, "zoom",
715                                   val_list.p_list->p_values[i].f_float );
716                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
717                                      _("Zoom mode: %s"),
718                                 text_list.p_list->p_values[i].var.psz_name );
719
720                     var_Change( p_vout, "zoom", VLC_VAR_FREELIST, &val_list, &text_list );
721                 }
722             }
723             else if( i_action == ACTIONID_CROP_TOP && p_vout )
724                 var_IncInteger( p_vout, "crop-top" );
725             else if( i_action == ACTIONID_UNCROP_TOP && p_vout )
726                 var_DecInteger( p_vout, "crop-top" );
727             else if( i_action == ACTIONID_CROP_BOTTOM && p_vout )
728                 var_IncInteger( p_vout, "crop-bottom" );
729             else if( i_action == ACTIONID_UNCROP_BOTTOM && p_vout )
730                  var_DecInteger( p_vout, "crop-bottom" );
731             else if( i_action == ACTIONID_CROP_LEFT && p_vout )
732                  var_IncInteger( p_vout, "crop-left" );
733             else if( i_action == ACTIONID_UNCROP_LEFT && p_vout )
734                  var_DecInteger( p_vout, "crop-left" );
735             else if( i_action == ACTIONID_CROP_RIGHT && p_vout )
736                  var_IncInteger( p_vout, "crop-right" );
737             else if( i_action == ACTIONID_UNCROP_RIGHT && p_vout )
738                  var_DecInteger( p_vout, "crop-right" );
739
740             else if( i_action == ACTIONID_NEXT )
741             {
742                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN, _("Next") );
743                 playlist_Next( p_playlist );
744             }
745             else if( i_action == ACTIONID_PREV )
746             {
747                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
748                                  _("Previous") );
749                 playlist_Prev( p_playlist );
750             }
751             else if( i_action == ACTIONID_STOP )
752             {
753                 playlist_Stop( p_playlist );
754             }
755             else if( i_action == ACTIONID_FRAME_NEXT )
756             {
757                 var_SetVoid( p_input, "frame-next" );
758                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
759                                  _("Next frame") );
760             }
761             else if( i_action == ACTIONID_FASTER )
762             {
763                 var_SetVoid( p_input, "rate-faster" );
764                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
765                                  _("Faster") );
766             }
767             else if( i_action == ACTIONID_SLOWER )
768             {
769                 var_SetVoid( p_input, "rate-slower" );
770                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
771                                  _("Slower") );
772             }
773             else if( i_action == ACTIONID_RATE_NORMAL )
774             {
775                 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
776                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
777                                  _("1.00x") );
778             }
779             else if( i_action == ACTIONID_RATE_FASTER_FINE ||
780                      i_action == ACTIONID_RATE_SLOWER_FINE )
781             {
782                 /* The playback rate is defined by INPUT_RATE_DEFAULT / "rate"
783                  * and we want to increase/decrease it by 0.1 while making sure
784                  * that the resulting playback rate is a multiple of 0.1
785                  */
786                 int i_rate = var_GetInteger( p_input, "rate" );
787                 if( i_rate == 0 )
788                     i_rate = INPUT_RATE_MIN;
789                 int i_sign = i_rate < 0 ? -1 : 1;
790                 const int i_dir = i_action == ACTIONID_RATE_FASTER_FINE ? 1 : -1;
791
792                 const double f_speed = floor( ( (double)INPUT_RATE_DEFAULT / abs(i_rate) + 0.05 ) / 0.1 + i_dir ) * 0.1;
793                 if( f_speed <= (double)INPUT_RATE_DEFAULT / INPUT_RATE_MAX ) /* Needed to avoid infinity */
794                     i_rate = INPUT_RATE_MAX;
795                 else
796                     i_rate = INPUT_RATE_DEFAULT / f_speed + 0.5;
797
798                 i_rate = i_sign * __MIN( __MAX( i_rate, INPUT_RATE_MIN ), INPUT_RATE_MAX );
799
800                 var_SetInteger( p_input, "rate", i_rate );
801
802                 char psz_msg[7+1];
803                 snprintf( psz_msg, sizeof(psz_msg), _("%.2fx"), (double)INPUT_RATE_DEFAULT / i_rate );
804                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN, psz_msg );
805             }
806             else if( i_action == ACTIONID_POSITION && b_seekable )
807             {
808                 DisplayPosition( p_intf, p_vout, p_input );
809             }
810             else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
811                      i_action <= ACTIONID_PLAY_BOOKMARK10 )
812             {
813                 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
814             }
815             else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
816                      i_action <= ACTIONID_SET_BOOKMARK10 )
817             {
818                 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
819             }
820             /* Only makes sense with DVD */
821             else if( i_action == ACTIONID_TITLE_PREV )
822                 var_SetVoid( p_input, "prev-title" );
823             else if( i_action == ACTIONID_TITLE_NEXT )
824                 var_SetVoid( p_input, "next-title" );
825             else if( i_action == ACTIONID_CHAPTER_PREV )
826                 var_SetVoid( p_input, "prev-chapter" );
827             else if( i_action == ACTIONID_CHAPTER_NEXT )
828                 var_SetVoid( p_input, "next-chapter" );
829             else if( i_action == ACTIONID_DISC_MENU )
830                 var_SetInteger( p_input, "title  0", 2 );
831
832             else if( i_action == ACTIONID_SUBDELAY_DOWN )
833             {
834                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
835                 i_delay -= 50000;    /* 50 ms */
836                 var_SetTime( p_input, "spu-delay", i_delay );
837                 ClearChannels( p_intf, p_vout );
838                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
839                                  _( "Subtitle delay %i ms" ),
840                                  (int)(i_delay/1000) );
841             }
842             else if( i_action == ACTIONID_SUBDELAY_UP )
843             {
844                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
845                 i_delay += 50000;    /* 50 ms */
846                 var_SetTime( p_input, "spu-delay", i_delay );
847                 ClearChannels( p_intf, p_vout );
848                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
849                                 _( "Subtitle delay %i ms" ),
850                                  (int)(i_delay/1000) );
851             }
852             else if( i_action == ACTIONID_AUDIODELAY_DOWN )
853             {
854                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
855                 i_delay -= 50000;    /* 50 ms */
856                 var_SetTime( p_input, "audio-delay", i_delay );
857                 ClearChannels( p_intf, p_vout );
858                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
859                                 _( "Audio delay %i ms" ),
860                                  (int)(i_delay/1000) );
861             }
862             else if( i_action == ACTIONID_AUDIODELAY_UP )
863             {
864                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
865                 i_delay += 50000;    /* 50 ms */
866                 var_SetTime( p_input, "audio-delay", i_delay );
867                 ClearChannels( p_intf, p_vout );
868                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
869                                 _( "Audio delay %i ms" ),
870                                  (int)(i_delay/1000) );
871             }
872             else if( i_action == ACTIONID_PLAY )
873             {
874                 var_Get( p_input, "rate", &val );
875                 if( val.i_int != INPUT_RATE_DEFAULT )
876                 {
877                     /* Return to normal speed */
878                     var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
879                 }
880                 else
881                 {
882                     ClearChannels( p_intf, p_vout );
883                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
884                                   OSD_PLAY_ICON );
885                     playlist_Play( p_playlist );
886                 }
887             }
888             else if( i_action == ACTIONID_MENU_ON )
889             {
890                 osd_MenuShow( VLC_OBJECT(p_intf) );
891             }
892             else if( i_action == ACTIONID_MENU_OFF )
893             {
894                 osd_MenuHide( VLC_OBJECT(p_intf) );
895             }
896             else if( i_action == ACTIONID_MENU_LEFT )
897             {
898                 osd_MenuPrev( VLC_OBJECT(p_intf) );
899             }
900             else if( i_action == ACTIONID_MENU_RIGHT )
901             {
902                 osd_MenuNext( VLC_OBJECT(p_intf) );
903             }
904             else if( i_action == ACTIONID_MENU_UP )
905             {
906                 osd_MenuUp( VLC_OBJECT(p_intf) );
907             }
908             else if( i_action == ACTIONID_MENU_DOWN )
909             {
910                 osd_MenuDown( VLC_OBJECT(p_intf) );
911             }
912             else if( i_action == ACTIONID_MENU_SELECT )
913             {
914                 osd_MenuActivate( VLC_OBJECT(p_intf) );
915             }
916             else if( i_action == ACTIONID_RECORD )
917             {
918                 if( var_GetBool( p_input, "can-record" ) )
919                 {
920                     const bool b_record = !var_GetBool( p_input, "record" );
921
922                     if( b_record )
923                         vout_OSDMessage( p_intf, DEFAULT_CHAN, _("Recording") );
924                     else
925                         vout_OSDMessage( p_intf, DEFAULT_CHAN, _("Recording done") );
926                     var_SetBool( p_input, "record", b_record );
927                 }
928             }
929         }
930         if( p_vout )
931             vlc_object_release( p_vout );
932         if( p_input )
933             vlc_object_release( p_input );
934     }
935
936     /* dead code */
937     abort();
938     vlc_cleanup_pop();
939 }
940
941 static int GetAction( intf_thread_t *p_intf )
942 {
943     intf_sys_t *p_sys = p_intf->p_sys;
944     int i_ret;
945
946     vlc_mutex_lock( &p_sys->lock );
947     mutex_cleanup_push( &p_sys->lock );
948
949     while( p_sys->i_size == 0 )
950         vlc_cond_wait( &p_sys->wait, &p_sys->lock );
951
952     i_ret = p_sys->p_actions[ 0 ];
953     p_sys->i_size--;
954     for( int i = 0; i < p_sys->i_size; i++ )
955         p_sys->p_actions[i] = p_sys->p_actions[i + 1];
956
957     vlc_cleanup_run();
958     return i_ret;
959 }
960
961 static int PutAction( intf_thread_t *p_intf, int i_action )
962 {
963     intf_sys_t *p_sys = p_intf->p_sys;
964     int i_ret = VLC_EGENERIC;
965
966     vlc_mutex_lock( &p_sys->lock );
967     if ( p_sys->i_size >= BUFFER_SIZE )
968         msg_Warn( p_intf, "event buffer full, dropping key actions" );
969     else
970         p_sys->p_actions[p_sys->i_size++] = i_action;
971
972     vlc_cond_signal( &p_sys->wait );
973     vlc_mutex_unlock( &p_sys->lock );
974     return i_ret;
975 }
976
977 /*****************************************************************************
978  * SpecialKeyEvent: callback for mouse events
979  *****************************************************************************/
980 static int SpecialKeyEvent( vlc_object_t *libvlc, char const *psz_var,
981                             vlc_value_t oldval, vlc_value_t newval,
982                             void *p_data )
983 {
984     intf_thread_t *p_intf = (intf_thread_t *)p_data;
985     int i_action = 0;
986
987     (void)psz_var;
988     (void)oldval;
989
990     int i_mode = p_intf->p_sys->i_mousewheel_mode;
991
992     /* Special action for mouse event */
993     /* FIXME: rework hotkeys handling to allow more than 1 event
994      * to trigger one same action */
995     switch (newval.i_int & KEY_SPECIAL)
996     {
997         case KEY_MOUSEWHEELUP:
998             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ? ACTIONID_VOL_UP
999                                  : ACTIONID_JUMP_FORWARD_EXTRASHORT;
1000             break;
1001         case KEY_MOUSEWHEELDOWN:
1002             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ? ACTIONID_VOL_DOWN
1003                                 : ACTIONID_JUMP_BACKWARD_EXTRASHORT;
1004             break;
1005         case KEY_MOUSEWHEELLEFT:
1006             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ?
1007                         ACTIONID_JUMP_BACKWARD_EXTRASHORT : ACTIONID_VOL_DOWN;
1008             break;
1009         case KEY_MOUSEWHEELRIGHT:
1010             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ?
1011                         ACTIONID_JUMP_FORWARD_EXTRASHORT : ACTIONID_VOL_UP;
1012             break;
1013         case KEY_MENU:
1014             var_SetBool( libvlc, "intf-popupmenu", true );
1015             break;
1016         default:
1017           return VLC_SUCCESS;
1018     }
1019
1020     if( i_mode == NO_MOUSEWHEEL ) return VLC_SUCCESS;
1021
1022     if( i_action )
1023         return PutAction( p_intf, i_action );
1024     return VLC_SUCCESS;
1025 }
1026
1027 /*****************************************************************************
1028  * ActionEvent: callback for hotkey actions
1029  *****************************************************************************/
1030 static int ActionEvent( vlc_object_t *libvlc, char const *psz_var,
1031                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1032 {
1033     intf_thread_t *p_intf = (intf_thread_t *)p_data;
1034
1035     (void)libvlc;
1036     (void)psz_var;
1037     (void)oldval;
1038
1039     return PutAction( p_intf, newval.i_int );
1040 }
1041
1042 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
1043 {
1044     char *psz_bookmark_name;
1045     if( asprintf( &psz_bookmark_name, "bookmark%i", i_num ) == -1 )
1046         return;
1047
1048     playlist_t *p_playlist = pl_Hold( p_intf );
1049     char *psz_bookmark = var_CreateGetString( p_intf, psz_bookmark_name );
1050
1051     PL_LOCK;
1052     FOREACH_ARRAY( playlist_item_t *p_item, p_playlist->items )
1053         char *psz_uri = input_item_GetURI( p_item->p_input );
1054         if( !strcmp( psz_bookmark, psz_uri ) )
1055         {
1056             free( psz_uri );
1057             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
1058                               NULL, p_item );
1059             break;
1060         }
1061         else
1062             free( psz_uri );
1063     FOREACH_END();
1064     PL_UNLOCK;
1065
1066     free( psz_bookmark );
1067     free( psz_bookmark_name );
1068     pl_Release( p_intf );
1069 }
1070
1071 static void SetBookmark( intf_thread_t *p_intf, int i_num )
1072 {
1073     char *psz_bookmark_name;
1074     if( asprintf( &psz_bookmark_name, "bookmark%i", i_num ) == -1 )
1075         return;
1076
1077     playlist_t *p_playlist = pl_Hold( p_intf );
1078     var_Create( p_intf, psz_bookmark_name,
1079                 VLC_VAR_STRING|VLC_VAR_DOINHERIT );
1080     playlist_item_t * p_item = playlist_CurrentPlayingItem( p_playlist );
1081     if( p_item )
1082     {
1083         char *psz_uri = input_item_GetURI( p_item->p_input );
1084         config_PutPsz( p_intf, psz_bookmark_name, psz_uri);
1085         msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num, psz_uri);
1086         free( psz_uri );
1087         config_SaveConfigFile( p_intf, "hotkeys" );
1088     }
1089
1090     pl_Release( p_intf );
1091     free( psz_bookmark_name );
1092 }
1093
1094 static void DisplayPosition( intf_thread_t *p_intf, vout_thread_t *p_vout,
1095                              input_thread_t *p_input )
1096 {
1097     char psz_duration[MSTRTIME_MAX_SIZE];
1098     char psz_time[MSTRTIME_MAX_SIZE];
1099     vlc_value_t time, pos;
1100     mtime_t i_seconds;
1101
1102     if( p_vout == NULL ) return;
1103
1104     ClearChannels( p_intf, p_vout );
1105
1106     var_Get( p_input, "time", &time );
1107     i_seconds = time.i_time / 1000000;
1108     secstotimestr ( psz_time, i_seconds );
1109
1110     var_Get( p_input, "length", &time );
1111     if( time.i_time > 0 )
1112     {
1113         secstotimestr( psz_duration, time.i_time / 1000000 );
1114         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, "%s / %s",
1115                          psz_time, psz_duration );
1116     }
1117     else if( i_seconds > 0 )
1118     {
1119         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, "%s", psz_time );
1120     }
1121
1122     if( p_vout->b_fullscreen )
1123     {
1124         var_Get( p_input, "position", &pos );
1125         vout_OSDSlider( VLC_OBJECT( p_input ), POSITION_WIDGET_CHAN,
1126                         pos.f_float * 100, OSD_HOR_SLIDER );
1127     }
1128 }
1129
1130 static void DisplayVolume( intf_thread_t *p_intf, vout_thread_t *p_vout,
1131                            audio_volume_t i_vol )
1132 {
1133     if( p_vout == NULL )
1134     {
1135         return;
1136     }
1137     ClearChannels( p_intf, p_vout );
1138
1139     if( p_vout->b_fullscreen )
1140     {
1141         vout_OSDSlider( VLC_OBJECT( p_vout ), VOLUME_WIDGET_CHAN,
1142             i_vol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
1143     }
1144     else
1145     {
1146         vout_OSDMessage( p_vout, VOLUME_TEXT_CHAN, _( "Volume %d%%" ),
1147                          i_vol*400/AOUT_VOLUME_MAX );
1148     }
1149 }
1150
1151 static void ClearChannels( intf_thread_t *p_intf, vout_thread_t *p_vout )
1152 {
1153     int i;
1154
1155     if( p_vout )
1156     {
1157         spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
1158         for( i = 0; i < CHANNELS_NUMBER; i++ )
1159         {
1160             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1161                          p_intf->p_sys->p_channels[ i ] );
1162         }
1163     }
1164 }