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