]> git.sesse.net Git - vlc/blob - modules/control/hotkeys.c
added a 'mute' hotkey ('m')
[vlc] / modules / control / hotkeys.c
1 /*****************************************************************************
2  * hotkeys.c: Hotkey handling for vlc
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: hotkeys.c,v 1.12 2003/12/11 01:36:12 yoann Exp $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31 #include <vlc/input.h>
32 #include <vlc/vout.h>
33 #include <vlc/aout.h>
34 #include <osd.h>
35
36 #include "vlc_keys.h"
37
38 #define BUFFER_SIZE 10
39 /*****************************************************************************
40  * intf_sys_t: description and status of FB interface
41  *****************************************************************************/
42 struct intf_sys_t
43 {
44     vlc_mutex_t         change_lock;  /* mutex to keep the callback
45                                        * and the main loop from
46                                        * stepping on each others
47                                        * toes */
48     int                 p_keys[ BUFFER_SIZE ]; /* buffer that contains
49                                                 * keyevents */
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 */
53 };
54
55 /*****************************************************************************
56  * Local prototypes
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
67 /*****************************************************************************
68  * Module descriptor
69  *****************************************************************************/
70 vlc_module_begin();
71     set_description( _("hotkey interface") );
72     set_capability( "interface", 0 );
73     set_callbacks( Open, Close );
74 vlc_module_end();
75
76 /*****************************************************************************
77  * Open: initialize interface
78  *****************************************************************************/
79 static int Open( vlc_object_t *p_this )
80 {
81     intf_thread_t *p_intf = (intf_thread_t *)p_this;
82
83     /* Allocate instance and initialize some members */
84     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
85     if( p_intf->p_sys == NULL )
86     {
87         msg_Err( p_intf, "out of memory" );
88         return 1;
89     }
90     vlc_mutex_init( p_intf, &p_intf->p_sys->change_lock );
91     p_intf->p_sys->i_size = 0;
92     p_intf->pf_run = Run;
93
94     p_intf->p_sys->p_input = NULL;
95     p_intf->p_sys->p_vout = NULL;
96
97     var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
98     return 0;
99 }
100
101 /*****************************************************************************
102  * Close: destroy interface
103  *****************************************************************************/
104 static void Close( vlc_object_t *p_this )
105 {
106     intf_thread_t *p_intf = (intf_thread_t *)p_this;
107
108     if( p_intf->p_sys->p_input )
109     {
110         vlc_object_release( p_intf->p_sys->p_input );
111     }
112     if( p_intf->p_sys->p_vout )
113     {
114         vlc_object_release( p_intf->p_sys->p_vout );
115     }
116     /* Destroy structure */
117     free( p_intf->p_sys );
118 }
119
120 /*****************************************************************************
121  * Run: main loop
122  *****************************************************************************/
123 static void Run( intf_thread_t *p_intf )
124 {
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;
129     vlc_value_t val;
130     int i;
131
132     /* Initialize hotkey structure */
133     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
134     {
135         var_Create( p_intf->p_vlc, p_hotkeys[i].psz_action,
136                     VLC_VAR_HOTKEY | VLC_VAR_DOINHERIT );
137
138         var_AddCallback( p_intf->p_vlc, p_hotkeys[i].psz_action,
139                          ActionKeyCB, NULL );
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 );
142     }
143
144     while( !p_intf->b_die )
145     {
146         int i_key, i_action;
147
148         /* Sleep a bit */
149         msleep( INTF_IDLE_SLEEP );
150
151         /* Update the input */
152         if( p_intf->p_sys->p_input == NULL )
153         {
154             p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
155                                                       FIND_ANYWHERE );
156         }
157         else if( p_intf->p_sys->p_input->b_dead )
158         {
159             vlc_object_release( p_intf->p_sys->p_input );
160             p_intf->p_sys->p_input = NULL;
161         }
162         p_input = p_intf->p_sys->p_input;
163
164         /* Update the vout */
165         if( p_vout == NULL )
166         {
167             p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
168                                       FIND_ANYWHERE );
169             p_intf->p_sys->p_vout = p_vout;
170         }
171         else if( p_vout->b_die )
172         {
173             vlc_object_release( p_vout );
174             p_vout = NULL;
175             p_intf->p_sys->p_vout = NULL;
176         }
177
178         /* Find action triggered by hotkey */
179         i_action = 0;
180         i_key = GetKey( p_intf );
181         for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
182         {
183             if( p_hotkeys[i].i_key == i_key )
184             {
185                  i_action = p_hotkeys[i].i_action;
186             }
187         }
188
189         if( !i_action )
190         {
191             /* No key pressed, sleep a bit more */
192             msleep( INTF_IDLE_SLEEP );
193             continue;
194         }
195
196         if( i_action == ACTIONID_QUIT )
197         {
198             p_intf->p_vlc->b_die = VLC_TRUE;
199             vout_OSDMessage( VLC_OBJECT(p_intf), _("Quit" ) );
200             continue;
201         }
202         else if( i_action == ACTIONID_VOL_UP )
203         {
204             audio_volume_t i_newvol;
205             char string[9];
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 );
209         }
210         else if( i_action == ACTIONID_VOL_DOWN )
211         {
212             audio_volume_t i_newvol;
213             char string[9];
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 );
217         }
218         else if( i_action == ACTIONID_VOL_MUTE )
219         {
220             audio_volume_t i_newvol = -1;
221             aout_VolumeMute( p_intf, &i_newvol );
222             if( i_newvol == 0 )
223             {
224                 vout_OSDMessage( VLC_OBJECT(p_intf), "Mute" );
225             }
226             else
227             {
228                 char string[9];
229                 sprintf( string, "Vol %d%%", i_newvol*100/AOUT_VOLUME_MAX );
230                 vout_OSDMessage( VLC_OBJECT(p_intf), string );
231             }
232         }
233         else if( i_action == ACTIONID_FULLSCREEN )
234         {
235             if( p_vout )
236             {
237                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
238             }
239         }
240         else if( i_action == ACTIONID_PLAY )
241         {
242             p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
243                                           FIND_ANYWHERE );
244             if( p_playlist )
245             {
246                 vlc_mutex_lock( &p_playlist->object_lock );
247                 if( p_playlist->i_size )
248                 {
249                     vlc_mutex_unlock( &p_playlist->object_lock );
250                     playlist_Play( p_playlist );
251                     vlc_object_release( p_playlist );
252                 }
253             }
254         }
255         else if( i_action == ACTIONID_PLAY_PAUSE )
256         {
257             val.i_int = PLAYING_S;
258             if( p_input )
259             {
260                 var_Get( p_input, "state", &val );
261             }
262             if( p_input && val.i_int != PAUSE_S )
263             {
264                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Pause" ) );
265                 val.i_int = PAUSE_S;
266                 var_Set( p_input, "state", val );
267             }
268             else
269             {
270                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
271                                               FIND_ANYWHERE );
272                 if( p_playlist )
273                 {
274                     vlc_mutex_lock( &p_playlist->object_lock );
275                     if( p_playlist->i_size )
276                     {
277                         vlc_mutex_unlock( &p_playlist->object_lock );
278                         vout_OSDMessage( VLC_OBJECT(p_intf), _( "Play" ) );
279                         playlist_Play( p_playlist );
280                         vlc_object_release( p_playlist );
281                     }
282                 }
283             }
284         }
285         else if( p_input )
286         {
287             if( i_action == ACTIONID_PAUSE )
288             {
289                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Pause" ) );
290                 val.i_int = PAUSE_S;
291                 var_Set( p_input, "state", val );
292             }
293             else if( i_action == ACTIONID_JUMP_BACKWARD_10SEC )
294             {
295                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -10 seconds" ) );
296                 val.i_time = -10000000;
297                 var_Set( p_input, "time-offset", val );
298             }
299             else if( i_action == ACTIONID_JUMP_FORWARD_10SEC )
300             {
301                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +10 seconds" ) );
302                 val.i_time = 10000000;
303                 var_Set( p_input, "time-offset", val );
304             }
305             else if( i_action == ACTIONID_JUMP_BACKWARD_1MIN )
306             {
307                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -1 minute" ) );
308                 val.i_time = -60000000;
309                 var_Set( p_input, "time-offset", val );
310             }
311             else if( i_action == ACTIONID_JUMP_FORWARD_1MIN )
312             {
313                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +1 minute" ) );
314                 val.i_time = 60000000;
315                 var_Set( p_input, "time-offset", val );
316             }
317             else if( i_action == ACTIONID_JUMP_BACKWARD_5MIN )
318             {
319                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -5 minutes" ) );
320                 val.i_time = -300000000;
321                 var_Set( p_input, "time-offset", val );
322             }
323             else if( i_action == ACTIONID_JUMP_FORWARD_5MIN )
324             {
325                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +5 minutes" ) );
326                 val.i_time = 300000000;
327                 var_Set( p_input, "time-offset", val );
328             }
329             else if( i_action == ACTIONID_NEXT )
330             {
331                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
332                                               FIND_ANYWHERE );
333                 if( p_playlist )
334                 {
335                     playlist_Next( p_playlist );
336                     vlc_object_release( p_playlist );
337                 }
338             }
339             else if( i_action == ACTIONID_PREV )
340             {
341                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
342                                               FIND_ANYWHERE );
343                 if( p_playlist )
344                 {
345                     playlist_Prev( p_playlist );
346                     vlc_object_release( p_playlist );
347                 }
348             }
349             else if( i_action == ACTIONID_STOP )
350             {
351                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
352                                               FIND_ANYWHERE );
353                 if( p_playlist )
354                 {
355                     playlist_Stop( p_playlist );
356                     vlc_object_release( p_playlist );
357                 }
358             }
359             else if( i_action == ACTIONID_FASTER )
360             {
361                 vlc_value_t val; val.b_bool = VLC_TRUE;
362                 var_Set( p_input, "rate-faster", val );
363             }
364             else if( i_action == ACTIONID_FASTER )
365             {
366                 vlc_value_t val; val.b_bool = VLC_TRUE;
367                 var_Set( p_input, "rate-slower", val );
368             }
369             else if( i_action == ACTIONID_POSITION )
370             {
371                 playlist_t *p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
372                                               FIND_ANYWHERE );
373                 char psz_duration[MSTRTIME_MAX_SIZE];
374                 char psz_time[MSTRTIME_MAX_SIZE];
375                 vlc_value_t time;
376                 mtime_t i_seconds;
377                                                                                                                             
378                 var_Get( p_input, "time", &time );
379                                                                                                                             
380                 if( p_playlist )
381                 {
382                     mtime_t dur =
383                         p_playlist->pp_items[p_playlist->i_index]->i_duration;
384
385                     i_seconds = time.i_time / 1000000;
386                     secstotimestr ( psz_time, i_seconds );
387                                                                                                                             
388                     if( dur != -1 )
389                     {
390                         char psz_position[2*MSTRTIME_MAX_SIZE + 3];
391                         secstotimestr( psz_duration, dur/1000000 );
392                         strcpy( psz_position, psz_time );
393                         strcat( psz_position, " / " );
394                         strcat( psz_position, psz_duration );
395                         vout_OSDMessage( VLC_OBJECT(p_playlist), psz_position );
396                     }
397                     else if( i_seconds > 0 )
398                     { 
399                         vout_OSDMessage( VLC_OBJECT(p_playlist), psz_time );
400                     }
401                 }
402                                                                                                                             
403                 vlc_object_release( p_playlist );
404             }
405         }
406
407     }
408 }
409
410 static int GetKey( intf_thread_t *p_intf)
411 {
412     vlc_mutex_lock( &p_intf->p_sys->change_lock );
413     if ( p_intf->p_sys->i_size == 0 )
414     {
415         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
416         return -1;
417     }
418     else
419     {
420         int i_return = p_intf->p_sys->p_keys[ 0 ];
421         int i;
422         p_intf->p_sys->i_size--;
423         for ( i = 0; i < BUFFER_SIZE - 1; i++)
424         {
425             p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
426         }
427         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
428         return i_return;
429     }
430 }
431
432 /*****************************************************************************
433  * KeyEvent: callback for keyboard events
434  *****************************************************************************/
435 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
436                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
437 {
438     intf_thread_t *p_intf = (intf_thread_t *)p_data;
439     vlc_mutex_lock( &p_intf->p_sys->change_lock );
440     if ( p_intf->p_sys->i_size == BUFFER_SIZE )
441     {
442         msg_Warn( p_intf, "Event buffer full, dropping keypress" );
443         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
444         return VLC_EGENERIC;
445     }
446     else
447     {
448         p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
449         p_intf->p_sys->i_size++;
450     }
451     vlc_mutex_unlock( &p_intf->p_sys->change_lock );
452
453     return VLC_SUCCESS;
454 }
455
456 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
457                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
458 {
459     vlc_t *p_vlc = (vlc_t *)p_this;
460     struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
461     int i;
462
463     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
464     {
465         if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
466         {
467             p_hotkeys[i].i_key = newval.i_int;
468         }
469     }
470
471     return VLC_SUCCESS;
472 }