4447e1773110f0c2da20a22686216d6fbcfb181b
[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     aout_instance_t *p_aout = NULL;
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     for( ;; )
172     {
173         input_thread_t *p_input;
174         vout_thread_t *p_last_vout;
175         int i_action;
176
177         vlc_restorecancel( canc );
178         i_action = GetAction( p_intf );
179
180         canc = vlc_savecancel();
181
182         /* Update the input */
183         p_input = playlist_CurrentInput( p_playlist );
184
185         /* Update the vout */
186         p_last_vout = p_vout;
187         p_vout = p_input ? input_GetVout( p_input ) : NULL;
188
189         /* Update the aout */
190         p_aout = p_input ? input_GetAout( p_input ) : NULL;
191
192         /* Register OSD channels */
193         if( p_vout && p_vout != p_last_vout )
194         {
195             int i;
196             for( i = 0; i < CHANNELS_NUMBER; i++ )
197             {
198                 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
199                              &p_intf->p_sys->p_channels[ i ] );
200             }
201         }
202
203         /* Quit */
204         if( i_action == ACTIONID_QUIT )
205         {
206             libvlc_Quit( p_intf->p_libvlc );
207
208             ClearChannels( p_intf, p_vout );
209             vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Quit" ) );
210             goto cleanup_and_continue;
211         }
212         /* Volume and audio actions */
213         else if( i_action == ACTIONID_VOL_UP )
214         {
215             audio_volume_t i_newvol;
216             aout_VolumeUp( p_intf, 1, &i_newvol );
217             DisplayVolume( p_intf, p_vout, i_newvol );
218         }
219         else if( i_action == ACTIONID_VOL_DOWN )
220         {
221             audio_volume_t i_newvol;
222             aout_VolumeDown( p_intf, 1, &i_newvol );
223             DisplayVolume( p_intf, p_vout, i_newvol );
224         }
225         else if( i_action == ACTIONID_VOL_MUTE )
226         {
227             audio_volume_t i_newvol = -1;
228             aout_VolumeMute( p_intf, &i_newvol );
229             if( p_vout )
230             {
231                 if( i_newvol == 0 )
232                 {
233                     ClearChannels( p_intf, p_vout );
234                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
235                                   OSD_MUTE_ICON );
236                 }
237                 else
238                 {
239                     DisplayVolume( p_intf, p_vout, i_newvol );
240                 }
241             }
242         }
243         /* Interface showing */
244         else if( i_action == ACTIONID_INTF_SHOW )
245             var_SetBool( p_intf->p_libvlc, "intf-show", true );
246         else if( i_action == ACTIONID_INTF_HIDE )
247             var_SetBool( p_intf->p_libvlc, "intf-show", false );
248         /* Video Output actions */
249         else if( i_action == ACTIONID_SNAPSHOT )
250         {
251             if( p_vout ) var_TriggerCallback( p_vout, "video-snapshot" );
252         }
253         else if( i_action == ACTIONID_TOGGLE_FULLSCREEN )
254         {
255             vlc_object_t *obj = p_vout ? VLC_OBJECT(p_vout)
256                                        : VLC_OBJECT(p_playlist);
257             bool b = var_GetBool( obj, "fullscreen" );
258             var_SetBool( obj, "fullscreen", !b );
259         }
260         else if( i_action == ACTIONID_LEAVE_FULLSCREEN )
261         {
262             if( p_vout && var_GetBool( p_vout, "fullscreen" ) )
263             {
264                 var_SetBool( p_vout, "fullscreen", false );
265             }
266         }
267         else if( i_action == ACTIONID_ZOOM_QUARTER ||
268                  i_action == ACTIONID_ZOOM_HALF ||
269                  i_action == ACTIONID_ZOOM_ORIGINAL ||
270                  i_action == ACTIONID_ZOOM_DOUBLE )
271         {
272             if( p_vout )
273             {
274                 float f;
275                 switch( i_action )
276                 {
277                     case ACTIONID_ZOOM_QUARTER:  f = 0.25; break;
278                     case ACTIONID_ZOOM_HALF:     f = 0.5;  break;
279                     case ACTIONID_ZOOM_ORIGINAL: f = 1.;   break;
280                      /*case ACTIONID_ZOOM_DOUBLE:*/
281                     default:                     f = 2.;   break;
282                 }
283                 var_SetFloat( p_vout, "zoom", f );
284             }
285         }
286 #ifdef WIN32
287         else if( i_action == ACTIONID_WALLPAPER )
288         {   /* FIXME: this is invalid if not using DirectX output!!! */
289             vlc_object_t *obj = p_vout ? VLC_OBJECT(p_vout)
290                                        : VLC_OBJECT(p_playlist);
291             bool b = var_GetBool( obj, "directx-wallpaper" );
292             var_SetBool( obj, "directx-wallpaper", !b );
293         }
294 #endif
295         /* Playlist actions */
296         else if( i_action == ACTIONID_LOOP )
297         {
298             /* Toggle Normal -> Loop -> Repeat -> Normal ... */
299             if( var_GetBool( p_playlist, "repeat" ) )
300                 var_SetBool( p_playlist, "repeat", false );
301             else
302             if( var_GetBool( p_playlist, "loop" ) )
303             { /* FIXME: this is not atomic, we should use a real tristate */
304                 var_SetBool( p_playlist, "loop", false );
305                 var_SetBool( p_playlist, "repeat", true );
306             }
307             else
308                 var_SetBool( p_playlist, "loop", true );
309         }
310         else if( i_action == ACTIONID_RANDOM )
311         {
312             bool b = var_GetBool( p_playlist, "random" );
313             var_SetBool( p_playlist, "random", !b );
314         }
315         else if( i_action == ACTIONID_PLAY_PAUSE )
316         {
317             if( p_input )
318             {
319                 ClearChannels( p_intf, p_vout );
320
321                 int state = var_GetInteger( p_input, "state" );
322                 if( state != PAUSE_S )
323                 {
324                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
325                                   OSD_PAUSE_ICON );
326                     state = PAUSE_S;
327                 }
328                 else
329                 {
330                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
331                                   OSD_PLAY_ICON );
332                     state = PLAYING_S;
333                 }
334                 var_SetInteger( p_input, "state", state );
335             }
336             else
337             {
338                 playlist_Play( p_playlist );
339             }
340         }
341         else if( ( i_action == ACTIONID_AUDIODEVICE_CYCLE ) && p_aout )
342         {
343             vlc_value_t val, list, list2;
344             int i_count, i;
345
346             var_Get( p_aout, "audio-device", &val );
347             var_Change( p_aout, "audio-device", VLC_VAR_GETCHOICES,
348                     &list, &list2 );
349             i_count = list.p_list->i_count;
350
351             if( i_count > 1 )
352             {
353                 for( i = 0; i < i_count; i++ )
354                 {
355                     if( val.i_int == list.p_list->p_values[i].i_int )
356                     {
357                         break;
358                     }
359                 }
360                 if( i == i_count )
361                 {
362                     msg_Warn( p_aout,
363                             "invalid current audio device, selecting 0" );
364                     var_Set( p_aout, "audio-device",
365                             list.p_list->p_values[0] );
366                     i = 0;
367                 }
368                 else if( i == i_count -1 )
369                 {
370                     var_Set( p_aout, "audio-device",
371                             list.p_list->p_values[0] );
372                     i = 0;
373                 }
374                 else
375                 {
376                     var_Set( p_aout, "audio-device",
377                             list.p_list->p_values[i+1] );
378                     i++;
379                 }
380                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
381                         _("Audio Device: %s"),
382                         list2.p_list->p_values[i].psz_string);
383             }
384             var_FreeList( &list, &list2 );
385         }
386         /* Input options */
387         else if( p_input )
388         {
389             bool b_seekable = var_GetBool( p_input, "can-seek" );
390             int i_interval =0;
391
392             if( i_action == ACTIONID_PAUSE )
393             {
394                 if( var_GetInteger( p_input, "state" ) != PAUSE_S )
395                 {
396                     ClearChannels( p_intf, p_vout );
397                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
398                                   OSD_PAUSE_ICON );
399                     var_SetInteger( p_input, "state", PAUSE_S );
400                 }
401             }
402             else if( i_action == ACTIONID_JUMP_BACKWARD_EXTRASHORT
403                      && b_seekable )
404             {
405 #define SET_TIME( a, b ) \
406     i_interval = config_GetInt( p_input, a "-jump-size" ); \
407     if( i_interval > 0 ) { \
408         mtime_t i_time = (mtime_t)(i_interval * b) * 1000000L; \
409         var_SetTime( p_input, "time-offset", i_time ); \
410         DisplayPosition( p_intf, p_vout, p_input ); \
411     }
412                 SET_TIME( "extrashort", -1 );
413             }
414             else if( i_action == ACTIONID_JUMP_FORWARD_EXTRASHORT && b_seekable )
415             {
416                 SET_TIME( "extrashort", 1 );
417             }
418             else if( i_action == ACTIONID_JUMP_BACKWARD_SHORT && b_seekable )
419             {
420                 SET_TIME( "short", -1 );
421             }
422             else if( i_action == ACTIONID_JUMP_FORWARD_SHORT && b_seekable )
423             {
424                 SET_TIME( "short", 1 );
425             }
426             else if( i_action == ACTIONID_JUMP_BACKWARD_MEDIUM && b_seekable )
427             {
428                 SET_TIME( "medium", -1 );
429             }
430             else if( i_action == ACTIONID_JUMP_FORWARD_MEDIUM && b_seekable )
431             {
432                 SET_TIME( "medium", 1 );
433             }
434             else if( i_action == ACTIONID_JUMP_BACKWARD_LONG && b_seekable )
435             {
436                 SET_TIME( "long", -1 );
437             }
438             else if( i_action == ACTIONID_JUMP_FORWARD_LONG && b_seekable )
439             {
440                 SET_TIME( "long", 1 );
441 #undef SET_TIME
442             }
443             else if( i_action == ACTIONID_AUDIO_TRACK )
444             {
445                 vlc_value_t val, list, list2;
446                 int i_count, i;
447                 var_Get( p_input, "audio-es", &val );
448                 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
449                             &list, &list2 );
450                 i_count = list.p_list->i_count;
451                 if( i_count > 1 )
452                 {
453                     for( i = 0; i < i_count; i++ )
454                     {
455                         if( val.i_int == list.p_list->p_values[i].i_int )
456                         {
457                             break;
458                         }
459                     }
460                     /* value of audio-es was not in choices list */
461                     if( i == i_count )
462                     {
463                         msg_Warn( p_input,
464                                   "invalid current audio track, selecting 0" );
465                         i = 0;
466                     }
467                     else if( i == i_count - 1 )
468                         i = 1;
469                     else
470                         i++;
471                     var_Set( p_input, "audio-es", list.p_list->p_values[i] );
472                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
473                                      _("Audio track: %s"),
474                                      list2.p_list->p_values[i].psz_string );
475                 }
476                 var_FreeList( &list, &list2 );
477             }
478             else if( i_action == ACTIONID_SUBTITLE_TRACK )
479             {
480                 vlc_value_t val, list, list2;
481                 int i_count, i;
482                 var_Get( p_input, "spu-es", &val );
483
484                 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
485                             &list, &list2 );
486                 i_count = list.p_list->i_count;
487                 if( i_count <= 1 )
488                 {
489                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
490                                      _("Subtitle track: %s"), _("N/A") );
491                     var_FreeList( &list, &list2 );
492                     goto cleanup_and_continue;
493                 }
494                 for( i = 0; i < i_count; i++ )
495                 {
496                     if( val.i_int == list.p_list->p_values[i].i_int )
497                     {
498                         break;
499                     }
500                 }
501                 /* value of spu-es was not in choices list */
502                 if( i == i_count )
503                 {
504                     msg_Warn( p_input,
505                               "invalid current subtitle track, selecting 0" );
506                     i = 0;
507                 }
508                 else if( i == i_count - 1 )
509                     i = 0;
510                 else
511                     i++;
512                 var_Set( p_input, "spu-es", list.p_list->p_values[i] );
513                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
514                                  _("Subtitle track: %s"),
515                                  list2.p_list->p_values[i].psz_string );
516                 var_FreeList( &list, &list2 );
517             }
518             else if( i_action == ACTIONID_ASPECT_RATIO && p_vout )
519             {
520                 vlc_value_t val={0}, val_list, text_list;
521                 var_Get( p_vout, "aspect-ratio", &val );
522                 if( var_Change( p_vout, "aspect-ratio", VLC_VAR_GETLIST,
523                                 &val_list, &text_list ) >= 0 )
524                 {
525                     int i;
526                     for( i = 0; i < val_list.p_list->i_count; i++ )
527                     {
528                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
529                                      val.psz_string ) )
530                         {
531                             i++;
532                             break;
533                         }
534                     }
535                     if( i == val_list.p_list->i_count ) i = 0;
536                     var_SetString( p_vout, "aspect-ratio",
537                                    val_list.p_list->p_values[i].psz_string );
538                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
539                                      _("Aspect ratio: %s"),
540                                      text_list.p_list->p_values[i].psz_string );
541
542                     var_FreeList( &val_list, &text_list );
543                 }
544                 free( val.psz_string );
545             }
546             else if( i_action == ACTIONID_CROP && p_vout )
547             {
548                 vlc_value_t val={0}, val_list, text_list;
549                 var_Get( p_vout, "crop", &val );
550                 if( var_Change( p_vout, "crop", VLC_VAR_GETLIST,
551                                 &val_list, &text_list ) >= 0 )
552                 {
553                     int i;
554                     for( i = 0; i < val_list.p_list->i_count; i++ )
555                     {
556                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
557                                      val.psz_string ) )
558                         {
559                             i++;
560                             break;
561                         }
562                     }
563                     if( i == val_list.p_list->i_count ) i = 0;
564                     var_SetString( p_vout, "crop",
565                                    val_list.p_list->p_values[i].psz_string );
566                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
567                                      _("Crop: %s"),
568                                      text_list.p_list->p_values[i].psz_string );
569
570                     var_FreeList( &val_list, &text_list );
571                 }
572                 free( val.psz_string );
573             }
574             else if( i_action == ACTIONID_TOGGLE_AUTOSCALE && p_vout )
575             {
576                 float f_scalefactor = var_GetFloat( p_vout, "scale" );
577                 if ( f_scalefactor != 1.0 )
578                 {
579                     var_SetFloat( p_vout, "scale", 1.0 );
580                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
581                                          _("Zooming reset") );
582                 }
583                 else
584                 {
585                     bool b_autoscale = !var_GetBool( p_vout, "autoscale" );
586                     var_SetBool( p_vout, "autoscale", b_autoscale );
587                     if( b_autoscale )
588                         vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
589                                          _("Scaled to screen") );
590                     else
591                         vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
592                                          _("Original Size") );
593                 }
594             }
595             else if( i_action == ACTIONID_SCALE_UP && p_vout )
596             {
597                float f_scalefactor;
598
599                f_scalefactor = var_GetFloat( p_vout, "scale" );
600                if( f_scalefactor < 10. )
601                    f_scalefactor += .1;
602                var_SetFloat( p_vout, "scale", f_scalefactor );
603             }
604             else if( i_action == ACTIONID_SCALE_DOWN && p_vout )
605             {
606                float f_scalefactor;
607
608                f_scalefactor = var_GetFloat( p_vout, "scale" );
609                if( f_scalefactor > .3 )
610                    f_scalefactor -= .1;
611                var_SetFloat( p_vout, "scale", f_scalefactor );
612             }
613             else if( i_action == ACTIONID_DEINTERLACE && p_vout )
614             {
615                 vlc_value_t val={0}, val_list, text_list;
616                 var_Get( p_vout, "deinterlace", &val );
617                 if( var_Change( p_vout, "deinterlace", VLC_VAR_GETLIST,
618                                 &val_list, &text_list ) >= 0 )
619                 {
620                     int i;
621                     for( i = 0; i < val_list.p_list->i_count; i++ )
622                     {
623                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
624                                      val.psz_string ) )
625                         {
626                             i++;
627                             break;
628                         }
629                     }
630                     if( i == val_list.p_list->i_count ) i = 0;
631                     var_SetString( p_vout, "deinterlace",
632                                    val_list.p_list->p_values[i].psz_string );
633                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
634                                      _("Deinterlace mode: %s"),
635                                      text_list.p_list->p_values[i].psz_string );
636
637                     var_FreeList( &val_list, &text_list );
638                 }
639                 free( val.psz_string );
640             }
641             else if( ( i_action == ACTIONID_ZOOM || i_action == ACTIONID_UNZOOM ) && p_vout )
642             {
643                 vlc_value_t val={0}, val_list, text_list;
644                 var_Get( p_vout, "zoom", &val );
645                 if( var_Change( p_vout, "zoom", VLC_VAR_GETLIST,
646                                 &val_list, &text_list ) >= 0 )
647                 {
648                     int i;
649                     for( i = 0; i < val_list.p_list->i_count; i++ )
650                     {
651                         if( val_list.p_list->p_values[i].f_float
652                            == val.f_float )
653                         {
654                             if( i_action == ACTIONID_ZOOM )
655                                 i++;
656                             else /* ACTIONID_UNZOOM */
657                                 i--;
658                             break;
659                         }
660                     }
661                     if( i == val_list.p_list->i_count ) i = 0;
662                     if( i == -1 ) i = val_list.p_list->i_count-1;
663                     var_SetFloat( p_vout, "zoom",
664                                   val_list.p_list->p_values[i].f_float );
665                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
666                                      _("Zoom mode: %s"),
667                                 text_list.p_list->p_values[i].var.psz_name );
668
669                     var_FreeList( &val_list, &text_list );
670                 }
671             }
672             else if( i_action == ACTIONID_CROP_TOP && p_vout )
673                 var_IncInteger( p_vout, "crop-top" );
674             else if( i_action == ACTIONID_UNCROP_TOP && p_vout )
675                 var_DecInteger( p_vout, "crop-top" );
676             else if( i_action == ACTIONID_CROP_BOTTOM && p_vout )
677                 var_IncInteger( p_vout, "crop-bottom" );
678             else if( i_action == ACTIONID_UNCROP_BOTTOM && p_vout )
679                  var_DecInteger( p_vout, "crop-bottom" );
680             else if( i_action == ACTIONID_CROP_LEFT && p_vout )
681                  var_IncInteger( p_vout, "crop-left" );
682             else if( i_action == ACTIONID_UNCROP_LEFT && p_vout )
683                  var_DecInteger( p_vout, "crop-left" );
684             else if( i_action == ACTIONID_CROP_RIGHT && p_vout )
685                  var_IncInteger( p_vout, "crop-right" );
686             else if( i_action == ACTIONID_UNCROP_RIGHT && p_vout )
687                  var_DecInteger( p_vout, "crop-right" );
688
689             else if( i_action == ACTIONID_NEXT )
690             {
691                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN, _("Next") );
692                 playlist_Next( p_playlist );
693             }
694             else if( i_action == ACTIONID_PREV )
695             {
696                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
697                                  _("Previous") );
698                 playlist_Prev( p_playlist );
699             }
700             else if( i_action == ACTIONID_STOP )
701             {
702                 playlist_Stop( p_playlist );
703             }
704             else if( i_action == ACTIONID_FRAME_NEXT )
705             {
706                 var_SetVoid( p_input, "frame-next" );
707                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
708                                  _("Next frame") );
709             }
710             else if( i_action == ACTIONID_FASTER )
711             {
712                 var_SetVoid( p_input, "rate-faster" );
713                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
714                                  _("Faster") );
715             }
716             else if( i_action == ACTIONID_SLOWER )
717             {
718                 var_SetVoid( p_input, "rate-slower" );
719                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
720                                  _("Slower") );
721             }
722             else if( i_action == ACTIONID_RATE_NORMAL )
723             {
724                 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
725                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
726                                  _("1.00x") );
727             }
728             else if( i_action == ACTIONID_RATE_FASTER_FINE ||
729                      i_action == ACTIONID_RATE_SLOWER_FINE )
730             {
731                 /* The playback rate is defined by INPUT_RATE_DEFAULT / "rate"
732                  * and we want to increase/decrease it by 0.1 while making sure
733                  * that the resulting playback rate is a multiple of 0.1
734                  */
735                 int i_rate = var_GetInteger( p_input, "rate" );
736                 if( i_rate == 0 )
737                     i_rate = INPUT_RATE_MIN;
738                 int i_sign = i_rate < 0 ? -1 : 1;
739                 const int i_dir = i_action == ACTIONID_RATE_FASTER_FINE ? 1 : -1;
740
741                 const double f_speed = floor( ( (double)INPUT_RATE_DEFAULT / abs(i_rate) + 0.05 ) / 0.1 + i_dir ) * 0.1;
742                 if( f_speed <= (double)INPUT_RATE_DEFAULT / INPUT_RATE_MAX ) /* Needed to avoid infinity */
743                     i_rate = INPUT_RATE_MAX;
744                 else
745                     i_rate = INPUT_RATE_DEFAULT / f_speed + 0.5;
746
747                 i_rate = i_sign * __MIN( __MAX( i_rate, INPUT_RATE_MIN ), INPUT_RATE_MAX );
748
749                 var_SetInteger( p_input, "rate", i_rate );
750
751                 char psz_msg[7+1];
752                 snprintf( psz_msg, sizeof(psz_msg), _("%.2fx"), (double)INPUT_RATE_DEFAULT / i_rate );
753                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN, psz_msg );
754             }
755             else if( i_action == ACTIONID_POSITION && b_seekable )
756             {
757                 DisplayPosition( p_intf, p_vout, p_input );
758             }
759             else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
760                      i_action <= ACTIONID_PLAY_BOOKMARK10 )
761             {
762                 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
763             }
764             else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
765                      i_action <= ACTIONID_SET_BOOKMARK10 )
766             {
767                 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
768             }
769             /* Only makes sense with DVD */
770             else if( i_action == ACTIONID_TITLE_PREV )
771                 var_SetVoid( p_input, "prev-title" );
772             else if( i_action == ACTIONID_TITLE_NEXT )
773                 var_SetVoid( p_input, "next-title" );
774             else if( i_action == ACTIONID_CHAPTER_PREV )
775                 var_SetVoid( p_input, "prev-chapter" );
776             else if( i_action == ACTIONID_CHAPTER_NEXT )
777                 var_SetVoid( p_input, "next-chapter" );
778             else if( i_action == ACTIONID_DISC_MENU )
779                 var_SetInteger( p_input, "title  0", 2 );
780
781             else if( i_action == ACTIONID_SUBDELAY_DOWN )
782             {
783                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
784                 i_delay -= 50000;    /* 50 ms */
785                 var_SetTime( p_input, "spu-delay", i_delay );
786                 ClearChannels( p_intf, p_vout );
787                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
788                                  _( "Subtitle delay %i ms" ),
789                                  (int)(i_delay/1000) );
790             }
791             else if( i_action == ACTIONID_SUBDELAY_UP )
792             {
793                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
794                 i_delay += 50000;    /* 50 ms */
795                 var_SetTime( p_input, "spu-delay", i_delay );
796                 ClearChannels( p_intf, p_vout );
797                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
798                                 _( "Subtitle delay %i ms" ),
799                                  (int)(i_delay/1000) );
800             }
801             else if( i_action == ACTIONID_AUDIODELAY_DOWN )
802             {
803                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
804                 i_delay -= 50000;    /* 50 ms */
805                 var_SetTime( p_input, "audio-delay", i_delay );
806                 ClearChannels( p_intf, p_vout );
807                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
808                                 _( "Audio delay %i ms" ),
809                                  (int)(i_delay/1000) );
810             }
811             else if( i_action == ACTIONID_AUDIODELAY_UP )
812             {
813                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
814                 i_delay += 50000;    /* 50 ms */
815                 var_SetTime( p_input, "audio-delay", i_delay );
816                 ClearChannels( p_intf, p_vout );
817                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
818                                 _( "Audio delay %i ms" ),
819                                  (int)(i_delay/1000) );
820             }
821             else if( i_action == ACTIONID_PLAY )
822             {
823                 if( var_GetInteger( p_input, "rate" ) != INPUT_RATE_DEFAULT )
824                     /* Return to normal speed */
825                     var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
826                 else
827                 {
828                     ClearChannels( p_intf, p_vout );
829                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
830                                   OSD_PLAY_ICON );
831                     playlist_Play( p_playlist );
832                 }
833             }
834             else if( i_action == ACTIONID_MENU_ON )
835             {
836                 osd_MenuShow( VLC_OBJECT(p_intf) );
837             }
838             else if( i_action == ACTIONID_MENU_OFF )
839             {
840                 osd_MenuHide( VLC_OBJECT(p_intf) );
841             }
842             else if( i_action == ACTIONID_MENU_LEFT )
843             {
844                 osd_MenuPrev( VLC_OBJECT(p_intf) );
845             }
846             else if( i_action == ACTIONID_MENU_RIGHT )
847             {
848                 osd_MenuNext( VLC_OBJECT(p_intf) );
849             }
850             else if( i_action == ACTIONID_MENU_UP )
851             {
852                 osd_MenuUp( VLC_OBJECT(p_intf) );
853             }
854             else if( i_action == ACTIONID_MENU_DOWN )
855             {
856                 osd_MenuDown( VLC_OBJECT(p_intf) );
857             }
858             else if( i_action == ACTIONID_MENU_SELECT )
859             {
860                 osd_MenuActivate( VLC_OBJECT(p_intf) );
861             }
862             else if( i_action == ACTIONID_RECORD )
863             {
864                 if( var_GetBool( p_input, "can-record" ) )
865                 {
866                     const bool b_record = !var_GetBool( p_input, "record" );
867
868                     if( b_record )
869                         vout_OSDMessage( p_intf, DEFAULT_CHAN, _("Recording") );
870                     else
871                         vout_OSDMessage( p_intf, DEFAULT_CHAN, _("Recording done") );
872                     var_SetBool( p_input, "record", b_record );
873                 }
874             }
875         }
876 cleanup_and_continue:
877         if( p_aout )
878             vlc_object_release( p_aout );
879         if( p_vout )
880             vlc_object_release( p_vout );
881         if( p_input )
882             vlc_object_release( p_input );
883     }
884
885     /* dead code */
886     abort();
887     vlc_cleanup_pop();
888 }
889
890 static int GetAction( intf_thread_t *p_intf )
891 {
892     intf_sys_t *p_sys = p_intf->p_sys;
893     int i_ret;
894
895     vlc_mutex_lock( &p_sys->lock );
896     mutex_cleanup_push( &p_sys->lock );
897
898     while( p_sys->i_size == 0 )
899         vlc_cond_wait( &p_sys->wait, &p_sys->lock );
900
901     i_ret = p_sys->p_actions[ 0 ];
902     p_sys->i_size--;
903     for( int i = 0; i < p_sys->i_size; i++ )
904         p_sys->p_actions[i] = p_sys->p_actions[i + 1];
905
906     vlc_cleanup_run();
907     return i_ret;
908 }
909
910 static int PutAction( intf_thread_t *p_intf, int i_action )
911 {
912     intf_sys_t *p_sys = p_intf->p_sys;
913     int i_ret = VLC_EGENERIC;
914
915     vlc_mutex_lock( &p_sys->lock );
916     if ( p_sys->i_size >= BUFFER_SIZE )
917         msg_Warn( p_intf, "event buffer full, dropping key actions" );
918     else
919         p_sys->p_actions[p_sys->i_size++] = i_action;
920
921     vlc_cond_signal( &p_sys->wait );
922     vlc_mutex_unlock( &p_sys->lock );
923     return i_ret;
924 }
925
926 /*****************************************************************************
927  * SpecialKeyEvent: callback for mouse events
928  *****************************************************************************/
929 static int SpecialKeyEvent( vlc_object_t *libvlc, char const *psz_var,
930                             vlc_value_t oldval, vlc_value_t newval,
931                             void *p_data )
932 {
933     intf_thread_t *p_intf = (intf_thread_t *)p_data;
934     int i_action = 0;
935
936     (void)psz_var;
937     (void)oldval;
938
939     int i_mode = p_intf->p_sys->i_mousewheel_mode;
940
941     /* Special action for mouse event */
942     /* FIXME: rework hotkeys handling to allow more than 1 event
943      * to trigger one same action */
944     switch (newval.i_int & ~KEY_MODIFIER)
945     {
946         case KEY_MOUSEWHEELUP:
947             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ? ACTIONID_VOL_UP
948                                  : ACTIONID_JUMP_FORWARD_EXTRASHORT;
949             break;
950         case KEY_MOUSEWHEELDOWN:
951             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ? ACTIONID_VOL_DOWN
952                                 : ACTIONID_JUMP_BACKWARD_EXTRASHORT;
953             break;
954         case KEY_MOUSEWHEELLEFT:
955             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ?
956                         ACTIONID_JUMP_BACKWARD_EXTRASHORT : ACTIONID_VOL_DOWN;
957             break;
958         case KEY_MOUSEWHEELRIGHT:
959             i_action = (i_mode == MOUSEWHEEL_VOLUME ) ?
960                         ACTIONID_JUMP_FORWARD_EXTRASHORT : ACTIONID_VOL_UP;
961             break;
962         case KEY_MENU:
963             var_SetBool( libvlc, "intf-popupmenu", true );
964             break;
965     }
966
967     if( i_mode == NO_MOUSEWHEEL ) return VLC_SUCCESS;
968
969     if( i_action )
970         return PutAction( p_intf, i_action );
971     return VLC_SUCCESS;
972 }
973
974 /*****************************************************************************
975  * ActionEvent: callback for hotkey actions
976  *****************************************************************************/
977 static int ActionEvent( vlc_object_t *libvlc, char const *psz_var,
978                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
979 {
980     intf_thread_t *p_intf = (intf_thread_t *)p_data;
981
982     (void)libvlc;
983     (void)psz_var;
984     (void)oldval;
985
986     return PutAction( p_intf, newval.i_int );
987 }
988
989 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
990 {
991     char *psz_bookmark_name;
992     if( asprintf( &psz_bookmark_name, "bookmark%i", i_num ) == -1 )
993         return;
994
995     playlist_t *p_playlist = pl_Hold( p_intf );
996     char *psz_bookmark = var_CreateGetString( p_intf, psz_bookmark_name );
997
998     PL_LOCK;
999     FOREACH_ARRAY( playlist_item_t *p_item, p_playlist->items )
1000         char *psz_uri = input_item_GetURI( p_item->p_input );
1001         if( !strcmp( psz_bookmark, psz_uri ) )
1002         {
1003             free( psz_uri );
1004             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
1005                               NULL, p_item );
1006             break;
1007         }
1008         else
1009             free( psz_uri );
1010     FOREACH_END();
1011     PL_UNLOCK;
1012
1013     free( psz_bookmark );
1014     free( psz_bookmark_name );
1015     pl_Release( p_intf );
1016 }
1017
1018 static void SetBookmark( intf_thread_t *p_intf, int i_num )
1019 {
1020     char *psz_bookmark_name;
1021     if( asprintf( &psz_bookmark_name, "bookmark%i", i_num ) == -1 )
1022         return;
1023
1024     playlist_t *p_playlist = pl_Hold( p_intf );
1025     var_Create( p_intf, psz_bookmark_name,
1026                 VLC_VAR_STRING|VLC_VAR_DOINHERIT );
1027     playlist_item_t * p_item = playlist_CurrentPlayingItem( p_playlist );
1028     if( p_item )
1029     {
1030         char *psz_uri = input_item_GetURI( p_item->p_input );
1031         config_PutPsz( p_intf, psz_bookmark_name, psz_uri);
1032         msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num, psz_uri);
1033         free( psz_uri );
1034         config_SaveConfigFile( p_intf, "hotkeys" );
1035     }
1036
1037     pl_Release( p_intf );
1038     free( psz_bookmark_name );
1039 }
1040
1041 static void DisplayPosition( intf_thread_t *p_intf, vout_thread_t *p_vout,
1042                              input_thread_t *p_input )
1043 {
1044     char psz_duration[MSTRTIME_MAX_SIZE];
1045     char psz_time[MSTRTIME_MAX_SIZE];
1046     vlc_value_t time, pos;
1047     mtime_t i_seconds;
1048
1049     if( p_vout == NULL ) return;
1050
1051     ClearChannels( p_intf, p_vout );
1052
1053     var_Get( p_input, "time", &time );
1054     i_seconds = time.i_time / 1000000;
1055     secstotimestr ( psz_time, i_seconds );
1056
1057     var_Get( p_input, "length", &time );
1058     if( time.i_time > 0 )
1059     {
1060         secstotimestr( psz_duration, time.i_time / 1000000 );
1061         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, "%s / %s",
1062                          psz_time, psz_duration );
1063     }
1064     else if( i_seconds > 0 )
1065     {
1066         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, "%s", psz_time );
1067     }
1068
1069     if( p_vout->b_fullscreen )
1070     {
1071         var_Get( p_input, "position", &pos );
1072         vout_OSDSlider( VLC_OBJECT( p_input ), POSITION_WIDGET_CHAN,
1073                         pos.f_float * 100, OSD_HOR_SLIDER );
1074     }
1075 }
1076
1077 static void DisplayVolume( intf_thread_t *p_intf, vout_thread_t *p_vout,
1078                            audio_volume_t i_vol )
1079 {
1080     if( p_vout == NULL )
1081     {
1082         return;
1083     }
1084     ClearChannels( p_intf, p_vout );
1085
1086     if( p_vout->b_fullscreen )
1087     {
1088         vout_OSDSlider( VLC_OBJECT( p_vout ), VOLUME_WIDGET_CHAN,
1089             i_vol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
1090     }
1091     else
1092     {
1093         vout_OSDMessage( p_vout, VOLUME_TEXT_CHAN, _( "Volume %d%%" ),
1094                          i_vol*400/AOUT_VOLUME_MAX );
1095     }
1096 }
1097
1098 static void ClearChannels( intf_thread_t *p_intf, vout_thread_t *p_vout )
1099 {
1100     int i;
1101
1102     if( p_vout )
1103     {
1104         spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
1105         for( i = 0; i < CHANNELS_NUMBER; i++ )
1106         {
1107             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1108                          p_intf->p_sys->p_channels[ i ] );
1109         }
1110     }
1111 }