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