]> git.sesse.net Git - vlc/blob - src/interface/interface.c
. kludge con pour �viter que �a segfaulte plus en sortant
[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     if( p_intf->p_playlist )
161     {
162         p_intf->p_input = input_CreateThread( INPUT_METHOD_TS_FILE, (void *)p_intf->p_playlist, p_intf->i_list_index, 0, p_main->p_intf->p_vout, p_main->p_aout, NULL );
163     }
164     /* Execute the initialization script - if a positive number is returned,
165      * the script could be executed but failed */
166     else if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
167     {
168         intf_ErrMsg("warning: error(s) during startup script\n");
169     }
170
171     /* Main loop */
172     while(!p_intf->b_die)
173     {
174         /* Flush waiting messages */
175         intf_FlushMsg();
176
177         /* Manage specific interface */
178         p_intf->p_sys_manage( p_intf );
179
180         /* Check attached threads status */
181         if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
182         {
183             /* FIXME: add aout error detection ?? */
184             p_intf->b_die = 1;
185         }
186         if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
187         {
188             input_DestroyThread( p_intf->p_input, NULL );
189             p_intf->p_input = NULL;
190             intf_DbgMsg("Input thread destroyed\n");
191         }
192
193         /* Sleep to avoid using all CPU - since some interfaces needs to access
194          * keyboard events, a 100ms delay is a good compromise */
195         msleep( INTF_IDLE_SLEEP );
196     }
197 }
198
199 /*****************************************************************************
200  * intf_Destroy: clean interface after main loop
201  *****************************************************************************
202  * This function destroys specific interfaces and close output devices.
203  *****************************************************************************/
204 void intf_Destroy( intf_thread_t *p_intf )
205 {
206     /* Destroy interfaces */
207     p_intf->p_sys_destroy( p_intf );
208     intf_ConsoleDestroy( p_intf->p_console );
209
210     /* Unload channels */
211     UnloadChannels( p_intf );
212
213     /* XXX: Close plugin - we don't do it because it makes the Gnome
214      * plugin segfaulting */
215     /*TrashPlugin( p_intf->intf_plugin );*/
216
217     /* Close plugin */
218     TrashPlugin( p_intf->intf_plugin );
219
220     /* Free structure */
221     free( p_intf );
222 }
223
224 /*****************************************************************************
225  * intf_SelectChannel: change channel
226  *****************************************************************************
227  * Kill existing input, if any, and try to open a new one, using an input
228  * configuration table.
229  *****************************************************************************/
230 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
231 {
232     intf_channel_t *    p_channel;                                /* channel */
233
234     /* Look for channel in array */
235     if( p_intf->p_channel != NULL )
236     {
237         for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
238         {
239             if( p_channel->i_channel == i_channel )
240             {
241                 /*
242                  * Change channel
243                  */
244
245                 /* Kill existing input, if any */
246                 if( p_intf->p_input != NULL )
247                 {
248                     input_DestroyThread( p_intf->p_input, NULL );
249                 }
250
251                 intf_Msg("Channel %d: %s\n", i_channel, p_channel->psz_description );
252
253                 /* Open a new input */
254                 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
255                                                       p_channel->i_input_port, p_channel->i_input_vlan,
256                                                       p_intf->p_vout, p_main->p_aout, NULL );
257                 return( p_intf->p_input == NULL );
258             }
259         }
260     }
261
262     /* Channel does not exist */
263     intf_Msg("Channel %d does not exist\n", i_channel );
264     return( 1 );
265 }
266
267 /*****************************************************************************
268  * intf_ProcessKey: process standard keys
269  *****************************************************************************
270  * This function will process standard keys and return non 0 if the key was
271  * unknown.
272  *****************************************************************************/
273 int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
274 {
275     static int i_volbackup;
276     
277     switch( i_key )
278     {
279     case 'Q':                                                  /* quit order */
280     case 'q':
281     case 27:                                                   /* escape key */
282     case 3:                                                            /* ^C */
283         p_intf->b_die = 1;
284         break;
285     case '0':                                               /* source change */
286     case '1':
287     case '2':
288     case '3':
289     case '4':
290     case '5':
291     case '6':
292     case '7':
293     case '8':
294     case '9':
295         /* Change channel - return code is ignored since SelectChannel displays
296          * its own error messages */
297         intf_SelectChannel( p_intf, i_key - '0' );
298         break;
299     case '+':                                                    /* volume + */
300         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol < VOLMAX) )
301             p_main->p_aout->vol += VOLSTEP;
302         break;
303     case '-':                                                    /* volume - */
304         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol > VOLSTEP) )
305             p_main->p_aout->vol -= VOLSTEP;
306         break;
307     case 'M':                                                 /* toggle mute */
308     case 'm':
309         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol))
310         {
311             i_volbackup = p_main->p_aout->vol;
312             p_main->p_aout->vol = 0;
313         }
314         else if( (p_main->p_aout != NULL) && (!p_main->p_aout->vol))
315             p_main->p_aout->vol = i_volbackup;
316         break;
317     case 'g':                                                     /* gamma - */
318         if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
319         {
320             vlc_mutex_lock( &p_intf->p_vout->change_lock );
321             p_intf->p_vout->f_gamma   -= INTF_GAMMA_STEP;
322             p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
323             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
324         }
325         break;
326     case 'G':                                                     /* gamma + */
327         if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
328         {
329             vlc_mutex_lock( &p_intf->p_vout->change_lock );
330             p_intf->p_vout->f_gamma   += INTF_GAMMA_STEP;
331             p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
332             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
333         }
334         break;
335     case 'c':                                            /* toggle grayscale */
336         if( p_intf->p_vout != NULL )
337         {
338             vlc_mutex_lock( &p_intf->p_vout->change_lock );
339             p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
340             p_intf->p_vout->i_changes  |= VOUT_GRAYSCALE_CHANGE;
341             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
342         }
343         break;
344     case ' ':                                            /* toggle interface */
345         if( p_intf->p_vout != NULL )
346         {
347             vlc_mutex_lock( &p_intf->p_vout->change_lock );
348             p_intf->p_vout->b_interface     = !p_intf->p_vout->b_interface;
349             p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
350             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
351         }
352         break;
353     case 'i':                                                 /* toggle info */
354         if( p_intf->p_vout != NULL )
355         {
356             vlc_mutex_lock( &p_intf->p_vout->change_lock );
357             p_intf->p_vout->b_info     = !p_intf->p_vout->b_info;
358             p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
359             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
360         }
361         break;
362     case 's':                                              /* toggle scaling */
363         if( p_intf->p_vout != NULL )
364         {
365             vlc_mutex_lock( &p_intf->p_vout->change_lock );
366             p_intf->p_vout->b_scale    = !p_intf->p_vout->b_scale;
367             p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
368             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
369         }
370         break;
371    default:                                                   /* unknown key */
372         return( 1 );
373     }
374
375     return( 0 );
376 }
377
378 /* following functions are local */
379
380 /*****************************************************************************
381  * LoadChannels: load channels description from a file
382  *****************************************************************************
383  * This structe describes all interface-specific data of the main (interface)
384  * thread.
385  * Each line of the file is a semicolon separated list of the following
386  * fields :
387  *      integer         channel number
388  *      string          channel description
389  *      integer         input method (see input.h)
390  *      string          input source
391  *      integer         input port
392  *      integer         input vlan
393  * The last field must end with a semicolon.
394  * Comments and empty lines are not explicitely allowed, but lines with parsing
395  * errors are ignored without warning.
396  *****************************************************************************/
397 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
398 {
399     FILE *              p_file;                                      /* file */
400     intf_channel_t *    p_channel;                        /* current channel */
401     char                psz_line[INTF_MAX_CMD_SIZE];          /* line buffer */
402     int                 i_index;                   /* channel or field index */
403
404     /* Set default value */
405     p_intf->p_channel = NULL;
406
407     /* Open file */
408     p_file = fopen( psz_filename, "r" );
409     if( p_file == NULL )
410     {
411         intf_ErrMsg("error: can't open %s (%s)\n", psz_filename, strerror(errno));
412         return( 1 );
413     }
414
415     /* First pass: count number of lines */
416     for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
417     {
418         ;
419     }
420
421     if( i_index != 0 )
422     {
423         /* Allocate array and rewind - some of the lines may be invalid, and the
424          * array will probably be larger than the actual number of channels, but
425          * it has no consequence. */
426         p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
427         if( p_intf->p_channel == NULL )
428         {
429             intf_ErrMsg("error: %s\n", strerror(ENOMEM));
430             fclose( p_file );
431             return( 1 );
432         }
433         p_channel = p_intf->p_channel;
434         rewind( p_file );
435
436         /* Second pass: read channels descriptions */
437         while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
438         {
439             if( !ParseChannel( p_channel, psz_line ) )
440             {
441                 intf_DbgMsg( "channel [%d] %s : method %d (%s:%d vlan %d)\n",
442                          p_channel->i_channel, p_channel->psz_description,
443                          p_channel->i_input_method,
444                          p_channel->psz_input_source,
445                          p_channel->i_input_port, p_channel->i_input_vlan );
446                 p_channel++;
447             }
448         }
449
450         /* Add marker at the end of the array */
451         p_channel->i_channel = -1;
452     }
453
454     /* Close file */
455     fclose( p_file );
456     return( 0 );
457 }
458
459 /*****************************************************************************
460  * UnloadChannels: unload channels description
461  *****************************************************************************
462  * This function free all resources allocated by LoadChannels, if any.
463  *****************************************************************************/
464 static void UnloadChannels( intf_thread_t *p_intf )
465 {
466     int i_channel;                                          /* channel index */
467
468     if( p_intf->p_channel != NULL )
469     {
470         /* Free allocated strings */
471         for( i_channel = 0;
472              p_intf->p_channel[ i_channel ].i_channel != -1;
473              i_channel++ )
474         {
475             if( p_intf->p_channel[ i_channel ].psz_description != NULL )
476             {
477                 free( p_intf->p_channel[ i_channel ].psz_description );
478             }
479             if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
480             {
481                 free( p_intf->p_channel[ i_channel ].psz_input_source );
482             }
483         }
484
485         /* Free array */
486         free( p_intf->p_channel );
487         p_intf->p_channel = NULL;
488     }
489 }
490
491
492 /*****************************************************************************
493  * ParseChannel: parse a channel description line
494  *****************************************************************************
495  * See LoadChannels. This function return non 0 on parsing error.
496  *****************************************************************************/
497 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
498 {
499     char *      psz_index;                              /* current character */
500     char *      psz_end;                           /* end pointer for strtol */
501     int         i_field;                        /* field number, -1 on error */
502     int         i_field_length;             /* field length, for text fields */
503
504     /* Set some default fields */
505     p_channel->i_channel =              0;
506     p_channel->psz_description =        NULL;
507     p_channel->i_input_method =         0;
508     p_channel->psz_input_source =       NULL;
509     p_channel->i_input_port =           0;
510     p_channel->i_input_vlan =           0;
511
512     /* Parse string */
513     i_field = 0;
514     for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
515     {
516         if( *psz_index == ';' )
517         {
518             /* Mark end of field */
519             *psz_index = '\0';
520
521             /* Parse field */
522             switch( i_field++ )
523             {
524             case 0:                                        /* channel number */
525                 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
526                 if( (*psz_str == '\0') || (*psz_end != '\0') )
527                 {
528                     i_field = -1;
529                 }
530                 break;
531             case 1:                                   /* channel description */
532                 i_field_length = strlen( psz_str );
533                 if( i_field_length != 0 )
534                 {
535                     p_channel->psz_description = malloc( i_field_length + 1 );
536                     if( p_channel->psz_description == NULL )
537                     {
538                         intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
539                         i_field = -1;
540                     }
541                     else
542                     {
543                         strcpy( p_channel->psz_description, psz_str );
544                     }
545                 }
546                 break;
547             case 2:                                          /* input method */
548                 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
549                 if( (*psz_str == '\0') || (*psz_end != '\0') )
550                 {
551                     i_field = -1;
552                 }
553                 break;
554             case 3:                                          /* input source */
555                 i_field_length = strlen( psz_str );
556                 if( i_field_length != 0 )
557                 {
558                     p_channel->psz_input_source = malloc( i_field_length + 1 );
559                     if( p_channel->psz_input_source == NULL )
560                     {
561                         intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
562                         i_field = -1;
563                     }
564                     else
565                     {
566                         strcpy( p_channel->psz_input_source, psz_str );
567                     }
568                 }
569                 break;
570             case 4:                                            /* input port */
571                 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
572                 if( (*psz_str == '\0') || (*psz_end != '\0') )
573                 {
574                     i_field = -1;
575                 }
576                 break;
577             case 5:                                            /* input vlan */
578                 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
579                 if( (*psz_str == '\0') || (*psz_end != '\0') )
580                 {
581                     i_field = -1;
582                 }
583                 break;
584                 /* ... following fields are ignored */
585             }
586
587             /* Set new beginning of field */
588             psz_str = psz_index + 1;
589         }
590     }
591
592     /* At least the first three fields must be parsed sucessfully for function
593      * success. Other parsing errors are returned using i_field = -1. */
594     if( i_field < 3 )
595     {
596         /* Function fails. Free allocated strings */
597         if( p_channel->psz_description != NULL )
598         {
599             free( p_channel->psz_description );
600         }
601         if( p_channel->psz_input_source != NULL )
602         {
603             free( p_channel->psz_input_source );
604         }
605         return( 1 );
606     }
607
608     /* Return success */
609     return( 0 );
610 }