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