1 /*****************************************************************************
2 * hotkeys.c: Hotkey handling for vlc
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc/input.h>
38 #define BUFFER_SIZE 10
39 /*****************************************************************************
40 * intf_sys_t: description and status of FB interface
41 *****************************************************************************/
44 vlc_mutex_t change_lock; /* mutex to keep the callback
45 * and the main loop from
46 * stepping on each others
48 int p_keys[ BUFFER_SIZE ]; /* buffer that contains
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 */
55 /*****************************************************************************
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 int GetPosition ( intf_thread_t * );
70 /*****************************************************************************
72 *****************************************************************************/
73 #define BOOKMARK1_TEXT N_("Playlist bookmark 1")
74 #define BOOKMARK2_TEXT N_("Playlist bookmark 2")
75 #define BOOKMARK3_TEXT N_("Playlist bookmark 3")
76 #define BOOKMARK4_TEXT N_("Playlist bookmark 4")
77 #define BOOKMARK5_TEXT N_("Playlist bookmark 5")
78 #define BOOKMARK6_TEXT N_("Playlist bookmark 6")
79 #define BOOKMARK7_TEXT N_("Playlist bookmark 7")
80 #define BOOKMARK8_TEXT N_("Playlist bookmark 8")
81 #define BOOKMARK9_TEXT N_("Playlist bookmark 9")
82 #define BOOKMARK10_TEXT N_("Playlist bookmark 10")
83 #define BOOKMARK_LONGTEXT N_( \
84 "This option allows you to define playlist bookmarks.")
87 set_description( _("Hotkeys management interface") );
88 add_string( "bookmark1", NULL, NULL,
89 BOOKMARK1_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
90 add_string( "bookmark2", NULL, NULL,
91 BOOKMARK2_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
92 add_string( "bookmark3", NULL, NULL,
93 BOOKMARK3_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
94 add_string( "bookmark4", NULL, NULL,
95 BOOKMARK4_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
96 add_string( "bookmark5", NULL, NULL,
97 BOOKMARK5_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
98 add_string( "bookmark6", NULL, NULL,
99 BOOKMARK6_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
100 add_string( "bookmark7", NULL, NULL,
101 BOOKMARK7_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
102 add_string( "bookmark8", NULL, NULL,
103 BOOKMARK8_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
104 add_string( "bookmark9", NULL, NULL,
105 BOOKMARK9_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
106 add_string( "bookmark10", NULL, NULL,
107 BOOKMARK10_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
109 set_capability( "interface", 0 );
110 set_callbacks( Open, Close );
113 /*****************************************************************************
114 * Open: initialize interface
115 *****************************************************************************/
116 static int Open( vlc_object_t *p_this )
118 intf_thread_t *p_intf = (intf_thread_t *)p_this;
120 /* Allocate instance and initialize some members */
121 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
122 if( p_intf->p_sys == NULL )
124 msg_Err( p_intf, "out of memory" );
127 vlc_mutex_init( p_intf, &p_intf->p_sys->change_lock );
128 p_intf->p_sys->i_size = 0;
129 p_intf->pf_run = Run;
131 p_intf->p_sys->p_input = NULL;
132 p_intf->p_sys->p_vout = NULL;
134 var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
138 /*****************************************************************************
139 * Close: destroy interface
140 *****************************************************************************/
141 static void Close( vlc_object_t *p_this )
143 intf_thread_t *p_intf = (intf_thread_t *)p_this;
145 if( p_intf->p_sys->p_input )
147 vlc_object_release( p_intf->p_sys->p_input );
149 if( p_intf->p_sys->p_vout )
151 vlc_object_release( p_intf->p_sys->p_vout );
153 /* Destroy structure */
154 free( p_intf->p_sys );
157 /*****************************************************************************
159 *****************************************************************************/
160 static void Run( intf_thread_t *p_intf )
162 playlist_t *p_playlist;
163 input_thread_t *p_input;
164 vout_thread_t *p_vout = NULL;
165 struct hotkey *p_hotkeys = p_intf->p_vlc->p_hotkeys;
169 /* Initialize hotkey structure */
170 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
172 var_Create( p_intf->p_vlc, p_hotkeys[i].psz_action,
173 VLC_VAR_HOTKEY | VLC_VAR_DOINHERIT );
175 var_AddCallback( p_intf->p_vlc, p_hotkeys[i].psz_action,
177 var_Get( p_intf->p_vlc, p_hotkeys[i].psz_action, &val );
178 var_Set( p_intf->p_vlc, p_hotkeys[i].psz_action, val );
181 while( !p_intf->b_die )
186 msleep( INTF_IDLE_SLEEP );
188 /* Update the input */
189 if( p_intf->p_sys->p_input == NULL )
191 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
194 else if( p_intf->p_sys->p_input->b_dead )
196 vlc_object_release( p_intf->p_sys->p_input );
197 p_intf->p_sys->p_input = NULL;
199 p_input = p_intf->p_sys->p_input;
201 /* Update the vout */
204 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
205 p_intf->p_sys->p_vout = p_vout;
207 else if( p_vout->b_die )
209 vlc_object_release( p_vout );
211 p_intf->p_sys->p_vout = NULL;
214 /* Find action triggered by hotkey */
216 i_key = GetKey( p_intf );
217 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
219 if( p_hotkeys[i].i_key == i_key )
221 i_action = p_hotkeys[i].i_action;
227 /* No key pressed, sleep a bit more */
228 msleep( INTF_IDLE_SLEEP );
232 if( i_action == ACTIONID_QUIT )
234 p_intf->p_vlc->b_die = VLC_TRUE;
235 vout_OSDMessage( p_intf, _( "Quit" ) );
238 else if( i_action == ACTIONID_VOL_UP )
240 audio_volume_t i_newvol;
241 aout_VolumeUp( p_intf, 1, &i_newvol );
242 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
244 vout_OSDSlider( VLC_OBJECT( p_intf ),
245 i_newvol*100/AOUT_VOLUME_MAX, 1 );
249 vout_OSDMessage( p_intf, "Vol %d%%",
250 2*i_newvol*100/AOUT_VOLUME_MAX );
253 else if( i_action == ACTIONID_VOL_DOWN )
255 audio_volume_t i_newvol;
256 aout_VolumeDown( p_intf, 1, &i_newvol );
257 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
259 vout_OSDSlider( VLC_OBJECT( p_intf ),
260 i_newvol*100/AOUT_VOLUME_MAX, 1 );
264 vout_OSDMessage( p_intf, "Vol %d%%",
265 2*i_newvol*100/AOUT_VOLUME_MAX );
269 else if( i_action == ACTIONID_SUBDELAY_DOWN )
272 if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
276 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
277 vout_OSDMessage( p_intf, "Subtitle delay %i ms", i_delay*100);
280 else if( i_action == ACTIONID_SUBDELAY_UP )
283 if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
287 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
288 vout_OSDMessage( p_intf, "Subtitle delay %i ms", i_delay*100);
291 else if( i_action == ACTIONID_VOL_MUTE )
293 audio_volume_t i_newvol = -1;
294 aout_VolumeMute( p_intf, &i_newvol );
297 vout_OSDMessage( p_intf, _( "Mute" ) );
301 vout_OSDMessage( p_intf, "Vol %d%%",
302 i_newvol * 100 / AOUT_VOLUME_MAX );
305 else if( i_action == ACTIONID_FULLSCREEN && p_vout )
307 var_Get( p_vout, "fullscreen", &val );
308 var_Set( p_vout, "fullscreen", (vlc_value_t)!val.b_bool );
310 else if( i_action == ACTIONID_PLAY )
312 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
316 playlist_Play( p_playlist );
317 vlc_object_release( p_playlist );
320 else if( i_action == ACTIONID_PLAY_PAUSE )
322 val.i_int = PLAYING_S;
325 var_Get( p_input, "state", &val );
327 if( p_input && val.i_int != PAUSE_S )
329 vout_OSDMessage( p_intf, _( "Pause" ) );
331 var_Set( p_input, "state", val );
335 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
339 vout_OSDMessage( p_intf, _( "Play" ) );
340 playlist_Play( p_playlist );
341 vlc_object_release( p_playlist );
347 vlc_bool_t b_seekable = p_input->stream.b_seekable;
349 if( i_action == ACTIONID_PAUSE )
351 vout_OSDMessage( p_intf, _( "Pause" ) );
353 var_Set( p_input, "state", val );
355 else if( i_action == ACTIONID_JUMP_BACKWARD_10SEC && b_seekable )
357 val.i_time = -10000000;
358 var_Set( p_input, "time-offset", val );
359 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
361 vout_OSDSlider( VLC_OBJECT( p_intf ),
362 GetPosition( p_intf ), 0 );
366 vout_OSDMessage( p_intf, _( "Jump -10 seconds" ) );
369 else if( i_action == ACTIONID_JUMP_FORWARD_10SEC && b_seekable )
371 val.i_time = 10000000;
372 var_Set( p_input, "time-offset", val );
373 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
375 vout_OSDSlider( VLC_OBJECT( p_intf ),
376 GetPosition( p_intf ), 0 );
380 vout_OSDMessage( p_intf, _( "Jump +10 seconds" ) );
383 else if( i_action == ACTIONID_JUMP_BACKWARD_1MIN && b_seekable )
385 val.i_time = -60000000;
386 var_Set( p_input, "time-offset", val );
387 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
389 vout_OSDSlider( VLC_OBJECT( p_intf ),
390 GetPosition( p_intf ), 0 );
394 vout_OSDMessage( p_intf, _( "Jump -1 minute" ) );
397 else if( i_action == ACTIONID_JUMP_FORWARD_1MIN && b_seekable )
399 val.i_time = 60000000;
400 var_Set( p_input, "time-offset", val );
401 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
403 vout_OSDSlider( VLC_OBJECT( p_intf ),
404 GetPosition( p_intf ), 0 );
408 vout_OSDMessage( p_intf, _( "Jump +1 minute" ) );
411 else if( i_action == ACTIONID_JUMP_BACKWARD_5MIN && b_seekable )
413 vout_OSDMessage( p_intf, _( "Jump -5 minutes" ) );
414 val.i_time = -300000000;
415 var_Set( p_input, "time-offset", val );
416 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
418 vout_OSDSlider( VLC_OBJECT( p_intf ),
419 GetPosition( p_intf ), 0 );
423 vout_OSDMessage( p_intf, _( "Jump -5 minutes" ) );
426 else if( i_action == ACTIONID_JUMP_FORWARD_5MIN && b_seekable )
428 val.i_time = 300000000;
429 var_Set( p_input, "time-offset", val );
430 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
432 vout_OSDSlider( VLC_OBJECT( p_intf ),
433 GetPosition( p_intf ), 0 );
437 vout_OSDMessage( p_intf, _( "Jump +5 minutes" ) );
440 else if( i_action == ACTIONID_NEXT )
442 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
446 playlist_Next( p_playlist );
447 vlc_object_release( p_playlist );
450 else if( i_action == ACTIONID_PREV )
452 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
456 playlist_Prev( p_playlist );
457 vlc_object_release( p_playlist );
460 else if( i_action == ACTIONID_STOP )
462 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
466 playlist_Stop( p_playlist );
467 vlc_object_release( p_playlist );
470 else if( i_action == ACTIONID_FASTER )
472 vlc_value_t val; val.b_bool = VLC_TRUE;
473 var_Set( p_input, "rate-faster", val );
475 else if( i_action == ACTIONID_SLOWER )
477 vlc_value_t val; val.b_bool = VLC_TRUE;
478 var_Set( p_input, "rate-slower", val );
480 else if( i_action == ACTIONID_POSITION )
482 char psz_duration[MSTRTIME_MAX_SIZE];
483 char psz_time[MSTRTIME_MAX_SIZE];
487 var_Get( p_input, "time", &time );
488 i_seconds = time.i_time / 1000000;
489 secstotimestr ( psz_time, i_seconds );
491 var_Get( p_input, "length", &time );
492 if( time.i_time > 0 )
494 secstotimestr( psz_duration, time.i_time / 1000000 );
495 vout_OSDMessage( p_input, "%s / %s",
496 psz_time, psz_duration );
498 else if( i_seconds > 0 )
500 vout_OSDMessage( p_input, psz_time );
503 else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
504 i_action <= ACTIONID_PLAY_BOOKMARK10 )
506 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
508 else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
509 i_action <= ACTIONID_SET_BOOKMARK10 )
511 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
517 static int GetKey( intf_thread_t *p_intf)
519 vlc_mutex_lock( &p_intf->p_sys->change_lock );
520 if ( p_intf->p_sys->i_size == 0 )
522 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
527 int i_return = p_intf->p_sys->p_keys[ 0 ];
529 p_intf->p_sys->i_size--;
530 for ( i = 0; i < BUFFER_SIZE - 1; i++)
532 p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
534 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
539 /*****************************************************************************
540 * KeyEvent: callback for keyboard events
541 *****************************************************************************/
542 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
543 vlc_value_t oldval, vlc_value_t newval, void *p_data )
545 intf_thread_t *p_intf = (intf_thread_t *)p_data;
546 vlc_mutex_lock( &p_intf->p_sys->change_lock );
547 if ( p_intf->p_sys->i_size == BUFFER_SIZE )
549 msg_Warn( p_intf, "event buffer full, dropping keypress" );
550 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
555 p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
556 p_intf->p_sys->i_size++;
558 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
563 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
564 vlc_value_t oldval, vlc_value_t newval, void *p_data )
566 vlc_t *p_vlc = (vlc_t *)p_this;
567 struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
570 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
572 if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
574 p_hotkeys[i].i_key = newval.i_int;
581 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
585 char psz_bookmark_name[11];
586 playlist_t *p_playlist =
587 vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
589 sprintf( psz_bookmark_name, "bookmark%i", i_num );
590 var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
591 var_Get( p_intf, psz_bookmark_name, &val );
595 char *psz_bookmark = strdup( val.psz_string );
596 for( i_position = 0; i_position < p_playlist->i_size; i_position++)
598 if( !strcmp( psz_bookmark,
599 p_playlist->pp_items[i_position]->input.psz_uri ) )
601 playlist_Goto( p_playlist, i_position );
605 vlc_object_release( p_playlist );
609 static void SetBookmark( intf_thread_t *p_intf, int i_num )
612 playlist_t *p_playlist =
613 vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
616 char psz_bookmark_name[11];
617 sprintf( psz_bookmark_name, "bookmark%i", i_num );
618 var_Create( p_intf, psz_bookmark_name,
619 VLC_VAR_STRING|VLC_VAR_DOINHERIT );
620 val.psz_string = strdup( p_playlist->pp_items[p_playlist->i_index]->input.psz_uri );
621 var_Set( p_intf, psz_bookmark_name, val );
622 msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num,
624 vlc_object_release( p_playlist );
628 static int GetPosition( intf_thread_t *p_intf )
630 input_thread_t *p_input =
631 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
636 var_Get( p_input, "position", &pos );
637 vlc_object_release( p_input );
638 return pos.f_float * 100;