]> git.sesse.net Git - vlc/blob - src/interface/interface.c
. autod�tection des plugins
[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 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
35 #include <sys/uio.h>                                          /* for input.h */
36
37 #include "config.h"
38 #include "common.h"
39 #include "threads.h"
40 #include "mtime.h"
41 #include "plugins.h"
42 #include "playlist.h"
43 #include "input.h"
44
45 #include "audio_output.h"
46
47 #include "intf_msg.h"
48 #include "interface.h"
49 #include "intf_cmd.h"
50 #include "intf_console.h"
51
52 #include "video.h"
53 #include "video_output.h"
54
55 #include "main.h"
56
57 /*****************************************************************************
58  * intf_channel_t: channel description
59  *****************************************************************************
60  * A 'channel' is a descriptor of an input method. It is used to switch easily
61  * from source to source without having to specify the whole input thread
62  * configuration. The channels array, stored in the interface thread object, is
63  * loaded in intf_Create, and unloaded in intf_Destroy.
64  *****************************************************************************/
65 typedef struct intf_channel_s
66 {
67     /* Channel description */
68     int         i_channel;            /* channel number, -1 for end of array */
69     char *      psz_description;              /* channel description (owned) */
70
71     /* Input configuration */
72     int         i_input_method;                   /* input method descriptor */
73     char *      psz_input_source;                   /* source string (owned) */
74     int         i_input_port;                                        /* port */
75     int         i_input_vlan;                                        /* vlan */
76 } intf_channel_t;
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 static int      LoadChannels    ( intf_thread_t *p_intf, char *psz_filename );
82 static void     UnloadChannels  ( intf_thread_t *p_intf );
83 static int      ParseChannel    ( intf_channel_t *p_channel, char *psz_str );
84
85 /*****************************************************************************
86  * intf_Create: prepare interface before main loop
87  *****************************************************************************
88  * This function opens output devices and creates specific interfaces. It sends
89  * its own error messages.
90  *****************************************************************************/
91 intf_thread_t* intf_Create( void )
92 {
93     intf_thread_t * p_intf;
94     typedef void    ( intf_getplugin_t ) ( intf_thread_t * p_intf );
95     int             i_index;
96     int             i_best_index = 0, i_best_score = 0;
97
98     /* Allocate structure */
99     p_intf = malloc( sizeof( intf_thread_t ) );
100     if( !p_intf )
101     {
102         intf_ErrMsg("error: %s\n", strerror( ENOMEM ) );
103         return( NULL );
104     }
105
106     /* Get a suitable interface plugin */
107     for( i_index = 0 ; i_index < p_main->p_bank->i_plugin_count ; i_index++ )
108     {
109         /* If there's a plugin in p_info ... */
110         if( p_main->p_bank->p_info[ i_index ] != NULL )
111         {
112             /* ... and if this plugin provides the functions we want ... */
113             if( p_main->p_bank->p_info[ i_index ]->intf_GetPlugin != NULL )
114             {
115                 /* ... and if this plugin has a good score ... */
116                 if( p_main->p_bank->p_info[ i_index ]->i_score > i_best_score )
117                 {
118                     /* ... then take it */
119                     i_best_score = p_main->p_bank->p_info[ i_index ]->i_score;
120                     i_best_index = i_index;
121                 }
122             }
123         }
124     }
125
126     if( i_best_score == 0 )
127     {
128         free( p_intf );
129         return( NULL );
130     }
131
132     /* Get the plugin functions */
133     ( (intf_getplugin_t *)
134       p_main->p_bank->p_info[ i_best_index ]->intf_GetPlugin )( p_intf );
135
136     /* Initialize structure */
137     p_intf->b_die =     0;
138     p_intf->p_vout =    NULL;
139     p_intf->p_input =   NULL;
140
141     /* Load channels - the pointer will be set to NULL on failure. The
142      * return value is ignored since the program can work without
143      * channels */
144     LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
145
146     /* Start interfaces */
147     p_intf->p_console = intf_ConsoleCreate();
148     if( p_intf->p_console == NULL )
149     {
150         intf_ErrMsg("error: can't create control console\n");
151         free( p_intf );
152         return( NULL );
153     }
154     if( p_intf->p_sys_create( p_intf ) )
155     {
156         intf_ErrMsg("error: can't create interface\n");
157         intf_ConsoleDestroy( p_intf->p_console );
158         free( p_intf );
159         return( NULL );
160     }
161
162     intf_Msg("Interface initialized\n");
163     return( p_intf );
164 }
165
166 /*****************************************************************************
167  * intf_Run
168  *****************************************************************************
169  * Initialization script and main interface loop.
170  *****************************************************************************/
171 void intf_Run( intf_thread_t *p_intf )
172 {
173     if( p_main->p_playlist->p_list )
174     {
175         p_intf->p_input = input_CreateThread( INPUT_METHOD_TS_FILE, NULL, 0, 0, p_main->p_intf->p_vout, p_main->p_aout, NULL );
176     }
177     /* Execute the initialization script - if a positive number is returned,
178      * the script could be executed but failed */
179     else if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
180     {
181         intf_ErrMsg("warning: error(s) during startup script\n");
182     }
183
184     /* Main loop */
185     while(!p_intf->b_die)
186     {
187         /* Flush waiting messages */
188         intf_FlushMsg();
189
190         /* Manage specific interface */
191         p_intf->p_sys_manage( p_intf );
192
193         /* Check attached threads status */
194         if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
195         {
196             /* FIXME: add aout error detection ?? */
197             p_intf->b_die = 1;
198         }
199         if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
200         {
201             input_DestroyThread( p_intf->p_input, NULL );
202             p_intf->p_input = NULL;
203             intf_DbgMsg("Input thread destroyed\n");
204         }
205
206         /* Sleep to avoid using all CPU - since some interfaces needs to access
207          * keyboard events, a 100ms delay is a good compromise */
208         msleep( INTF_IDLE_SLEEP );
209     }
210 }
211
212 /*****************************************************************************
213  * intf_Destroy: clean interface after main loop
214  *****************************************************************************
215  * This function destroys specific interfaces and close output devices.
216  *****************************************************************************/
217 void intf_Destroy( intf_thread_t *p_intf )
218 {
219     /* Destroy interfaces */
220     p_intf->p_sys_destroy( p_intf );
221     intf_ConsoleDestroy( p_intf->p_console );
222
223     /* Unload channels */
224     UnloadChannels( p_intf );
225
226     /* Free structure */
227     free( p_intf );
228 }
229
230 /*****************************************************************************
231  * intf_SelectChannel: change channel
232  *****************************************************************************
233  * Kill existing input, if any, and try to open a new one, using an input
234  * configuration table.
235  *****************************************************************************/
236 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
237 {
238     intf_channel_t *    p_channel;                                /* channel */
239
240     /* Look for channel in array */
241     if( p_intf->p_channel != NULL )
242     {
243         for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
244         {
245             if( p_channel->i_channel == i_channel )
246             {
247                 /*
248                  * Change channel
249                  */
250
251                 /* Kill existing input, if any */
252                 if( p_intf->p_input != NULL )
253                 {
254                     input_DestroyThread( p_intf->p_input, NULL );
255                 }
256
257                 intf_Msg("Channel %d: %s\n", i_channel, p_channel->psz_description );
258
259                 /* Open a new input */
260                 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
261                                                       p_channel->i_input_port, p_channel->i_input_vlan,
262                                                       p_intf->p_vout, p_main->p_aout, NULL );
263                 return( p_intf->p_input == NULL );
264             }
265         }
266     }
267
268     /* Channel does not exist */
269     intf_Msg("Channel %d does not exist\n", i_channel );
270     return( 1 );
271 }
272
273 /*****************************************************************************
274  * intf_ProcessKey: process standard keys
275  *****************************************************************************
276  * This function will process standard keys and return non 0 if the key was
277  * unknown.
278  *****************************************************************************/
279 int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
280 {
281     static int i_volbackup;
282     
283     switch( i_key )
284     {
285     case 'Q':                                                  /* quit order */
286     case 'q':
287     case 27:                                                   /* escape key */
288     case 3:                                                            /* ^C */
289         p_intf->b_die = 1;
290         break;
291     case '0':                                               /* source change */
292     case '1':
293     case '2':
294     case '3':
295     case '4':
296     case '5':
297     case '6':
298     case '7':
299     case '8':
300     case '9':
301         /* Change channel - return code is ignored since SelectChannel displays
302          * its own error messages */
303         intf_SelectChannel( p_intf, i_key - '0' );
304         break;
305     case '+':                                                    /* volume + */
306         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol < VOLMAX) )
307             p_main->p_aout->vol += VOLSTEP;
308         break;
309     case '-':                                                    /* volume - */
310         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol > VOLSTEP) )
311             p_main->p_aout->vol -= VOLSTEP;
312         break;
313     case 'M':                                                 /* toggle mute */
314     case 'm':
315         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol))
316         {
317             i_volbackup = p_main->p_aout->vol;
318             p_main->p_aout->vol = 0;
319         }
320         else if( (p_main->p_aout != NULL) && (!p_main->p_aout->vol))
321             p_main->p_aout->vol = i_volbackup;
322         break;
323     case 'g':                                                     /* gamma - */
324         if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
325         {
326             vlc_mutex_lock( &p_intf->p_vout->change_lock );
327             p_intf->p_vout->f_gamma   -= INTF_GAMMA_STEP;
328             p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
329             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
330         }
331         break;
332     case 'G':                                                     /* gamma + */
333         if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
334         {
335             vlc_mutex_lock( &p_intf->p_vout->change_lock );
336             p_intf->p_vout->f_gamma   += INTF_GAMMA_STEP;
337             p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
338             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
339         }
340         break;
341     case 'c':                                            /* toggle grayscale */
342         if( p_intf->p_vout != NULL )
343         {
344             vlc_mutex_lock( &p_intf->p_vout->change_lock );
345             p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
346             p_intf->p_vout->i_changes  |= VOUT_GRAYSCALE_CHANGE;
347             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
348         }
349         break;
350     case ' ':                                            /* toggle interface */
351         if( p_intf->p_vout != NULL )
352         {
353             vlc_mutex_lock( &p_intf->p_vout->change_lock );
354             p_intf->p_vout->b_interface     = !p_intf->p_vout->b_interface;
355             p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
356             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
357         }
358         break;
359     case 'i':                                                 /* toggle info */
360         if( p_intf->p_vout != NULL )
361         {
362             vlc_mutex_lock( &p_intf->p_vout->change_lock );
363             p_intf->p_vout->b_info     = !p_intf->p_vout->b_info;
364             p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
365             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
366         }
367         break;
368     case 's':                                              /* toggle scaling */
369         if( p_intf->p_vout != NULL )
370         {
371             vlc_mutex_lock( &p_intf->p_vout->change_lock );
372             p_intf->p_vout->b_scale    = !p_intf->p_vout->b_scale;
373             p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
374             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
375         }
376         break;
377    default:                                                   /* unknown key */
378         return( 1 );
379     }
380
381     return( 0 );
382 }
383
384 /* following functions are local */
385
386 /*****************************************************************************
387  * LoadChannels: load channels description from a file
388  *****************************************************************************
389  * This structe describes all interface-specific data of the main (interface)
390  * thread.
391  * Each line of the file is a semicolon separated list of the following
392  * fields :
393  *      integer         channel number
394  *      string          channel description
395  *      integer         input method (see input.h)
396  *      string          input source
397  *      integer         input port
398  *      integer         input vlan
399  * The last field must end with a semicolon.
400  * Comments and empty lines are not explicitely allowed, but lines with parsing
401  * errors are ignored without warning.
402  *****************************************************************************/
403 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
404 {
405     FILE *              p_file;                                      /* file */
406     intf_channel_t *    p_channel;                        /* current channel */
407     char                psz_line[INTF_MAX_CMD_SIZE];          /* line buffer */
408     int                 i_index;                   /* channel or field index */
409
410     /* Set default value */
411     p_intf->p_channel = NULL;
412
413     /* Open file */
414     p_file = fopen( psz_filename, "r" );
415     if( p_file == NULL )
416     {
417         intf_ErrMsg("error: can't open %s (%s)\n", psz_filename, strerror(errno));
418         return( 1 );
419     }
420
421     /* First pass: count number of lines */
422     for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
423     {
424         ;
425     }
426
427     if( i_index != 0 )
428     {
429         /* Allocate array and rewind - some of the lines may be invalid, and the
430          * array will probably be larger than the actual number of channels, but
431          * it has no consequence. */
432         p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
433         if( p_intf->p_channel == NULL )
434         {
435             intf_ErrMsg("error: %s\n", strerror(ENOMEM));
436             fclose( p_file );
437             return( 1 );
438         }
439         p_channel = p_intf->p_channel;
440         rewind( p_file );
441
442         /* Second pass: read channels descriptions */
443         while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
444         {
445             if( !ParseChannel( p_channel, psz_line ) )
446             {
447                 intf_DbgMsg( "channel [%d] %s : method %d (%s:%d vlan %d)\n",
448                          p_channel->i_channel, p_channel->psz_description,
449                          p_channel->i_input_method,
450                          p_channel->psz_input_source,
451                          p_channel->i_input_port, p_channel->i_input_vlan );
452                 p_channel++;
453             }
454         }
455
456         /* Add marker at the end of the array */
457         p_channel->i_channel = -1;
458     }
459
460     /* Close file */
461     fclose( p_file );
462     return( 0 );
463 }
464
465 /*****************************************************************************
466  * UnloadChannels: unload channels description
467  *****************************************************************************
468  * This function free all resources allocated by LoadChannels, if any.
469  *****************************************************************************/
470 static void UnloadChannels( intf_thread_t *p_intf )
471 {
472     int i_channel;                                          /* channel index */
473
474     if( p_intf->p_channel != NULL )
475     {
476         /* Free allocated strings */
477         for( i_channel = 0;
478              p_intf->p_channel[ i_channel ].i_channel != -1;
479              i_channel++ )
480         {
481             if( p_intf->p_channel[ i_channel ].psz_description != NULL )
482             {
483                 free( p_intf->p_channel[ i_channel ].psz_description );
484             }
485             if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
486             {
487                 free( p_intf->p_channel[ i_channel ].psz_input_source );
488             }
489         }
490
491         /* Free array */
492         free( p_intf->p_channel );
493         p_intf->p_channel = NULL;
494     }
495 }
496
497
498 /*****************************************************************************
499  * ParseChannel: parse a channel description line
500  *****************************************************************************
501  * See LoadChannels. This function return non 0 on parsing error.
502  *****************************************************************************/
503 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
504 {
505     char *      psz_index;                              /* current character */
506     char *      psz_end;                           /* end pointer for strtol */
507     int         i_field;                        /* field number, -1 on error */
508     int         i_field_length;             /* field length, for text fields */
509
510     /* Set some default fields */
511     p_channel->i_channel =              0;
512     p_channel->psz_description =        NULL;
513     p_channel->i_input_method =         0;
514     p_channel->psz_input_source =       NULL;
515     p_channel->i_input_port =           0;
516     p_channel->i_input_vlan =           0;
517
518     /* Parse string */
519     i_field = 0;
520     for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
521     {
522         if( *psz_index == ';' )
523         {
524             /* Mark end of field */
525             *psz_index = '\0';
526
527             /* Parse field */
528             switch( i_field++ )
529             {
530             case 0:                                        /* channel number */
531                 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
532                 if( (*psz_str == '\0') || (*psz_end != '\0') )
533                 {
534                     i_field = -1;
535                 }
536                 break;
537             case 1:                                   /* channel description */
538                 i_field_length = strlen( psz_str );
539                 if( i_field_length != 0 )
540                 {
541                     p_channel->psz_description = malloc( i_field_length + 1 );
542                     if( p_channel->psz_description == NULL )
543                     {
544                         intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
545                         i_field = -1;
546                     }
547                     else
548                     {
549                         strcpy( p_channel->psz_description, psz_str );
550                     }
551                 }
552                 break;
553             case 2:                                          /* input method */
554                 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
555                 if( (*psz_str == '\0') || (*psz_end != '\0') )
556                 {
557                     i_field = -1;
558                 }
559                 break;
560             case 3:                                          /* input source */
561                 i_field_length = strlen( psz_str );
562                 if( i_field_length != 0 )
563                 {
564                     p_channel->psz_input_source = malloc( i_field_length + 1 );
565                     if( p_channel->psz_input_source == NULL )
566                     {
567                         intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
568                         i_field = -1;
569                     }
570                     else
571                     {
572                         strcpy( p_channel->psz_input_source, psz_str );
573                     }
574                 }
575                 break;
576             case 4:                                            /* input port */
577                 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
578                 if( (*psz_str == '\0') || (*psz_end != '\0') )
579                 {
580                     i_field = -1;
581                 }
582                 break;
583             case 5:                                            /* input vlan */
584                 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
585                 if( (*psz_str == '\0') || (*psz_end != '\0') )
586                 {
587                     i_field = -1;
588                 }
589                 break;
590                 /* ... following fields are ignored */
591             }
592
593             /* Set new beginning of field */
594             psz_str = psz_index + 1;
595         }
596     }
597
598     /* At least the first three fields must be parsed sucessfully for function
599      * success. Other parsing errors are returned using i_field = -1. */
600     if( i_field < 3 )
601     {
602         /* Function fails. Free allocated strings */
603         if( p_channel->psz_description != NULL )
604         {
605             free( p_channel->psz_description );
606         }
607         if( p_channel->psz_input_source != NULL )
608         {
609             free( p_channel->psz_input_source );
610         }
611         return( 1 );
612     }
613
614     /* Return success */
615     return( 0 );
616 }