]> git.sesse.net Git - vlc/blob - modules/control/hotkeys.c
* control/hotkeys.c: Fix Play/Pause hotkey
[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                 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                 ClearChannels( p_intf, p_vout );
383                 vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
384                               OSD_PAUSE_ICON );
385                 val.i_int = PAUSE_S;
386                 var_Set( p_input, "state", val );
387             }
388             else if( i_action == ACTIONID_JUMP_BACKWARD_EXTRASHORT
389                      && b_seekable )
390             {
391 #define SET_TIME( a, b ) \
392     i_interval = config_GetInt( p_input, a "-jump-size" ); \
393     if( i_interval > 0 ) { \
394         val.i_time = ( (mtime_t)(i_interval * b) * 1000000L \
395                        * ((mtime_t)(1 << i_times))); \
396         var_Set( p_input, "time-offset", val ); \
397         DisplayPosition( p_intf, p_vout, p_input ); \
398     }
399                 SET_TIME( "extrashort", -1 );
400             }
401             else if( i_action == ACTIONID_JUMP_FORWARD_EXTRASHORT && b_seekable )
402             {
403                 SET_TIME( "extrashort", 1 );
404             }
405             else if( i_action == ACTIONID_JUMP_BACKWARD_SHORT && b_seekable )
406             {
407                 SET_TIME( "short", -1 );
408             }
409             else if( i_action == ACTIONID_JUMP_FORWARD_SHORT && b_seekable )
410             {
411                 SET_TIME( "short", 1 );
412             }
413             else if( i_action == ACTIONID_JUMP_BACKWARD_MEDIUM && b_seekable )
414             {
415                 SET_TIME( "medium", -1 );
416             }
417             else if( i_action == ACTIONID_JUMP_FORWARD_MEDIUM && b_seekable )
418             {
419                 SET_TIME( "medium", 1 );
420             }
421             else if( i_action == ACTIONID_JUMP_BACKWARD_LONG && b_seekable )
422             {
423                 SET_TIME( "long", -1 );
424             }
425             else if( i_action == ACTIONID_JUMP_FORWARD_LONG && b_seekable )
426             {
427                 SET_TIME( "long", 1 );
428 #undef SET_TIME
429             }
430             else if( i_action == ACTIONID_AUDIO_TRACK )
431             {
432                 vlc_value_t val, list, list2;
433                 int i_count, i;
434                 var_Get( p_input, "audio-es", &val );
435                 var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
436                             &list, &list2 );
437                 i_count = list.p_list->i_count;
438                 if( i_count <= 1 )
439                 {
440                     continue;
441                 }
442                 for( i = 0; i < i_count; i++ )
443                 {
444                     if( val.i_int == list.p_list->p_values[i].i_int )
445                     {
446                         break;
447                     }
448                 }
449                 /* value of audio-es was not in choices list */
450                 if( i == i_count )
451                 {
452                     msg_Warn( p_input,
453                               "invalid current audio track, selecting 0" );
454                     var_Set( p_input, "audio-es",
455                              list.p_list->p_values[0] );
456                     i = 0;
457                 }
458                 else if( i == i_count - 1 )
459                 {
460                     var_Set( p_input, "audio-es",
461                              list.p_list->p_values[1] );
462                     i = 1;
463                 }
464                 else
465                 {
466                     var_Set( p_input, "audio-es",
467                              list.p_list->p_values[i+1] );
468                     i++;
469                 }
470                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
471                                  _("Audio track: %s"),
472                                  list2.p_list->p_values[i].psz_string );
473             }
474             else if( i_action == ACTIONID_SUBTITLE_TRACK )
475             {
476                 vlc_value_t val, list, list2;
477                 int i_count, i;
478                 var_Get( p_input, "spu-es", &val );
479
480                 var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
481                             &list, &list2 );
482                 i_count = list.p_list->i_count;
483                 if( i_count <= 1 )
484                 {
485                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
486                                      _("Subtitle track: %s"), _("N/A") );
487                     continue;
488                 }
489                 for( i = 0; i < i_count; i++ )
490                 {
491                     if( val.i_int == list.p_list->p_values[i].i_int )
492                     {
493                         break;
494                     }
495                 }
496                 /* value of spu-es was not in choices list */
497                 if( i == i_count )
498                 {
499                     msg_Warn( p_input,
500                               "invalid current subtitle track, selecting 0" );
501                     var_Set( p_input, "spu-es", list.p_list->p_values[0] );
502                     i = 0;
503                 }
504                 else if( i == i_count - 1 )
505                 {
506                     var_Set( p_input, "spu-es", list.p_list->p_values[0] );
507                     i = 0;
508                 }
509                 else
510                 {
511                     var_Set( p_input, "spu-es", list.p_list->p_values[i+1] );
512                     i = i + 1;
513                 }
514                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
515                                  _("Subtitle track: %s"),
516                                  list2.p_list->p_values[i].psz_string );
517             }
518             else if( i_action == ACTIONID_ASPECT_RATIO && p_vout )
519             {
520                 vlc_value_t val={0}, val_list, text_list;
521                 var_Get( p_vout, "aspect-ratio", &val );
522                 if( var_Change( p_vout, "aspect-ratio", VLC_VAR_GETLIST,
523                                 &val_list, &text_list ) >= 0 )
524                 {
525                     int i;
526                     for( i = 0; i < val_list.p_list->i_count; i++ )
527                     {
528                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
529                                      val.psz_string ) )
530                         {
531                             i++;
532                             break;
533                         }
534                     }
535                     if( i == val_list.p_list->i_count ) i = 0;
536                     var_SetString( p_vout, "aspect-ratio",
537                                    val_list.p_list->p_values[i].psz_string );
538                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
539                                      _("Aspect ratio: %s"),
540                                      text_list.p_list->p_values[i].psz_string );
541                 }
542                 free( val.psz_string );
543             }
544             else if( i_action == ACTIONID_CROP && p_vout )
545             {
546                 vlc_value_t val={0}, val_list, text_list;
547                 var_Get( p_vout, "crop", &val );
548                 if( var_Change( p_vout, "crop", VLC_VAR_GETLIST,
549                                 &val_list, &text_list ) >= 0 )
550                 {
551                     int i;
552                     for( i = 0; i < val_list.p_list->i_count; i++ )
553                     {
554                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
555                                      val.psz_string ) )
556                         {
557                             i++;
558                             break;
559                         }
560                     }
561                     if( i == val_list.p_list->i_count ) i = 0;
562                     var_SetString( p_vout, "crop",
563                                    val_list.p_list->p_values[i].psz_string );
564                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
565                                      _("Crop: %s"),
566                                      text_list.p_list->p_values[i].psz_string );
567                 }
568                 free( val.psz_string );
569             }
570             else if( i_action == ACTIONID_DEINTERLACE && p_vout )
571             {
572                 vlc_value_t val={0}, val_list, text_list;
573                 var_Get( p_vout, "deinterlace", &val );
574                 if( var_Change( p_vout, "deinterlace", VLC_VAR_GETLIST,
575                                 &val_list, &text_list ) >= 0 )
576                 {
577                     int i;
578                     for( i = 0; i < val_list.p_list->i_count; i++ )
579                     {
580                         if( !strcmp( val_list.p_list->p_values[i].psz_string,
581                                      val.psz_string ) )
582                         {
583                             i++;
584                             break;
585                         }
586                     }
587                     if( i == val_list.p_list->i_count ) i = 0;
588                     var_SetString( p_vout, "deinterlace",
589                                    val_list.p_list->p_values[i].psz_string );
590                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
591                                      _("Deinterlace mode: %s"),
592                                      text_list.p_list->p_values[i].psz_string );
593                 }
594                 free( val.psz_string );
595             }
596             else if( ( i_action == ACTIONID_ZOOM || i_action == ACTIONID_UNZOOM ) && p_vout )
597             {
598                 vlc_value_t val={0}, val_list, text_list;
599                 var_Get( p_vout, "zoom", &val );
600                 if( var_Change( p_vout, "zoom", VLC_VAR_GETLIST,
601                                 &val_list, &text_list ) >= 0 )
602                 {
603                     int i;
604                     for( i = 0; i < val_list.p_list->i_count; i++ )
605                     {
606                         if( val_list.p_list->p_values[i].f_float
607                            == val.f_float )
608                         {
609                             if( i_action == ACTIONID_ZOOM )
610                                 i++;
611                             else /* ACTIONID_UNZOOM */
612                                 i--;
613                             break;
614                         }
615                     }
616                     if( i == val_list.p_list->i_count ) i = 0;
617                     if( i == -1 ) i = val_list.p_list->i_count-1;
618                     var_SetFloat( p_vout, "zoom",
619                                   val_list.p_list->p_values[i].f_float );
620                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
621                                      _("Zoom mode: %s"),
622                                 text_list.p_list->p_values[i].var.psz_name );
623                 }
624             }
625             else if( i_action == ACTIONID_CROP_TOP && p_vout )
626             {
627                 int i_val = var_GetInteger( p_vout, "crop-top" );
628                 var_SetInteger( p_vout, "crop-top", i_val+1 );
629             }
630             else if( i_action == ACTIONID_UNCROP_TOP && p_vout )
631             {
632                 int i_val = var_GetInteger( p_vout, "crop-top" );
633                 if( i_val != 0 )
634                     var_SetInteger( p_vout, "crop-top", i_val-1 );
635             }
636             else if( i_action == ACTIONID_CROP_BOTTOM && p_vout )
637             {
638                 int i_val = var_GetInteger( p_vout, "crop-bottom" );
639                 var_SetInteger( p_vout, "crop-bottom", i_val+1 );
640             }
641             else if( i_action == ACTIONID_UNCROP_BOTTOM && p_vout )
642             {
643                 int i_val = var_GetInteger( p_vout, "crop-bottom" );
644                 if( i_val != 0 )
645                     var_SetInteger( p_vout, "crop-bottom", i_val-1 );
646             }
647             else if( i_action == ACTIONID_CROP_LEFT && p_vout )
648             {
649                 int i_val = var_GetInteger( p_vout, "crop-left" );
650                 var_SetInteger( p_vout, "crop-left", i_val+1 );
651             }
652             else if( i_action == ACTIONID_UNCROP_LEFT && p_vout )
653             {
654                 int i_val = var_GetInteger( p_vout, "crop-left" );
655                 if( i_val != 0 )
656                     var_SetInteger( p_vout, "crop-left", i_val-1 );
657             }
658             else if( i_action == ACTIONID_CROP_RIGHT && p_vout )
659             {
660                 int i_val = var_GetInteger( p_vout, "crop-right" );
661                 var_SetInteger( p_vout, "crop-right", i_val+1 );
662             }
663             else if( i_action == ACTIONID_UNCROP_RIGHT && p_vout )
664             {
665                 int i_val = var_GetInteger( p_vout, "crop-right" );
666                 if( i_val != 0 )
667                     var_SetInteger( p_vout, "crop-right", i_val-1 );
668             }
669             else if( i_action == ACTIONID_NEXT )
670             {
671                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
672                                               FIND_ANYWHERE );
673                 if( p_playlist )
674                 {
675                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
676                                      _("Next") );
677                     playlist_Next( p_playlist );
678                     vlc_object_release( p_playlist );
679                 }
680             }
681             else if( i_action == ACTIONID_PREV )
682             {
683                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
684                                               FIND_ANYWHERE );
685                 if( p_playlist )
686                 {
687                     vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
688                                      _("Previous") );
689                     playlist_Prev( p_playlist );
690                     vlc_object_release( p_playlist );
691                 }
692             }
693             else if( i_action == ACTIONID_STOP )
694             {
695                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
696                                               FIND_ANYWHERE );
697                 if( p_playlist )
698                 {
699                     playlist_Stop( p_playlist );
700                     vlc_object_release( p_playlist );
701                 }
702             }
703             else if( i_action == ACTIONID_FASTER )
704             {
705                 vlc_value_t val;
706                 val.b_bool = VLC_TRUE;
707                 var_Set( p_input, "rate-faster", val );
708                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
709                                  _("Faster") );
710             }
711             else if( i_action == ACTIONID_SLOWER )
712             {
713                 vlc_value_t val;
714                 val.b_bool = VLC_TRUE;
715                 var_Set( p_input, "rate-slower", val );
716                 vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
717                                  _("Slower") );
718             }
719             else if( i_action == ACTIONID_POSITION && b_seekable )
720             {
721                 DisplayPosition( p_intf, p_vout, p_input );
722             }
723             else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
724                      i_action <= ACTIONID_PLAY_BOOKMARK10 )
725             {
726                 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
727             }
728             else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
729                      i_action <= ACTIONID_SET_BOOKMARK10 )
730             {
731                 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
732             }
733             /* Only makes sense with DVD */
734             else if( i_action == ACTIONID_TITLE_PREV )
735             {
736                 val.b_bool = VLC_TRUE;
737                 var_Set( p_input, "prev-title", val );
738             }
739             else if( i_action == ACTIONID_TITLE_NEXT )
740             {
741                 val.b_bool = VLC_TRUE;
742                 var_Set( p_input, "next-title", val );
743             }
744             else if( i_action == ACTIONID_CHAPTER_PREV )
745             {
746                 val.b_bool = VLC_TRUE;
747                 var_Set( p_input, "prev-chapter", val );
748             }
749             else if( i_action == ACTIONID_CHAPTER_NEXT )
750             {
751                 val.b_bool = VLC_TRUE;
752                 var_Set( p_input, "next-chapter", val );
753             }
754             else if( i_action == ACTIONID_DISC_MENU )
755             {
756                 vlc_value_t val; val.i_int = 2;
757                 var_Set( p_input, "title  0", val);
758             }
759             else if( i_action == ACTIONID_SUBDELAY_DOWN )
760             {
761                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
762
763                 i_delay -= 50000;    /* 50 ms */
764
765                 var_SetTime( p_input, "spu-delay", i_delay );
766                 ClearChannels( p_intf, p_vout );
767                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Subtitle delay %i ms",
768                                  (int)(i_delay/1000) );
769             }
770             else if( i_action == ACTIONID_SUBDELAY_UP )
771             {
772                 int64_t i_delay = var_GetTime( p_input, "spu-delay" );
773
774                 i_delay += 50000;    /* 50 ms */
775
776                 var_SetTime( p_input, "spu-delay", i_delay );
777                 ClearChannels( p_intf, p_vout );
778                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Subtitle delay %i ms",
779                                  (int)(i_delay/1000) );
780             }
781             else if( i_action == ACTIONID_AUDIODELAY_DOWN )
782             {
783                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
784
785                 i_delay -= 50000;    /* 50 ms */
786
787                 var_SetTime( p_input, "audio-delay", i_delay );
788                 ClearChannels( p_intf, p_vout );
789                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Audio delay %i ms",
790                                  (int)(i_delay/1000) );
791             }
792             else if( i_action == ACTIONID_AUDIODELAY_UP )
793             {
794                 int64_t i_delay = var_GetTime( p_input, "audio-delay" );
795
796                 i_delay += 50000;    /* 50 ms */
797
798                 var_SetTime( p_input, "audio-delay", i_delay );
799                 ClearChannels( p_intf, p_vout );
800                 vout_OSDMessage( p_intf, DEFAULT_CHAN, "Audio delay %i ms",
801                                  (int)(i_delay/1000) );
802             }
803             else if( i_action == ACTIONID_PLAY )
804             {
805                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
806                                               FIND_ANYWHERE );
807                 if( p_playlist )
808                 {
809                     var_Get( p_input, "rate", &val );
810                     msg_Dbg( p_input, "rate %d", val.i_int );
811                     if( val.i_int != INPUT_RATE_DEFAULT )
812                     {
813                         /* Return to normal speed */
814                         val.i_int = INPUT_RATE_DEFAULT;
815                         var_Set( p_input, "rate", val );
816                     }
817                     else
818                     {
819                         ClearChannels( p_intf, p_vout );
820                         vout_OSDIcon( VLC_OBJECT( p_intf ), DEFAULT_CHAN,
821                                       OSD_PAUSE_ICON );
822                         playlist_Play( p_playlist );
823                     }
824                     vlc_object_release( p_playlist );
825                 }
826             }
827         }
828     }
829 }
830
831 static int GetKey( intf_thread_t *p_intf)
832 {
833     vlc_mutex_lock( &p_intf->p_sys->change_lock );
834     if ( p_intf->p_sys->i_size == 0 )
835     {
836         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
837         return -1;
838     }
839     else
840     {
841         int i_return = p_intf->p_sys->p_keys[ 0 ];
842         int i;
843         p_intf->p_sys->i_size--;
844         for ( i = 0; i < BUFFER_SIZE - 1; i++)
845         {
846             p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
847         }
848         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
849         return i_return;
850     }
851 }
852
853 /*****************************************************************************
854  * KeyEvent: callback for keyboard events
855  *****************************************************************************/
856 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
857                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
858 {
859     intf_thread_t *p_intf = (intf_thread_t *)p_data;
860     vlc_mutex_lock( &p_intf->p_sys->change_lock );
861     if ( p_intf->p_sys->i_size == BUFFER_SIZE )
862     {
863         msg_Warn( p_intf, "event buffer full, dropping keypress" );
864         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
865         return VLC_EGENERIC;
866     }
867     else
868     {
869         p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
870         p_intf->p_sys->i_size++;
871     }
872     vlc_mutex_unlock( &p_intf->p_sys->change_lock );
873
874     return VLC_SUCCESS;
875 }
876
877 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
878                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
879 {
880     vlc_t *p_vlc = (vlc_t *)p_this;
881     struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
882     mtime_t i_date;
883     int i;
884
885     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
886     {
887         if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
888         {
889             p_hotkeys[i].i_key = newval.i_int;
890             /* do hotkey accounting */
891             i_date = mdate();
892             if( (p_hotkeys[i].i_delta_date > 0) &&
893                 (p_hotkeys[i].i_delta_date <= (i_date - p_hotkeys[i].i_last_date) ) )
894                 p_hotkeys[i].i_times = 0;
895             else
896                 p_hotkeys[i].i_times++;
897             p_hotkeys[i].i_last_date = i_date;
898         }
899     }
900
901     return VLC_SUCCESS;
902 }
903
904 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
905 {
906     vlc_value_t val;
907     int i;
908     char psz_bookmark_name[11];
909     playlist_t *p_playlist =
910         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
911
912     sprintf( psz_bookmark_name, "bookmark%i", i_num );
913     var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
914     var_Get( p_intf, psz_bookmark_name, &val );
915
916     if( p_playlist )
917     {
918         char *psz_bookmark = strdup( val.psz_string );
919         for( i = 0; i < p_playlist->pp_all_items; i++)
920         {
921             if( !strcmp( psz_bookmark,
922                          p_playlist->pp_items[i]->p_input->psz_uri ) )
923             {
924                 playlist_LockControl( p_playlist, PLAYLIST_ITEMPLAY,
925                                       p_playlist->pp_items[i] );
926                 break;
927             }
928         }
929         vlc_object_release( p_playlist );
930     }
931 }
932
933 static void SetBookmark( intf_thread_t *p_intf, int i_num )
934 {
935     playlist_t *p_playlist =
936         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
937     if( p_playlist )
938     {
939         char psz_bookmark_name[11];
940         sprintf( psz_bookmark_name, "bookmark%i", i_num );
941         var_Create( p_intf, psz_bookmark_name,
942                     VLC_VAR_STRING|VLC_VAR_DOINHERIT );
943         if( p_playlist->status.p_item )
944         {
945             config_PutPsz( p_intf, psz_bookmark_name,
946                            p_playlist->status.p_item->p_input->psz_uri);
947             msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num,
948                            p_playlist->status.p_item->p_input->psz_uri);
949             config_SaveConfigFile( p_intf, "hotkeys" );
950         }
951         vlc_object_release( p_playlist );
952     }
953 }
954
955 static void DisplayPosition( intf_thread_t *p_intf, vout_thread_t *p_vout,
956                              input_thread_t *p_input )
957 {
958     char psz_duration[MSTRTIME_MAX_SIZE];
959     char psz_time[MSTRTIME_MAX_SIZE];
960     vlc_value_t time, pos;
961     mtime_t i_seconds;
962
963     if( p_vout == NULL )
964     {
965         return;
966     }
967     ClearChannels( p_intf, p_vout );
968
969     var_Get( p_input, "time", &time );
970     i_seconds = time.i_time / 1000000;
971     secstotimestr ( psz_time, i_seconds );
972
973     var_Get( p_input, "length", &time );
974     if( time.i_time > 0 )
975     {
976         secstotimestr( psz_duration, time.i_time / 1000000 );
977         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, "%s / %s",
978                          psz_time, psz_duration );
979     }
980     else if( i_seconds > 0 )
981     {
982         vout_OSDMessage( p_input, POSITION_TEXT_CHAN, psz_time );
983     }
984
985     if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
986     {
987         var_Get( p_input, "position", &pos );
988         vout_OSDSlider( VLC_OBJECT( p_input ), POSITION_WIDGET_CHAN,
989                         pos.f_float * 100, OSD_HOR_SLIDER );
990     }
991 }
992
993 static void DisplayVolume( intf_thread_t *p_intf, vout_thread_t *p_vout,
994                            audio_volume_t i_vol )
995 {
996     if( p_vout == NULL )
997     {
998         return;
999     }
1000     ClearChannels( p_intf, p_vout );
1001
1002     if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
1003     {
1004         vout_OSDSlider( VLC_OBJECT( p_vout ), VOLUME_WIDGET_CHAN,
1005             i_vol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
1006     }
1007     else
1008     {
1009         vout_OSDMessage( p_vout, VOLUME_TEXT_CHAN, "Volume %d%%",
1010                          i_vol*400/AOUT_VOLUME_MAX );
1011     }
1012 }
1013
1014 static void ClearChannels( intf_thread_t *p_intf, vout_thread_t *p_vout )
1015 {
1016     int i;
1017
1018     if( p_vout )
1019     {
1020         spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
1021         for( i = 0; i < CHANNELS_NUMBER; i++ )
1022         {
1023             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1024                          p_intf->p_sys->p_channels[ i ] );
1025         }
1026     }
1027 }