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