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 );
244 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
246 vout_OSDSlider( VLC_OBJECT( p_intf ),
247 i_newvol*100/AOUT_VOLUME_MAX, 1 );
251 vout_OSDMessage( p_intf, "Vol %d%%",
252 2*i_newvol*100/AOUT_VOLUME_MAX );
256 else if( i_action == ACTIONID_VOL_DOWN )
258 audio_volume_t i_newvol;
259 aout_VolumeDown( p_intf, 1, &i_newvol );
262 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
264 vout_OSDSlider( VLC_OBJECT( p_intf ),
265 i_newvol*100/AOUT_VOLUME_MAX, 1 );
269 vout_OSDMessage( p_intf, "Vol %d%%",
270 2*i_newvol*100/AOUT_VOLUME_MAX );
275 else if( i_action == ACTIONID_SUBDELAY_DOWN )
278 if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
282 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
283 vout_OSDMessage( p_intf, "Subtitle delay %i ms", i_delay*100);
286 else if( i_action == ACTIONID_SUBDELAY_UP )
289 if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
293 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
294 vout_OSDMessage( p_intf, "Subtitle delay %i ms", i_delay*100);
297 else if( i_action == ACTIONID_VOL_MUTE )
299 audio_volume_t i_newvol = -1;
300 aout_VolumeMute( p_intf, &i_newvol );
303 vout_OSDMessage( p_intf, _( "Mute" ) );
307 vout_OSDMessage( p_intf, "Vol %d%%",
308 i_newvol * 100 / AOUT_VOLUME_MAX );
311 else if( i_action == ACTIONID_FULLSCREEN && p_vout )
313 var_Get( p_vout, "fullscreen", &val );
314 var_Set( p_vout, "fullscreen", (vlc_value_t)!val.b_bool );
316 else if( i_action == ACTIONID_PLAY )
318 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
322 playlist_Play( p_playlist );
323 vlc_object_release( p_playlist );
326 else if( i_action == ACTIONID_PLAY_PAUSE )
328 val.i_int = PLAYING_S;
331 var_Get( p_input, "state", &val );
333 if( p_input && val.i_int != PAUSE_S )
335 vout_OSDMessage( p_intf, _( "Pause" ) );
337 var_Set( p_input, "state", val );
341 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
345 vout_OSDMessage( p_intf, _( "Play" ) );
346 playlist_Play( p_playlist );
347 vlc_object_release( p_playlist );
353 vlc_bool_t b_seekable = p_input->stream.b_seekable;
355 if( i_action == ACTIONID_PAUSE )
357 vout_OSDMessage( p_intf, _( "Pause" ) );
359 var_Set( p_input, "state", val );
361 else if( i_action == ACTIONID_JUMP_BACKWARD_10SEC && b_seekable )
363 val.i_time = -10000000;
364 var_Set( p_input, "time-offset", val );
365 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
367 vout_OSDSlider( VLC_OBJECT( p_intf ),
368 GetPosition( p_intf ), 0 );
372 vout_OSDMessage( p_intf, _( "Jump -10 seconds" ) );
375 else if( i_action == ACTIONID_JUMP_FORWARD_10SEC && b_seekable )
377 val.i_time = 10000000;
378 var_Set( p_input, "time-offset", val );
381 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
383 vout_OSDSlider( VLC_OBJECT( p_intf ),
384 GetPosition( p_intf ), 0 );
388 vout_OSDMessage( p_intf, _( "Jump +10 seconds" ) );
392 else if( i_action == ACTIONID_JUMP_BACKWARD_1MIN && b_seekable )
394 val.i_time = -60000000;
395 var_Set( p_input, "time-offset", val );
398 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
400 vout_OSDSlider( VLC_OBJECT( p_intf ),
401 GetPosition( p_intf ), 0 );
405 vout_OSDMessage( p_intf, _( "Jump -1 minute" ) );
409 else if( i_action == ACTIONID_JUMP_FORWARD_1MIN && b_seekable )
411 val.i_time = 60000000;
412 var_Set( p_input, "time-offset", val );
415 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
417 vout_OSDSlider( VLC_OBJECT( p_intf ),
418 GetPosition( p_intf ), 0 );
422 vout_OSDMessage( p_intf, _( "Jump +1 minute" ) );
426 else if( i_action == ACTIONID_JUMP_BACKWARD_5MIN && b_seekable )
428 vout_OSDMessage( p_intf, _( "Jump -5 minutes" ) );
429 val.i_time = -300000000;
430 var_Set( p_input, "time-offset", val );
433 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
435 vout_OSDSlider( VLC_OBJECT( p_intf ),
436 GetPosition( p_intf ), 0 );
440 vout_OSDMessage( p_intf, _( "Jump -5 minutes" ) );
444 else if( i_action == ACTIONID_JUMP_FORWARD_5MIN && b_seekable )
446 val.i_time = 300000000;
447 var_Set( p_input, "time-offset", val );
450 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
452 vout_OSDSlider( VLC_OBJECT( p_intf ),
453 GetPosition( p_intf ), 0 );
457 vout_OSDMessage( p_intf, _( "Jump +5 minutes" ) );
461 else if( i_action == ACTIONID_NEXT )
463 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
467 playlist_Next( p_playlist );
468 vlc_object_release( p_playlist );
471 else if( i_action == ACTIONID_PREV )
473 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
477 playlist_Prev( p_playlist );
478 vlc_object_release( p_playlist );
481 else if( i_action == ACTIONID_STOP )
483 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
487 playlist_Stop( p_playlist );
488 vlc_object_release( p_playlist );
491 else if( i_action == ACTIONID_FASTER )
493 vlc_value_t val; val.b_bool = VLC_TRUE;
494 var_Set( p_input, "rate-faster", val );
496 else if( i_action == ACTIONID_SLOWER )
498 vlc_value_t val; val.b_bool = VLC_TRUE;
499 var_Set( p_input, "rate-slower", val );
501 else if( i_action == ACTIONID_POSITION )
503 char psz_duration[MSTRTIME_MAX_SIZE];
504 char psz_time[MSTRTIME_MAX_SIZE];
508 var_Get( p_input, "time", &time );
509 i_seconds = time.i_time / 1000000;
510 secstotimestr ( psz_time, i_seconds );
512 var_Get( p_input, "length", &time );
513 if( time.i_time > 0 )
515 secstotimestr( psz_duration, time.i_time / 1000000 );
516 vout_OSDMessage( p_input, "%s / %s",
517 psz_time, psz_duration );
519 else if( i_seconds > 0 )
521 vout_OSDMessage( p_input, psz_time );
524 else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
525 i_action <= ACTIONID_PLAY_BOOKMARK10 )
527 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
529 else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
530 i_action <= ACTIONID_SET_BOOKMARK10 )
532 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
538 static int GetKey( intf_thread_t *p_intf)
540 vlc_mutex_lock( &p_intf->p_sys->change_lock );
541 if ( p_intf->p_sys->i_size == 0 )
543 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
548 int i_return = p_intf->p_sys->p_keys[ 0 ];
550 p_intf->p_sys->i_size--;
551 for ( i = 0; i < BUFFER_SIZE - 1; i++)
553 p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
555 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
560 /*****************************************************************************
561 * KeyEvent: callback for keyboard events
562 *****************************************************************************/
563 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
564 vlc_value_t oldval, vlc_value_t newval, void *p_data )
566 intf_thread_t *p_intf = (intf_thread_t *)p_data;
567 vlc_mutex_lock( &p_intf->p_sys->change_lock );
568 if ( p_intf->p_sys->i_size == BUFFER_SIZE )
570 msg_Warn( p_intf, "event buffer full, dropping keypress" );
571 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
576 p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
577 p_intf->p_sys->i_size++;
579 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
584 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
585 vlc_value_t oldval, vlc_value_t newval, void *p_data )
587 vlc_t *p_vlc = (vlc_t *)p_this;
588 struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
591 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
593 if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
595 p_hotkeys[i].i_key = newval.i_int;
602 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
606 char psz_bookmark_name[11];
607 playlist_t *p_playlist =
608 vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
610 sprintf( psz_bookmark_name, "bookmark%i", i_num );
611 var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
612 var_Get( p_intf, psz_bookmark_name, &val );
616 char *psz_bookmark = strdup( val.psz_string );
617 for( i_position = 0; i_position < p_playlist->i_size; i_position++)
619 if( !strcmp( psz_bookmark,
620 p_playlist->pp_items[i_position]->input.psz_uri ) )
622 playlist_Goto( p_playlist, i_position );
626 vlc_object_release( p_playlist );
630 static void SetBookmark( intf_thread_t *p_intf, int i_num )
633 playlist_t *p_playlist =
634 vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
637 char psz_bookmark_name[11];
638 sprintf( psz_bookmark_name, "bookmark%i", i_num );
639 var_Create( p_intf, psz_bookmark_name,
640 VLC_VAR_STRING|VLC_VAR_DOINHERIT );
641 val.psz_string = strdup( p_playlist->pp_items[p_playlist->i_index]->input.psz_uri );
642 var_Set( p_intf, psz_bookmark_name, val );
643 msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num,
645 vlc_object_release( p_playlist );
649 static int GetPosition( intf_thread_t *p_intf )
651 input_thread_t *p_input =
652 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
657 var_Get( p_input, "position", &pos );
658 vlc_object_release( p_input );
659 return pos.f_float * 100;