]> git.sesse.net Git - vlc/blob - modules/control/hotkeys.c
* modules/control/hotkeys.c: don't display OSD slider if stream is not seekable...
[vlc] / modules / control / hotkeys.c
1 /*****************************************************************************
2  * hotkeys.c: Hotkey handling for vlc
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31 #include <vlc/input.h>
32 #include <vlc/vout.h>
33 #include <vlc/aout.h>
34 #include <osd.h>
35
36 #include "vlc_keys.h"
37
38 #define BUFFER_SIZE 10
39 /*****************************************************************************
40  * intf_sys_t: description and status of FB interface
41  *****************************************************************************/
42 struct intf_sys_t
43 {
44     vlc_mutex_t         change_lock;  /* mutex to keep the callback
45                                        * and the main loop from
46                                        * stepping on each others
47                                        * toes */
48     int                 p_keys[ BUFFER_SIZE ]; /* buffer that contains
49                                                 * keyevents */
50     int                 i_size;        /* number of events in buffer */
51     input_thread_t *    p_input;       /* pointer to input */
52     vout_thread_t *     p_vout;        /* pointer to vout object */
53 };
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int  Open    ( vlc_object_t * );
59 static void Close   ( vlc_object_t * );
60 static void Run     ( intf_thread_t * );
61 static int  GetKey  ( intf_thread_t *);
62 static int  KeyEvent( vlc_object_t *, char const *,
63                       vlc_value_t, vlc_value_t, void * );
64 static int  ActionKeyCB( vlc_object_t *, char const *,
65                          vlc_value_t, vlc_value_t, void * );
66 static void PlayBookmark( intf_thread_t *, int );
67 static void SetBookmark ( intf_thread_t *, int );
68 static void DisplayPosition( vout_thread_t *, input_thread_t * );
69 static void DisplayVolume  ( vout_thread_t *, audio_volume_t );
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 #define BOOKMARK1_TEXT N_("Playlist bookmark 1")
75 #define BOOKMARK2_TEXT N_("Playlist bookmark 2")
76 #define BOOKMARK3_TEXT N_("Playlist bookmark 3")
77 #define BOOKMARK4_TEXT N_("Playlist bookmark 4")
78 #define BOOKMARK5_TEXT N_("Playlist bookmark 5")
79 #define BOOKMARK6_TEXT N_("Playlist bookmark 6")
80 #define BOOKMARK7_TEXT N_("Playlist bookmark 7")
81 #define BOOKMARK8_TEXT N_("Playlist bookmark 8")
82 #define BOOKMARK9_TEXT N_("Playlist bookmark 9")
83 #define BOOKMARK10_TEXT N_("Playlist bookmark 10")
84 #define BOOKMARK_LONGTEXT N_( \
85     "This option allows you to define playlist bookmarks.")
86
87 vlc_module_begin();
88     set_description( _("Hotkeys management interface") );
89     add_string( "bookmark1", NULL, NULL,
90                 BOOKMARK1_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
91     add_string( "bookmark2", NULL, NULL,
92                 BOOKMARK2_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
93     add_string( "bookmark3", NULL, NULL,
94                 BOOKMARK3_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
95     add_string( "bookmark4", NULL, NULL,
96                 BOOKMARK4_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
97     add_string( "bookmark5", NULL, NULL,
98                 BOOKMARK5_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
99     add_string( "bookmark6", NULL, NULL,
100                 BOOKMARK6_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
101     add_string( "bookmark7", NULL, NULL,
102                 BOOKMARK7_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
103     add_string( "bookmark8", NULL, NULL,
104                 BOOKMARK8_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
105     add_string( "bookmark9", NULL, NULL,
106                 BOOKMARK9_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
107     add_string( "bookmark10", NULL, NULL,
108                 BOOKMARK10_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
109
110     set_capability( "interface", 0 );
111     set_callbacks( Open, Close );
112 vlc_module_end();
113
114 /*****************************************************************************
115  * Open: initialize interface
116  *****************************************************************************/
117 static int Open( vlc_object_t *p_this )
118 {
119     intf_thread_t *p_intf = (intf_thread_t *)p_this;
120
121     /* Allocate instance and initialize some members */
122     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
123     if( p_intf->p_sys == NULL )
124     {
125         msg_Err( p_intf, "out of memory" );
126         return 1;
127     }
128     vlc_mutex_init( p_intf, &p_intf->p_sys->change_lock );
129     p_intf->p_sys->i_size = 0;
130     p_intf->pf_run = Run;
131
132     p_intf->p_sys->p_input = NULL;
133     p_intf->p_sys->p_vout = NULL;
134
135     var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
136     return 0;
137 }
138
139 /*****************************************************************************
140  * Close: destroy interface
141  *****************************************************************************/
142 static void Close( vlc_object_t *p_this )
143 {
144     intf_thread_t *p_intf = (intf_thread_t *)p_this;
145
146     if( p_intf->p_sys->p_input )
147     {
148         vlc_object_release( p_intf->p_sys->p_input );
149     }
150     if( p_intf->p_sys->p_vout )
151     {
152         vlc_object_release( p_intf->p_sys->p_vout );
153     }
154     /* Destroy structure */
155     free( p_intf->p_sys );
156 }
157
158 /*****************************************************************************
159  * Run: main loop
160  *****************************************************************************/
161 static void Run( intf_thread_t *p_intf )
162 {
163     playlist_t *p_playlist;
164     input_thread_t *p_input;
165     vout_thread_t *p_vout = NULL;
166     struct hotkey *p_hotkeys = p_intf->p_vlc->p_hotkeys;
167     vlc_value_t val;
168     int i;
169
170     /* Initialize hotkey structure */
171     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
172     {
173         var_Create( p_intf->p_vlc, p_hotkeys[i].psz_action,
174                     VLC_VAR_HOTKEY | VLC_VAR_DOINHERIT );
175
176         var_AddCallback( p_intf->p_vlc, p_hotkeys[i].psz_action,
177                          ActionKeyCB, NULL );
178         var_Get( p_intf->p_vlc, p_hotkeys[i].psz_action, &val );
179         var_Set( p_intf->p_vlc, p_hotkeys[i].psz_action, val );
180     }
181
182     while( !p_intf->b_die )
183     {
184         int i_key, i_action;
185
186         /* Sleep a bit */
187         msleep( INTF_IDLE_SLEEP );
188
189         /* Update the input */
190         if( p_intf->p_sys->p_input == NULL )
191         {
192             p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
193                                                       FIND_ANYWHERE );
194         }
195         else if( p_intf->p_sys->p_input->b_dead )
196         {
197             vlc_object_release( p_intf->p_sys->p_input );
198             p_intf->p_sys->p_input = NULL;
199         }
200         p_input = p_intf->p_sys->p_input;
201
202         /* Update the 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         /* Find action triggered by hotkey */
216         i_action = 0;
217         i_key = GetKey( p_intf );
218         for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
219         {
220             if( p_hotkeys[i].i_key == i_key )
221             {
222                  i_action = p_hotkeys[i].i_action;
223             }
224         }
225
226         if( !i_action )
227         {
228             /* No key pressed, sleep a bit more */
229             msleep( INTF_IDLE_SLEEP );
230             continue;
231         }
232
233         if( i_action == ACTIONID_QUIT )
234         {
235             p_intf->p_vlc->b_die = VLC_TRUE;
236             vout_OSDMessage( p_intf, SOLO_CHAN, _( "Quit" ) );
237             continue;
238         }
239         else if( i_action == ACTIONID_VOL_UP )
240         {
241             audio_volume_t i_newvol;
242             aout_VolumeUp( p_intf, 1, &i_newvol );
243             DisplayVolume( p_vout, i_newvol );
244         }
245         else if( i_action == ACTIONID_VOL_DOWN )
246         {
247             audio_volume_t i_newvol;
248             aout_VolumeDown( p_intf, 1, &i_newvol );
249             DisplayVolume( p_vout, i_newvol );
250         }
251         else if( i_action == ACTIONID_VOL_MUTE )
252         {
253             audio_volume_t i_newvol = -1;
254             aout_VolumeMute( p_intf, &i_newvol );
255             if( p_vout )
256             {
257                 if( i_newvol == 0 )
258                 {
259                     vout_OSDIcon( VLC_OBJECT( p_intf ), OSD_MUTE_ICON );
260                 }
261                 else
262                 {
263                     DisplayVolume( p_vout, i_newvol );
264                 }
265             }
266         }
267
268         else if( i_action == ACTIONID_SUBDELAY_DOWN )
269         {
270             int i_delay;
271             if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
272                 VLC_SUCCESS )
273             {
274                 i_delay--;
275                 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
276                 vout_OSDMessage( p_intf, SOLO_CHAN, "Subtitle delay %i ms",
277                                  i_delay*100);
278             }
279         }
280         else if( i_action == ACTIONID_SUBDELAY_UP )
281         {
282             int i_delay;
283             if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
284                 VLC_SUCCESS )
285             {
286                 i_delay++;
287                 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
288                 vout_OSDMessage( p_intf, SOLO_CHAN, "Subtitle delay %i ms", 
289                                  i_delay*100);
290             }
291         }
292         else if( i_action == ACTIONID_FULLSCREEN && p_vout )
293         {
294             var_Get( p_vout, "fullscreen", &val );
295             var_Set( p_vout, "fullscreen", (vlc_value_t)!val.b_bool );
296         }
297         else if( i_action == ACTIONID_PLAY )
298         {
299             p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
300                                           FIND_ANYWHERE );
301             if( p_playlist )
302             {
303                 playlist_Play( p_playlist );
304                 vlc_object_release( p_playlist );
305             }
306         }
307         else if( i_action == ACTIONID_PLAY_PAUSE )
308         {
309             val.i_int = PLAYING_S;
310             if( p_input )
311             {
312                 var_Get( p_input, "state", &val );
313             }
314             if( p_input && val.i_int != PAUSE_S )
315             {
316                 vout_OSDIcon( VLC_OBJECT( p_intf ), OSD_PAUSE_ICON );
317                 val.i_int = PAUSE_S;
318                 var_Set( p_input, "state", val );
319             }
320             else
321             {
322                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
323                                               FIND_ANYWHERE );
324                 if( p_playlist )
325                 {
326                     vout_OSDIcon( VLC_OBJECT( p_intf ), OSD_PLAY_ICON );
327                     playlist_Play( p_playlist );
328                     vlc_object_release( p_playlist );
329                 }
330             }
331         }
332         else if( p_input )
333         {
334             vlc_bool_t b_seekable = p_input->stream.b_seekable;
335
336             if( i_action == ACTIONID_PAUSE )
337             {
338                 vout_OSDMessage( p_intf, SOLO_CHAN, _( "Pause" ) );
339                 val.i_int = PAUSE_S;
340                 var_Set( p_input, "state", val );
341             }
342             else if( i_action == ACTIONID_JUMP_BACKWARD_10SEC && b_seekable )
343             {
344                 val.i_time = -10000000;
345                 var_Set( p_input, "time-offset", val );
346                 DisplayPosition( p_vout, p_input );
347             }
348             else if( i_action == ACTIONID_JUMP_FORWARD_10SEC && b_seekable )
349             {
350                 val.i_time = 10000000;
351                 var_Set( p_input, "time-offset", val );
352                 DisplayPosition( p_vout, p_input );
353             }
354             else if( i_action == ACTIONID_JUMP_BACKWARD_1MIN && b_seekable )
355             {
356                 val.i_time = -60000000;
357                 var_Set( p_input, "time-offset", val );
358                 DisplayPosition( p_vout, p_input );
359             }
360             else if( i_action == ACTIONID_JUMP_FORWARD_1MIN && b_seekable )
361             {
362                 val.i_time = 60000000;
363                 var_Set( p_input, "time-offset", val );
364                 DisplayPosition( p_vout, p_input );
365             }
366             else if( i_action == ACTIONID_JUMP_BACKWARD_5MIN && b_seekable )
367             {
368                 val.i_time = -300000000;
369                 var_Set( p_input, "time-offset", val );
370                 DisplayPosition( p_vout, p_input );
371             }
372             else if( i_action == ACTIONID_JUMP_FORWARD_5MIN && b_seekable )
373             {
374                 val.i_time = 300000000;
375                 var_Set( p_input, "time-offset", val );
376                 DisplayPosition( p_vout, p_input );
377             }
378             else if( i_action == ACTIONID_NEXT )
379             {
380                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
381                                               FIND_ANYWHERE );
382                 if( p_playlist )
383                 {
384                     playlist_Next( p_playlist );
385                     vlc_object_release( p_playlist );
386                 }
387             }
388             else if( i_action == ACTIONID_PREV )
389             {
390                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
391                                               FIND_ANYWHERE );
392                 if( p_playlist )
393                 {
394                     playlist_Prev( p_playlist );
395                     vlc_object_release( p_playlist );
396                 }
397             }
398             else if( i_action == ACTIONID_STOP )
399             {
400                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
401                                               FIND_ANYWHERE );
402                 if( p_playlist )
403                 {
404                     playlist_Stop( p_playlist );
405                     vlc_object_release( p_playlist );
406                 }
407             }
408             else if( i_action == ACTIONID_FASTER )
409             {
410                 vlc_value_t val; val.b_bool = VLC_TRUE;
411                 var_Set( p_input, "rate-faster", val );
412             }
413             else if( i_action == ACTIONID_SLOWER )
414             {
415                 vlc_value_t val; val.b_bool = VLC_TRUE;
416                 var_Set( p_input, "rate-slower", val );
417             }
418             else if( i_action == ACTIONID_POSITION && b_seekable )
419             {
420                 DisplayPosition( p_vout, p_input );
421             }
422             else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
423                      i_action <= ACTIONID_PLAY_BOOKMARK10 )
424             {
425                 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
426             }
427             else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
428                      i_action <= ACTIONID_SET_BOOKMARK10 )
429             {
430                 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
431             }
432         }
433     }
434 }
435
436 static int GetKey( intf_thread_t *p_intf)
437 {
438     vlc_mutex_lock( &p_intf->p_sys->change_lock );
439     if ( p_intf->p_sys->i_size == 0 )
440     {
441         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
442         return -1;
443     }
444     else
445     {
446         int i_return = p_intf->p_sys->p_keys[ 0 ];
447         int i;
448         p_intf->p_sys->i_size--;
449         for ( i = 0; i < BUFFER_SIZE - 1; i++)
450         {
451             p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
452         }
453         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
454         return i_return;
455     }
456 }
457
458 /*****************************************************************************
459  * KeyEvent: callback for keyboard events
460  *****************************************************************************/
461 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
462                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
463 {
464     intf_thread_t *p_intf = (intf_thread_t *)p_data;
465     vlc_mutex_lock( &p_intf->p_sys->change_lock );
466     if ( p_intf->p_sys->i_size == BUFFER_SIZE )
467     {
468         msg_Warn( p_intf, "event buffer full, dropping keypress" );
469         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
470         return VLC_EGENERIC;
471     }
472     else
473     {
474         p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
475         p_intf->p_sys->i_size++;
476     }
477     vlc_mutex_unlock( &p_intf->p_sys->change_lock );
478
479     return VLC_SUCCESS;
480 }
481
482 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
483                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
484 {
485     vlc_t *p_vlc = (vlc_t *)p_this;
486     struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
487     int i;
488
489     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
490     {
491         if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
492         {
493             p_hotkeys[i].i_key = newval.i_int;
494         }
495     }
496
497     return VLC_SUCCESS;
498 }
499
500 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
501 {
502     vlc_value_t val;
503     int i_position;
504     char psz_bookmark_name[11];
505     playlist_t *p_playlist =
506         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
507
508     sprintf( psz_bookmark_name, "bookmark%i", i_num );
509     var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
510     var_Get( p_intf, psz_bookmark_name, &val );
511
512     if( p_playlist )
513     {
514         char *psz_bookmark = strdup( val.psz_string );
515         for( i_position = 0; i_position < p_playlist->i_size; i_position++)
516         {
517             if( !strcmp( psz_bookmark,
518                          p_playlist->pp_items[i_position]->input.psz_uri ) )
519             {
520                 playlist_Goto( p_playlist, i_position );
521                 break;
522             }
523         }
524         vlc_object_release( p_playlist );
525     }
526 }
527
528 static void SetBookmark( intf_thread_t *p_intf, int i_num )
529 {
530     vlc_value_t val;
531     playlist_t *p_playlist =
532         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
533     if( p_playlist )
534     {
535         char psz_bookmark_name[11];
536         sprintf( psz_bookmark_name, "bookmark%i", i_num );
537         var_Create( p_intf, psz_bookmark_name,
538                     VLC_VAR_STRING|VLC_VAR_DOINHERIT );
539         val.psz_string = strdup( p_playlist->pp_items[p_playlist->i_index]->input.psz_uri );
540         var_Set( p_intf, psz_bookmark_name, val );
541         msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num,
542                   val.psz_string );
543         vlc_object_release( p_playlist );
544     }
545 }
546
547 static void DisplayPosition( vout_thread_t *p_vout, input_thread_t *p_input )
548 {
549     char psz_duration[MSTRTIME_MAX_SIZE];
550     char psz_time[MSTRTIME_MAX_SIZE];
551     vlc_value_t time, pos;
552     mtime_t i_seconds;
553
554     if( p_vout == NULL )
555     {
556         return;
557     }
558
559     var_Get( p_input, "time", &time );
560     i_seconds = time.i_time / 1000000;
561     secstotimestr ( psz_time, i_seconds );
562
563     var_Get( p_input, "length", &time );
564     if( time.i_time > 0 )
565     {
566         secstotimestr( psz_duration, time.i_time / 1000000 );
567         vout_OSDMessage( p_input, POSITION_CHAN, "%s / %s",
568                          psz_time, psz_duration );
569     }
570     else if( i_seconds > 0 )
571     {
572         vout_OSDMessage( p_input, POSITION_CHAN, psz_time );
573     }
574
575     if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
576     {
577         var_Get( p_input, "position", &pos );
578         vout_OSDSlider( VLC_OBJECT( p_input ), POSITION_CHAN,
579                         pos.f_float * 100, OSD_HOR_SLIDER );
580     }
581 }
582
583 static void DisplayVolume( vout_thread_t *p_vout, audio_volume_t i_vol )
584 {
585     if( p_vout == NULL )
586     {
587         return;
588     }
589
590     if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
591     {
592         vout_OSDSlider( VLC_OBJECT( p_vout ), VOLUME_CHAN,
593             i_vol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
594     }
595     else
596     {
597         vout_OSDMessage( p_vout, VOLUME_CHAN, "Vol %d%%",
598                          2*i_vol*100/AOUT_VOLUME_MAX );
599     }
600 }