]> git.sesse.net Git - vlc/blob - modules/control/hotkeys.c
3a9a172007eda6f0f4197281c2a255b8b3df1335
[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.11 2003/12/10 20:56:09 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_FULLSCREEN )
219         {
220             if( p_vout )
221             {
222                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
223             }
224         }
225         else if( i_action == ACTIONID_PLAY )
226         {
227             p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
228                                           FIND_ANYWHERE );
229             if( p_playlist )
230             {
231                 vlc_mutex_lock( &p_playlist->object_lock );
232                 if( p_playlist->i_size )
233                 {
234                     vlc_mutex_unlock( &p_playlist->object_lock );
235                     playlist_Play( p_playlist );
236                     vlc_object_release( p_playlist );
237                 }
238             }
239         }
240         else if( i_action == ACTIONID_PLAY_PAUSE )
241         {
242             val.i_int = PLAYING_S;
243             if( p_input )
244             {
245                 var_Get( p_input, "state", &val );
246             }
247             if( p_input && val.i_int != PAUSE_S )
248             {
249                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Pause" ) );
250                 val.i_int = PAUSE_S;
251                 var_Set( p_input, "state", val );
252             }
253             else
254             {
255                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
256                                               FIND_ANYWHERE );
257                 if( p_playlist )
258                 {
259                     vlc_mutex_lock( &p_playlist->object_lock );
260                     if( p_playlist->i_size )
261                     {
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 );
266                     }
267                 }
268             }
269         }
270         else if( p_input )
271         {
272             if( i_action == ACTIONID_PAUSE )
273             {
274                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Pause" ) );
275                 val.i_int = PAUSE_S;
276                 var_Set( p_input, "state", val );
277             }
278             else if( i_action == ACTIONID_JUMP_BACKWARD_10SEC )
279             {
280                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -10 seconds" ) );
281                 val.i_time = -10000000;
282                 var_Set( p_input, "time-offset", val );
283             }
284             else if( i_action == ACTIONID_JUMP_FORWARD_10SEC )
285             {
286                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +10 seconds" ) );
287                 val.i_time = 10000000;
288                 var_Set( p_input, "time-offset", val );
289             }
290             else if( i_action == ACTIONID_JUMP_BACKWARD_1MIN )
291             {
292                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -1 minute" ) );
293                 val.i_time = -60000000;
294                 var_Set( p_input, "time-offset", val );
295             }
296             else if( i_action == ACTIONID_JUMP_FORWARD_1MIN )
297             {
298                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +1 minute" ) );
299                 val.i_time = 60000000;
300                 var_Set( p_input, "time-offset", val );
301             }
302             else if( i_action == ACTIONID_JUMP_BACKWARD_5MIN )
303             {
304                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump -5 minutes" ) );
305                 val.i_time = -300000000;
306                 var_Set( p_input, "time-offset", val );
307             }
308             else if( i_action == ACTIONID_JUMP_FORWARD_5MIN )
309             {
310                 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Jump +5 minutes" ) );
311                 val.i_time = 300000000;
312                 var_Set( p_input, "time-offset", val );
313             }
314             else if( i_action == ACTIONID_NEXT )
315             {
316                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
317                                               FIND_ANYWHERE );
318                 if( p_playlist )
319                 {
320                     playlist_Next( p_playlist );
321                     vlc_object_release( p_playlist );
322                 }
323             }
324             else if( i_action == ACTIONID_PREV )
325             {
326                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
327                                               FIND_ANYWHERE );
328                 if( p_playlist )
329                 {
330                     playlist_Prev( p_playlist );
331                     vlc_object_release( p_playlist );
332                 }
333             }
334             else if( i_action == ACTIONID_STOP )
335             {
336                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
337                                               FIND_ANYWHERE );
338                 if( p_playlist )
339                 {
340                     playlist_Stop( p_playlist );
341                     vlc_object_release( p_playlist );
342                 }
343             }
344             else if( i_action == ACTIONID_FASTER )
345             {
346                 vlc_value_t val; val.b_bool = VLC_TRUE;
347                 var_Set( p_input, "rate-faster", val );
348             }
349             else if( i_action == ACTIONID_FASTER )
350             {
351                 vlc_value_t val; val.b_bool = VLC_TRUE;
352                 var_Set( p_input, "rate-slower", val );
353             }
354             else if( i_action == ACTIONID_POSITION )
355             {
356                 playlist_t *p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
357                                               FIND_ANYWHERE );
358                 char psz_duration[MSTRTIME_MAX_SIZE];
359                 char psz_time[MSTRTIME_MAX_SIZE];
360                 vlc_value_t time;
361                 mtime_t i_seconds;
362                                                                                                                             
363                 var_Get( p_input, "time", &time );
364                                                                                                                             
365                 if( p_playlist )
366                 {
367                     mtime_t dur =
368                         p_playlist->pp_items[p_playlist->i_index]->i_duration;
369
370                     i_seconds = time.i_time / 1000000;
371                     secstotimestr ( psz_time, i_seconds );
372                                                                                                                             
373                     if( dur != -1 )
374                     {
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 );
381                     }
382                     else if( i_seconds > 0 )
383                     { 
384                         vout_OSDMessage( VLC_OBJECT(p_playlist), psz_time );
385                     }
386                 }
387                                                                                                                             
388                 vlc_object_release( p_playlist );
389             }
390         }
391
392     }
393 }
394
395 static int GetKey( intf_thread_t *p_intf)
396 {
397     vlc_mutex_lock( &p_intf->p_sys->change_lock );
398     if ( p_intf->p_sys->i_size == 0 )
399     {
400         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
401         return -1;
402     }
403     else
404     {
405         int i_return = p_intf->p_sys->p_keys[ 0 ];
406         int i;
407         p_intf->p_sys->i_size--;
408         for ( i = 0; i < BUFFER_SIZE - 1; i++)
409         {
410             p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
411         }
412         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
413         return i_return;
414     }
415 }
416
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 )
422 {
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 )
426     {
427         msg_Warn( p_intf, "Event buffer full, dropping keypress" );
428         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
429         return VLC_EGENERIC;
430     }
431     else
432     {
433         p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
434         p_intf->p_sys->i_size++;
435     }
436     vlc_mutex_unlock( &p_intf->p_sys->change_lock );
437
438     return VLC_SUCCESS;
439 }
440
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 )
443 {
444     vlc_t *p_vlc = (vlc_t *)p_this;
445     struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
446     int i;
447
448     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
449     {
450         if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
451         {
452             p_hotkeys[i].i_key = newval.i_int;
453         }
454     }
455
456     return VLC_SUCCESS;
457 }