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