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