]> git.sesse.net Git - vlc/blob - modules/control/hotkeys.c
* modules/control/hotkeys.c: fixed fullscreen hotkey when video filters are used.
[vlc] / modules / control / hotkeys.c
1 /*****************************************************************************
2  * hotkeys.c: Hotkey handling for vlc
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
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 static void PlayBookmark( intf_thread_t *, int );
67 static void SetBookmark ( intf_thread_t *, int );
68
69 /*****************************************************************************
70  * Module descriptor
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.")
84
85 vlc_module_begin();
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 ); 
107     
108     set_capability( "interface", 0 );
109     set_callbacks( Open, Close );
110 vlc_module_end();
111
112 /*****************************************************************************
113  * Open: initialize interface
114  *****************************************************************************/
115 static int Open( vlc_object_t *p_this )
116 {
117     intf_thread_t *p_intf = (intf_thread_t *)p_this;
118
119     /* Allocate instance and initialize some members */
120     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
121     if( p_intf->p_sys == NULL )
122     {
123         msg_Err( p_intf, "out of memory" );
124         return 1;
125     }
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;
129
130     p_intf->p_sys->p_input = NULL;
131     p_intf->p_sys->p_vout = NULL;
132
133     var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
134     return 0;
135 }
136
137 /*****************************************************************************
138  * Close: destroy interface
139  *****************************************************************************/
140 static void Close( vlc_object_t *p_this )
141 {
142     intf_thread_t *p_intf = (intf_thread_t *)p_this;
143
144     if( p_intf->p_sys->p_input )
145     {
146         vlc_object_release( p_intf->p_sys->p_input );
147     }
148     if( p_intf->p_sys->p_vout )
149     {
150         vlc_object_release( p_intf->p_sys->p_vout );
151     }
152     /* Destroy structure */
153     free( p_intf->p_sys );
154 }
155
156 /*****************************************************************************
157  * Run: main loop
158  *****************************************************************************/
159 static void Run( intf_thread_t *p_intf )
160 {
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;
165     vlc_value_t val;
166     int i;
167
168     /* Initialize hotkey structure */
169     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
170     {
171         var_Create( p_intf->p_vlc, p_hotkeys[i].psz_action,
172                     VLC_VAR_HOTKEY | VLC_VAR_DOINHERIT );
173
174         var_AddCallback( p_intf->p_vlc, p_hotkeys[i].psz_action,
175                          ActionKeyCB, NULL );
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 );
178     }
179
180     while( !p_intf->b_die )
181     {
182         int i_key, i_action;
183
184         /* Sleep a bit */
185         msleep( INTF_IDLE_SLEEP );
186
187         /* Update the input */
188         if( p_intf->p_sys->p_input == NULL )
189         {
190             p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
191                                                       FIND_ANYWHERE );
192         }
193         else if( p_intf->p_sys->p_input->b_dead )
194         {
195             vlc_object_release( p_intf->p_sys->p_input );
196             p_intf->p_sys->p_input = NULL;
197         }
198         p_input = p_intf->p_sys->p_input;
199
200         /* Update the vout */
201         if( p_vout == NULL )
202         {
203             p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
204                                       FIND_ANYWHERE );
205             p_intf->p_sys->p_vout = p_vout;
206         }
207         else if( p_vout->b_die )
208         {
209             vlc_object_release( p_vout );
210             p_vout = NULL;
211             p_intf->p_sys->p_vout = NULL;
212         }
213
214         /* Find action triggered by hotkey */
215         i_action = 0;
216         i_key = GetKey( p_intf );
217         for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
218         {
219             if( p_hotkeys[i].i_key == i_key )
220             {
221                  i_action = p_hotkeys[i].i_action;
222             }
223         }
224
225         if( !i_action )
226         {
227             /* No key pressed, sleep a bit more */
228             msleep( INTF_IDLE_SLEEP );
229             continue;
230         }
231
232         if( i_action == ACTIONID_QUIT )
233         {
234             p_intf->p_vlc->b_die = VLC_TRUE;
235             vout_OSDMessage( p_intf, _( "Quit" ) );
236             continue;
237         }
238         else if( i_action == ACTIONID_VOL_UP )
239         {
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 );
243         }
244         else if( i_action == ACTIONID_VOL_DOWN )
245         {
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);
249         }
250         else if( i_action == ACTIONID_SUBDELAY_DOWN )
251         {
252             int i_delay;
253             if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
254                              VLC_SUCCESS )
255             {
256                 i_delay--;
257                 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
258                 vout_OSDMessage( p_intf, "Subtitle delay %i ms",i_delay*100);
259             }
260         }
261         else if( i_action == ACTIONID_SUBDELAY_UP )
262         {
263             int i_delay;
264             if( input_Control( p_input, INPUT_GET_SUBDELAY, &i_delay ) ==
265                              VLC_SUCCESS )
266             {
267                 i_delay++;
268                 input_Control( p_input, INPUT_SET_SUBDELAY, i_delay );
269                 vout_OSDMessage( p_intf, "Subtitle delay %i ms",i_delay*100);
270             }
271         }
272         else if( i_action == ACTIONID_VOL_MUTE )
273         {
274             audio_volume_t i_newvol = -1;
275             aout_VolumeMute( p_intf, &i_newvol );
276             if( i_newvol == 0 )
277             {
278                 vout_OSDMessage( p_intf, _( "Mute" ) );
279             }
280             else
281             {
282                 vout_OSDMessage( p_intf, "Vol %d%%",
283                                  i_newvol*100/AOUT_VOLUME_MAX );
284             }
285         }
286         else if( i_action == ACTIONID_FULLSCREEN && p_vout )
287         {
288             var_Get( p_vout, "fullscreen", &val );
289             var_Set( p_vout, "fullscreen", (vlc_value_t)!val.b_bool );
290         }
291         else if( i_action == ACTIONID_PLAY )
292         {
293             p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
294                                           FIND_ANYWHERE );
295             if( p_playlist )
296             {
297                 vlc_mutex_lock( &p_playlist->object_lock );
298                 if( p_playlist->i_size )
299                 {
300                     vlc_mutex_unlock( &p_playlist->object_lock );
301                     playlist_Play( p_playlist );
302                     vlc_object_release( p_playlist );
303                 }
304             }
305         }
306         else if( i_action == ACTIONID_PLAY_PAUSE )
307         {
308             val.i_int = PLAYING_S;
309             if( p_input )
310             {
311                 var_Get( p_input, "state", &val );
312             }
313             if( p_input && val.i_int != PAUSE_S )
314             {
315                 vout_OSDMessage( p_intf, _( "Pause" ) );
316                 val.i_int = PAUSE_S;
317                 var_Set( p_input, "state", val );
318             }
319             else
320             {
321                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
322                                               FIND_ANYWHERE );
323                 if( p_playlist )
324                 {
325                     vlc_mutex_lock( &p_playlist->object_lock );
326                     if( p_playlist->i_size )
327                     {
328                         vlc_mutex_unlock( &p_playlist->object_lock );
329                         vout_OSDMessage( p_intf, _( "Play" ) );
330                         playlist_Play( p_playlist );
331                         vlc_object_release( p_playlist );
332                     }
333                 }
334             }
335         }
336         else if( p_input )
337         {
338             if( i_action == ACTIONID_PAUSE )
339             {
340                 vout_OSDMessage( p_intf, _( "Pause" ) );
341                 val.i_int = PAUSE_S;
342                 var_Set( p_input, "state", val );
343             }
344             else if( i_action == ACTIONID_JUMP_BACKWARD_10SEC )
345             {
346                 vout_OSDMessage( p_intf, _( "Jump -10 seconds" ) );
347                 val.i_time = -10000000;
348                 var_Set( p_input, "time-offset", val );
349             }
350             else if( i_action == ACTIONID_JUMP_FORWARD_10SEC )
351             {
352                 vout_OSDMessage( p_intf, _( "Jump +10 seconds" ) );
353                 val.i_time = 10000000;
354                 var_Set( p_input, "time-offset", val );
355             }
356             else if( i_action == ACTIONID_JUMP_BACKWARD_1MIN )
357             {
358                 vout_OSDMessage( p_intf, _( "Jump -1 minute" ) );
359                 val.i_time = -60000000;
360                 var_Set( p_input, "time-offset", val );
361             }
362             else if( i_action == ACTIONID_JUMP_FORWARD_1MIN )
363             {
364                 vout_OSDMessage( p_intf, _( "Jump +1 minute" ) );
365                 val.i_time = 60000000;
366                 var_Set( p_input, "time-offset", val );
367             }
368             else if( i_action == ACTIONID_JUMP_BACKWARD_5MIN )
369             {
370                 vout_OSDMessage( p_intf, _( "Jump -5 minutes" ) );
371                 val.i_time = -300000000;
372                 var_Set( p_input, "time-offset", val );
373             }
374             else if( i_action == ACTIONID_JUMP_FORWARD_5MIN )
375             {
376                 vout_OSDMessage( p_intf, _( "Jump +5 minutes" ) );
377                 val.i_time = 300000000;
378                 var_Set( p_input, "time-offset", val );
379             }
380             else if( i_action == ACTIONID_NEXT )
381             {
382                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
383                                               FIND_ANYWHERE );
384                 if( p_playlist )
385                 {
386                     playlist_Next( p_playlist );
387                     vlc_object_release( p_playlist );
388                 }
389             }
390             else if( i_action == ACTIONID_PREV )
391             {
392                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
393                                               FIND_ANYWHERE );
394                 if( p_playlist )
395                 {
396                     playlist_Prev( p_playlist );
397                     vlc_object_release( p_playlist );
398                 }
399             }
400             else if( i_action == ACTIONID_STOP )
401             {
402                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
403                                               FIND_ANYWHERE );
404                 if( p_playlist )
405                 {
406                     playlist_Stop( p_playlist );
407                     vlc_object_release( p_playlist );
408                 }
409             }
410             else if( i_action == ACTIONID_FASTER )
411             {
412                 vlc_value_t val; val.b_bool = VLC_TRUE;
413                 var_Set( p_input, "rate-faster", val );
414             }
415             else if( i_action == ACTIONID_SLOWER )
416             {
417                 vlc_value_t val; val.b_bool = VLC_TRUE;
418                 var_Set( p_input, "rate-slower", val );
419             }
420             else if( i_action == ACTIONID_POSITION )
421             {
422                 playlist_t *p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
423                                               FIND_ANYWHERE );
424                 char psz_duration[MSTRTIME_MAX_SIZE];
425                 char psz_time[MSTRTIME_MAX_SIZE];
426                 vlc_value_t time;
427                 mtime_t i_seconds;
428                                                                                                                             
429                 var_Get( p_input, "time", &time );
430                                                                                                                             
431                 if( p_playlist )
432                 {
433                     mtime_t dur =
434                         p_playlist->pp_items[p_playlist->i_index]->input.i_duration;
435
436                     i_seconds = time.i_time / 1000000;
437                     secstotimestr ( psz_time, i_seconds );
438                                                                                                                             
439                     if( dur != -1 )
440                     {
441                         secstotimestr( psz_duration, dur/1000000 );
442                         vout_OSDMessage( p_playlist, "%s / %s", psz_time, psz_duration );
443                     }
444                     else if( i_seconds > 0 )
445                     { 
446                         vout_OSDMessage( p_playlist, psz_time );
447                     }
448                     vlc_object_release( p_playlist );
449                 }
450             }
451             else if( i_action >= ACTIONID_PLAY_BOOKMARK1 &&
452                      i_action <= ACTIONID_PLAY_BOOKMARK10 )
453             {
454                 PlayBookmark( p_intf, i_action - ACTIONID_PLAY_BOOKMARK1 + 1 );
455             }
456             else if( i_action >= ACTIONID_SET_BOOKMARK1 &&
457                      i_action <= ACTIONID_SET_BOOKMARK10 )
458             {
459                 SetBookmark( p_intf, i_action - ACTIONID_SET_BOOKMARK1 + 1 );
460             }
461         }
462     }
463 }
464
465 static int GetKey( intf_thread_t *p_intf)
466 {
467     vlc_mutex_lock( &p_intf->p_sys->change_lock );
468     if ( p_intf->p_sys->i_size == 0 )
469     {
470         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
471         return -1;
472     }
473     else
474     {
475         int i_return = p_intf->p_sys->p_keys[ 0 ];
476         int i;
477         p_intf->p_sys->i_size--;
478         for ( i = 0; i < BUFFER_SIZE - 1; i++)
479         {
480             p_intf->p_sys->p_keys[ i ] = p_intf->p_sys->p_keys[ i + 1 ];
481         }
482         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
483         return i_return;
484     }
485 }
486
487 /*****************************************************************************
488  * KeyEvent: callback for keyboard events
489  *****************************************************************************/
490 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
491                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
492 {
493     intf_thread_t *p_intf = (intf_thread_t *)p_data;
494     vlc_mutex_lock( &p_intf->p_sys->change_lock );
495     if ( p_intf->p_sys->i_size == BUFFER_SIZE )
496     {
497         msg_Warn( p_intf, "event buffer full, dropping keypress" );
498         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
499         return VLC_EGENERIC;
500     }
501     else
502     {
503         p_intf->p_sys->p_keys[ p_intf->p_sys->i_size ] = newval.i_int;
504         p_intf->p_sys->i_size++;
505     }
506     vlc_mutex_unlock( &p_intf->p_sys->change_lock );
507
508     return VLC_SUCCESS;
509 }
510
511 static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
512                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
513 {
514     vlc_t *p_vlc = (vlc_t *)p_this;
515     struct hotkey *p_hotkeys = p_vlc->p_hotkeys;
516     int i;
517
518     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
519     {
520         if( !strcmp( p_hotkeys[i].psz_action, psz_var ) )
521         {
522             p_hotkeys[i].i_key = newval.i_int;
523         }
524     }
525
526     return VLC_SUCCESS;
527 }
528
529 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
530 {   
531     vlc_value_t val;
532     int i_position;
533     char psz_bookmark_name[11];
534     playlist_t *p_playlist = vlc_object_find( p_intf,
535              VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
536     
537     sprintf( psz_bookmark_name, "bookmark%i", i_num );
538     var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
539     var_Get( p_intf, psz_bookmark_name, &val );
540                                                                                                                     
541     if( p_playlist )
542     {
543         char *psz_bookmark = strdup( val.psz_string );
544         for( i_position = 0 ; i_position < p_playlist->i_size ; i_position++)
545         {
546             if( !strcmp( psz_bookmark, p_playlist->pp_items[i_position]->input.psz_uri ) )
547             {
548                 playlist_Goto( p_playlist, i_position );
549                 break;
550             }
551         }
552         vlc_object_release( p_playlist );
553     }
554 }
555
556 static void SetBookmark( intf_thread_t *p_intf, int i_num )
557 {
558     vlc_value_t val;
559     playlist_t *p_playlist = vlc_object_find( p_intf,
560             VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
561     if( p_playlist )
562     {
563         char psz_bookmark_name[11];
564         sprintf( psz_bookmark_name, "bookmark%i", i_num );
565         var_Create( p_intf, psz_bookmark_name, VLC_VAR_STRING|VLC_VAR_DOINHERIT );
566         val.psz_string = strdup( p_playlist->pp_items[p_playlist->i_index]->input.psz_uri );
567         var_Set( p_intf, psz_bookmark_name, val );
568         msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num, val.psz_string );
569         vlc_object_release( p_playlist );
570     }
571 }