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