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