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