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 * );
69 static void DisplayPosition( input_thread_t * );
71 /*****************************************************************************
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.")
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 );
110 set_capability( "interface", 0 );
111 set_callbacks( Open, Close );
114 /*****************************************************************************
115 * Open: initialize interface
116 *****************************************************************************/
117 static int Open( vlc_object_t *p_this )
119 intf_thread_t *p_intf = (intf_thread_t *)p_this;
121 /* Allocate instance and initialize some members */
122 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
123 if( p_intf->p_sys == NULL )
125 msg_Err( p_intf, "out of memory" );
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;
132 p_intf->p_sys->p_input = NULL;
133 p_intf->p_sys->p_vout = NULL;
135 var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
139 /*****************************************************************************
140 * Close: destroy interface
141 *****************************************************************************/
142 static void Close( vlc_object_t *p_this )
144 intf_thread_t *p_intf = (intf_thread_t *)p_this;
146 if( p_intf->p_sys->p_input )
148 vlc_object_release( p_intf->p_sys->p_input );
150 if( p_intf->p_sys->p_vout )
152 vlc_object_release( p_intf->p_sys->p_vout );
154 /* Destroy structure */
155 free( p_intf->p_sys );
158 /*****************************************************************************
160 *****************************************************************************/
161 static void Run( intf_thread_t *p_intf )
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;
170 /* Initialize hotkey structure */
171 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
173 var_Create( p_intf->p_vlc, p_hotkeys[i].psz_action,
174 VLC_VAR_HOTKEY | VLC_VAR_DOINHERIT );
176 var_AddCallback( p_intf->p_vlc, p_hotkeys[i].psz_action,
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 );
182 while( !p_intf->b_die )
187 msleep( INTF_IDLE_SLEEP );
189 /* Update the input */
190 if( p_intf->p_sys->p_input == NULL )
192 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
195 else if( p_intf->p_sys->p_input->b_dead )
197 vlc_object_release( p_intf->p_sys->p_input );
198 p_intf->p_sys->p_input = NULL;
200 p_input = p_intf->p_sys->p_input;
202 /* Update the vout */
205 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
206 p_intf->p_sys->p_vout = p_vout;
208 else if( p_vout->b_die )
210 vlc_object_release( p_vout );
212 p_intf->p_sys->p_vout = NULL;
215 /* Find action triggered by hotkey */
217 i_key = GetKey( p_intf );
218 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
220 if( p_hotkeys[i].i_key == i_key )
222 i_action = p_hotkeys[i].i_action;
228 /* No key pressed, sleep a bit more */
229 msleep( INTF_IDLE_SLEEP );
233 if( i_action == ACTIONID_QUIT )
235 p_intf->p_vlc->b_die = VLC_TRUE;
236 vout_OSDMessage( p_intf, SOLO_CHAN, _( "Quit" ) );
239 else if( i_action == ACTIONID_VOL_UP )
241 audio_volume_t i_newvol;
242 aout_VolumeUp( p_intf, 1, &i_newvol );
245 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
247 vout_OSDSlider( VLC_OBJECT( p_intf ), VOLUME_CHAN,
248 i_newvol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
252 vout_OSDMessage( p_intf, VOLUME_CHAN, "Vol %d%%",
253 2*i_newvol*100/AOUT_VOLUME_MAX );
257 else if( i_action == ACTIONID_VOL_DOWN )
259 audio_volume_t i_newvol;
260 aout_VolumeDown( p_intf, 1, &i_newvol );
263 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
265 vout_OSDSlider( VLC_OBJECT( p_intf ), VOLUME_CHAN,
266 i_newvol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
270 vout_OSDMessage( p_intf, VOLUME_CHAN, "Vol %d%%",
271 2*i_newvol*100/AOUT_VOLUME_MAX );
276 else if( i_action == ACTIONID_VOL_MUTE )
278 audio_volume_t i_newvol = -1;
279 aout_VolumeMute( p_intf, &i_newvol );
284 vout_OSDMessage( p_intf, SOLO_CHAN, _( "Mute" ) );
285 vout_OSDIcon( VLC_OBJECT( p_intf ), OSD_MUTE_ICON );
289 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
291 vout_OSDSlider( VLC_OBJECT( p_intf ), VOLUME_CHAN,
292 i_newvol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
296 vout_OSDMessage( p_intf, VOLUME_CHAN, "Vol %d%%",
297 i_newvol * 100 / AOUT_VOLUME_MAX );
303 else if( i_action == ACTIONID_SUBDELAY_DOWN )
306 if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
310 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
311 vout_OSDMessage( p_intf, SOLO_CHAN, "Subtitle delay %i ms",
315 else if( i_action == ACTIONID_SUBDELAY_UP )
318 if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
322 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
323 vout_OSDMessage( p_intf, SOLO_CHAN, "Subtitle delay %i ms",
327 else if( i_action == ACTIONID_FULLSCREEN && p_vout )
329 var_Get( p_vout, "fullscreen", &val );
330 var_Set( p_vout, "fullscreen", (vlc_value_t)!val.b_bool );
332 else if( i_action == ACTIONID_PLAY )
334 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
338 playlist_Play( p_playlist );
339 vlc_object_release( p_playlist );
342 else if( i_action == ACTIONID_PLAY_PAUSE )
344 val.i_int = PLAYING_S;
347 var_Get( p_input, "state", &val );
349 if( p_input && val.i_int != PAUSE_S )
351 vout_OSDIcon( VLC_OBJECT( p_intf ), OSD_PAUSE_ICON );
353 var_Set( p_input, "state", val );
357 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
361 vout_OSDIcon( VLC_OBJECT( p_intf ), OSD_PLAY_ICON );
362 playlist_Play( p_playlist );
363 vlc_object_release( p_playlist );
369 vlc_bool_t b_seekable = p_input->stream.b_seekable;
371 if( i_action == ACTIONID_PAUSE )
373 vout_OSDMessage( p_intf, SOLO_CHAN, _( "Pause" ) );
375 var_Set( p_input, "state", val );
377 else if( i_action == ACTIONID_JUMP_BACKWARD_10SEC && b_seekable )
379 val.i_time = -10000000;
380 var_Set( p_input, "time-offset", val );
383 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
385 DisplayPosition( p_input );
386 vout_OSDSlider( VLC_OBJECT( p_intf ), POSITION_CHAN,
387 GetPosition( p_intf ), OSD_HOR_SLIDER );
391 vout_OSDMessage( p_intf, SOLO_CHAN,
392 _( "Jump -10 seconds" ) );
396 else if( i_action == ACTIONID_JUMP_FORWARD_10SEC && b_seekable )
398 val.i_time = 10000000;
399 var_Set( p_input, "time-offset", val );
402 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
404 DisplayPosition( p_input );
405 vout_OSDSlider( VLC_OBJECT( p_intf ), POSITION_CHAN,
406 GetPosition( p_intf ), OSD_HOR_SLIDER );
410 vout_OSDMessage( p_intf, POSITION_CHAN,
411 _( "Jump +10 seconds" ) );
415 else if( i_action == ACTIONID_JUMP_BACKWARD_1MIN && b_seekable )
417 val.i_time = -60000000;
418 var_Set( p_input, "time-offset", val );
421 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
423 DisplayPosition( p_input );
424 vout_OSDSlider( VLC_OBJECT( p_intf ), POSITION_CHAN,
425 GetPosition( p_intf ), OSD_HOR_SLIDER );
429 vout_OSDMessage( p_intf, SOLO_CHAN,
430 _( "Jump -1 minute" ) );
434 else if( i_action == ACTIONID_JUMP_FORWARD_1MIN && b_seekable )
436 val.i_time = 60000000;
437 var_Set( p_input, "time-offset", val );
440 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
442 DisplayPosition( p_input );
443 vout_OSDSlider( VLC_OBJECT( p_intf ), POSITION_CHAN,
444 GetPosition( p_intf ), OSD_HOR_SLIDER );
448 vout_OSDMessage( p_intf, SOLO_CHAN,
449 _( "Jump +1 minute" ) );
453 else if( i_action == ACTIONID_JUMP_BACKWARD_5MIN && b_seekable )
455 val.i_time = -300000000;
456 var_Set( p_input, "time-offset", val );
459 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
461 DisplayPosition( p_input );
462 vout_OSDSlider( VLC_OBJECT( p_intf ), POSITION_CHAN,
463 GetPosition( p_intf ), OSD_HOR_SLIDER );
467 vout_OSDMessage( p_intf, SOLO_CHAN,
468 _( "Jump -5 minutes" ) );
472 else if( i_action == ACTIONID_JUMP_FORWARD_5MIN && b_seekable )
474 val.i_time = 300000000;
475 var_Set( p_input, "time-offset", val );
478 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
480 DisplayPosition( p_input );
481 vout_OSDSlider( VLC_OBJECT( p_intf ), POSITION_CHAN,
482 GetPosition( p_intf ), OSD_HOR_SLIDER );
486 vout_OSDMessage( p_intf, SOLO_CHAN,
487 _( "Jump +5 minutes" ) );
491 else if( i_action == ACTIONID_NEXT )
493 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
497 playlist_Next( p_playlist );
498 vlc_object_release( p_playlist );
501 else if( i_action == ACTIONID_PREV )
503 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
507 playlist_Prev( p_playlist );
508 vlc_object_release( p_playlist );
511 else if( i_action == ACTIONID_STOP )
513 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
517 playlist_Stop( p_playlist );
518 vlc_object_release( p_playlist );
521 else if( i_action == ACTIONID_FASTER )
523 vlc_value_t val; val.b_bool = VLC_TRUE;
524 var_Set( p_input, "rate-faster", val );
526 else if( i_action == ACTIONID_SLOWER )
528 vlc_value_t val; val.b_bool = VLC_TRUE;
529 var_Set( p_input, "rate-slower", val );
531 else if( i_action == ACTIONID_POSITION )
533 DisplayPosition( p_input );
535 else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
536 i_action <= ACTIONID_PLAY_BOOKMARK10 )
538 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
540 else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
541 i_action <= ACTIONID_SET_BOOKMARK10 )
543 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
549 static int GetKey( intf_thread_t *p_intf)
551 vlc_mutex_lock( &p_intf->p_sys->change_lock );
552 if ( p_intf->p_sys->i_size == 0 )
554 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
559 int i_return = p_intf->p_sys->p_keys[ 0 ];
561 p_intf->p_sys->i_size--;
562 for ( i = 0; i < BUFFER_SIZE - 1; i++)
564 p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
566 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
571 /*****************************************************************************
572 * KeyEvent: callback for keyboard events
573 *****************************************************************************/
574 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
575 vlc_value_t oldval, vlc_value_t newval, void *p_data )
577 intf_thread_t *p_intf = (intf_thread_t *)p_data;
578 vlc_mutex_lock( &p_intf->p_sys->change_lock );
579 if ( p_intf->p_sys->i_size == BUFFER_SIZE )
581 msg_Warn( p_intf, "event buffer full, dropping keypress" );
582 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
587 p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
588 p_intf->p_sys->i_size++;
590 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
595 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
596 vlc_value_t oldval, vlc_value_t newval, void *p_data )
598 vlc_t *p_vlc = (vlc_t *)p_this;
599 struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
602 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
604 if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
606 p_hotkeys[i].i_key = newval.i_int;
613 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
617 char psz_bookmark_name[11];
618 playlist_t *p_playlist =
619 vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
621 sprintf( psz_bookmark_name, "bookmark%i", i_num );
622 var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
623 var_Get( p_intf, psz_bookmark_name, &val );
627 char *psz_bookmark = strdup( val.psz_string );
628 for( i_position = 0; i_position < p_playlist->i_size; i_position++)
630 if( !strcmp( psz_bookmark,
631 p_playlist->pp_items[i_position]->input.psz_uri ) )
633 playlist_Goto( p_playlist, i_position );
637 vlc_object_release( p_playlist );
641 static void SetBookmark( intf_thread_t *p_intf, int i_num )
644 playlist_t *p_playlist =
645 vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
648 char psz_bookmark_name[11];
649 sprintf( psz_bookmark_name, "bookmark%i", i_num );
650 var_Create( p_intf, psz_bookmark_name,
651 VLC_VAR_STRING|VLC_VAR_DOINHERIT );
652 val.psz_string = strdup( p_playlist->pp_items[p_playlist->i_index]->input.psz_uri );
653 var_Set( p_intf, psz_bookmark_name, val );
654 msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num,
656 vlc_object_release( p_playlist );
660 static int GetPosition( intf_thread_t *p_intf )
662 input_thread_t *p_input =
663 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
668 var_Get( p_input, "position", &pos );
669 vlc_object_release( p_input );
670 return pos.f_float * 100;
675 static void DisplayPosition( input_thread_t *p_input )
677 char psz_duration[MSTRTIME_MAX_SIZE];
678 char psz_time[MSTRTIME_MAX_SIZE];
682 var_Get( p_input, "time", &time );
683 i_seconds = time.i_time / 1000000;
684 secstotimestr ( psz_time, i_seconds );
686 var_Get( p_input, "length", &time );
687 if( time.i_time > 0 )
689 secstotimestr( psz_duration, time.i_time / 1000000 );
690 vout_OSDMessage( p_input, POSITION_CHAN, "%s / %s",
691 psz_time, psz_duration );
693 else if( i_seconds > 0 )
695 vout_OSDMessage( p_input, POSITION_CHAN, psz_time );