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