]> git.sesse.net Git - vlc/blob - src/interface/interface.c
402eede8adf4ecde2046b77b00ff8d1b33afc682
[vlc] / src / interface / interface.c
1 /*****************************************************************************
2  * interface.c: interface access for other threads
3  * This library provides basic functions for threads to interact with user
4  * interface, such as command line.
5  *****************************************************************************
6  * Copyright (C) 1998, 1999, 2000 VideoLAN
7  * $Id: interface.c,v 1.74 2001/04/30 15:00:59 massiot Exp $
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "defs.h"
30
31 #include <errno.h>                                                 /* ENOMEM */
32 #include <stdlib.h>                                      /* free(), strtol() */
33 #include <stdio.h>                                                   /* FILE */
34 #include <string.h>                                            /* strerror() */
35 #include <sys/types.h>                                              /* off_t */
36
37 #include "config.h"
38 #include "common.h"
39 #include "threads.h"
40 #include "mtime.h"
41 #include "modules.h"
42
43 #include "stream_control.h"
44 #include "input_ext-intf.h"
45
46 #include "audio_output.h"
47
48 #include "intf_msg.h"
49 #include "interface.h"
50 #include "intf_playlist.h"
51 #include "intf_channels.h"
52 #include "keystrokes.h"
53
54 #include "video.h"
55 #include "video_output.h"
56
57 #include "main.h"
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static void intf_Manage( intf_thread_t *p_intf );
63
64 /*****************************************************************************
65  * intf_Create: prepare interface before main loop
66  *****************************************************************************
67  * This function opens output devices and creates specific interfaces. It sends
68  * its own error messages.
69  *****************************************************************************/
70 intf_thread_t* intf_Create( void )
71 {
72     intf_thread_t * p_intf;
73
74     /* Allocate structure */
75     p_intf = malloc( sizeof( intf_thread_t ) );
76     if( !p_intf )
77     {
78         intf_ErrMsg( "intf error: cannot create interface thread (%s)",
79                      strerror( ENOMEM ) );
80         return( NULL );
81     }
82
83     /* Choose the best module */
84     p_intf->p_module = module_Need( p_main->p_bank,
85                                     MODULE_CAPABILITY_INTF, NULL );
86
87     if( p_intf->p_module == NULL )
88     {
89         intf_ErrMsg( "intf error: no suitable intf module" );
90         free( p_intf );
91         return( NULL );
92     }
93
94 #define f p_intf->p_module->p_functions->intf.functions.intf
95     p_intf->pf_open       = f.pf_open;
96     p_intf->pf_close      = f.pf_close;
97     p_intf->pf_run        = f.pf_run;
98 #undef f
99
100     /* Initialize callbacks */
101     p_intf->pf_manage     = intf_Manage;
102
103     /* Initialize structure */
104     p_intf->b_die         = 0;
105
106     p_intf->p_input       = NULL;
107     p_intf->p_keys        = NULL;
108     p_intf->b_menu        = 0;
109     p_intf->b_menu_change = 0;
110
111     if( p_intf->pf_open( p_intf ) )
112     {
113         intf_ErrMsg("intf error: cannot create interface");
114         module_Unneed( p_main->p_bank, p_intf->p_module );
115         free( p_intf );
116         return( NULL );
117     }
118
119     /* Initialize mutexes */
120     vlc_mutex_init( &p_intf->change_lock );
121
122     /* Load channels - the pointer will be set to NULL on failure. The
123      * return value is ignored since the program can work without
124      * channels */
125     intf_LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR,
126                                                     INTF_CHANNELS_DEFAULT ));
127
128     intf_Msg("intf: interface initialized");
129     return( p_intf );
130 }
131
132 /*****************************************************************************
133  * intf_Manage: manage interface
134  *****************************************************************************
135  * This function has to be called regularly by the interface plugin. It
136  * checks for playlist end, module expiration, message flushing, and a few
137  * other useful things.
138  *****************************************************************************/
139 static void intf_Manage( intf_thread_t *p_intf )
140 {
141     /* Flush waiting messages */
142     intf_FlushMsg();
143
144     /* Manage module bank */
145     module_ManageBank( p_main->p_bank );
146
147     if( ( p_intf->p_input != NULL ) &&
148             ( p_intf->p_input->b_error || p_intf->p_input->b_eof ) )
149     {
150         input_DestroyThread( p_intf->p_input, NULL );
151         p_intf->p_input = NULL;
152         intf_DbgMsg("Input thread destroyed");
153     }
154
155     /* If no stream is being played, try to find one */
156     if( p_intf->p_input == NULL && !p_intf->b_die )
157     {
158         /* Select the next playlist item */
159         intf_PlaylistNext( p_main->p_playlist );
160
161         if( p_main->p_playlist->i_index == -1 )
162         {
163             /*    FIXME: wait for user to add stuff to playlist ? */
164 #if 0
165             p_intf->b_die = 1;
166 #endif
167         }
168         else
169         {
170             p_intf->p_input =
171                 input_CreateThread( &p_main->p_playlist->current, NULL );
172         }
173     }
174 }
175
176 /*****************************************************************************
177  * intf_Destroy: clean interface after main loop
178  *****************************************************************************
179  * This function destroys specific interfaces and close output devices.
180  *****************************************************************************/
181 void intf_Destroy( intf_thread_t *p_intf )
182 {
183     p_intf_key  p_cur;
184     p_intf_key  p_next;
185
186     /* Unload channels */
187     intf_UnloadChannels( p_intf );
188
189     /* Destroy interfaces */
190     p_intf->pf_close( p_intf );
191
192     /* Close input thread, if any (blocking) */
193     if( p_intf->p_input )
194     {   
195         input_DestroyThread( p_intf->p_input, NULL );
196     }
197
198     /* Destroy keymap */
199     p_cur = p_intf->p_keys;
200     while( p_cur != NULL)
201     {
202         p_next = p_cur->next;
203         free(p_cur);
204         p_cur = p_next;
205     }
206          
207     /* Unlock module */
208     module_Unneed( p_main->p_bank, p_intf->p_module );
209
210     vlc_mutex_destroy( &p_intf->change_lock );
211
212     /* Free structure */
213     free( p_intf );
214 }
215
216 /*****************************************************************************
217  * intf_AssignKey: assign standartkeys                                       *
218  *****************************************************************************
219  * This function fills in the associative array that links the key pressed   *
220  * and the key we use internally. Support one extra parameter.               *
221  ****************************************************************************/
222 void intf_AssignKey( intf_thread_t *p_intf, int r_key, int f_key, int param)
223 {
224     p_intf_key  p_cur =  p_intf->p_keys;
225     if( p_cur == NULL )
226     {
227         p_cur = (p_intf_key )(malloc ( sizeof( intf_key ) ) );
228         p_cur->received_key = r_key;
229         p_cur->forwarded.key = f_key;
230         p_cur->forwarded.param = param; 
231         p_cur->next = NULL;
232         p_intf->p_keys = p_cur;
233     } 
234     else 
235     {
236         while( p_cur->next != NULL && p_cur ->received_key != r_key)
237         {
238             p_cur = p_cur->next;
239         }
240         if( p_cur->next == NULL )
241         {   
242             p_cur->next  = ( p_intf_key )( malloc( sizeof( intf_key ) ) );
243             p_cur = p_cur->next;
244             p_cur->next = NULL;
245             p_cur->forwarded.param = param; 
246             p_cur->received_key = r_key;
247         }
248         p_cur->forwarded.key = f_key;
249     }        
250 }
251
252 /* Basic getKey function... */
253 keyparm intf_GetKey( intf_thread_t *p_intf, int r_key)
254 {   
255     keyparm reply;
256     
257     p_intf_key current = p_intf->p_keys;
258     while(current != NULL && current->received_key != r_key)
259     {    
260         current = current->next;
261     }
262     if(current == NULL)
263     {   /* didn't find any key in the array */ 
264         reply.key = INTF_KEY_UNKNOWN;
265         reply.param = 0;
266     }
267     else
268     {
269         reply.key = current->forwarded.key;
270         reply.param = current->forwarded.param;
271     }
272     return reply;
273 }
274
275 /*****************************************************************************
276 * intf_AssignNormalKeys: used for normal interfaces.
277 *****************************************************************************
278 * This function assign the basic key to the normal keys.
279 *****************************************************************************/
280
281 void intf_AssignNormalKeys( intf_thread_t *p_intf)
282 {
283     p_intf->p_intf_get_key = intf_GetKey;
284
285     intf_AssignKey( p_intf , 'Q', INTF_KEY_QUIT, 0);
286     intf_AssignKey( p_intf , 'q', INTF_KEY_QUIT, 0);
287     intf_AssignKey( p_intf ,  27, INTF_KEY_QUIT, 0);
288     intf_AssignKey( p_intf ,   3, INTF_KEY_QUIT, 0);
289     intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
290     intf_AssignKey( p_intf , '1', INTF_KEY_SET_CHANNEL, 1);
291     intf_AssignKey( p_intf , '2', INTF_KEY_SET_CHANNEL, 2);
292     intf_AssignKey( p_intf , '3', INTF_KEY_SET_CHANNEL, 3);
293     intf_AssignKey( p_intf , '4', INTF_KEY_SET_CHANNEL, 4);
294     intf_AssignKey( p_intf , '5', INTF_KEY_SET_CHANNEL, 5);
295     intf_AssignKey( p_intf , '6', INTF_KEY_SET_CHANNEL, 6);
296     intf_AssignKey( p_intf , '7', INTF_KEY_SET_CHANNEL, 7);
297     intf_AssignKey( p_intf , '8', INTF_KEY_SET_CHANNEL, 8);
298     intf_AssignKey( p_intf , '9', INTF_KEY_SET_CHANNEL, 9);
299     intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
300     intf_AssignKey( p_intf , '+', INTF_KEY_INC_VOLUME, 0);
301     intf_AssignKey( p_intf , '-', INTF_KEY_DEC_VOLUME, 0);
302     intf_AssignKey( p_intf , 'm', INTF_KEY_TOGGLE_VOLUME, 0);
303     intf_AssignKey( p_intf , 'M', INTF_KEY_TOGGLE_VOLUME, 0);
304     intf_AssignKey( p_intf , 'g', INTF_KEY_DEC_GAMMA, 0);
305     intf_AssignKey( p_intf , 'G', INTF_KEY_INC_GAMMA, 0);
306     intf_AssignKey( p_intf , 'c', INTF_KEY_TOGGLE_GRAYSCALE, 0);
307     intf_AssignKey( p_intf , ' ', INTF_KEY_TOGGLE_INTERFACE, 0);
308     intf_AssignKey( p_intf , 'i', INTF_KEY_TOGGLE_INFO, 0);
309     intf_AssignKey( p_intf , 's', INTF_KEY_TOGGLE_SCALING, 0);
310     intf_AssignKey( p_intf , 'd', INTF_KEY_DUMP_STREAM, 0);
311 }   
312
313 /*****************************************************************************
314  * intf_ProcessKey: process standard keys
315  *****************************************************************************
316  * This function will process standard keys and return non 0 if the key was
317  * unknown.
318  *****************************************************************************/
319 int intf_ProcessKey( intf_thread_t *p_intf, int g_key )
320 {
321     static int i_volbackup;
322     keyparm k_reply;
323     
324     k_reply = intf_GetKey( p_intf, g_key); 
325     switch( k_reply.key )
326     {
327     case INTF_KEY_QUIT:                                        /* quit order */
328         p_intf->b_die = 1;
329         break;
330     case INTF_KEY_SET_CHANNEL:
331         /* Change channel - return code is ignored since SelectChannel displays
332          * its own error messages */
333 /*        intf_SelectChannel( p_intf, k_reply.param ); */
334 /*        network_ChannelJoin() */
335 /* FIXME : keyboard event is for the time being half handled by the interface
336  * half handled directly by the plugins. We should decide what to do. */        
337         break;
338     case INTF_KEY_INC_VOLUME:                                    /* volume + */
339         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol < VOLUME_MAX) )
340             p_main->p_aout->vol += VOLUME_STEP;
341         break;
342     case INTF_KEY_DEC_VOLUME:                                    /* volume - */
343         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol > VOLUME_STEP) )
344             p_main->p_aout->vol -= VOLUME_STEP;
345         break;
346     case INTF_KEY_TOGGLE_VOLUME:                              /* toggle mute */
347         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol))
348         {
349             i_volbackup = p_main->p_aout->vol;
350             p_main->p_aout->vol = 0;
351         }
352         else if( (p_main->p_aout != NULL) && (!p_main->p_aout->vol))
353             p_main->p_aout->vol = i_volbackup;
354         break;
355     case INTF_KEY_DEC_GAMMA:                                      /* gamma - */
356         if( (p_main->p_vout != NULL) && (p_main->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
357         {
358             /* FIXME: we should lock if called from the interface */
359             p_main->p_vout->f_gamma   -= INTF_GAMMA_STEP;
360             p_main->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
361         }
362         break;
363     case INTF_KEY_INC_GAMMA:                                      /* gamma + */
364         if( (p_main->p_vout != NULL) && (p_main->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
365         {
366             /* FIXME: we should lock if called from the interface */
367             p_main->p_vout->f_gamma   += INTF_GAMMA_STEP;
368             p_main->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
369         }
370         break;
371    case INTF_KEY_DUMP_STREAM:
372         if( p_intf->p_input != NULL )
373         {
374             vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
375             input_DumpStream( p_intf->p_input );
376             vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
377         }
378         break;
379    default:                                                   /* unknown key */
380         return( 1 );
381     }
382
383     return( 0 );
384 }