1 /*****************************************************************************
2 * hotkeys.c: Hotkey handling for vlc
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: hotkeys.c,v 1.11 2003/12/10 20:56:09 yoann 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() */
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 * );
67 /*****************************************************************************
69 *****************************************************************************/
71 set_description( _("hotkey interface") );
72 set_capability( "interface", 0 );
73 set_callbacks( Open, Close );
76 /*****************************************************************************
77 * Open: initialize interface
78 *****************************************************************************/
79 static int Open( vlc_object_t *p_this )
81 intf_thread_t *p_intf = (intf_thread_t *)p_this;
83 /* Allocate instance and initialize some members */
84 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
85 if( p_intf->p_sys == NULL )
87 msg_Err( p_intf, "out of memory" );
90 vlc_mutex_init( p_intf, &p_intf->p_sys->change_lock );
91 p_intf->p_sys->i_size = 0;
94 p_intf->p_sys->p_input = NULL;
95 p_intf->p_sys->p_vout = NULL;
97 var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
101 /*****************************************************************************
102 * Close: destroy interface
103 *****************************************************************************/
104 static void Close( vlc_object_t *p_this )
106 intf_thread_t *p_intf = (intf_thread_t *)p_this;
108 if( p_intf->p_sys->p_input )
110 vlc_object_release( p_intf->p_sys->p_input );
112 if( p_intf->p_sys->p_vout )
114 vlc_object_release( p_intf->p_sys->p_vout );
116 /* Destroy structure */
117 free( p_intf->p_sys );
120 /*****************************************************************************
122 *****************************************************************************/
123 static void Run( intf_thread_t *p_intf )
125 playlist_t *p_playlist;
126 input_thread_t *p_input;
127 vout_thread_t *p_vout = NULL;
128 struct hotkey *p_hotkeys = p_intf->p_vlc->p_hotkeys;
132 /* Initialize hotkey structure */
133 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
135 var_Create( p_intf->p_vlc, p_hotkeys[i].psz_action,
136 VLC_VAR_HOTKEY | VLC_VAR_DOINHERIT );
138 var_AddCallback( p_intf->p_vlc, p_hotkeys[i].psz_action,
140 var_Get( p_intf->p_vlc, p_hotkeys[i].psz_action, &val );
141 var_Set( p_intf->p_vlc, p_hotkeys[i].psz_action, val );
144 while( !p_intf->b_die )
149 msleep( INTF_IDLE_SLEEP );
151 /* Update the input */
152 if( p_intf->p_sys->p_input == NULL )
154 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
157 else if( p_intf->p_sys->p_input->b_dead )
159 vlc_object_release( p_intf->p_sys->p_input );
160 p_intf->p_sys->p_input = NULL;
162 p_input = p_intf->p_sys->p_input;
164 /* Update the vout */
167 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
169 p_intf->p_sys->p_vout = p_vout;
171 else if( p_vout->b_die )
173 vlc_object_release( p_vout );
175 p_intf->p_sys->p_vout = NULL;
178 /* Find action triggered by hotkey */
180 i_key = GetKey( p_intf );
181 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
183 if( p_hotkeys[i].i_key == i_key )
185 i_action = p_hotkeys[i].i_action;
191 /* No key pressed, sleep a bit more */
192 msleep( INTF_IDLE_SLEEP );
196 if( i_action == ACTIONID_QUIT )
198 p_intf->p_vlc->b_die = VLC_TRUE;
199 vout_OSDMessage( VLC_OBJECT(p_intf), _("Quit" ) );
202 else if( i_action == ACTIONID_VOL_UP )
204 audio_volume_t i_newvol;
206 aout_VolumeUp( p_intf, 1, &i_newvol );
207 sprintf( string, "Vol %d%%", i_newvol*100/AOUT_VOLUME_MAX );
208 vout_OSDMessage( VLC_OBJECT(p_intf), string );
210 else if( i_action == ACTIONID_VOL_DOWN )
212 audio_volume_t i_newvol;
214 aout_VolumeDown( p_intf, 1, &i_newvol );
215 sprintf( string, "Vol %d%%", i_newvol*100/AOUT_VOLUME_MAX );
216 vout_OSDMessage( VLC_OBJECT(p_intf), string );
218 else if( i_action == ACTIONID_FULLSCREEN )
222 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
225 else if( i_action == ACTIONID_PLAY )
227 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
231 vlc_mutex_lock( &p_playlist->object_lock );
232 if( p_playlist->i_size )
234 vlc_mutex_unlock( &p_playlist->object_lock );
235 playlist_Play( p_playlist );
236 vlc_object_release( p_playlist );
240 else if( i_action == ACTIONID_PLAY_PAUSE )
242 val.i_int = PLAYING_S;
245 var_Get( p_input, "state", &val );
247 if( p_input && val.i_int != PAUSE_S )
249 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Pause" ) );
251 var_Set( p_input, "state", val );
255 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
259 vlc_mutex_lock( &p_playlist->object_lock );
260 if( p_playlist->i_size )
262 vlc_mutex_unlock( &p_playlist->object_lock );
263 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Play" ) );
264 playlist_Play( p_playlist );
265 vlc_object_release( p_playlist );
272 if( i_action == ACTIONID_PAUSE )
274 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Pause" ) );
276 var_Set( p_input, "state", val );
278 else if( i_action == ACTIONID_JUMP_BACKWARD_10SEC )
280 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -10 seconds" ) );
281 val.i_time = -10000000;
282 var_Set( p_input, "time-offset", val );
284 else if( i_action == ACTIONID_JUMP_FORWARD_10SEC )
286 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +10 seconds" ) );
287 val.i_time = 10000000;
288 var_Set( p_input, "time-offset", val );
290 else if( i_action == ACTIONID_JUMP_BACKWARD_1MIN )
292 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -1 minute" ) );
293 val.i_time = -60000000;
294 var_Set( p_input, "time-offset", val );
296 else if( i_action == ACTIONID_JUMP_FORWARD_1MIN )
298 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +1 minute" ) );
299 val.i_time = 60000000;
300 var_Set( p_input, "time-offset", val );
302 else if( i_action == ACTIONID_JUMP_BACKWARD_5MIN )
304 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -5 minutes" ) );
305 val.i_time = -300000000;
306 var_Set( p_input, "time-offset", val );
308 else if( i_action == ACTIONID_JUMP_FORWARD_5MIN )
310 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +5 minutes" ) );
311 val.i_time = 300000000;
312 var_Set( p_input, "time-offset", val );
314 else if( i_action == ACTIONID_NEXT )
316 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
320 playlist_Next( p_playlist );
321 vlc_object_release( p_playlist );
324 else if( i_action == ACTIONID_PREV )
326 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
330 playlist_Prev( p_playlist );
331 vlc_object_release( p_playlist );
334 else if( i_action == ACTIONID_STOP )
336 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
340 playlist_Stop( p_playlist );
341 vlc_object_release( p_playlist );
344 else if( i_action == ACTIONID_FASTER )
346 vlc_value_t val; val.b_bool = VLC_TRUE;
347 var_Set( p_input, "rate-faster", val );
349 else if( i_action == ACTIONID_FASTER )
351 vlc_value_t val; val.b_bool = VLC_TRUE;
352 var_Set( p_input, "rate-slower", val );
354 else if( i_action == ACTIONID_POSITION )
356 playlist_t *p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
358 char psz_duration[MSTRTIME_MAX_SIZE];
359 char psz_time[MSTRTIME_MAX_SIZE];
363 var_Get( p_input, "time", &time );
368 p_playlist->pp_items[p_playlist->i_index]->i_duration;
370 i_seconds = time.i_time / 1000000;
371 secstotimestr ( psz_time, i_seconds );
375 char psz_position[2*MSTRTIME_MAX_SIZE + 3];
376 secstotimestr( psz_duration, dur/1000000 );
377 strcpy( psz_position, psz_time );
378 strcat( psz_position, " / " );
379 strcat( psz_position, psz_duration );
380 vout_OSDMessage( VLC_OBJECT(p_playlist), psz_position );
382 else if( i_seconds > 0 )
384 vout_OSDMessage( VLC_OBJECT(p_playlist), psz_time );
388 vlc_object_release( p_playlist );
395 static int GetKey( intf_thread_t *p_intf)
397 vlc_mutex_lock( &p_intf->p_sys->change_lock );
398 if ( p_intf->p_sys->i_size == 0 )
400 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
405 int i_return = p_intf->p_sys->p_keys[ 0 ];
407 p_intf->p_sys->i_size--;
408 for ( i = 0; i < BUFFER_SIZE - 1; i++)
410 p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
412 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
417 /*****************************************************************************
418 * KeyEvent: callback for keyboard events
419 *****************************************************************************/
420 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
421 vlc_value_t oldval, vlc_value_t newval, void *p_data )
423 intf_thread_t *p_intf = (intf_thread_t *)p_data;
424 vlc_mutex_lock( &p_intf->p_sys->change_lock );
425 if ( p_intf->p_sys->i_size == BUFFER_SIZE )
427 msg_Warn( p_intf, "Event buffer full, dropping keypress" );
428 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
433 p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
434 p_intf->p_sys->i_size++;
436 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
441 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
442 vlc_value_t oldval, vlc_value_t newval, void *p_data )
444 vlc_t *p_vlc = (vlc_t *)p_this;
445 struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
448 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
450 if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
452 p_hotkeys[i].i_key = newval.i_int;