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 );
69 /*****************************************************************************
71 *****************************************************************************/
72 #define BOOKMARK1_TEXT N_("Playlist bookmark 1")
73 #define BOOKMARK2_TEXT N_("Playlist bookmark 2")
74 #define BOOKMARK3_TEXT N_("Playlist bookmark 3")
75 #define BOOKMARK4_TEXT N_("Playlist bookmark 4")
76 #define BOOKMARK5_TEXT N_("Playlist bookmark 5")
77 #define BOOKMARK6_TEXT N_("Playlist bookmark 6")
78 #define BOOKMARK7_TEXT N_("Playlist bookmark 7")
79 #define BOOKMARK8_TEXT N_("Playlist bookmark 8")
80 #define BOOKMARK9_TEXT N_("Playlist bookmark 9")
81 #define BOOKMARK10_TEXT N_("Playlist bookmark 10")
82 #define BOOKMARK_LONGTEXT N_( \
83 "This option allows you to define playlist bookmarks.")
86 set_description( _("Hotkeys management interface") );
87 add_string( "bookmark1", NULL, NULL,
88 BOOKMARK1_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
89 add_string( "bookmark2", NULL, NULL,
90 BOOKMARK2_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
91 add_string( "bookmark3", NULL, NULL,
92 BOOKMARK3_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
93 add_string( "bookmark4", NULL, NULL,
94 BOOKMARK4_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
95 add_string( "bookmark5", NULL, NULL,
96 BOOKMARK5_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
97 add_string( "bookmark6", NULL, NULL,
98 BOOKMARK6_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
99 add_string( "bookmark7", NULL, NULL,
100 BOOKMARK7_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
101 add_string( "bookmark8", NULL, NULL,
102 BOOKMARK8_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
103 add_string( "bookmark9", NULL, NULL,
104 BOOKMARK9_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
105 add_string( "bookmark10", NULL, NULL,
106 BOOKMARK10_TEXT, BOOKMARK_LONGTEXT, VLC_FALSE );
108 set_capability( "interface", 0 );
109 set_callbacks( Open, Close );
112 /*****************************************************************************
113 * Open: initialize interface
114 *****************************************************************************/
115 static int Open( vlc_object_t *p_this )
117 intf_thread_t *p_intf = (intf_thread_t *)p_this;
119 /* Allocate instance and initialize some members */
120 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
121 if( p_intf->p_sys == NULL )
123 msg_Err( p_intf, "out of memory" );
126 vlc_mutex_init( p_intf, &p_intf->p_sys->change_lock );
127 p_intf->p_sys->i_size = 0;
128 p_intf->pf_run = Run;
130 p_intf->p_sys->p_input = NULL;
131 p_intf->p_sys->p_vout = NULL;
133 var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
137 /*****************************************************************************
138 * Close: destroy interface
139 *****************************************************************************/
140 static void Close( vlc_object_t *p_this )
142 intf_thread_t *p_intf = (intf_thread_t *)p_this;
144 if( p_intf->p_sys->p_input )
146 vlc_object_release( p_intf->p_sys->p_input );
148 if( p_intf->p_sys->p_vout )
150 vlc_object_release( p_intf->p_sys->p_vout );
152 /* Destroy structure */
153 free( p_intf->p_sys );
156 /*****************************************************************************
158 *****************************************************************************/
159 static void Run( intf_thread_t *p_intf )
161 playlist_t *p_playlist;
162 input_thread_t *p_input;
163 vout_thread_t *p_vout = NULL;
164 struct hotkey *p_hotkeys = p_intf->p_vlc->p_hotkeys;
168 /* Initialize hotkey structure */
169 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
171 var_Create( p_intf->p_vlc, p_hotkeys[i].psz_action,
172 VLC_VAR_HOTKEY | VLC_VAR_DOINHERIT );
174 var_AddCallback( p_intf->p_vlc, p_hotkeys[i].psz_action,
176 var_Get( p_intf->p_vlc, p_hotkeys[i].psz_action, &val );
177 var_Set( p_intf->p_vlc, p_hotkeys[i].psz_action, val );
180 while( !p_intf->b_die )
185 msleep( INTF_IDLE_SLEEP );
187 /* Update the input */
188 if( p_intf->p_sys->p_input == NULL )
190 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
193 else if( p_intf->p_sys->p_input->b_dead )
195 vlc_object_release( p_intf->p_sys->p_input );
196 p_intf->p_sys->p_input = NULL;
198 p_input = p_intf->p_sys->p_input;
200 /* Update the vout */
203 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
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 vout_OSDMessage( p_intf, "Vol %d%%", i_newvol*100/AOUT_VOLUME_MAX );
244 else if( i_action == ACTIONID_VOL_DOWN )
246 audio_volume_t i_newvol;
247 aout_VolumeDown( p_intf, 1, &i_newvol );
248 vout_OSDMessage( p_intf, "Vol %d%%", i_newvol*100/AOUT_VOLUME_MAX);
250 else if( i_action == ACTIONID_SUBDELAY_DOWN )
253 if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
257 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
258 vout_OSDMessage( p_intf, "Subtitle delay %i ms",i_delay*100);
261 else if( i_action == ACTIONID_SUBDELAY_UP )
264 if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
268 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
269 vout_OSDMessage( p_intf, "Subtitle delay %i ms",i_delay*100);
272 else if( i_action == ACTIONID_VOL_MUTE )
274 audio_volume_t i_newvol = -1;
275 aout_VolumeMute( p_intf, &i_newvol );
278 vout_OSDMessage( p_intf, _( "Mute" ) );
282 vout_OSDMessage( p_intf, "Vol %d%%",
283 i_newvol*100/AOUT_VOLUME_MAX );
286 else if( i_action == ACTIONID_FULLSCREEN )
290 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
293 else if( i_action == ACTIONID_PLAY )
295 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
299 vlc_mutex_lock( &p_playlist->object_lock );
300 if( p_playlist->i_size )
302 vlc_mutex_unlock( &p_playlist->object_lock );
303 playlist_Play( p_playlist );
304 vlc_object_release( p_playlist );
308 else if( i_action == ACTIONID_PLAY_PAUSE )
310 val.i_int = PLAYING_S;
313 var_Get( p_input, "state", &val );
315 if( p_input && val.i_int != PAUSE_S )
317 vout_OSDMessage( p_intf, _( "Pause" ) );
319 var_Set( p_input, "state", val );
323 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
327 vlc_mutex_lock( &p_playlist->object_lock );
328 if( p_playlist->i_size )
330 vlc_mutex_unlock( &p_playlist->object_lock );
331 vout_OSDMessage( p_intf, _( "Play" ) );
332 playlist_Play( p_playlist );
333 vlc_object_release( p_playlist );
340 if( i_action == ACTIONID_PAUSE )
342 vout_OSDMessage( p_intf, _( "Pause" ) );
344 var_Set( p_input, "state", val );
346 else if( i_action == ACTIONID_JUMP_BACKWARD_10SEC )
348 vout_OSDMessage( p_intf, _( "Jump -10 seconds" ) );
349 val.i_time = -10000000;
350 var_Set( p_input, "time-offset", val );
352 else if( i_action == ACTIONID_JUMP_FORWARD_10SEC )
354 vout_OSDMessage( p_intf, _( "Jump +10 seconds" ) );
355 val.i_time = 10000000;
356 var_Set( p_input, "time-offset", val );
358 else if( i_action == ACTIONID_JUMP_BACKWARD_1MIN )
360 vout_OSDMessage( p_intf, _( "Jump -1 minute" ) );
361 val.i_time = -60000000;
362 var_Set( p_input, "time-offset", val );
364 else if( i_action == ACTIONID_JUMP_FORWARD_1MIN )
366 vout_OSDMessage( p_intf, _( "Jump +1 minute" ) );
367 val.i_time = 60000000;
368 var_Set( p_input, "time-offset", val );
370 else if( i_action == ACTIONID_JUMP_BACKWARD_5MIN )
372 vout_OSDMessage( p_intf, _( "Jump -5 minutes" ) );
373 val.i_time = -300000000;
374 var_Set( p_input, "time-offset", val );
376 else if( i_action == ACTIONID_JUMP_FORWARD_5MIN )
378 vout_OSDMessage( p_intf, _( "Jump +5 minutes" ) );
379 val.i_time = 300000000;
380 var_Set( p_input, "time-offset", val );
382 else if( i_action == ACTIONID_NEXT )
384 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
388 playlist_Next( p_playlist );
389 vlc_object_release( p_playlist );
392 else if( i_action == ACTIONID_PREV )
394 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
398 playlist_Prev( p_playlist );
399 vlc_object_release( p_playlist );
402 else if( i_action == ACTIONID_STOP )
404 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
408 playlist_Stop( p_playlist );
409 vlc_object_release( p_playlist );
412 else if( i_action == ACTIONID_FASTER )
414 vlc_value_t val; val.b_bool = VLC_TRUE;
415 var_Set( p_input, "rate-faster", val );
417 else if( i_action == ACTIONID_FASTER )
419 vlc_value_t val; val.b_bool = VLC_TRUE;
420 var_Set( p_input, "rate-slower", val );
422 else if( i_action == ACTIONID_POSITION )
424 playlist_t *p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
426 char psz_duration[MSTRTIME_MAX_SIZE];
427 char psz_time[MSTRTIME_MAX_SIZE];
431 var_Get( p_input, "time", &time );
436 p_playlist->pp_items[p_playlist->i_index]->i_duration;
438 i_seconds = time.i_time / 1000000;
439 secstotimestr ( psz_time, i_seconds );
443 secstotimestr( psz_duration, dur/1000000 );
444 vout_OSDMessage( p_playlist, "%s / %s", psz_time, psz_duration );
446 else if( i_seconds > 0 )
448 vout_OSDMessage( p_playlist, psz_time );
450 vlc_object_release( p_playlist );
453 else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
454 i_action <= ACTIONID_PLAY_BOOKMARK10 )
456 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
458 else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
459 i_action <= ACTIONID_SET_BOOKMARK10 )
461 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
467 static int GetKey( intf_thread_t *p_intf)
469 vlc_mutex_lock( &p_intf->p_sys->change_lock );
470 if ( p_intf->p_sys->i_size == 0 )
472 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
477 int i_return = p_intf->p_sys->p_keys[ 0 ];
479 p_intf->p_sys->i_size--;
480 for ( i = 0; i < BUFFER_SIZE - 1; i++)
482 p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
484 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
489 /*****************************************************************************
490 * KeyEvent: callback for keyboard events
491 *****************************************************************************/
492 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
493 vlc_value_t oldval, vlc_value_t newval, void *p_data )
495 intf_thread_t *p_intf = (intf_thread_t *)p_data;
496 vlc_mutex_lock( &p_intf->p_sys->change_lock );
497 if ( p_intf->p_sys->i_size == BUFFER_SIZE )
499 msg_Warn( p_intf, "event buffer full, dropping keypress" );
500 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
505 p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
506 p_intf->p_sys->i_size++;
508 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
513 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
514 vlc_value_t oldval, vlc_value_t newval, void *p_data )
516 vlc_t *p_vlc = (vlc_t *)p_this;
517 struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
520 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
522 if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
524 p_hotkeys[i].i_key = newval.i_int;
531 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
535 char psz_bookmark_name[11];
536 playlist_t *p_playlist = vlc_object_find( p_intf,
537 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
539 sprintf( psz_bookmark_name, "bookmark%i", i_num );
540 var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
541 var_Get( p_intf, psz_bookmark_name, &val );
545 char *psz_bookmark = strdup( val.psz_string );
546 for( i_position = 0 ; i_position < p_playlist->i_size ; i_position++)
548 if( !strcmp( psz_bookmark, p_playlist->pp_items[i_position]->psz_uri ) )
550 playlist_Goto( p_playlist, i_position );
554 vlc_object_release( p_playlist );
558 static void SetBookmark( intf_thread_t *p_intf, int i_num )
561 playlist_t *p_playlist = vlc_object_find( p_intf,
562 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
565 char psz_bookmark_name[11];
566 sprintf( psz_bookmark_name, "bookmark%i", i_num );
567 var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
568 val.psz_string = strdup( p_playlist->pp_items[p_playlist->i_index]->psz_uri );
569 var_Set( p_intf, psz_bookmark_name, val );
570 msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num, val.psz_string );
571 vlc_object_release( p_playlist );