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