]> git.sesse.net Git - vlc/blob - src/interface/interface.c
* Rewrote the video decoder to take advantage of several processors (SMP mode) ;
[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.80 2001/07/18 14:21:00 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( 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_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_WarnMsg( 1, "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( );
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 //        vlc_mutex_lock( &p_main->p_playlist->change_lock );
158
159         if( !p_main->p_playlist->b_stopped )
160         {
161             /* Select the next playlist item */
162             intf_PlaylistNext( p_main->p_playlist );
163
164             /* don't loop by default: stop at playlist end */
165             if( p_main->p_playlist->i_index == -1 )
166             {
167                 p_main->p_playlist->b_stopped = 1;
168             }
169             else
170             {
171                 p_main->p_playlist->b_stopped = 0;
172                 p_main->p_playlist->i_mode = PLAYLIST_FORWARD + 
173                     main_GetIntVariable( PLAYLIST_LOOP_VAR,
174                                          PLAYLIST_LOOP_DEFAULT );
175                 p_intf->p_input =
176                     input_CreateThread( &p_main->p_playlist->current, NULL );
177             }
178         }
179         else
180         {
181             /* playing has been stopped: we no longer need outputs */
182             if( p_aout_bank->i_count )
183             {
184                 /* FIXME kludge that does not work with several outputs */
185                 aout_DestroyThread( p_aout_bank->pp_aout[0], NULL );
186                 p_aout_bank->i_count--;
187             }
188             if( p_vout_bank->i_count )
189             {
190                 vout_DestroyThread( p_vout_bank->pp_vout[0], NULL );
191                 p_vout_bank->i_count--;
192             }
193         }
194
195 //        vlc_mutex_unlock( &p_main->p_playlist->change_lock );
196     }
197 }
198
199 /*****************************************************************************
200  * intf_Destroy: clean interface after main loop
201  *****************************************************************************
202  * This function destroys specific interfaces and close output devices.
203  *****************************************************************************/
204 void intf_Destroy( intf_thread_t *p_intf )
205 {
206     p_intf_key  p_cur;
207     p_intf_key  p_next;
208
209     /* Unload channels */
210     intf_UnloadChannels( p_intf );
211
212     /* Destroy interfaces */
213     p_intf->pf_close( p_intf );
214
215     /* Close input thread, if any (blocking) */
216     if( p_intf->p_input )
217     {   
218         input_DestroyThread( p_intf->p_input, NULL );
219     }
220
221     /* Destroy keymap */
222     p_cur = p_intf->p_keys;
223     while( p_cur != NULL)
224     {
225         p_next = p_cur->next;
226         free(p_cur);
227         p_cur = p_next;
228     }
229          
230     /* Unlock module */
231     module_Unneed( p_intf->p_module );
232
233     vlc_mutex_destroy( &p_intf->change_lock );
234
235     /* Free structure */
236     free( p_intf );
237 }
238
239 /*****************************************************************************
240  * intf_AssignKey: assign standartkeys                                       *
241  *****************************************************************************
242  * This function fills in the associative array that links the key pressed   *
243  * and the key we use internally. Support one extra parameter.               *
244  ****************************************************************************/
245 void intf_AssignKey( intf_thread_t *p_intf, int r_key, int f_key, int param)
246 {
247     p_intf_key  p_cur =  p_intf->p_keys;
248     if( p_cur == NULL )
249     {
250         p_cur = (p_intf_key )(malloc ( sizeof( intf_key ) ) );
251         p_cur->received_key = r_key;
252         p_cur->forwarded.key = f_key;
253         p_cur->forwarded.param = param; 
254         p_cur->next = NULL;
255         p_intf->p_keys = p_cur;
256     } 
257     else 
258     {
259         while( p_cur->next != NULL && p_cur ->received_key != r_key)
260         {
261             p_cur = p_cur->next;
262         }
263         if( p_cur->next == NULL )
264         {   
265             p_cur->next  = ( p_intf_key )( malloc( sizeof( intf_key ) ) );
266             p_cur = p_cur->next;
267             p_cur->next = NULL;
268             p_cur->forwarded.param = param; 
269             p_cur->received_key = r_key;
270         }
271         p_cur->forwarded.key = f_key;
272     }        
273 }
274
275 /* Basic getKey function... */
276 keyparm intf_GetKey( intf_thread_t *p_intf, int r_key)
277 {   
278     keyparm reply;
279     
280     p_intf_key current = p_intf->p_keys;
281     while(current != NULL && current->received_key != r_key)
282     {    
283         current = current->next;
284     }
285     if(current == NULL)
286     {   /* didn't find any key in the array */ 
287         reply.key = INTF_KEY_UNKNOWN;
288         reply.param = 0;
289     }
290     else
291     {
292         reply.key = current->forwarded.key;
293         reply.param = current->forwarded.param;
294     }
295     return reply;
296 }
297
298 /*****************************************************************************
299 * intf_AssignNormalKeys: used for normal interfaces.
300 *****************************************************************************
301 * This function assign the basic key to the normal keys.
302 *****************************************************************************/
303
304 void intf_AssignNormalKeys( intf_thread_t *p_intf)
305 {
306     p_intf->p_intf_get_key = intf_GetKey;
307
308     intf_AssignKey( p_intf , 'Q', INTF_KEY_QUIT, 0);
309     intf_AssignKey( p_intf , 'q', INTF_KEY_QUIT, 0);
310     intf_AssignKey( p_intf ,  27, INTF_KEY_QUIT, 0);
311     intf_AssignKey( p_intf ,   3, INTF_KEY_QUIT, 0);
312     intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
313     intf_AssignKey( p_intf , '1', INTF_KEY_SET_CHANNEL, 1);
314     intf_AssignKey( p_intf , '2', INTF_KEY_SET_CHANNEL, 2);
315     intf_AssignKey( p_intf , '3', INTF_KEY_SET_CHANNEL, 3);
316     intf_AssignKey( p_intf , '4', INTF_KEY_SET_CHANNEL, 4);
317     intf_AssignKey( p_intf , '5', INTF_KEY_SET_CHANNEL, 5);
318     intf_AssignKey( p_intf , '6', INTF_KEY_SET_CHANNEL, 6);
319     intf_AssignKey( p_intf , '7', INTF_KEY_SET_CHANNEL, 7);
320     intf_AssignKey( p_intf , '8', INTF_KEY_SET_CHANNEL, 8);
321     intf_AssignKey( p_intf , '9', INTF_KEY_SET_CHANNEL, 9);
322     intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
323     intf_AssignKey( p_intf , '+', INTF_KEY_INC_VOLUME, 0);
324     intf_AssignKey( p_intf , '-', INTF_KEY_DEC_VOLUME, 0);
325     intf_AssignKey( p_intf , 'm', INTF_KEY_TOGGLE_VOLUME, 0);
326     intf_AssignKey( p_intf , 'M', INTF_KEY_TOGGLE_VOLUME, 0);
327     intf_AssignKey( p_intf , 'g', INTF_KEY_DEC_GAMMA, 0);
328     intf_AssignKey( p_intf , 'G', INTF_KEY_INC_GAMMA, 0);
329     intf_AssignKey( p_intf , 'c', INTF_KEY_TOGGLE_GRAYSCALE, 0);
330     intf_AssignKey( p_intf , ' ', INTF_KEY_TOGGLE_INTERFACE, 0);
331     intf_AssignKey( p_intf , 'i', INTF_KEY_TOGGLE_INFO, 0);
332     intf_AssignKey( p_intf , 's', INTF_KEY_TOGGLE_SCALING, 0);
333     intf_AssignKey( p_intf , 'd', INTF_KEY_DUMP_STREAM, 0);
334 }   
335
336 /*****************************************************************************
337  * intf_ProcessKey: process standard keys
338  *****************************************************************************
339  * This function will process standard keys and return non 0 if the key was
340  * unknown.
341  *****************************************************************************/
342 int intf_ProcessKey( intf_thread_t *p_intf, int g_key )
343 {
344     int i_index;
345     keyparm k_reply;
346     
347     k_reply = intf_GetKey( p_intf, g_key); 
348     switch( k_reply.key )
349     {
350     case INTF_KEY_QUIT:                                        /* quit order */
351         p_intf->b_die = 1;
352         break;
353
354     case INTF_KEY_SET_CHANNEL:
355         /* Change channel - return code is ignored since SelectChannel displays
356          * its own error messages */
357 /*        intf_SelectChannel( p_intf, k_reply.param ); */
358 /*        network_ChannelJoin() */
359 /* FIXME : keyboard event is for the time being half handled by the interface
360  * half handled directly by the plugins. We should decide what to do. */        
361         break;
362
363     case INTF_KEY_INC_VOLUME:                                    /* volume + */
364         vlc_mutex_lock( &p_aout_bank->lock );
365         for( i_index = 0 ; i_index < p_aout_bank->i_count ; i_index++ )
366         {
367             if( p_aout_bank->pp_aout[i_index]->i_volume
368                                                    < VOLUME_MAX - VOLUME_STEP )
369             {
370                 p_aout_bank->pp_aout[i_index]->i_volume += VOLUME_STEP;
371             }
372             else
373             {
374                 p_aout_bank->pp_aout[i_index]->i_volume = VOLUME_MAX;
375             }
376         }
377         vlc_mutex_unlock( &p_aout_bank->lock );
378         break;
379
380     case INTF_KEY_DEC_VOLUME:                                    /* volume - */
381         vlc_mutex_lock( &p_aout_bank->lock );
382         for( i_index = 0 ; i_index < p_aout_bank->i_count ; i_index++ )
383         {
384             if( p_aout_bank->pp_aout[i_index]->i_volume > VOLUME_STEP )
385             {
386                 p_aout_bank->pp_aout[i_index]->i_volume -= VOLUME_STEP;
387             }
388             else
389             {
390                 p_aout_bank->pp_aout[i_index]->i_volume = 0;
391             }
392         }
393         vlc_mutex_unlock( &p_aout_bank->lock );
394         break;
395
396     case INTF_KEY_TOGGLE_VOLUME:                              /* toggle mute */
397         /* Start/stop feeding audio data. */
398         if( p_intf->p_input != NULL )
399         {
400             input_ToggleMute( p_intf->p_input );
401         }
402
403         /* Start/stop playing sound. */
404         vlc_mutex_lock( &p_aout_bank->lock );
405         for( i_index = 0 ; i_index < p_aout_bank->i_count ; i_index++ )
406         {
407             if( p_aout_bank->pp_aout[i_index]->i_savedvolume )
408             {
409                 p_aout_bank->pp_aout[i_index]->i_volume =
410                                 p_aout_bank->pp_aout[i_index]->i_savedvolume;
411                 p_aout_bank->pp_aout[i_index]->i_savedvolume = 0;
412             }
413             else
414             {
415                 p_aout_bank->pp_aout[i_index]->i_savedvolume =
416                                 p_aout_bank->pp_aout[i_index]->i_volume;
417                 p_aout_bank->pp_aout[i_index]->i_volume = 0;
418             }
419         }
420         vlc_mutex_unlock( &p_aout_bank->lock );
421         break;
422
423 /* XXX: fix this later */
424 #if 0
425     case INTF_KEY_DEC_GAMMA:                                      /* gamma - */
426         if( (p_main->p_vout != NULL) && (p_main->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
427         {
428             /* FIXME: we should lock if called from the interface */
429             p_main->p_vout->f_gamma   -= INTF_GAMMA_STEP;
430             p_main->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
431         }
432         break;
433     case INTF_KEY_INC_GAMMA:                                      /* gamma + */
434         if( (p_main->p_vout != NULL) && (p_main->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
435         {
436             /* FIXME: we should lock if called from the interface */
437             p_main->p_vout->f_gamma   += INTF_GAMMA_STEP;
438             p_main->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
439         }
440         break;
441 #endif
442
443    case INTF_KEY_DUMP_STREAM:
444         if( p_intf->p_input != NULL )
445         {
446             vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
447             input_DumpStream( p_intf->p_input );
448             vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
449         }
450         break;
451
452     default:                                                  /* unknown key */
453         return( 1 );
454     }
455
456     return( 0 );
457 }
458