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