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