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 void DisplayPosition( vout_thread_t *, input_thread_t * );
69 static void DisplayVolume ( vout_thread_t *, audio_volume_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 );
243 DisplayVolume( p_vout, i_newvol );
245 else if( i_action == ACTIONID_VOL_DOWN )
247 audio_volume_t i_newvol;
248 aout_VolumeDown( p_intf, 1, &i_newvol );
249 DisplayVolume( p_vout, i_newvol );
251 else if( i_action == ACTIONID_VOL_MUTE )
253 audio_volume_t i_newvol = -1;
254 aout_VolumeMute( p_intf, &i_newvol );
259 vout_OSDIcon( VLC_OBJECT( p_intf ), OSD_MUTE_ICON );
263 DisplayVolume( p_vout, i_newvol );
268 else if( i_action == ACTIONID_SUBDELAY_DOWN )
271 if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
275 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
276 vout_OSDMessage( p_intf, SOLO_CHAN, "Subtitle delay %i ms",
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, SOLO_CHAN, "Subtitle delay %i ms",
292 else if( i_action == ACTIONID_FULLSCREEN && p_vout )
294 var_Get( p_vout, "fullscreen", &val );
295 var_Set( p_vout, "fullscreen", (vlc_value_t)!val.b_bool );
297 else if( i_action == ACTIONID_PLAY )
299 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
303 playlist_Play( p_playlist );
304 vlc_object_release( p_playlist );
307 else if( i_action == ACTIONID_PLAY_PAUSE )
309 val.i_int = PLAYING_S;
312 var_Get( p_input, "state", &val );
314 if( p_input && val.i_int != PAUSE_S )
316 vout_OSDIcon( VLC_OBJECT( p_intf ), OSD_PAUSE_ICON );
318 var_Set( p_input, "state", val );
322 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
326 vout_OSDIcon( VLC_OBJECT( p_intf ), OSD_PLAY_ICON );
327 playlist_Play( p_playlist );
328 vlc_object_release( p_playlist );
334 vlc_bool_t b_seekable = p_input->stream.b_seekable;
336 if( i_action == ACTIONID_PAUSE )
338 vout_OSDMessage( p_intf, SOLO_CHAN, _( "Pause" ) );
340 var_Set( p_input, "state", val );
342 else if( i_action == ACTIONID_JUMP_BACKWARD_10SEC && b_seekable )
344 val.i_time = -10000000;
345 var_Set( p_input, "time-offset", val );
346 DisplayPosition( p_vout, p_input );
348 else if( i_action == ACTIONID_JUMP_FORWARD_10SEC && b_seekable )
350 val.i_time = 10000000;
351 var_Set( p_input, "time-offset", val );
352 DisplayPosition( p_vout, p_input );
354 else if( i_action == ACTIONID_JUMP_BACKWARD_1MIN && b_seekable )
356 val.i_time = -60000000;
357 var_Set( p_input, "time-offset", val );
358 DisplayPosition( p_vout, p_input );
360 else if( i_action == ACTIONID_JUMP_FORWARD_1MIN && b_seekable )
362 val.i_time = 60000000;
363 var_Set( p_input, "time-offset", val );
364 DisplayPosition( p_vout, p_input );
366 else if( i_action == ACTIONID_JUMP_BACKWARD_5MIN && b_seekable )
368 val.i_time = -300000000;
369 var_Set( p_input, "time-offset", val );
370 DisplayPosition( p_vout, p_input );
372 else if( i_action == ACTIONID_JUMP_FORWARD_5MIN && b_seekable )
374 val.i_time = 300000000;
375 var_Set( p_input, "time-offset", val );
376 DisplayPosition( p_vout, p_input );
378 else if( i_action == ACTIONID_NEXT )
380 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
384 playlist_Next( p_playlist );
385 vlc_object_release( p_playlist );
388 else if( i_action == ACTIONID_PREV )
390 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
394 playlist_Prev( p_playlist );
395 vlc_object_release( p_playlist );
398 else if( i_action == ACTIONID_STOP )
400 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
404 playlist_Stop( p_playlist );
405 vlc_object_release( p_playlist );
408 else if( i_action == ACTIONID_FASTER )
410 vlc_value_t val; val.b_bool = VLC_TRUE;
411 var_Set( p_input, "rate-faster", val );
413 else if( i_action == ACTIONID_SLOWER )
415 vlc_value_t val; val.b_bool = VLC_TRUE;
416 var_Set( p_input, "rate-slower", val );
418 else if( i_action == ACTIONID_POSITION && b_seekable )
420 DisplayPosition( p_vout, p_input );
422 else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
423 i_action <= ACTIONID_PLAY_BOOKMARK10 )
425 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
427 else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
428 i_action <= ACTIONID_SET_BOOKMARK10 )
430 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
436 static int GetKey( intf_thread_t *p_intf)
438 vlc_mutex_lock( &p_intf->p_sys->change_lock );
439 if ( p_intf->p_sys->i_size == 0 )
441 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
446 int i_return = p_intf->p_sys->p_keys[ 0 ];
448 p_intf->p_sys->i_size--;
449 for ( i = 0; i < BUFFER_SIZE - 1; i++)
451 p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
453 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
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 )
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 )
468 msg_Warn( p_intf, "event buffer full, dropping keypress" );
469 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
474 p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
475 p_intf->p_sys->i_size++;
477 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
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 )
485 vlc_t *p_vlc = (vlc_t *)p_this;
486 struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
489 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
491 if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
493 p_hotkeys[i].i_key = newval.i_int;
500 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
504 char psz_bookmark_name[11];
505 playlist_t *p_playlist =
506 vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
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 );
514 char *psz_bookmark = strdup( val.psz_string );
515 for( i_position = 0; i_position < p_playlist->i_size; i_position++)
517 if( !strcmp( psz_bookmark,
518 p_playlist->pp_items[i_position]->input.psz_uri ) )
520 playlist_Goto( p_playlist, i_position );
524 vlc_object_release( p_playlist );
528 static void SetBookmark( intf_thread_t *p_intf, int i_num )
531 playlist_t *p_playlist =
532 vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
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,
543 vlc_object_release( p_playlist );
547 static void DisplayPosition( vout_thread_t *p_vout, input_thread_t *p_input )
549 char psz_duration[MSTRTIME_MAX_SIZE];
550 char psz_time[MSTRTIME_MAX_SIZE];
551 vlc_value_t time, pos;
559 var_Get( p_input, "time", &time );
560 i_seconds = time.i_time / 1000000;
561 secstotimestr ( psz_time, i_seconds );
563 var_Get( p_input, "length", &time );
564 if( time.i_time > 0 )
566 secstotimestr( psz_duration, time.i_time / 1000000 );
567 vout_OSDMessage( p_input, POSITION_CHAN, "%s / %s",
568 psz_time, psz_duration );
570 else if( i_seconds > 0 )
572 vout_OSDMessage( p_input, POSITION_CHAN, psz_time );
575 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
577 var_Get( p_input, "position", &pos );
578 vout_OSDSlider( VLC_OBJECT( p_input ), POSITION_CHAN,
579 pos.f_float * 100, OSD_HOR_SLIDER );
583 static void DisplayVolume( vout_thread_t *p_vout, audio_volume_t i_vol )
590 if( !p_vout->p_parent_intf || p_vout->b_fullscreen )
592 vout_OSDSlider( VLC_OBJECT( p_vout ), VOLUME_CHAN,
593 i_vol*100/AOUT_VOLUME_MAX, OSD_VERT_SLIDER );
597 vout_OSDMessage( p_vout, VOLUME_CHAN, "Vol %d%%",
598 2*i_vol*100/AOUT_VOLUME_MAX );