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