Fix a bunch of warnings and errors
[vlc.git] / modules / control / hotkeys.c
1 /*****************************************************************************
2  * hotkeys.c: Hotkey handling for vlc
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *          Jean-Paul Saman <jpsaman #_at_# m2x.nl>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32 #include <vlc/input.h>
33 #include <vlc/vout.h>
34 #include <vlc/aout.h>
35 #include <vlc_osd.h>
36
37 #include "vlc_keys.h"
38
39 #define BUFFER_SIZE 10
40
41 #define CHANNELS_NUMBER 4
42 #define VOLUME_TEXT_CHAN     p_intf->p_sys->p_channels[ 0 ]
43 #define VOLUME_WIDGET_CHAN   p_intf->p_sys->p_channels[ 1 ]
44 #define POSITION_TEXT_CHAN   p_intf->p_sys->p_channels[ 2 ]
45 #define POSITION_WIDGET_CHAN p_intf->p_sys->p_channels[ 3 ]
46 /*****************************************************************************
47  * intf_sys_t: description and status of FB interface
48  *****************************************************************************/
49 struct intf_sys_t
50 {
51     vlc_mutex_t         change_lock;  /* mutex to keep the callback
52                                        * and the main loop from
53                                        * stepping on each others
54                                        * toes */
55     int                 p_keys[ BUFFER_SIZE ]; /* buffer that contains
56                                                 * keyevents */
57     int                 i_size;        /* number of events in buffer */
58     int                 p_channels[ CHANNELS_NUMBER ]; /* contains registered
59                                                         * channel IDs */
60     input_thread_t *    p_input;       /* pointer to input */
61     vout_thread_t *     p_vout;        /* pointer to vout object */
62 };
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67 static int  Open    ( vlc_object_t * );
68 static void Close   ( vlc_object_t * );
69 static void Run     ( intf_thread_t * );
70 static int  GetKey  ( intf_thread_t *);
71 static int  KeyEvent( vlc_object_t *, char const *,
72                       vlc_value_t, vlc_value_t, void * );
73 static int  ActionKeyCB( vlc_object_t *, char const *,
74                          vlc_value_t, vlc_value_t, void * );
75 static void PlayBookmark( intf_thread_t *, int );
76 static void SetBookmark ( intf_thread_t *, int );
77 static void DisplayPosition( intf_thread_t *, vout_thread_t *, input_thread_t * );
78 static void DisplayVolume  ( intf_thread_t *, vout_thread_t *, audio_volume_t );
79 static void ClearChannels  ( intf_thread_t *, vout_thread_t * );
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 #define BOOKMARK1_TEXT    N_("Playlist bookmark 1")
85 #define BOOKMARK2_TEXT    N_("Playlist bookmark 2")
86 #define BOOKMARK3_TEXT    N_("Playlist bookmark 3")
87 #define BOOKMARK4_TEXT    N_("Playlist bookmark 4")
88 #define BOOKMARK5_TEXT    N_("Playlist bookmark 5")
89 #define BOOKMARK6_TEXT    N_("Playlist bookmark 6")
90 #define BOOKMARK7_TEXT    N_("Playlist bookmark 7")
91 #define BOOKMARK8_TEXT    N_("Playlist bookmark 8")
92 #define BOOKMARK9_TEXT    N_("Playlist bookmark 9")
93 #define BOOKMARK10_TEXT   N_("Playlist bookmark 10")
94 #define BOOKMARK_LONGTEXT N_("Define playlist bookmarks.")
95
96 vlc_module_begin();
97     set_shortname( _("Hotkeys") );
98     set_description( _("Hotkeys management interface") );
99     set_capability( "interface", 0 );
100     set_callbacks( Open, Close );
101 vlc_module_end();
102
103 /*****************************************************************************
104  * Open: initialize interface
105  *****************************************************************************/
106 static int Open( vlc_object_t *p_this )
107 {
108     intf_thread_t *p_intf = (intf_thread_t *)p_this;
109
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                 ClearChannels( p_intf, p_vout );
344
345                 var_Get( p_input, "state", &val );
346                 if( val.i_int != PAUSE_S )
347                 {
348                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
349                                   OSD_PAUSE_ICON );
350                     val.i_int = PAUSE_S;
351                 }
352                 else
353                 {
354                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
355                                   OSD_PLAY_ICON );
356                     val.i_int = PLAYING_S;
357                 }
358                 var_Set( p_input, "state", val );
359             }
360             else
361             {
362                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
363                                               FIND_ANYWHERE );
364                 if( p_playlist )
365                 {
366                     playlist_Play( p_playlist );
367                     vlc_object_release( p_playlist );
368                 }
369             }
370         }
371         else if( p_input )
372         {
373             /* FIXME --fenrir
374              * How to get a valid value ?
375              * That's not that easy with some special stream
376              */
377             vlc_bool_t b_seekable = VLC_TRUE;
378             int i_interval =0;
379
380             if( i_action == ACTIONID_PAUSE )
381             {
382                 var_Get( p_input, "state", &val );
383                 if( val.i_int != PAUSE_S )
384                 {
385                     ClearChannels( p_intf, p_vout );
386                     vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
387                                   OSD_PAUSE_ICON );
388                     val.i_int = PAUSE_S;
389                     var_Set( p_input, "state", val );
390                 }
391             }
392             else if( i_action == ACTIONID_JUMP_BACKWARD_EXTRASHORT
393                      && b_seekable )
394             {
395 #define SET_TIME( a, b ) \
396     i_interval = config_GetInt( p_input, a "-jump-size" ); \
397     if( i_interval > 0 ) { \
398         val.i_time = ( (mtime_t)(i_interval * b) * 1000000L \
399                        * ((mtime_t)(1 << i_times))); \
400         var_Set( p_input, "time-offset", val ); \
401         DisplayPosition( p_intf, p_vout, p_input ); \
402     }
403                 SET_TIME( "extrashort", -1 );
404             }
405             else if( i_action == ACTIONID_JUMP_FORWARD_EXTRASHORT && b_seekable )
406             {
407                 SET_TIME( "extrashort", 1 );
408             }
409             else if( i_action == ACTIONID_JUMP_BACKWARD_SHORT && b_seekable )
410             {
411                 SET_TIME( "short", -1 );
412             }
413             else if( i_action == ACTIONID_JUMP_FORWARD_SHORT && b_seekable )
414             {
415                 SET_TIME( "short", 1 );
416             }
417             else if( i_action == ACTIONID_JUMP_BACKWARD_MEDIUM && b_seekable )
418             {
419                 SET_TIME( "medium", -1 );
420             }
421             else if( i_action == ACTIONID_JUMP_FORWARD_MEDIUM && b_seekable )
422             {
423                 SET_TIME( "medium", 1 );
424             }
425             else if( i_action == ACTIONID_JUMP_BACKWARD_LONG && b_seekable )
426             {
427                 SET_TIME( "long", -1 );
428             }
429             else if( i_action == ACTIONID_JUMP_FORWARD_LONG && b_seekable )
430             {
431                 SET_TIME( "long", 1 );
432 #undef SET_TIME
433             }
434             else if( i_action == ACTIONID_AUDIO_TRACK )
435             {
436                 vlc_value_t val, list, list2;
437                 int i_count, i;
438                 var_Get( p_input, "audio-es", &val );
439                 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
440                             &list, &list2 );
441                 i_count = list.p_list->i_count;
442                 if( i_count <= 1 )
443                 {
444                     continue;
445                 }
446                 for( i = 0; i < i_count; i++ )
447                 {
448                     if( val.i_int == list.p_list->p_values[i].i_int )
449                     {
450                         break;
451                     }
452                 }
453                 /* value of audio-es was not in choices list */
454                 if( i == i_count )
455                 {
456                     msg_Warn( p_input,
457                               "invalid current audio track, selecting 0" );
458                     var_Set( p_input, "audio-es",
459                              list.p_list->p_values[0] );
460                     i = 0;
461                 }
462                 else if( i == i_count - 1 )
463                 {
464                     var_Set( p_input, "audio-es",
465                              list.p_list->p_values[1] );
466                     i = 1;
467                 }
468                 else
469                 {
470                     var_Set( p_input, "audio-es",
471                              list.p_list->p_values[i+1] );
472                     i++;
473                 }
474                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
475                                  _("Audio track: %s"),
476                                  list2.p_list->p_values[i].psz_string );
477             }
478             else if( i_action == ACTIONID_SUBTITLE_TRACK )
479             {
480                 vlc_value_t val, list, list2;
481                 int i_count, i;
482                 var_Get( p_input, "spu-es", &val );
483
484                 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
485                             &list, &list2 );
486                 i_count = list.p_list->i_count;
487                 if( i_count <= 1 )
488                 {
489                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
490                                      _("Subtitle track: %s"), _("N/A") );
491                     continue;
492                 }
493                 for( i = 0; i < i_count; i++ )
494                 {
495                     if( val.i_int == list.p_list->p_values[i].i_int )
496                     {
497                         break;
498                     }
499                 }
500                 /* value of spu-es was not in choices list */
501                 if( i == i_count )
502                 {
503                     msg_Warn( p_input,
504                               "invalid current subtitle track, selecting 0" );
505                     var_Set( p_input, "spu-es", list.p_list->p_values[0] );
506                     i = 0;
507                 }
508                 else if( i == i_count - 1 )
509                 {
510                     var_Set( p_input, "spu-es", list.p_list->p_values[0] );
511                     i = 0;
512                 }
513                 else
514                 {
515                     var_Set( p_input, "spu-es", list.p_list->p_values[i+1] );
516                     i = i + 1;
517                 }
518                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
519                                  _("Subtitle track: %s"),
520                                  list2.p_list->p_values[i].psz_string );
521             }
522             else if( i_action == ACTIONID_ASPECT_RATIO && p_vout )
523             {
524                 vlc_value_t val={0}, val_list, text_list;
525                 var_Get( p_vout, "aspect-ratio", &val );
526                 if( var_Change( p_vout, "aspect-ratio", VLC_VAR_GETLIST,
527                                 &val_list, &text_list ) >= 0 )
528                 {
529                     int i;
530                     for( i = 0; i < val_list.p_list->i_count; i++ )
531                     {
532                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
533                                      val.psz_string ) )
534                         {
535                             i++;
536                             break;
537                         }
538                     }
539                     if( i == val_list.p_list->i_count ) i = 0;
540                     var_SetString( p_vout, "aspect-ratio",
541                                    val_list.p_list->p_values[i].psz_string );
542                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
543                                      _("Aspect ratio: %s"),
544                                      text_list.p_list->p_values[i].psz_string );
545                 }
546                 free( val.psz_string );
547             }
548             else if( i_action == ACTIONID_CROP && p_vout )
549             {
550                 vlc_value_t val={0}, val_list, text_list;
551                 var_Get( p_vout, "crop", &val );
552                 if( var_Change( p_vout, "crop", VLC_VAR_GETLIST,
553                                 &val_list, &text_list ) >= 0 )
554                 {
555                     int i;
556                     for( i = 0; i < val_list.p_list->i_count; i++ )
557                     {
558                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
559                                      val.psz_string ) )
560                         {
561                             i++;
562                             break;
563                         }
564                     }
565                     if( i == val_list.p_list->i_count ) i = 0;
566                     var_SetString( p_vout, "crop",
567                                    val_list.p_list->p_values[i].psz_string );
568                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
569                                      _("Crop: %s"),
570                                      text_list.p_list->p_values[i].psz_string );
571                 }
572                 free( val.psz_string );
573             }
574             else if( i_action == ACTIONID_DEINTERLACE && p_vout )
575             {
576                 vlc_value_t val={0}, val_list, text_list;
577                 var_Get( p_vout, "deinterlace", &val );
578                 if( var_Change( p_vout, "deinterlace", VLC_VAR_GETLIST,
579                                 &val_list, &text_list ) >= 0 )
580                 {
581                     int i;
582                     for( i = 0; i < val_list.p_list->i_count; i++ )
583                     {
584                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
585                                      val.psz_string ) )
586                         {
587                             i++;
588                             break;
589                         }
590                     }
591                     if( i == val_list.p_list->i_count ) i = 0;
592                     var_SetString( p_vout, "deinterlace",
593                                    val_list.p_list->p_values[i].psz_string );
594                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
595                                      _("Deinterlace mode: %s"),
596                                      text_list.p_list->p_values[i].psz_string );
597                 }
598                 free( val.psz_string );
599             }
600             else if( ( i_action == ACTIONID_ZOOM || i_action == ACTIONID_UNZOOM ) && p_vout )
601             {
602                 vlc_value_t val={0}, val_list, text_list;
603                 var_Get( p_vout, "zoom", &val );
604                 if( var_Change( p_vout, "zoom", VLC_VAR_GETLIST,
605                                 &val_list, &text_list ) >= 0 )
606                 {
607                     int i;
608                     for( i = 0; i < val_list.p_list->i_count; i++ )
609                     {
610                         if( val_list.p_list->p_values[i].f_float
611                            == val.f_float )
612                         {
613                             if( i_action == ACTIONID_ZOOM )
614                                 i++;
615                             else /* ACTIONID_UNZOOM */
616                                 i--;
617                             break;
618                         }
619                     }
620                     if( i == val_list.p_list->i_count ) i = 0;
621                     if( i == -1 ) i = val_list.p_list->i_count-1;
622                     var_SetFloat( p_vout, "zoom",
623                                   val_list.p_list->p_values[i].f_float );
624                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
625                                      _("Zoom mode: %s"),
626                                 text_list.p_list->p_values[i].var.psz_name );
627                 }
628             }
629             else if( i_action == ACTIONID_CROP_TOP && p_vout )
630             {
631                 int i_val = var_GetInteger( p_vout, "crop-top" );
632                 var_SetInteger( p_vout, "crop-top", i_val+1 );
633             }
634             else if( i_action == ACTIONID_UNCROP_TOP && p_vout )
635             {
636                 int i_val = var_GetInteger( p_vout, "crop-top" );
637                 if( i_val != 0 )
638                     var_SetInteger( p_vout, "crop-top", i_val-1 );
639             }
640             else if( i_action == ACTIONID_CROP_BOTTOM && p_vout )
641             {
642                 int i_val = var_GetInteger( p_vout, "crop-bottom" );
643                 var_SetInteger( p_vout, "crop-bottom", i_val+1 );
644             }
645             else if( i_action == ACTIONID_UNCROP_BOTTOM && p_vout )
646             {
647                 int i_val = var_GetInteger( p_vout, "crop-bottom" );
648                 if( i_val != 0 )
649                     var_SetInteger( p_vout, "crop-bottom", i_val-1 );
650             }
651             else if( i_action == ACTIONID_CROP_LEFT && p_vout )
652             {
653                 int i_val = var_GetInteger( p_vout, "crop-left" );
654                 var_SetInteger( p_vout, "crop-left", i_val+1 );
655             }
656             else if( i_action == ACTIONID_UNCROP_LEFT && p_vout )
657             {
658                 int i_val = var_GetInteger( p_vout, "crop-left" );
659                 if( i_val != 0 )
660                     var_SetInteger( p_vout, "crop-left", i_val-1 );
661             }
662             else if( i_action == ACTIONID_CROP_RIGHT && p_vout )
663             {
664                 int i_val = var_GetInteger( p_vout, "crop-right" );
665                 var_SetInteger( p_vout, "crop-right", i_val+1 );
666             }
667             else if( i_action == ACTIONID_UNCROP_RIGHT && p_vout )
668             {
669                 int i_val = var_GetInteger( p_vout, "crop-right" );
670                 if( i_val != 0 )
671                     var_SetInteger( p_vout, "crop-right", i_val-1 );
672             }
673             else if( i_action == ACTIONID_NEXT )
674             {
675                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
676                                               FIND_ANYWHERE );
677                 if( p_playlist )
678                 {
679                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
680                                      _("Next") );
681                     playlist_Next( p_playlist );
682                     vlc_object_release( p_playlist );
683                 }
684             }
685             else if( i_action == ACTIONID_PREV )
686             {
687                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
688                                               FIND_ANYWHERE );
689                 if( p_playlist )
690                 {
691                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
692                                      _("Previous") );
693                     playlist_Prev( p_playlist );
694                     vlc_object_release( p_playlist );
695                 }
696             }
697             else if( i_action == ACTIONID_STOP )
698             {
699                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
700                                               FIND_ANYWHERE );
701                 if( p_playlist )
702                 {
703                     playlist_Stop( p_playlist );
704                     vlc_object_release( p_playlist );
705                 }
706             }
707             else if( i_action == ACTIONID_FASTER )
708             {
709                 vlc_value_t val;
710                 val.b_bool = VLC_TRUE;
711                 var_Set( p_input, "rate-faster", val );
712                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
713                                  _("Faster") );
714             }
715             else if( i_action == ACTIONID_SLOWER )
716             {
717                 vlc_value_t val;
718                 val.b_bool = VLC_TRUE;
719                 var_Set( p_input, "rate-slower", val );
720                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
721                                  _("Slower") );
722             }
723             else if( i_action == ACTIONID_POSITION && b_seekable )
724             {
725                 DisplayPosition( p_intf, p_vout, p_input );
726             }
727             else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
728                      i_action <= ACTIONID_PLAY_BOOKMARK10 )
729             {
730                 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
731             }
732             else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
733                      i_action <= ACTIONID_SET_BOOKMARK10 )
734             {
735                 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
736             }
737             /* Only makes sense with DVD */
738             else if( i_action == ACTIONID_TITLE_PREV )
739             {
740                 val.b_bool = VLC_TRUE;
741                 var_Set( p_input, "prev-title", val );
742             }
743             else if( i_action == ACTIONID_TITLE_NEXT )
744             {
745                 val.b_bool = VLC_TRUE;
746                 var_Set( p_input, "next-title", val );
747             }
748             else if( i_action == ACTIONID_CHAPTER_PREV )
749             {
750                 val.b_bool = VLC_TRUE;
751                 var_Set( p_input, "prev-chapter", val );
752             }
753             else if( i_action == ACTIONID_CHAPTER_NEXT )
754             {
755                 val.b_bool = VLC_TRUE;
756                 var_Set( p_input, "next-chapter", val );
757             }
758             else if( i_action == ACTIONID_DISC_MENU )
759             {
760                 vlc_value_t val; val.i_int = 2;
761                 var_Set( p_input, "title  0", val);
762             }
763             else if( i_action == ACTIONID_SUBDELAY_DOWN )
764             {
765                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
766
767                 i_delay -= 50000;    /* 50 ms */
768
769                 var_SetTime( p_input, "spu-delay", i_delay );
770                 ClearChannels( p_intf, p_vout );
771                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Subtitle delay %i ms",
772                                  (int)(i_delay/1000) );
773             }
774             else if( i_action == ACTIONID_SUBDELAY_UP )
775             {
776                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
777
778                 i_delay += 50000;    /* 50 ms */
779
780                 var_SetTime( p_input, "spu-delay", i_delay );
781                 ClearChannels( p_intf, p_vout );
782                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Subtitle delay %i ms",
783                                  (int)(i_delay/1000) );
784             }
785             else if( i_action == ACTIONID_AUDIODELAY_DOWN )
786             {
787                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
788
789                 i_delay -= 50000;    /* 50 ms */
790
791                 var_SetTime( p_input, "audio-delay", i_delay );
792                 ClearChannels( p_intf, p_vout );
793                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Audio delay %i ms",
794                                  (int)(i_delay/1000) );
795             }
796             else if( i_action == ACTIONID_AUDIODELAY_UP )
797             {
798                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
799
800                 i_delay += 50000;    /* 50 ms */
801
802                 var_SetTime( p_input, "audio-delay", i_delay );
803                 ClearChannels( p_intf, p_vout );
804                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Audio delay %i ms",
805                                  (int)(i_delay/1000) );
806             }
807             else if( i_action == ACTIONID_PLAY )
808             {
809                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
810                                               FIND_ANYWHERE );
811                 if( p_playlist )
812                 {
813                     var_Get( p_input, "rate", &val );
814                     msg_Dbg( p_input, "rate %d", val.i_int );
815                     if( val.i_int != INPUT_RATE_DEFAULT )
816                     {
817                         /* Return to normal speed */
818                         val.i_int = INPUT_RATE_DEFAULT;
819                         var_Set( p_input, "rate", val );
820                     }
821                     else
822                     {
823                         ClearChannels( p_intf, p_vout );
824                         vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
825                                       OSD_PLAY_ICON );
826                         playlist_Play( p_playlist );
827                     }
828                     vlc_object_release( p_playlist );
829                 }
830             }
831         }
832     }
833 }
834
835 static int GetKey( intf_thread_t *p_intf)
836 {
837     vlc_mutex_lock( &p_intf->p_sys->change_lock );
838     if ( p_intf->p_sys->i_size == 0 )
839     {
840         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
841         return -1;
842     }
843     else
844     {
845         int i_return = p_intf->p_sys->p_keys[ 0 ];
846         int i;
847         p_intf->p_sys->i_size--;
848         for ( i = 0; i < BUFFER_SIZE - 1; i++)
849         {
850             p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
851         }
852         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
853         return i_return;
854     }
855 }
856
857 /*****************************************************************************
858  * KeyEvent: callback for keyboard events
859  *****************************************************************************/
860 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
861                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
862 {
863     intf_thread_t *p_intf = (intf_thread_t *)p_data;
864     vlc_mutex_lock( &p_intf->p_sys->change_lock );
865     if ( p_intf->p_sys->i_size == BUFFER_SIZE )
866     {
867         msg_Warn( p_intf, "event buffer full, dropping keypress" );
868         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
869         return VLC_EGENERIC;
870     }
871     else
872     {
873         p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
874         p_intf->p_sys->i_size++;
875     }
876     vlc_mutex_unlock( &p_intf->p_sys->change_lock );
877
878     return VLC_SUCCESS;
879 }
880
881 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
882                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
883 {
884     vlc_t *p_vlc = (vlc_t *)p_this;
885     struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
886     mtime_t i_date;
887     int i;
888
889     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
890     {
891         if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
892         {
893             p_hotkeys[i].i_key = newval.i_int;
894             /* do hotkey accounting */
895             i_date = mdate();
896             if( (p_hotkeys[i].i_delta_date > 0) &&
897                 (p_hotkeys[i].i_delta_date <= (i_date - p_hotkeys[i].i_last_date) ) )
898                 p_hotkeys[i].i_times = 0;
899             else
900                 p_hotkeys[i].i_times++;
901             p_hotkeys[i].i_last_date = i_date;
902         }
903     }
904
905     return VLC_SUCCESS;
906 }
907
908 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
909 {
910     vlc_value_t val;
911     int i;
912     char psz_bookmark_name[11];
913     playlist_t *p_playlist =
914         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
915
916     sprintf( psz_bookmark_name, "bookmark%i", i_num );
917     var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
918     var_Get( p_intf, psz_bookmark_name, &val );
919
920     if( p_playlist )
921     {
922         char *psz_bookmark = strdup( val.psz_string );
923         for( i = 0; i < p_playlist->i_size; i++)
924         {
925             if( !strcmp( psz_bookmark,
926                          p_playlist->pp_items[i]->p_input->psz_uri ) )
927             {
928                 playlist_LockControl( p_playlist, PLAYLIST_VIEWPLAY, NULL,
929                                       p_playlist->pp_items[i] );
930                 break;
931             }
932         }
933         vlc_object_release( p_playlist );
934     }
935 }
936
937 static void SetBookmark( intf_thread_t *p_intf, int i_num )
938 {
939     playlist_t *p_playlist =
940         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
941     if( p_playlist )
942     {
943         char psz_bookmark_name[11];
944         sprintf( psz_bookmark_name, "bookmark%i", i_num );
945         var_Create( p_intf, psz_bookmark_name,
946                     VLC_VAR_STRING|VLC_VAR_DOINHERIT );
947         if( p_playlist->status.p_item )
948         {
949             config_PutPsz( p_intf, psz_bookmark_name,
950                            p_playlist->status.p_item->p_input->psz_uri);
951             msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num,
952                            p_playlist->status.p_item->p_input->psz_uri);
953             config_SaveConfigFile( p_intf, "hotkeys" );
954         }
955         vlc_object_release( p_playlist );
956     }
957 }
958
959 static void DisplayPosition( intf_thread_t *p_intf, vout_thread_t *p_vout,
960                              input_thread_t *p_input )
961 {
962     char psz_duration[MSTRTIME_MAX_SIZE];
963     char psz_time[MSTRTIME_MAX_SIZE];
964     vlc_value_t time, pos;
965     mtime_t i_seconds;
966
967     if( p_vout == NULL )
968     {
969         return;
970     }
971     ClearChannels( p_intf, p_vout );
972
973     var_Get( p_input, "time", &time );
974     i_seconds = time.i_time / 1000000;
975     secstotimestr ( psz_time, i_seconds );
976
977     var_Get( p_input, "length", &time );
978     if( time.i_time > 0 )
979     {
980         secstotimestr( psz_duration, time.i_time / 1000000 );
981         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, "%s / %s",
982                          psz_time, psz_duration );
983     }
984     else if( i_seconds > 0 )
985     {
986         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, psz_time );
987     }
988
989     if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
990     {
991         var_Get( p_input, "position", &pos );
992         vout_OSDSlider( VLC_OBJECT( p_input ), POSITION_WIDGET_CHAN,
993                         pos.f_float * 100, OSD_HOR_SLIDER );
994     }
995 }
996
997 static void DisplayVolume( intf_thread_t *p_intf, vout_thread_t *p_vout,
998                            audio_volume_t i_vol )
999 {
1000     if( p_vout == NULL )
1001     {
1002         return;
1003     }
1004     ClearChannels( p_intf, p_vout );
1005
1006     if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
1007     {
1008         vout_OSDSlider( VLC_OBJECT( p_vout ), VOLUME_WIDGET_CHAN,
1009             i_vol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
1010     }
1011     else
1012     {
1013         vout_OSDMessage( p_vout, VOLUME_TEXT_CHAN, "Volume %d%%",
1014                          i_vol*400/AOUT_VOLUME_MAX );
1015     }
1016 }
1017
1018 static void ClearChannels( intf_thread_t *p_intf, vout_thread_t *p_vout )
1019 {
1020     int i;
1021
1022     if( p_vout )
1023     {
1024         spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
1025         for( i = 0; i < CHANNELS_NUMBER; i++ )
1026         {
1027             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1028                          p_intf->p_sys->p_channels[ i ] );
1029         }
1030     }
1031 }