1 /*****************************************************************************
2 * hotkeys.c: Hotkey handling for vlc
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: hotkeys.c,v 1.7 2003/10/31 18:18:46 gbazin Exp $
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() */
32 #include <vlc/input.h>
39 #define BUFFER_SIZE 10
40 /*****************************************************************************
41 * intf_sys_t: description and status of FB interface
42 *****************************************************************************/
45 vlc_mutex_t change_lock; /* mutex to keep the callback
46 * and the main loop from
47 * stepping on each others
49 int p_keys[ BUFFER_SIZE ]; /* buffer that contains
51 int i_size; /* number of events in buffer */
52 input_thread_t * p_input; /* pointer to input */
53 vout_thread_t * p_vout; /* pointer to vout object */
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close ( vlc_object_t * );
61 static void Run ( intf_thread_t * );
62 static int GetKey ( intf_thread_t *);
63 static int KeyEvent( vlc_object_t *, char const *,
64 vlc_value_t, vlc_value_t, void * );
65 static int ActionKeyCB( vlc_object_t *, char const *,
66 vlc_value_t, vlc_value_t, void * );
68 /*****************************************************************************
70 *****************************************************************************/
72 set_description( _("hotkey interface") );
73 set_capability( "interface", 0 );
74 set_callbacks( Open, Close );
77 /*****************************************************************************
78 * Open: initialize interface
79 *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
82 intf_thread_t *p_intf = (intf_thread_t *)p_this;
84 /* Allocate instance and initialize some members */
85 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
86 if( p_intf->p_sys == NULL )
88 msg_Err( p_intf, "out of memory" );
91 vlc_mutex_init( p_intf, &p_intf->p_sys->change_lock );
92 p_intf->p_sys->i_size = 0;
95 p_intf->p_sys->p_input = NULL;
96 p_intf->p_sys->p_vout = NULL;
98 var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
102 /*****************************************************************************
103 * Close: destroy interface
104 *****************************************************************************/
105 static void Close( vlc_object_t *p_this )
107 intf_thread_t *p_intf = (intf_thread_t *)p_this;
109 if( p_intf->p_sys->p_input )
111 vlc_object_release( p_intf->p_sys->p_input );
113 if( p_intf->p_sys->p_vout )
115 vlc_object_release( p_intf->p_sys->p_vout );
117 /* Destroy structure */
118 free( p_intf->p_sys );
121 /*****************************************************************************
123 *****************************************************************************/
124 static void Run( intf_thread_t *p_intf )
126 playlist_t *p_playlist;
127 input_thread_t *p_input;
128 vout_thread_t *p_vout = NULL;
129 struct hotkey *p_hotkeys = p_intf->p_vlc->p_hotkeys;
133 /* Initialize hotkey structure */
134 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
136 var_Create( p_intf->p_vlc, p_hotkeys[i].psz_action,
137 VLC_VAR_HOTKEY | VLC_VAR_DOINHERIT );
139 var_AddCallback( p_intf->p_vlc, p_hotkeys[i].psz_action,
141 var_Get( p_intf->p_vlc, p_hotkeys[i].psz_action, &val );
142 var_Set( p_intf->p_vlc, p_hotkeys[i].psz_action, val );
145 while( !p_intf->b_die )
150 msleep( INTF_IDLE_SLEEP );
152 /* Update the input */
153 if( p_intf->p_sys->p_input == NULL )
155 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
158 else if( p_intf->p_sys->p_input->b_dead )
160 vlc_object_release( p_intf->p_sys->p_input );
161 p_intf->p_sys->p_input = NULL;
163 p_input = p_intf->p_sys->p_input;
165 /* Update the vout */
168 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
170 p_intf->p_sys->p_vout = p_vout;
172 else if( p_vout->b_die )
174 vlc_object_release( p_vout );
176 p_intf->p_sys->p_vout = NULL;
179 /* Find action triggered by hotkey */
181 i_key = GetKey( p_intf );
182 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
184 if( p_hotkeys[i].i_key == i_key )
186 i_action = p_hotkeys[i].i_action;
192 /* No key pressed, sleep a bit more */
193 msleep( INTF_IDLE_SLEEP );
197 if( i_action == ACTIONID_QUIT )
199 p_intf->p_vlc->b_die = VLC_TRUE;
200 vout_OSDMessage( VLC_OBJECT(p_intf), _("Quit" ) );
203 else if( i_action == ACTIONID_VOL_UP )
205 audio_volume_t i_newvol;
207 aout_VolumeUp( p_intf, 1, &i_newvol );
208 sprintf( string, "Vol %d%%", i_newvol*100/AOUT_VOLUME_MAX );
209 vout_OSDMessage( VLC_OBJECT(p_intf), string );
211 else if( i_action == ACTIONID_VOL_DOWN )
213 audio_volume_t i_newvol;
215 aout_VolumeDown( p_intf, 1, &i_newvol );
216 sprintf( string, "Vol %d%%", i_newvol*100/AOUT_VOLUME_MAX );
217 vout_OSDMessage( VLC_OBJECT(p_intf), string );
219 else if( i_action == ACTIONID_FULLSCREEN )
223 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
226 else if( i_action == ACTIONID_PLAY )
228 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
232 vlc_mutex_lock( &p_playlist->object_lock );
233 if( p_playlist->i_size )
235 vlc_mutex_unlock( &p_playlist->object_lock );
236 playlist_Play( p_playlist );
237 vlc_object_release( p_playlist );
241 else if( i_action == ACTIONID_PLAY_PAUSE )
244 p_input->stream.control.i_status != PAUSE_S )
246 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Pause" ) );
247 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
251 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
255 vlc_mutex_lock( &p_playlist->object_lock );
256 if( p_playlist->i_size )
258 vlc_mutex_unlock( &p_playlist->object_lock );
259 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Play" ) );
260 playlist_Play( p_playlist );
261 vlc_object_release( p_playlist );
270 if( i_action == ACTIONID_PAUSE )
272 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Pause" ) );
273 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
275 else if( i_action == ACTIONID_JUMP_BACKWARD_10SEC )
277 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -10 seconds" ) );
278 demux_Control( p_input, DEMUX_GET_TIME, &i_time );
279 demux_Control( p_input, DEMUX_SET_TIME, i_time - 10000000 );
281 else if( i_action == ACTIONID_JUMP_FORWARD_10SEC )
283 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +10 seconds" ) );
284 demux_Control( p_input, DEMUX_GET_TIME, &i_time );
285 demux_Control( p_input, DEMUX_SET_TIME, i_time + 10000000 );
287 else if( i_action == ACTIONID_JUMP_BACKWARD_1MIN )
289 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -1 minute" ) );
290 demux_Control( p_input, DEMUX_GET_TIME, &i_time );
291 demux_Control( p_input, DEMUX_SET_TIME, i_time - 60000000 );
293 else if( i_action == ACTIONID_JUMP_FORWARD_1MIN )
295 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +1 minute" ) );
296 demux_Control( p_input, DEMUX_GET_TIME, &i_time );
297 demux_Control( p_input, DEMUX_SET_TIME, i_time + 60000000 );
299 else if( i_action == ACTIONID_JUMP_BACKWARD_5MIN )
301 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -5 minutes" ) );
302 demux_Control( p_input, DEMUX_GET_TIME, &i_time );
303 demux_Control( p_input, DEMUX_SET_TIME, i_time - 300000000 );
305 else if( i_action == ACTIONID_JUMP_FORWARD_5MIN )
307 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +5 minutes" ) );
308 demux_Control( p_input, DEMUX_GET_TIME, &i_time );
309 demux_Control( p_input, DEMUX_SET_TIME, i_time + 300000000 );
311 else if( i_action == ACTIONID_NEXT )
313 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
317 playlist_Next( p_playlist );
318 vlc_object_release( p_playlist );
321 else if( i_action == ACTIONID_PREV )
323 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
327 playlist_Prev( p_playlist );
328 vlc_object_release( p_playlist );
331 else if( i_action == ACTIONID_STOP )
333 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
337 playlist_Stop( p_playlist );
338 vlc_object_release( p_playlist );
341 else if( i_action == ACTIONID_FASTER )
343 vlc_value_t val; val.b_bool = VLC_TRUE;
344 var_Set( p_input, "rate-faster", val );
346 else if( i_action == ACTIONID_FASTER )
348 vlc_value_t val; val.b_bool = VLC_TRUE;
349 var_Set( p_input, "rate-slower", val );
356 static int GetKey( intf_thread_t *p_intf)
358 vlc_mutex_lock( &p_intf->p_sys->change_lock );
359 if ( p_intf->p_sys->i_size == 0 )
361 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
366 int i_return = p_intf->p_sys->p_keys[ 0 ];
368 p_intf->p_sys->i_size--;
369 for ( i = 0; i < BUFFER_SIZE - 1; i++)
371 p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
373 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
378 /*****************************************************************************
379 * KeyEvent: callback for keyboard events
380 *****************************************************************************/
381 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
382 vlc_value_t oldval, vlc_value_t newval, void *p_data )
384 intf_thread_t *p_intf = (intf_thread_t *)p_data;
385 vlc_mutex_lock( &p_intf->p_sys->change_lock );
386 if ( p_intf->p_sys->i_size == BUFFER_SIZE )
388 msg_Warn( p_intf, "Event buffer full, dropping keypress" );
389 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
394 p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
395 p_intf->p_sys->i_size++;
397 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
402 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
403 vlc_value_t oldval, vlc_value_t newval, void *p_data )
405 vlc_t *p_vlc = (vlc_t *)p_this;
406 struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
409 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
411 if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
413 p_hotkeys[i].i_key = newval.i_int;