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