]> git.sesse.net Git - vlc/blob - modules/control/hotkeys.c
Merge back branch 0.8.6-playlist-vlm to trunk.
[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 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
110     /* Allocate instance and initialize some members */
111     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
112     if( p_intf->p_sys == NULL )
113     {
114         msg_Err( p_intf, "out of memory" );
115         return 1;
116     }
117     vlc_mutex_init( p_intf, &p_intf->p_sys->change_lock );
118     p_intf->p_sys->i_size = 0;
119     p_intf->pf_run = Run;
120
121     p_intf->p_sys->p_input = NULL;
122     p_intf->p_sys->p_vout = NULL;
123
124     var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
125     return 0;
126 }
127
128 /*****************************************************************************
129  * Close: destroy interface
130  *****************************************************************************/
131 static void Close( vlc_object_t *p_this )
132 {
133     intf_thread_t *p_intf = (intf_thread_t *)p_this;
134
135     var_DelCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
136     if( p_intf->p_sys->p_input )
137     {
138         vlc_object_release( p_intf->p_sys->p_input );
139     }
140     if( p_intf->p_sys->p_vout )
141     {
142         vlc_object_release( p_intf->p_sys->p_vout );
143     }
144     /* Destroy structure */
145     free( p_intf->p_sys );
146 }
147
148 /*****************************************************************************
149  * Run: main loop
150  *****************************************************************************/
151 static void Run( intf_thread_t *p_intf )
152 {
153     playlist_t *p_playlist = NULL;
154     input_thread_t *p_input = NULL;
155     vout_thread_t *p_vout = NULL;
156     vout_thread_t *p_last_vout = NULL;
157     struct hotkey *p_hotkeys = p_intf->p_vlc->p_hotkeys;
158     vlc_value_t val;
159     int i;
160
161     /* Initialize hotkey structure */
162     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
163     {
164         var_Create( p_intf->p_vlc, p_hotkeys[i].psz_action,
165                     VLC_VAR_HOTKEY | VLC_VAR_DOINHERIT );
166
167         var_AddCallback( p_intf->p_vlc, p_hotkeys[i].psz_action,
168                          ActionKeyCB, NULL );
169         var_Get( p_intf->p_vlc, p_hotkeys[i].psz_action, &val );
170         var_Set( p_intf->p_vlc, p_hotkeys[i].psz_action, val );
171     }
172
173     while( !p_intf->b_die )
174     {
175         int i_key, i_action;
176         int i_times = 0;
177
178         /* Sleep a bit */
179         msleep( INTF_IDLE_SLEEP );
180
181         /* Update the input */
182         if( p_intf->p_sys->p_input == NULL )
183         {
184             p_playlist = (playlist_t *)vlc_object_find( p_intf,
185                                          VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
186             if( p_playlist )
187             {
188                 p_intf->p_sys->p_input = p_playlist->p_input;
189                 if( p_intf->p_sys->p_input )
190                     vlc_object_yield( p_intf->p_sys->p_input );
191                 vlc_object_release( p_playlist );
192             }
193         }
194         else if( p_intf->p_sys->p_input->b_dead )
195         {
196             vlc_object_release( p_intf->p_sys->p_input );
197             p_intf->p_sys->p_input = NULL;
198         }
199         p_input = p_intf->p_sys->p_input;
200
201         /* Update the vout */
202         p_last_vout = p_intf->p_sys->p_vout;
203         if( p_vout == NULL )
204         {
205             p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
206             p_intf->p_sys->p_vout = p_vout;
207         }
208         else if( p_vout->b_die )
209         {
210             vlc_object_release( p_vout );
211             p_vout = NULL;
212             p_intf->p_sys->p_vout = NULL;
213         }
214
215         /* Register OSD channels */
216         if( p_vout && p_vout != p_last_vout )
217         {
218             for( i = 0; i < CHANNELS_NUMBER; i++ )
219             {
220                 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
221                              &p_intf->p_sys->p_channels[ i ] );
222             }
223         }
224
225         /* Find action triggered by hotkey */
226         i_action = 0;
227         i_key = GetKey( p_intf );
228         for( i = 0; i_key != -1 && p_hotkeys[i].psz_action != NULL; i++ )
229         {
230             if( p_hotkeys[i].i_key == i_key )
231             {
232                 i_action = p_hotkeys[i].i_action;
233                 i_times  = p_hotkeys[i].i_times;
234                 /* times key pressed within max. delta time */
235                 p_hotkeys[i].i_times = 0;
236             }
237         }
238
239         if( !i_action )
240         {
241             /* No key pressed, sleep a bit more */
242             msleep( INTF_IDLE_SLEEP );
243             continue;
244         }
245
246         if( i_action == ACTIONID_QUIT )
247         {
248             p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
249                                     FIND_ANYWHERE );
250             if( p_playlist )
251             {
252                 playlist_Stop( p_playlist );
253                 vlc_object_release( p_playlist );
254             }
255             /* Playlist is stopped now kill vlc. */
256             p_intf->p_vlc->b_die = VLC_TRUE;
257             ClearChannels( p_intf, p_vout );
258             vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Quit" ) );
259             continue;
260         }
261         else if( i_action == ACTIONID_VOL_UP )
262         {
263             audio_volume_t i_newvol;
264             aout_VolumeUp( p_intf, 1, &i_newvol );
265             DisplayVolume( p_intf, p_vout, i_newvol );
266         }
267         else if( i_action == ACTIONID_VOL_DOWN )
268         {
269             audio_volume_t i_newvol;
270             aout_VolumeDown( p_intf, 1, &i_newvol );
271             DisplayVolume( p_intf, p_vout, i_newvol );
272         }
273         else if( i_action == ACTIONID_VOL_MUTE )
274         {
275             audio_volume_t i_newvol = -1;
276             aout_VolumeMute( p_intf, &i_newvol );
277             if( p_vout )
278             {
279                 if( i_newvol == 0 )
280                 {
281                     ClearChannels( p_intf, p_vout );
282                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
283                                   OSD_MUTE_ICON );
284                 }
285                 else
286                 {
287                     DisplayVolume( p_intf, p_vout, i_newvol );
288                 }
289             }
290         }
291         else if( i_action == ACTIONID_INTF_SHOW )
292         {
293             val.b_bool = VLC_TRUE;
294             p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
295                                           FIND_ANYWHERE );
296             if( p_playlist )
297             {
298                 var_Set( p_playlist, "intf-show", val );
299                 vlc_object_release( p_playlist );
300             }
301         }
302         else if( i_action == ACTIONID_INTF_HIDE )
303         {
304             val.b_bool = VLC_FALSE;
305             p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
306                                           FIND_ANYWHERE );
307             if( p_playlist )
308             {
309                 var_Set( p_playlist, "intf-show", val );
310                 vlc_object_release( p_playlist );
311             }
312         }
313         else if( i_action == ACTIONID_SNAPSHOT )
314         {
315             if( p_vout ) vout_Control( p_vout, VOUT_SNAPSHOT );
316         }
317         else if( i_action == ACTIONID_FULLSCREEN )
318         {
319             if( p_vout )
320             {
321                 var_Get( p_vout, "fullscreen", &val );
322                 val.b_bool = !val.b_bool;
323                 var_Set( p_vout, "fullscreen", val );
324             }
325             else
326             {
327                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
328                                           FIND_ANYWHERE );
329                 if( p_playlist )
330                 {
331                     var_Get( p_playlist, "fullscreen", &val );
332                     val.b_bool = !val.b_bool;
333                     var_Set( p_playlist, "fullscreen", val );
334                     vlc_object_release( p_playlist );
335                 }
336             }
337         }
338         else if( i_action == ACTIONID_PLAY_PAUSE )
339         {
340             val.i_int = PLAYING_S;
341             if( p_input )
342             {
343                 var_Get( p_input, "state", &val );
344             }
345             if( p_input && val.i_int != PAUSE_S )
346             {
347                 ClearChannels( p_intf, p_vout );
348                 vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
349                               OSD_PAUSE_ICON );
350                 val.i_int = PAUSE_S;
351                 var_Set( p_input, "state", val );
352             }
353             else
354             {
355                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
356                                               FIND_ANYWHERE );
357                 if( p_playlist )
358                 {
359                     ClearChannels( p_intf, p_vout );
360                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
361                                   OSD_PLAY_ICON );
362                     playlist_Play( p_playlist );
363                     vlc_object_release( p_playlist );
364                 }
365             }
366         }
367         else if( p_input )
368         {
369             /* FIXME --fenrir
370              * How to get a valid value ?
371              * That's not that easy with some special stream
372              */
373             vlc_bool_t b_seekable = VLC_TRUE;
374             int i_interval =0;
375
376             if( i_action == ACTIONID_PAUSE )
377             {
378                 ClearChannels( p_intf, p_vout );
379                 vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
380                               OSD_PAUSE_ICON );
381                 val.i_int = PAUSE_S;
382                 var_Set( p_input, "state", val );
383             }
384             else if( i_action == ACTIONID_JUMP_BACKWARD_EXTRASHORT
385                      && b_seekable )
386             {
387 #define SET_TIME( a, b ) \
388     i_interval = config_GetInt( p_input, a "-jump-size" ); \
389     if( i_interval > 0 ) { \
390         val.i_time = ( (mtime_t)(i_interval * b) * 1000000L \
391                        * ((mtime_t)(1 << i_times))); \
392         var_Set( p_input, "time-offset", val ); \
393         DisplayPosition( p_intf, p_vout, p_input ); \
394     }
395                 SET_TIME( "extrashort", -1 );
396             }
397             else if( i_action == ACTIONID_JUMP_FORWARD_EXTRASHORT && b_seekable )
398             {
399                 SET_TIME( "extrashort", 1 );
400             }
401             else if( i_action == ACTIONID_JUMP_BACKWARD_SHORT && b_seekable )
402             {
403                 SET_TIME( "short", -1 );
404             }
405             else if( i_action == ACTIONID_JUMP_FORWARD_SHORT && b_seekable )
406             {
407                 SET_TIME( "short", 1 );
408             }
409             else if( i_action == ACTIONID_JUMP_BACKWARD_MEDIUM && b_seekable )
410             {
411                 SET_TIME( "medium", -1 );
412             }
413             else if( i_action == ACTIONID_JUMP_FORWARD_MEDIUM && b_seekable )
414             {
415                 SET_TIME( "medium", 1 );
416             }
417             else if( i_action == ACTIONID_JUMP_BACKWARD_LONG && b_seekable )
418             {
419                 SET_TIME( "long", -1 );
420             }
421             else if( i_action == ACTIONID_JUMP_FORWARD_LONG && b_seekable )
422             {
423                 SET_TIME( "long", 1 );
424 #undef SET_TIME
425             }
426             else if( i_action == ACTIONID_AUDIO_TRACK )
427             {
428                 vlc_value_t val, list, list2;
429                 int i_count, i;
430                 var_Get( p_input, "audio-es", &val );
431                 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
432                             &list, &list2 );
433                 i_count = list.p_list->i_count;
434                 if( i_count <= 1 )
435                 {
436                     continue;
437                 }
438                 for( i = 0; i < i_count; i++ )
439                 {
440                     if( val.i_int == list.p_list->p_values[i].i_int )
441                     {
442                         break;
443                     }
444                 }
445                 /* value of audio-es was not in choices list */
446                 if( i == i_count )
447                 {
448                     msg_Warn( p_input,
449                               "invalid current audio track, selecting 0" );
450                     var_Set( p_input, "audio-es",
451                              list.p_list->p_values[0] );
452                     i = 0;
453                 }
454                 else if( i == i_count - 1 )
455                 {
456                     var_Set( p_input, "audio-es",
457                              list.p_list->p_values[1] );
458                     i = 1;
459                 }
460                 else
461                 {
462                     var_Set( p_input, "audio-es",
463                              list.p_list->p_values[i+1] );
464                     i++;
465                 }
466                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
467                                  _("Audio track: %s"),
468                                  list2.p_list->p_values[i].psz_string );
469             }
470             else if( i_action == ACTIONID_SUBTITLE_TRACK )
471             {
472                 vlc_value_t val, list, list2;
473                 int i_count, i;
474                 var_Get( p_input, "spu-es", &val );
475
476                 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
477                             &list, &list2 );
478                 i_count = list.p_list->i_count;
479                 if( i_count <= 1 )
480                 {
481                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
482                                      _("Subtitle track: %s"), _("N/A") );
483                     continue;
484                 }
485                 for( i = 0; i < i_count; i++ )
486                 {
487                     if( val.i_int == list.p_list->p_values[i].i_int )
488                     {
489                         break;
490                     }
491                 }
492                 /* value of spu-es was not in choices list */
493                 if( i == i_count )
494                 {
495                     msg_Warn( p_input,
496                               "invalid current subtitle track, selecting 0" );
497                     var_Set( p_input, "spu-es", list.p_list->p_values[0] );
498                     i = 0;
499                 }
500                 else if( i == i_count - 1 )
501                 {
502                     var_Set( p_input, "spu-es", list.p_list->p_values[0] );
503                     i = 0;
504                 }
505                 else
506                 {
507                     var_Set( p_input, "spu-es", list.p_list->p_values[i+1] );
508                     i = i + 1;
509                 }
510                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
511                                  _("Subtitle track: %s"),
512                                  list2.p_list->p_values[i].psz_string );
513             }
514             else if( i_action == ACTIONID_ASPECT_RATIO && p_vout )
515             {
516                 vlc_value_t val={0}, val_list, text_list;
517                 var_Get( p_vout, "aspect-ratio", &val );
518                 if( var_Change( p_vout, "aspect-ratio", VLC_VAR_GETLIST,
519                                 &val_list, &text_list ) >= 0 )
520                 {
521                     int i;
522                     for( i = 0; i < val_list.p_list->i_count; i++ )
523                     {
524                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
525                                      val.psz_string ) )
526                         {
527                             i++;
528                             break;
529                         }
530                     }
531                     if( i == val_list.p_list->i_count ) i = 0;
532                     var_SetString( p_vout, "aspect-ratio",
533                                    val_list.p_list->p_values[i].psz_string );
534                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
535                                      _("Aspect ratio: %s"),
536                                      text_list.p_list->p_values[i].psz_string );
537                 }
538                 free( val.psz_string );
539             }
540             else if( i_action == ACTIONID_CROP && p_vout )
541             {
542                 vlc_value_t val={0}, val_list, text_list;
543                 var_Get( p_vout, "crop", &val );
544                 if( var_Change( p_vout, "crop", VLC_VAR_GETLIST,
545                                 &val_list, &text_list ) >= 0 )
546                 {
547                     int i;
548                     for( i = 0; i < val_list.p_list->i_count; i++ )
549                     {
550                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
551                                      val.psz_string ) )
552                         {
553                             i++;
554                             break;
555                         }
556                     }
557                     if( i == val_list.p_list->i_count ) i = 0;
558                     var_SetString( p_vout, "crop",
559                                    val_list.p_list->p_values[i].psz_string );
560                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
561                                      _("Crop: %s"),
562                                      text_list.p_list->p_values[i].psz_string );
563                 }
564                 free( val.psz_string );
565             }
566             else if( i_action == ACTIONID_DEINTERLACE && p_vout )
567             {
568                 vlc_value_t val={0}, val_list, text_list;
569                 var_Get( p_vout, "deinterlace", &val );
570                 if( var_Change( p_vout, "deinterlace", VLC_VAR_GETLIST,
571                                 &val_list, &text_list ) >= 0 )
572                 {
573                     int i;
574                     for( i = 0; i < val_list.p_list->i_count; i++ )
575                     {
576                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
577                                      val.psz_string ) )
578                         {
579                             i++;
580                             break;
581                         }
582                     }
583                     if( i == val_list.p_list->i_count ) i = 0;
584                     var_SetString( p_vout, "deinterlace",
585                                    val_list.p_list->p_values[i].psz_string );
586                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
587                                      _("Deinterlace mode: %s"),
588                                      text_list.p_list->p_values[i].psz_string );
589                 }
590                 free( val.psz_string );
591             }
592             else if( ( i_action == ACTIONID_ZOOM || i_action == ACTIONID_UNZOOM ) && p_vout )
593             {
594                 vlc_value_t val={0}, val_list, text_list;
595                 var_Get( p_vout, "zoom", &val );
596                 if( var_Change( p_vout, "zoom", VLC_VAR_GETLIST,
597                                 &val_list, &text_list ) >= 0 )
598                 {
599                     int i;
600                     for( i = 0; i < val_list.p_list->i_count; i++ )
601                     {
602                         if( val_list.p_list->p_values[i].f_float
603                            == val.f_float )
604                         {
605                             if( i_action == ACTIONID_ZOOM )
606                                 i++;
607                             else /* ACTIONID_UNZOOM */
608                                 i--;
609                             break;
610                         }
611                     }
612                     if( i == val_list.p_list->i_count ) i = 0;
613                     if( i == -1 ) i = val_list.p_list->i_count-1;
614                     var_SetFloat( p_vout, "zoom",
615                                   val_list.p_list->p_values[i].f_float );
616                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
617                                      _("Zoom mode: %s"),
618                                 text_list.p_list->p_values[i].var.psz_name );
619                 }
620             }
621             else if( i_action == ACTIONID_CROP_TOP && p_vout )
622             {
623                 int i_val = var_GetInteger( p_vout, "crop-top" );
624                 var_SetInteger( p_vout, "crop-top", i_val+1 );
625             }
626             else if( i_action == ACTIONID_UNCROP_TOP && p_vout )
627             {
628                 int i_val = var_GetInteger( p_vout, "crop-top" );
629                 if( i_val != 0 )
630                     var_SetInteger( p_vout, "crop-top", i_val-1 );
631             }
632             else if( i_action == ACTIONID_CROP_BOTTOM && p_vout )
633             {
634                 int i_val = var_GetInteger( p_vout, "crop-bottom" );
635                 var_SetInteger( p_vout, "crop-bottom", i_val+1 );
636             }
637             else if( i_action == ACTIONID_UNCROP_BOTTOM && p_vout )
638             {
639                 int i_val = var_GetInteger( p_vout, "crop-bottom" );
640                 if( i_val != 0 )
641                     var_SetInteger( p_vout, "crop-bottom", i_val-1 );
642             }
643             else if( i_action == ACTIONID_CROP_LEFT && p_vout )
644             {
645                 int i_val = var_GetInteger( p_vout, "crop-left" );
646                 var_SetInteger( p_vout, "crop-left", i_val+1 );
647             }
648             else if( i_action == ACTIONID_UNCROP_LEFT && p_vout )
649             {
650                 int i_val = var_GetInteger( p_vout, "crop-left" );
651                 if( i_val != 0 )
652                     var_SetInteger( p_vout, "crop-left", i_val-1 );
653             }
654             else if( i_action == ACTIONID_CROP_RIGHT && p_vout )
655             {
656                 int i_val = var_GetInteger( p_vout, "crop-right" );
657                 var_SetInteger( p_vout, "crop-right", i_val+1 );
658             }
659             else if( i_action == ACTIONID_UNCROP_RIGHT && p_vout )
660             {
661                 int i_val = var_GetInteger( p_vout, "crop-right" );
662                 if( i_val != 0 )
663                     var_SetInteger( p_vout, "crop-right", i_val-1 );
664             }
665             else if( i_action == ACTIONID_NEXT )
666             {
667                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
668                                               FIND_ANYWHERE );
669                 if( p_playlist )
670                 {
671                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
672                                      _("Next") );
673                     playlist_Next( p_playlist );
674                     vlc_object_release( p_playlist );
675                 }
676             }
677             else if( i_action == ACTIONID_PREV )
678             {
679                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
680                                               FIND_ANYWHERE );
681                 if( p_playlist )
682                 {
683                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
684                                      _("Previous") );
685                     playlist_Prev( p_playlist );
686                     vlc_object_release( p_playlist );
687                 }
688             }
689             else if( i_action == ACTIONID_STOP )
690             {
691                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
692                                               FIND_ANYWHERE );
693                 if( p_playlist )
694                 {
695                     playlist_Stop( p_playlist );
696                     vlc_object_release( p_playlist );
697                 }
698             }
699             else if( i_action == ACTIONID_FASTER )
700             {
701                 vlc_value_t val;
702                 val.b_bool = VLC_TRUE;
703                 var_Set( p_input, "rate-faster", val );
704                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
705                                  _("Faster") );
706             }
707             else if( i_action == ACTIONID_SLOWER )
708             {
709                 vlc_value_t val;
710                 val.b_bool = VLC_TRUE;
711                 var_Set( p_input, "rate-slower", val );
712                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
713                                  _("Slower") );
714             }
715             else if( i_action == ACTIONID_POSITION && b_seekable )
716             {
717                 DisplayPosition( p_intf, p_vout, p_input );
718             }
719             else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
720                      i_action <= ACTIONID_PLAY_BOOKMARK10 )
721             {
722                 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
723             }
724             else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
725                      i_action <= ACTIONID_SET_BOOKMARK10 )
726             {
727                 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
728             }
729             /* Only makes sense with DVD */
730             else if( i_action == ACTIONID_TITLE_PREV )
731             {
732                 val.b_bool = VLC_TRUE;
733                 var_Set( p_input, "prev-title", val );
734             }
735             else if( i_action == ACTIONID_TITLE_NEXT )
736             {
737                 val.b_bool = VLC_TRUE;
738                 var_Set( p_input, "next-title", val );
739             }
740             else if( i_action == ACTIONID_CHAPTER_PREV )
741             {
742                 val.b_bool = VLC_TRUE;
743                 var_Set( p_input, "prev-chapter", val );
744             }
745             else if( i_action == ACTIONID_CHAPTER_NEXT )
746             {
747                 val.b_bool = VLC_TRUE;
748                 var_Set( p_input, "next-chapter", val );
749             }
750             else if( i_action == ACTIONID_DISC_MENU )
751             {
752                 vlc_value_t val; val.i_int = 2;
753                 var_Set( p_input, "title  0", val);
754             }
755             else if( i_action == ACTIONID_SUBDELAY_DOWN )
756             {
757                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
758
759                 i_delay -= 50000;    /* 50 ms */
760
761                 var_SetTime( p_input, "spu-delay", i_delay );
762                 ClearChannels( p_intf, p_vout );
763                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Subtitle delay %i ms",
764                                  (int)(i_delay/1000) );
765             }
766             else if( i_action == ACTIONID_SUBDELAY_UP )
767             {
768                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
769
770                 i_delay += 50000;    /* 50 ms */
771
772                 var_SetTime( p_input, "spu-delay", i_delay );
773                 ClearChannels( p_intf, p_vout );
774                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Subtitle delay %i ms",
775                                  (int)(i_delay/1000) );
776             }
777             else if( i_action == ACTIONID_AUDIODELAY_DOWN )
778             {
779                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
780
781                 i_delay -= 50000;    /* 50 ms */
782
783                 var_SetTime( p_input, "audio-delay", i_delay );
784                 ClearChannels( p_intf, p_vout );
785                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Audio delay %i ms",
786                                  (int)(i_delay/1000) );
787             }
788             else if( i_action == ACTIONID_AUDIODELAY_UP )
789             {
790                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
791
792                 i_delay += 50000;    /* 50 ms */
793
794                 var_SetTime( p_input, "audio-delay", i_delay );
795                 ClearChannels( p_intf, p_vout );
796                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Audio delay %i ms",
797                                  (int)(i_delay/1000) );
798             }
799             else if( i_action == ACTIONID_PLAY )
800             {
801                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
802                                               FIND_ANYWHERE );
803                 if( p_playlist )
804                 {
805                     var_Get( p_input, "rate", &val );
806                     msg_Dbg( p_input, "rate %d", val.i_int );
807                     if( val.i_int != INPUT_RATE_DEFAULT )
808                     {
809                         /* Return to normal speed */
810                         val.i_int = INPUT_RATE_DEFAULT;
811                         var_Set( p_input, "rate", val );
812                     }
813                     else
814                     {
815                         ClearChannels( p_intf, p_vout );
816                         vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
817                                       OSD_PAUSE_ICON );
818                         playlist_Play( p_playlist );
819                     }
820                     vlc_object_release( p_playlist );
821                 }
822             }
823         }
824     }
825 }
826
827 static int GetKey( intf_thread_t *p_intf)
828 {
829     vlc_mutex_lock( &p_intf->p_sys->change_lock );
830     if ( p_intf->p_sys->i_size == 0 )
831     {
832         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
833         return -1;
834     }
835     else
836     {
837         int i_return = p_intf->p_sys->p_keys[ 0 ];
838         int i;
839         p_intf->p_sys->i_size--;
840         for ( i = 0; i < BUFFER_SIZE - 1; i++)
841         {
842             p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
843         }
844         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
845         return i_return;
846     }
847 }
848
849 /*****************************************************************************
850  * KeyEvent: callback for keyboard events
851  *****************************************************************************/
852 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
853                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
854 {
855     intf_thread_t *p_intf = (intf_thread_t *)p_data;
856     vlc_mutex_lock( &p_intf->p_sys->change_lock );
857     if ( p_intf->p_sys->i_size == BUFFER_SIZE )
858     {
859         msg_Warn( p_intf, "event buffer full, dropping keypress" );
860         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
861         return VLC_EGENERIC;
862     }
863     else
864     {
865         p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
866         p_intf->p_sys->i_size++;
867     }
868     vlc_mutex_unlock( &p_intf->p_sys->change_lock );
869
870     return VLC_SUCCESS;
871 }
872
873 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
874                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
875 {
876     vlc_t *p_vlc = (vlc_t *)p_this;
877     struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
878     mtime_t i_date;
879     int i;
880
881     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
882     {
883         if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
884         {
885             p_hotkeys[i].i_key = newval.i_int;
886             /* do hotkey accounting */
887             i_date = mdate();
888             if( (p_hotkeys[i].i_delta_date > 0) &&
889                 (p_hotkeys[i].i_delta_date <= (i_date - p_hotkeys[i].i_last_date) ) )
890                 p_hotkeys[i].i_times = 0;
891             else
892                 p_hotkeys[i].i_times++;
893             p_hotkeys[i].i_last_date = i_date;
894         }
895     }
896
897     return VLC_SUCCESS;
898 }
899
900 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
901 {
902     vlc_value_t val;
903     int i;
904     char psz_bookmark_name[11];
905     playlist_t *p_playlist =
906         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
907
908     sprintf( psz_bookmark_name, "bookmark%i", i_num );
909     var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
910     var_Get( p_intf, psz_bookmark_name, &val );
911
912     if( p_playlist )
913     {
914         char *psz_bookmark = strdup( val.psz_string );
915         for( i = 0; i < p_playlist->pp_all_items; i++)
916         {
917             if( !strcmp( psz_bookmark,
918                          p_playlist->pp_items[i]->p_input->psz_uri ) )
919             {
920                 playlist_LockControl( p_playlist, PLAYLIST_ITEMPLAY,
921                                       p_playlist->pp_items[i] );
922                 break;
923             }
924         }
925         vlc_object_release( p_playlist );
926     }
927 }
928
929 static void SetBookmark( intf_thread_t *p_intf, int i_num )
930 {
931     playlist_t *p_playlist =
932         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
933     if( p_playlist )
934     {
935         char psz_bookmark_name[11];
936         sprintf( psz_bookmark_name, "bookmark%i", i_num );
937         var_Create( p_intf, psz_bookmark_name,
938                     VLC_VAR_STRING|VLC_VAR_DOINHERIT );
939         if( p_playlist->status.p_item )
940         {
941             config_PutPsz( p_intf, psz_bookmark_name,
942                            p_playlist->status.p_item->p_input->psz_uri);
943             msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num,
944                            p_playlist->status.p_item->p_input->psz_uri);
945             config_SaveConfigFile( p_intf, "hotkeys" );
946         }
947         vlc_object_release( p_playlist );
948     }
949 }
950
951 static void DisplayPosition( intf_thread_t *p_intf, vout_thread_t *p_vout,
952                              input_thread_t *p_input )
953 {
954     char psz_duration[MSTRTIME_MAX_SIZE];
955     char psz_time[MSTRTIME_MAX_SIZE];
956     vlc_value_t time, pos;
957     mtime_t i_seconds;
958
959     if( p_vout == NULL )
960     {
961         return;
962     }
963     ClearChannels( p_intf, p_vout );
964
965     var_Get( p_input, "time", &time );
966     i_seconds = time.i_time / 1000000;
967     secstotimestr ( psz_time, i_seconds );
968
969     var_Get( p_input, "length", &time );
970     if( time.i_time > 0 )
971     {
972         secstotimestr( psz_duration, time.i_time / 1000000 );
973         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, "%s / %s",
974                          psz_time, psz_duration );
975     }
976     else if( i_seconds > 0 )
977     {
978         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, psz_time );
979     }
980
981     if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
982     {
983         var_Get( p_input, "position", &pos );
984         vout_OSDSlider( VLC_OBJECT( p_input ), POSITION_WIDGET_CHAN,
985                         pos.f_float * 100, OSD_HOR_SLIDER );
986     }
987 }
988
989 static void DisplayVolume( intf_thread_t *p_intf, vout_thread_t *p_vout,
990                            audio_volume_t i_vol )
991 {
992     if( p_vout == NULL )
993     {
994         return;
995     }
996     ClearChannels( p_intf, p_vout );
997
998     if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
999     {
1000         vout_OSDSlider( VLC_OBJECT( p_vout ), VOLUME_WIDGET_CHAN,
1001             i_vol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
1002     }
1003     else
1004     {
1005         vout_OSDMessage( p_vout, VOLUME_TEXT_CHAN, "Volume %d%%",
1006                          i_vol*400/AOUT_VOLUME_MAX );
1007     }
1008 }
1009
1010 static void ClearChannels( intf_thread_t *p_intf, vout_thread_t *p_vout )
1011 {
1012     int i;
1013
1014     if( p_vout )
1015     {
1016         spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
1017         for( i = 0; i < CHANNELS_NUMBER; i++ )
1018         {
1019             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1020                          p_intf->p_sys->p_channels[ i ] );
1021         }
1022     }
1023 }