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