fe22dba2832378faa59b9d7c595c3a61e06a0db5
[vlc.git] / modules / control / hotkeys.c
1 /*****************************************************************************
2  * hotkeys.c: Hotkey handling for vlc
3  *****************************************************************************
4  * Copyright (C) 2005 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
43 #define BUFFER_SIZE 10
44
45 #define CHANNELS_NUMBER 4
46 #define VOLUME_TEXT_CHAN     p_intf->p_sys->p_channels[ 0 ]
47 #define VOLUME_WIDGET_CHAN   p_intf->p_sys->p_channels[ 1 ]
48 #define POSITION_TEXT_CHAN   p_intf->p_sys->p_channels[ 2 ]
49 #define POSITION_WIDGET_CHAN p_intf->p_sys->p_channels[ 3 ]
50 /*****************************************************************************
51  * intf_sys_t: description and status of FB interface
52  *****************************************************************************/
53 struct intf_sys_t
54 {
55     int                 p_actions[ BUFFER_SIZE ]; /* buffer that contains
56                                                    * action events */
57     int                 i_size;        /* number of events in buffer */
58     int                 p_channels[ CHANNELS_NUMBER ]; /* contains registered
59                                                         * channel IDs */
60     input_thread_t *    p_input;       /* pointer to input */
61     vout_thread_t *     p_vout;        /* pointer to vout object */
62 };
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67 static int  Open    ( vlc_object_t * );
68 static void Close   ( vlc_object_t * );
69 static void Run     ( intf_thread_t * );
70 static int  GetAction( intf_thread_t *);
71 static int  ActionEvent( vlc_object_t *, char const *,
72                          vlc_value_t, vlc_value_t, void * );
73 static int  SpecialKeyEvent( vlc_object_t *, char const *,
74                              vlc_value_t, vlc_value_t, void * );
75 static void PlayBookmark( intf_thread_t *, int );
76 static void SetBookmark ( intf_thread_t *, int );
77 static void DisplayPosition( intf_thread_t *, vout_thread_t *, input_thread_t * );
78 static void DisplayVolume  ( intf_thread_t *, vout_thread_t *, audio_volume_t );
79 static void ClearChannels  ( intf_thread_t *, vout_thread_t * );
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 #define BOOKMARK1_TEXT    N_("Playlist bookmark 1")
85 #define BOOKMARK2_TEXT    N_("Playlist bookmark 2")
86 #define BOOKMARK3_TEXT    N_("Playlist bookmark 3")
87 #define BOOKMARK4_TEXT    N_("Playlist bookmark 4")
88 #define BOOKMARK5_TEXT    N_("Playlist bookmark 5")
89 #define BOOKMARK6_TEXT    N_("Playlist bookmark 6")
90 #define BOOKMARK7_TEXT    N_("Playlist bookmark 7")
91 #define BOOKMARK8_TEXT    N_("Playlist bookmark 8")
92 #define BOOKMARK9_TEXT    N_("Playlist bookmark 9")
93 #define BOOKMARK10_TEXT   N_("Playlist bookmark 10")
94 #define BOOKMARK_LONGTEXT N_("Define playlist bookmarks.")
95
96 vlc_module_begin();
97     set_shortname( N_("Hotkeys") );
98     set_description( N_("Hotkeys management interface") );
99     set_capability( "interface", 0 );
100     set_callbacks( Open, Close );
101 vlc_module_end();
102
103 /*****************************************************************************
104  * Open: initialize interface
105  *****************************************************************************/
106 static int Open( vlc_object_t *p_this )
107 {
108     intf_thread_t *p_intf = (intf_thread_t *)p_this;
109     MALLOC_ERR( p_intf->p_sys, intf_sys_t );
110
111     p_intf->p_sys->i_size = 0;
112     p_intf->pf_run = Run;
113
114     var_AddCallback( p_intf->p_libvlc, "key-pressed", SpecialKeyEvent, p_intf );
115     var_AddCallback( p_intf->p_libvlc, "key-action", ActionEvent, p_intf );
116     return VLC_SUCCESS;
117 }
118
119 /*****************************************************************************
120  * Close: destroy interface
121  *****************************************************************************/
122 static void Close( vlc_object_t *p_this )
123 {
124     intf_thread_t *p_intf = (intf_thread_t *)p_this;
125
126     var_DelCallback( p_intf->p_libvlc, "key-action", ActionEvent, p_intf );
127     var_DelCallback( p_intf->p_libvlc, "key-pressed", SpecialKeyEvent, p_intf );
128
129     /* Destroy structure */
130     free( p_intf->p_sys );
131 }
132
133 /*****************************************************************************
134  * Run: main loop
135  *****************************************************************************/
136 static void Run( intf_thread_t *p_intf )
137 {
138     vout_thread_t *p_vout = NULL;
139     vlc_value_t val;
140     int i;
141     playlist_t *p_playlist = pl_Yield( p_intf );
142
143     /* Initialize hotkey structure */
144     for( struct hotkey *p_hotkey = p_intf->p_libvlc->p_hotkeys;
145          p_hotkey->psz_action != NULL;
146          p_hotkey++ )
147     {
148         p_hotkey->i_key = config_GetInt( p_intf, p_hotkey->psz_action );
149     }
150
151     for( ;; )
152     {
153         input_thread_t *p_input;
154         vout_thread_t *p_last_vout;
155         int i_action = GetAction( p_intf );
156
157         if( i_action == -1 )
158             break; /* die */
159
160         /* Update the input */
161         PL_LOCK;
162         p_input = p_playlist->p_input;
163         if( p_input )
164             vlc_object_yield( p_input );
165         PL_UNLOCK;
166
167         /* Update the vout */
168         p_last_vout = p_vout;
169         p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
170
171         /* Register OSD channels */
172         if( p_vout && p_vout != p_last_vout )
173         {
174             for( i = 0; i < CHANNELS_NUMBER; i++ )
175             {
176                 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
177                              &p_intf->p_sys->p_channels[ i ] );
178             }
179         }
180
181         /* Quit */
182         if( i_action == ACTIONID_QUIT )
183         {
184             if( p_playlist )
185                 playlist_Stop( p_playlist );
186             vlc_object_kill( p_intf->p_libvlc );
187             vlc_object_kill( p_intf );
188             ClearChannels( p_intf, p_vout );
189             vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Quit" ) );
190             if( p_vout )
191                 vlc_object_release( p_vout );
192             if( p_input )
193                 vlc_object_release( p_input );
194             continue;
195         }
196         /* Volume and audio actions */
197         else if( i_action == ACTIONID_VOL_UP )
198         {
199             audio_volume_t i_newvol;
200             aout_VolumeUp( p_intf, 1, &i_newvol );
201             DisplayVolume( p_intf, p_vout, i_newvol );
202         }
203         else if( i_action == ACTIONID_VOL_DOWN )
204         {
205             audio_volume_t i_newvol;
206             aout_VolumeDown( p_intf, 1, &i_newvol );
207             DisplayVolume( p_intf, p_vout, i_newvol );
208         }
209         else if( i_action == ACTIONID_VOL_MUTE )
210         {
211             audio_volume_t i_newvol = -1;
212             aout_VolumeMute( p_intf, &i_newvol );
213             if( p_vout )
214             {
215                 if( i_newvol == 0 )
216                 {
217                     ClearChannels( p_intf, p_vout );
218                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
219                                   OSD_MUTE_ICON );
220                 }
221                 else
222                 {
223                     DisplayVolume( p_intf, p_vout, i_newvol );
224                 }
225             }
226         }
227         /* Interface showing */
228         else if( i_action == ACTIONID_INTF_SHOW )
229             var_SetBool( p_playlist, "intf-show", true );
230         else if( i_action == ACTIONID_INTF_HIDE )
231             var_SetBool( p_playlist, "intf-show", false );
232         /* Video Output actions */
233         else if( i_action == ACTIONID_SNAPSHOT )
234         {
235             if( p_vout ) vout_Control( p_vout, VOUT_SNAPSHOT );
236         }
237         else if( i_action == ACTIONID_TOGGLE_FULLSCREEN )
238         {
239             if( p_vout )
240             {
241                 var_Get( p_vout, "fullscreen", &val );
242                 val.b_bool = !val.b_bool;
243                 var_Set( p_vout, "fullscreen", val );
244             }
245             else
246             {
247                 var_Get( p_playlist, "fullscreen", &val );
248                 val.b_bool = !val.b_bool;
249                 var_Set( p_playlist, "fullscreen", val );
250             }
251         }
252         else if( i_action == ACTIONID_LEAVE_FULLSCREEN )
253         {
254             if( p_vout && var_GetBool( p_vout, "fullscreen" ) )
255             {
256                 var_SetBool( p_vout, "fullscreen", false );
257             }
258         }
259         else if( i_action == ACTIONID_ZOOM_QUARTER ||
260                  i_action == ACTIONID_ZOOM_HALF ||
261                  i_action == ACTIONID_ZOOM_ORIGINAL ||
262                  i_action == ACTIONID_ZOOM_DOUBLE )
263         {
264             if( p_vout )
265             {
266                 if( i_action == ACTIONID_ZOOM_QUARTER )
267                     val.f_float = 0.25;
268                 if( i_action == ACTIONID_ZOOM_HALF )
269                     val.f_float = 0.5;
270                 if( i_action == ACTIONID_ZOOM_ORIGINAL )
271                     val.f_float = 1;
272                 if( i_action == ACTIONID_ZOOM_DOUBLE )
273                     val.f_float = 2;
274                 var_Set( p_vout, "zoom", val );
275             }
276         }
277         else if( i_action == ACTIONID_WALLPAPER )
278         {
279             if( p_vout )
280             {
281                 var_Get( p_vout, "directx-wallpaper", &val );
282                 val.b_bool = !val.b_bool;
283                 var_Set( p_vout, "directx-wallpaper", val );
284             }
285             else
286             {
287                 var_Get( p_playlist, "directx-wallpaper", &val );
288                 val.b_bool = !val.b_bool;
289                 var_Set( p_playlist, "directx-wallpaper", val );
290             }
291         }
292         /* Playlist actions */
293         else if( i_action == ACTIONID_LOOP )
294         {
295             /* Toggle Normal -> Loop -> Repeat -> Normal ... */
296             vlc_value_t val2;
297             var_Get( p_playlist, "loop", &val );
298             var_Get( p_playlist, "repeat", &val2 );
299             if( val2.b_bool == true )
300             {
301                 val.b_bool = false;
302                 val2.b_bool = false;
303             }
304             else if( val.b_bool == true )
305             {
306                 val.b_bool = false;
307                 val2.b_bool = true;
308             }
309             else
310             {
311                 val.b_bool = true;
312             }
313             var_Set( p_playlist, "loop", val );
314             var_Set( p_playlist, "repeat", val2 );
315         }
316         else if( i_action == ACTIONID_RANDOM )
317         {
318             var_Get( p_playlist, "random", &val );
319             val.b_bool = !val.b_bool;
320             var_Set( p_playlist, "random", val );
321         }
322         else if( i_action == ACTIONID_PLAY_PAUSE )
323         {
324             val.i_int = PLAYING_S;
325             if( p_input )
326             {
327                 ClearChannels( p_intf, p_vout );
328
329                 var_Get( p_input, "state", &val );
330                 if( val.i_int != PAUSE_S )
331                 {
332                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
333                                   OSD_PAUSE_ICON );
334                     val.i_int = PAUSE_S;
335                 }
336                 else
337                 {
338                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
339                                   OSD_PLAY_ICON );
340                     val.i_int = PLAYING_S;
341                 }
342                 var_Set( p_input, "state", val );
343             }
344             else
345             {
346                 playlist_Play( p_playlist );
347             }
348         }
349         else if( i_action == ACTIONID_AUDIODEVICE_CYCLE )
350         {
351             vlc_value_t val, list, list2;
352             int i_count, i;
353
354             aout_instance_t *p_aout =
355                 vlc_object_find( p_intf, VLC_OBJECT_AOUT, FIND_ANYWHERE );
356             var_Get( p_aout, "audio-device", &val );
357             var_Change( p_aout, "audio-device", VLC_VAR_GETCHOICES,
358                     &list, &list2 );
359             i_count = list.p_list->i_count;
360
361             /* Not enough device to switch between */
362             if( i_count <= 1 )
363                 continue;
364
365             for( i = 0; i < i_count; i++ )
366             {
367                 if( val.i_int == list.p_list->p_values[i].i_int )
368                 {
369                     break;
370                 }
371             }
372             if( i == i_count )
373             {
374                 msg_Warn( p_aout,
375                         "invalid current audio device, selecting 0" );
376                 var_Set( p_aout, "audio-device",
377                         list.p_list->p_values[0] );
378                 i = 0;
379             }
380             else if( i == i_count -1 )
381             {
382                 var_Set( p_aout, "audio-device",
383                         list.p_list->p_values[0] );
384                 i = 0;
385             }
386             else
387             {
388                 var_Set( p_aout, "audio-device",
389                         list.p_list->p_values[i+1] );
390                 i++;
391             }
392             vout_OSDMessage( p_intf, DEFAULT_CHAN,
393                     _("Audio Device: %s"),
394                     list2.p_list->p_values[i].psz_string);
395             vlc_object_release( p_aout );
396         }
397         /* Input options */
398         else if( p_input )
399         {
400             /* FIXME --fenrir
401              * How to get a valid value ?
402              * That's not that easy with some special stream
403              */
404             bool b_seekable = true;
405             int i_interval =0;
406
407             if( i_action == ACTIONID_PAUSE )
408             {
409                 var_Get( p_input, "state", &val );
410                 if( val.i_int != PAUSE_S )
411                 {
412                     ClearChannels( p_intf, p_vout );
413                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
414                                   OSD_PAUSE_ICON );
415                     val.i_int = PAUSE_S;
416                     var_Set( p_input, "state", val );
417                 }
418             }
419             else if( i_action == ACTIONID_JUMP_BACKWARD_EXTRASHORT
420                      && b_seekable )
421             {
422 #define SET_TIME( a, b ) \
423     i_interval = config_GetInt( p_input, a "-jump-size" ); \
424     if( i_interval > 0 ) { \
425         val.i_time = (mtime_t)(i_interval * b) * 1000000L; \
426         var_Set( p_input, "time-offset", val ); \
427         DisplayPosition( p_intf, p_vout, p_input ); \
428     }
429                 SET_TIME( "extrashort", -1 );
430             }
431             else if( i_action == ACTIONID_JUMP_FORWARD_EXTRASHORT && b_seekable )
432             {
433                 SET_TIME( "extrashort", 1 );
434             }
435             else if( i_action == ACTIONID_JUMP_BACKWARD_SHORT && b_seekable )
436             {
437                 SET_TIME( "short", -1 );
438             }
439             else if( i_action == ACTIONID_JUMP_FORWARD_SHORT && b_seekable )
440             {
441                 SET_TIME( "short", 1 );
442             }
443             else if( i_action == ACTIONID_JUMP_BACKWARD_MEDIUM && b_seekable )
444             {
445                 SET_TIME( "medium", -1 );
446             }
447             else if( i_action == ACTIONID_JUMP_FORWARD_MEDIUM && b_seekable )
448             {
449                 SET_TIME( "medium", 1 );
450             }
451             else if( i_action == ACTIONID_JUMP_BACKWARD_LONG && b_seekable )
452             {
453                 SET_TIME( "long", -1 );
454             }
455             else if( i_action == ACTIONID_JUMP_FORWARD_LONG && b_seekable )
456             {
457                 SET_TIME( "long", 1 );
458 #undef SET_TIME
459             }
460             else if( i_action == ACTIONID_AUDIO_TRACK )
461             {
462                 vlc_value_t val, list, list2;
463                 int i_count, i;
464                 var_Get( p_input, "audio-es", &val );
465                 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
466                             &list, &list2 );
467                 i_count = list.p_list->i_count;
468                 if( i_count <= 1 )
469                 {
470                     continue;
471                 }
472                 for( i = 0; i < i_count; i++ )
473                 {
474                     if( val.i_int == list.p_list->p_values[i].i_int )
475                     {
476                         break;
477                     }
478                 }
479                 /* value of audio-es was not in choices list */
480                 if( i == i_count )
481                 {
482                     msg_Warn( p_input,
483                               "invalid current audio track, selecting 0" );
484                     var_Set( p_input, "audio-es",
485                              list.p_list->p_values[0] );
486                     i = 0;
487                 }
488                 else if( i == i_count - 1 )
489                 {
490                     var_Set( p_input, "audio-es",
491                              list.p_list->p_values[1] );
492                     i = 1;
493                 }
494                 else
495                 {
496                     var_Set( p_input, "audio-es",
497                              list.p_list->p_values[i+1] );
498                     i++;
499                 }
500                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
501                                  _("Audio track: %s"),
502                                  list2.p_list->p_values[i].psz_string );
503             }
504             else if( i_action == ACTIONID_SUBTITLE_TRACK )
505             {
506                 vlc_value_t val, list, list2;
507                 int i_count, i;
508                 var_Get( p_input, "spu-es", &val );
509
510                 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
511                             &list, &list2 );
512                 i_count = list.p_list->i_count;
513                 if( i_count <= 1 )
514                 {
515                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
516                                      _("Subtitle track: %s"), _("N/A") );
517                     continue;
518                 }
519                 for( i = 0; i < i_count; i++ )
520                 {
521                     if( val.i_int == list.p_list->p_values[i].i_int )
522                     {
523                         break;
524                     }
525                 }
526                 /* value of spu-es was not in choices list */
527                 if( i == i_count )
528                 {
529                     msg_Warn( p_input,
530                               "invalid current subtitle track, selecting 0" );
531                     var_Set( p_input, "spu-es", list.p_list->p_values[0] );
532                     i = 0;
533                 }
534                 else if( i == i_count - 1 )
535                 {
536                     var_Set( p_input, "spu-es", list.p_list->p_values[0] );
537                     i = 0;
538                 }
539                 else
540                 {
541                     var_Set( p_input, "spu-es", list.p_list->p_values[i+1] );
542                     i = i + 1;
543                 }
544                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
545                                  _("Subtitle track: %s"),
546                                  list2.p_list->p_values[i].psz_string );
547             }
548             else if( i_action == ACTIONID_ASPECT_RATIO && p_vout )
549             {
550                 vlc_value_t val={0}, val_list, text_list;
551                 var_Get( p_vout, "aspect-ratio", &val );
552                 if( var_Change( p_vout, "aspect-ratio", VLC_VAR_GETLIST,
553                                 &val_list, &text_list ) >= 0 )
554                 {
555                     int i;
556                     for( i = 0; i < val_list.p_list->i_count; i++ )
557                     {
558                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
559                                      val.psz_string ) )
560                         {
561                             i++;
562                             break;
563                         }
564                     }
565                     if( i == val_list.p_list->i_count ) i = 0;
566                     var_SetString( p_vout, "aspect-ratio",
567                                    val_list.p_list->p_values[i].psz_string );
568                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
569                                      _("Aspect ratio: %s"),
570                                      text_list.p_list->p_values[i].psz_string );
571                 }
572                 free( val.psz_string );
573             }
574             else if( i_action == ACTIONID_CROP && p_vout )
575             {
576                 vlc_value_t val={0}, val_list, text_list;
577                 var_Get( p_vout, "crop", &val );
578                 if( var_Change( p_vout, "crop", VLC_VAR_GETLIST,
579                                 &val_list, &text_list ) >= 0 )
580                 {
581                     int i;
582                     for( i = 0; i < val_list.p_list->i_count; i++ )
583                     {
584                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
585                                      val.psz_string ) )
586                         {
587                             i++;
588                             break;
589                         }
590                     }
591                     if( i == val_list.p_list->i_count ) i = 0;
592                     var_SetString( p_vout, "crop",
593                                    val_list.p_list->p_values[i].psz_string );
594                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
595                                      _("Crop: %s"),
596                                      text_list.p_list->p_values[i].psz_string );
597                 }
598                 free( val.psz_string );
599             }
600             else if( i_action == ACTIONID_DEINTERLACE && p_vout )
601             {
602                 vlc_value_t val={0}, val_list, text_list;
603                 var_Get( p_vout, "deinterlace", &val );
604                 if( var_Change( p_vout, "deinterlace", VLC_VAR_GETLIST,
605                                 &val_list, &text_list ) >= 0 )
606                 {
607                     int i;
608                     for( i = 0; i < val_list.p_list->i_count; i++ )
609                     {
610                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
611                                      val.psz_string ) )
612                         {
613                             i++;
614                             break;
615                         }
616                     }
617                     if( i == val_list.p_list->i_count ) i = 0;
618                     var_SetString( p_vout, "deinterlace",
619                                    val_list.p_list->p_values[i].psz_string );
620                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
621                                      _("Deinterlace mode: %s"),
622                                      text_list.p_list->p_values[i].psz_string );
623                 }
624                 free( val.psz_string );
625             }
626             else if( ( i_action == ACTIONID_ZOOM || i_action == ACTIONID_UNZOOM ) && p_vout )
627             {
628                 vlc_value_t val={0}, val_list, text_list;
629                 var_Get( p_vout, "zoom", &val );
630                 if( var_Change( p_vout, "zoom", VLC_VAR_GETLIST,
631                                 &val_list, &text_list ) >= 0 )
632                 {
633                     int i;
634                     for( i = 0; i < val_list.p_list->i_count; i++ )
635                     {
636                         if( val_list.p_list->p_values[i].f_float
637                            == val.f_float )
638                         {
639                             if( i_action == ACTIONID_ZOOM )
640                                 i++;
641                             else /* ACTIONID_UNZOOM */
642                                 i--;
643                             break;
644                         }
645                     }
646                     if( i == val_list.p_list->i_count ) i = 0;
647                     if( i == -1 ) i = val_list.p_list->i_count-1;
648                     var_SetFloat( p_vout, "zoom",
649                                   val_list.p_list->p_values[i].f_float );
650                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
651                                      _("Zoom mode: %s"),
652                                 text_list.p_list->p_values[i].var.psz_name );
653                 }
654             }
655             else if( i_action == ACTIONID_CROP_TOP && p_vout )
656                 var_IncInteger( p_vout, "crop-top" );
657             else if( i_action == ACTIONID_UNCROP_TOP && p_vout )
658                 var_DecInteger( p_vout, "crop-top" );
659             else if( i_action == ACTIONID_CROP_BOTTOM && p_vout )
660                 var_IncInteger( p_vout, "crop-bottom" );
661             else if( i_action == ACTIONID_UNCROP_BOTTOM && p_vout )
662                  var_DecInteger( p_vout, "crop-bottom" );
663             else if( i_action == ACTIONID_CROP_LEFT && p_vout )
664                  var_IncInteger( p_vout, "crop-left" );
665             else if( i_action == ACTIONID_UNCROP_LEFT && p_vout )
666                  var_DecInteger( p_vout, "crop-left" );
667             else if( i_action == ACTIONID_CROP_RIGHT && p_vout )
668                  var_IncInteger( p_vout, "crop-right" );
669             else if( i_action == ACTIONID_UNCROP_RIGHT && p_vout )
670                  var_DecInteger( p_vout, "crop-right" );
671
672             else if( i_action == ACTIONID_NEXT )
673             {
674                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN, _("Next") );
675                 playlist_Next( p_playlist );
676             }
677             else if( i_action == ACTIONID_PREV )
678             {
679                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
680                                  _("Previous") );
681                 playlist_Prev( p_playlist );
682             }
683             else if( i_action == ACTIONID_STOP )
684             {
685                 playlist_Stop( p_playlist );
686             }
687             else if( i_action == ACTIONID_FASTER )
688             {
689                 var_SetVoid( p_input, "rate-faster" );
690                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
691                                  _("Faster") );
692             }
693             else if( i_action == ACTIONID_SLOWER )
694             {
695                 var_SetVoid( p_input, "rate-slower" );
696                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
697                                  _("Slower") );
698             }
699             else if( i_action == ACTIONID_POSITION && b_seekable )
700             {
701                 DisplayPosition( p_intf, p_vout, p_input );
702             }
703             else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
704                      i_action <= ACTIONID_PLAY_BOOKMARK10 )
705             {
706                 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
707             }
708             else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
709                      i_action <= ACTIONID_SET_BOOKMARK10 )
710             {
711                 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
712             }
713             /* Only makes sense with DVD */
714             else if( i_action == ACTIONID_TITLE_PREV )
715                 var_SetVoid( p_input, "prev-title" );
716             else if( i_action == ACTIONID_TITLE_NEXT )
717                 var_SetVoid( p_input, "next-title" );
718             else if( i_action == ACTIONID_CHAPTER_PREV )
719                 var_SetVoid( p_input, "prev-chapter" );
720             else if( i_action == ACTIONID_CHAPTER_NEXT )
721                 var_SetVoid( p_input, "next-chapter" );
722             else if( i_action == ACTIONID_DISC_MENU )
723                 var_SetInteger( p_input, "title  0", 2 );
724
725             else if( i_action == ACTIONID_SUBDELAY_DOWN )
726             {
727                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
728                 i_delay -= 50000;    /* 50 ms */
729                 var_SetTime( p_input, "spu-delay", i_delay );
730                 ClearChannels( p_intf, p_vout );
731                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
732                                  _( "Subtitle delay %i ms" ),
733                                  (int)(i_delay/1000) );
734             }
735             else if( i_action == ACTIONID_SUBDELAY_UP )
736             {
737                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
738                 i_delay += 50000;    /* 50 ms */
739                 var_SetTime( p_input, "spu-delay", i_delay );
740                 ClearChannels( p_intf, p_vout );
741                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
742                                 _( "Subtitle delay %i ms" ),
743                                  (int)(i_delay/1000) );
744             }
745             else if( i_action == ACTIONID_AUDIODELAY_DOWN )
746             {
747                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
748                 i_delay -= 50000;    /* 50 ms */
749                 var_SetTime( p_input, "audio-delay", i_delay );
750                 ClearChannels( p_intf, p_vout );
751                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
752                                 _( "Audio delay %i ms" ),
753                                  (int)(i_delay/1000) );
754             }
755             else if( i_action == ACTIONID_AUDIODELAY_UP )
756             {
757                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
758                 i_delay += 50000;    /* 50 ms */
759                 var_SetTime( p_input, "audio-delay", i_delay );
760                 ClearChannels( p_intf, p_vout );
761                 vout_OSDMessage( p_intf, DEFAULT_CHAN,
762                                 _( "Audio delay %i ms" ),
763                                  (int)(i_delay/1000) );
764             }
765             else if( i_action == ACTIONID_PLAY )
766             {
767                 var_Get( p_input, "rate", &val );
768                 if( val.i_int != INPUT_RATE_DEFAULT )
769                 {
770                     /* Return to normal speed */
771                     var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
772                 }
773                 else
774                 {
775                     ClearChannels( p_intf, p_vout );
776                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
777                                   OSD_PLAY_ICON );
778                     playlist_Play( p_playlist );
779                 }
780             }
781             else if( i_action == ACTIONID_MENU_ON )
782             {
783                 osd_MenuShow( VLC_OBJECT(p_intf) );
784             }
785             else if( i_action == ACTIONID_MENU_OFF )
786             {
787                 osd_MenuHide( VLC_OBJECT(p_intf) );
788             }
789             else if( i_action == ACTIONID_MENU_LEFT )
790             {
791                 osd_MenuPrev( VLC_OBJECT(p_intf) );
792             }
793             else if( i_action == ACTIONID_MENU_RIGHT )
794             {
795                 osd_MenuNext( VLC_OBJECT(p_intf) );
796             }
797             else if( i_action == ACTIONID_MENU_UP )
798             {
799                 osd_MenuUp( VLC_OBJECT(p_intf) );
800             }
801             else if( i_action == ACTIONID_MENU_DOWN )
802             {
803                 osd_MenuDown( VLC_OBJECT(p_intf) );
804             }
805             else if( i_action == ACTIONID_MENU_SELECT )
806             {
807                 osd_MenuActivate( VLC_OBJECT(p_intf) );
808             }
809         }
810         if( p_vout )
811             vlc_object_release( p_vout );
812         if( p_input )
813             vlc_object_release( p_input );
814     }
815     pl_Release( p_intf );
816 }
817
818 static int GetAction( intf_thread_t *p_intf )
819 {
820     intf_sys_t *p_sys = p_intf->p_sys;
821     int i_ret = -1;
822
823     vlc_object_lock( p_intf );
824     while( p_sys->i_size == 0 )
825     {
826         if( !vlc_object_alive( p_intf ) )
827             goto out;
828         vlc_object_wait( p_intf );
829     }
830
831     i_ret = p_sys->p_actions[ 0 ];
832     p_sys->i_size--;
833     for( int i = 0; i < p_sys->i_size; i++ )
834         p_sys->p_actions[i] = p_sys->p_actions[i + 1];
835
836 out:
837     vlc_object_unlock( p_intf );
838     return i_ret;
839 }
840
841 static int PutAction( intf_thread_t *p_intf, int i_action )
842 {
843     intf_sys_t *p_sys = p_intf->p_sys;
844     int i_ret = VLC_EGENERIC;
845
846     vlc_object_lock( p_intf );
847     if ( p_sys->i_size >= BUFFER_SIZE )
848         msg_Warn( p_intf, "event buffer full, dropping key actions" );
849     else
850         p_sys->p_actions[p_sys->i_size++] = i_action;
851
852     vlc_object_signal_unlocked( p_intf );
853     vlc_object_unlock( p_intf );
854     return i_ret;
855 }
856
857 /*****************************************************************************
858  * SpecialKeyEvent: callback for mouse events
859  *****************************************************************************/
860 static int SpecialKeyEvent( vlc_object_t *libvlc, char const *psz_var,
861                             vlc_value_t oldval, vlc_value_t newval,
862                             void *p_data )
863 {
864     intf_thread_t *p_intf = (intf_thread_t *)p_data;
865     int i_action;
866
867     (void)libvlc;
868     (void)psz_var;
869     (void)oldval;
870
871     /* Special action for mouse event */
872     /* FIXME: This should probably be configurable */
873     /* FIXME: rework hotkeys handling to allow more than 1 event
874      * to trigger one same action */
875     switch (newval.i_int & KEY_SPECIAL)
876     {
877         case KEY_MOUSEWHEELUP:
878             i_action = ACTIONID_VOL_UP;
879             break;
880         case KEY_MOUSEWHEELDOWN:
881             i_action = ACTIONID_VOL_DOWN;
882             break;
883         case KEY_MOUSEWHEELLEFT:
884             i_action = ACTIONID_JUMP_BACKWARD_EXTRASHORT;
885             break;
886         case KEY_MOUSEWHEELRIGHT:
887             i_action = ACTIONID_JUMP_FORWARD_EXTRASHORT;
888             break;
889         default:
890           return VLC_SUCCESS;
891     }
892
893     return PutAction( p_intf, i_action );
894 }
895
896 /*****************************************************************************
897  * ActionEvent: callback for hotkey actions
898  *****************************************************************************/
899 static int ActionEvent( vlc_object_t *libvlc, char const *psz_var,
900                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
901 {
902     intf_thread_t *p_intf = (intf_thread_t *)p_data;
903
904     (void)libvlc;
905     (void)psz_var;
906     (void)oldval;
907
908     return PutAction( p_intf, newval.i_int );
909 }
910
911 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
912 {
913     vlc_value_t val;
914     char psz_bookmark_name[11];
915     playlist_t *p_playlist = pl_Yield( p_intf );
916
917     sprintf( psz_bookmark_name, "bookmark%i", i_num );
918     var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
919     var_Get( p_intf, psz_bookmark_name, &val );
920
921     char *psz_bookmark = strdup( val.psz_string );
922     PL_LOCK;
923     FOREACH_ARRAY( playlist_item_t *p_item, p_playlist->items )
924         char *psz_uri = input_item_GetURI( p_item->p_input );
925         if( !strcmp( psz_bookmark, psz_uri ) )
926         {
927             free( psz_uri );
928             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true,
929                               NULL, p_item );
930             break;
931         }
932         else
933             free( psz_uri );
934     FOREACH_END();
935     PL_UNLOCK;
936     vlc_object_release( p_playlist );
937 }
938
939 static void SetBookmark( intf_thread_t *p_intf, int i_num )
940 {
941     playlist_t *p_playlist = pl_Yield( p_intf );
942     char psz_bookmark_name[11];
943     sprintf( psz_bookmark_name, "bookmark%i", i_num );
944     var_Create( p_intf, psz_bookmark_name,
945                 VLC_VAR_STRING|VLC_VAR_DOINHERIT );
946     if( p_playlist->status.p_item )
947     {
948         char *psz_uri = input_item_GetURI( p_playlist->status.p_item->p_input );
949         config_PutPsz( p_intf, psz_bookmark_name, psz_uri);
950         msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num, psz_uri);
951         free( psz_uri );
952         config_SaveConfigFile( p_intf, "hotkeys" );
953     }
954     pl_Release( p_intf );
955 }
956
957 static void DisplayPosition( intf_thread_t *p_intf, vout_thread_t *p_vout,
958                              input_thread_t *p_input )
959 {
960     char psz_duration[MSTRTIME_MAX_SIZE];
961     char psz_time[MSTRTIME_MAX_SIZE];
962     vlc_value_t time, pos;
963     mtime_t i_seconds;
964
965     if( p_vout == NULL ) return;
966
967     ClearChannels( p_intf, p_vout );
968
969     var_Get( p_input, "time", &time );
970     i_seconds = time.i_time / 1000000;
971     secstotimestr ( psz_time, i_seconds );
972
973     var_Get( p_input, "length", &time );
974     if( time.i_time > 0 )
975     {
976         secstotimestr( psz_duration, time.i_time / 1000000 );
977         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, (char *) "%s / %s",
978                          psz_time, psz_duration );
979     }
980     else if( i_seconds > 0 )
981     {
982         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, psz_time );
983     }
984
985     if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
986     {
987         var_Get( p_input, "position", &pos );
988         vout_OSDSlider( VLC_OBJECT( p_input ), POSITION_WIDGET_CHAN,
989                         pos.f_float * 100, OSD_HOR_SLIDER );
990     }
991 }
992
993 static void DisplayVolume( intf_thread_t *p_intf, vout_thread_t *p_vout,
994                            audio_volume_t i_vol )
995 {
996     if( p_vout == NULL )
997     {
998         return;
999     }
1000     ClearChannels( p_intf, p_vout );
1001
1002     if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
1003     {
1004         vout_OSDSlider( VLC_OBJECT( p_vout ), VOLUME_WIDGET_CHAN,
1005             i_vol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
1006     }
1007     else
1008     {
1009         vout_OSDMessage( p_vout, VOLUME_TEXT_CHAN, _( "Volume %d%%" ),
1010                          i_vol*400/AOUT_VOLUME_MAX );
1011     }
1012 }
1013
1014 static void ClearChannels( intf_thread_t *p_intf, vout_thread_t *p_vout )
1015 {
1016     int i;
1017
1018     if( p_vout )
1019     {
1020         spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
1021         for( i = 0; i < CHANNELS_NUMBER; i++ )
1022         {
1023             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1024                          p_intf->p_sys->p_channels[ i ] );
1025         }
1026     }
1027 }