]> git.sesse.net Git - vlc/blob - src/interface/interface.c
. fixed the bug-that-made-the-vlc-segfault-on-exit, which means that
[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
35 #include "config.h"
36 #include "common.h"
37 #include "threads.h"
38 #include "mtime.h"
39 #include "plugins.h"
40 #include "modules.h"
41 #include "playlist.h"
42
43 #include "stream_control.h"
44 #include "input_ext-intf.h"
45
46 #include "audio_output.h"
47
48 #include "intf_msg.h"
49 #include "interface.h"
50 #include "intf_cmd.h"
51 #include "intf_console.h"
52 #include "keystrokes.h"
53
54 #include "video.h"
55 #include "video_output.h"
56
57 #include "main.h"
58
59 /*****************************************************************************
60  * intf_channel_t: channel description
61  *****************************************************************************
62  * A 'channel' is a descriptor of an input method. It is used to switch easily
63  * from source to source without having to specify the whole input thread
64  * configuration. The channels array, stored in the interface thread object, is
65  * loaded in intf_Create, and unloaded in intf_Destroy.
66  *****************************************************************************/
67 typedef struct intf_channel_s
68 {
69     /* Channel description */
70     int         i_channel;            /* channel number, -1 for end of array */
71     char *      psz_description;              /* channel description (owned) */
72
73     /* Input configuration */
74     int         i_input_method;                   /* input method descriptor */
75     char *      psz_input_source;                   /* source string (owned) */
76     int         i_input_port;                                        /* port */
77     int         i_input_vlan_id;                                  /* vlan id */
78 } intf_channel_t;
79
80 /*****************************************************************************
81  * Local prototypes
82  *****************************************************************************/
83 static int      LoadChannels    ( intf_thread_t *p_intf, char *psz_filename );
84 static void     UnloadChannels  ( intf_thread_t *p_intf );
85 static int      ParseChannel    ( intf_channel_t *p_channel, char *psz_str );
86
87 /*****************************************************************************
88  * intf_Create: prepare interface before main loop
89  *****************************************************************************
90  * This function opens output devices and creates specific interfaces. It sends
91  * its own error messages.
92  *****************************************************************************/
93 intf_thread_t* intf_Create( void )
94 {
95     intf_thread_t * p_intf;
96     typedef void    ( intf_getplugin_t ) ( intf_thread_t * p_intf );
97     int             i_index;
98     int             i_best_index = 0, i_best_score = 0;
99
100     /* Allocate structure */
101     p_intf = malloc( sizeof( intf_thread_t ) );
102     if( !p_intf )
103     {
104         intf_ErrMsg( "intf error: cannot create interface thread (%s)",
105                      strerror( ENOMEM ) );
106         return( NULL );
107     }
108
109     /* Get a suitable interface plugin */
110     for( i_index = 0 ; i_index < p_main->p_bank->i_plugin_count ; i_index++ )
111     {
112         /* If there's a plugin in p_info ... */
113         if( p_main->p_bank->p_info[ i_index ] != NULL )
114         {
115             /* ... and if this plugin provides the functions we want ... */
116             if( p_main->p_bank->p_info[ i_index ]->intf_GetPlugin != NULL )
117             {
118                 /* ... and if this plugin has a good score ... */
119                 if( p_main->p_bank->p_info[ i_index ]->i_score > i_best_score )
120                 {
121                     /* ... then take it */
122                     i_best_score = p_main->p_bank->p_info[ i_index ]->i_score;
123                     i_best_index = i_index;
124                 }
125             }
126         }
127     }
128
129     if( i_best_score == 0 )
130     {
131         free( p_intf );
132         intf_ErrMsg( "intf error: no suitable plugin" );
133         return( NULL );
134     }
135
136     /* Get the plugin functions */
137     ( (intf_getplugin_t *)
138       p_main->p_bank->p_info[ i_best_index ]->intf_GetPlugin )( p_intf );
139
140     /* Initialize structure */
141     p_intf->b_die =     0;
142     p_intf->p_vout =    NULL;
143     p_intf->p_input =   NULL;
144     p_intf->p_keys =    NULL;
145
146     /* Warning level initialisation */
147     p_intf->i_warning_level = main_GetIntVariable( INTF_WARNING_VAR, INTF_WARNING_DEFAULT );
148     
149     /* Load channels - the pointer will be set to NULL on failure. The
150      * return value is ignored since the program can work without
151      * channels */
152     LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
153
154     /* Start interfaces */
155     p_intf->p_console = intf_ConsoleCreate();
156     if( p_intf->p_console == NULL )
157     {
158         intf_ErrMsg( "intf error: cannot create control console" );
159         free( p_intf );
160         return( NULL );
161     }
162     if( p_intf->p_sys_create( p_intf ) )
163     {
164         intf_ErrMsg("intf error: cannot create interface");
165         intf_ConsoleDestroy( p_intf->p_console );
166         free( p_intf );
167         return( NULL );
168     }
169
170     intf_Msg("intf: interface initialized");
171     return( p_intf );
172 }
173
174 /*****************************************************************************
175  * intf_Run
176  *****************************************************************************
177  * Initialization script and main interface loop.
178  *****************************************************************************/
179 void intf_Run( intf_thread_t *p_intf )
180 {
181     char * psz_server = main_GetPszVariable( INPUT_SERVER_VAR, NULL );
182     input_config_t *    p_input_config;
183
184     /* Flush messages before spawning input */
185     intf_FlushMsg();
186
187     /* If a server was specified */
188     if( psz_server )
189     {
190         if( (p_input_config =
191               (input_config_t *)malloc( sizeof(input_config_t) )) == NULL )
192         {
193             intf_ErrMsg( "intf error: cannot create input_config_t" );
194         }
195         else
196         {
197             p_input_config->i_method = INPUT_METHOD_UCAST;
198             p_input_config->p_source = psz_server;
199             p_input_config->p_default_aout = p_main->p_aout;
200             p_input_config->p_default_vout = p_intf->p_vout;
201
202             p_intf->p_input = input_CreateThread( p_input_config, NULL );
203         }
204     }
205     /* DVD mode */
206     else if( p_main->b_dvd )
207     {
208         if( (p_input_config =
209               (input_config_t *)malloc( sizeof(input_config_t) )) == NULL )
210         {
211             intf_ErrMsg( "intf error: cannot create input_config_t" );
212         }
213         else
214         {
215             p_input_config->i_method = INPUT_METHOD_DVD;
216             p_input_config->p_source = main_GetPszVariable( INPUT_DVD_DEVICE_VAR, INPUT_DVD_DEVICE_DEFAULT );
217             p_input_config->p_default_aout = p_main->p_aout;
218             p_input_config->p_default_vout = p_intf->p_vout;
219             p_intf->p_input = input_CreateThread( p_input_config, NULL );
220         }
221     }
222     /* Or if a file was specified */
223     else if( p_main->p_playlist->p_list != NULL )
224     {
225         if( (p_input_config =
226               (input_config_t *)malloc( sizeof(input_config_t) )) == NULL )
227         {
228             intf_ErrMsg( "intf error: cannot create input_config_t" );
229         }
230         else
231         {
232             p_input_config->i_method = INPUT_METHOD_FILE;
233             p_input_config->p_source = p_main->p_playlist->p_list[0]; /* FIXME ??? */
234             p_input_config->p_default_aout = p_main->p_aout;
235             p_input_config->p_default_vout = p_intf->p_vout;
236
237             p_intf->p_input = input_CreateThread( p_input_config, NULL );
238         }
239     }
240     /* Execute the initialization script - if a positive number is returned,
241      * the script could be executed but failed */
242     else if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
243     {
244         intf_ErrMsg( "intf error: errors occured during startup script" );
245     }
246
247     /* Main loop */
248     while(!p_intf->b_die)
249     {
250         /* Flush waiting messages */
251         intf_FlushMsg();
252
253         /* Manage specific interface */
254         p_intf->p_sys_manage( p_intf );
255
256         /* Manage module bank */
257         module_ManageBank( p_main->p_module_bank );
258
259         /* Check attached threads status */
260         if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
261         {
262             /* FIXME: add aout error detection ?? */
263             p_intf->b_die = 1;
264         }
265         if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
266         {
267             input_DestroyThread( p_intf->p_input, NULL );
268             p_intf->p_input = NULL;
269             intf_DbgMsg("Input thread destroyed");
270         }
271
272         /* Sleep to avoid using all CPU - since some interfaces needs to access
273          * keyboard events, a 100ms delay is a good compromise */
274         msleep( INTF_IDLE_SLEEP );
275     }
276 }
277
278 /*****************************************************************************
279  * intf_Destroy: clean interface after main loop
280  *****************************************************************************
281  * This function destroys specific interfaces and close output devices.
282  *****************************************************************************/
283 void intf_Destroy( intf_thread_t *p_intf )
284 {
285     p_intf_key  p_cur;
286     p_intf_key  p_next;
287     /* Destroy interfaces */
288     p_intf->p_sys_destroy( p_intf );
289     intf_ConsoleDestroy( p_intf->p_console );
290
291     /* Unload channels */
292     UnloadChannels( p_intf );
293
294     /* Destroy keymap */
295     p_cur = p_intf->p_keys;
296     while( p_cur != NULL)
297     {
298         p_next = p_cur->next;
299         free(p_cur);
300         p_cur = p_next;
301     }
302          
303     /* Free structure */
304     free( p_intf );
305 }
306
307 /*****************************************************************************
308  * intf_SelectChannel: change channel
309  *****************************************************************************
310  * Kill existing input, if any, and try to open a new one, using an input
311  * configuration table.
312  *****************************************************************************/
313 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
314 {
315     /* FIXME */
316 #if 0
317     intf_channel_t *    p_channel;                                /* channel */
318
319     /* Look for channel in array */
320     if( p_intf->p_channel != NULL )
321     {
322         for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
323         {
324             if( p_channel->i_channel == i_channel )
325             {
326             /*
327              * Change channel
328              */
329
330             /* Kill existing input, if any */
331             if( p_intf->p_input != NULL )
332             {
333                 input_DestroyThread( p_intf->p_input, NULL );
334             }
335
336             intf_Msg("Channel %d: %s", i_channel, p_channel->psz_description );
337
338             /* Open a new input */
339             p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
340                                   p_channel->i_input_port, p_channel->i_input_vlan_id,
341                                   p_intf->p_vout, p_main->p_aout, NULL );
342             return( p_intf->p_input == NULL );
343             }
344         }
345     }
346
347     /* Channel does not exist */
348     intf_Msg("Channel %d does not exist", i_channel );
349 #endif
350     return( 1 );
351 }
352
353 /*****************************************************************************
354  * intf_AssignKey: assign standartkeys                                       *
355  *****************************************************************************
356  * This function fills in the associative array that links the key pressed   *
357  * and the key we use internally. Support one extra parameter.               *
358  ****************************************************************************/
359 void intf_AssignKey( intf_thread_t *p_intf, int r_key, int f_key, int param)
360 {
361     p_intf_key  p_cur =  p_intf->p_keys;
362     if( p_cur == NULL )
363     {
364         p_cur = (p_intf_key )(malloc ( sizeof( intf_key ) ) );
365         p_cur->received_key = r_key;
366         p_cur->forwarded.key = f_key;
367         p_cur->forwarded.param = param; 
368         p_cur->next = NULL;
369         p_intf->p_keys = p_cur;
370     } 
371     else 
372     {
373         while( p_cur->next != NULL && p_cur ->received_key != r_key)
374         {
375             p_cur = p_cur->next;
376         }
377         if( p_cur->next == NULL )
378         {   
379             p_cur->next  = ( p_intf_key )( malloc( sizeof( intf_key ) ) );
380             p_cur = p_cur->next;
381             p_cur->next = NULL;
382             p_cur->forwarded.param = param; 
383             p_cur->received_key = r_key;
384         }
385         p_cur->forwarded.key = f_key;
386     }        
387 }
388
389 /* Basic getKey function... */
390 keyparm intf_GetKey( intf_thread_t *p_intf, int r_key)
391 {   
392     keyparm reply;
393     
394     p_intf_key current = p_intf->p_keys;
395     while(current != NULL && current->received_key != r_key)
396     {    
397         current = current->next;
398     }
399     if(current == NULL)
400     {   /* didn't find any key in the array */ 
401         reply.key = INTF_KEY_UNKNOWN;
402         reply.param = 0;
403     }
404     else
405     {
406         reply.key = current->forwarded.key;
407         reply.param = current->forwarded.param;
408     }
409     return reply;
410 }
411
412 /*****************************************************************************
413 * intf_AssignNormalKeys: used for normal interfaces.
414 *****************************************************************************
415 * This function assign the basic key to the normal keys.
416 *****************************************************************************/
417
418 void intf_AssignNormalKeys( intf_thread_t *p_intf)
419 {
420     p_intf->p_intf_get_key = intf_GetKey;
421
422     intf_AssignKey( p_intf , 'Q', INTF_KEY_QUIT, 0);
423     intf_AssignKey( p_intf , 'q', INTF_KEY_QUIT, 0);
424     intf_AssignKey( p_intf ,  27, INTF_KEY_QUIT, 0);
425     intf_AssignKey( p_intf ,   3, INTF_KEY_QUIT, 0);
426     intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
427     intf_AssignKey( p_intf , '1', INTF_KEY_SET_CHANNEL, 1);
428     intf_AssignKey( p_intf , '2', INTF_KEY_SET_CHANNEL, 2);
429     intf_AssignKey( p_intf , '3', INTF_KEY_SET_CHANNEL, 3);
430     intf_AssignKey( p_intf , '4', INTF_KEY_SET_CHANNEL, 4);
431     intf_AssignKey( p_intf , '5', INTF_KEY_SET_CHANNEL, 5);
432     intf_AssignKey( p_intf , '6', INTF_KEY_SET_CHANNEL, 6);
433     intf_AssignKey( p_intf , '7', INTF_KEY_SET_CHANNEL, 7);
434     intf_AssignKey( p_intf , '8', INTF_KEY_SET_CHANNEL, 8);
435     intf_AssignKey( p_intf , '9', INTF_KEY_SET_CHANNEL, 9);
436     intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
437     intf_AssignKey( p_intf , '+', INTF_KEY_INC_VOLUME, 0);
438     intf_AssignKey( p_intf , '-', INTF_KEY_DEC_VOLUME, 0);
439     intf_AssignKey( p_intf , 'm', INTF_KEY_TOGGLE_VOLUME, 0);
440     intf_AssignKey( p_intf , 'M', INTF_KEY_TOGGLE_VOLUME, 0);
441     intf_AssignKey( p_intf , 'g', INTF_KEY_DEC_GAMMA, 0);
442     intf_AssignKey( p_intf , 'G', INTF_KEY_INC_GAMMA, 0);
443     intf_AssignKey( p_intf , 'c', INTF_KEY_TOGGLE_GRAYSCALE, 0);
444     intf_AssignKey( p_intf , ' ', INTF_KEY_TOGGLE_INTERFACE, 0);
445     intf_AssignKey( p_intf , 'i', INTF_KEY_TOGGLE_INFO, 0);
446     intf_AssignKey( p_intf , 's', INTF_KEY_TOGGLE_SCALING, 0);
447 }   
448
449 /*****************************************************************************
450  * intf_ProcessKey: process standard keys
451  *****************************************************************************
452  * This function will process standard keys and return non 0 if the key was
453  * unknown.
454  *****************************************************************************/
455 int intf_ProcessKey( intf_thread_t *p_intf, int g_key )
456 {
457     static int i_volbackup;
458     keyparm k_reply;
459     
460     k_reply = intf_GetKey( p_intf, g_key); 
461     
462     switch( k_reply.key )
463     {
464     case INTF_KEY_QUIT:                                                  /* quit order */
465         p_intf->b_die = 1;
466         break;
467     case INTF_KEY_SET_CHANNEL:
468         /* Change channel - return code is ignored since SelectChannel displays
469          * its own error messages */
470         intf_SelectChannel( p_intf, k_reply.param );
471         break;
472     case INTF_KEY_INC_VOLUME:                                    /* volume + */
473         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol < VOLUME_MAX) )
474             p_main->p_aout->vol += VOLUME_STEP;
475         break;
476     case INTF_KEY_DEC_VOLUME:                                    /* volume - */
477         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol > VOLUME_STEP) )
478             p_main->p_aout->vol -= VOLUME_STEP;
479         break;
480     case INTF_KEY_TOGGLE_VOLUME:                              /* toggle mute */
481         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol))
482         {
483             i_volbackup = p_main->p_aout->vol;
484             p_main->p_aout->vol = 0;
485         }
486         else if( (p_main->p_aout != NULL) && (!p_main->p_aout->vol))
487             p_main->p_aout->vol = i_volbackup;
488         break;
489     case INTF_KEY_DEC_GAMMA:                                      /* gamma - */
490         if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
491         {
492             vlc_mutex_lock( &p_intf->p_vout->change_lock );
493             p_intf->p_vout->f_gamma   -= INTF_GAMMA_STEP;
494             p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
495             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
496         }
497         break;
498     case INTF_KEY_INC_GAMMA:                                      /* gamma + */
499         if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
500         {
501             vlc_mutex_lock( &p_intf->p_vout->change_lock );
502             p_intf->p_vout->f_gamma   += INTF_GAMMA_STEP;
503             p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
504             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
505         }
506         break;
507     case INTF_KEY_TOGGLE_GRAYSCALE:                      /* toggle grayscale */
508         if( p_intf->p_vout != NULL )
509         {
510             vlc_mutex_lock( &p_intf->p_vout->change_lock );
511             p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
512             p_intf->p_vout->i_changes  |= VOUT_GRAYSCALE_CHANGE;
513             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
514         }
515         break;
516     case INTF_KEY_TOGGLE_INTERFACE:                      /* toggle interface */
517         if( p_intf->p_vout != NULL )
518         {
519             vlc_mutex_lock( &p_intf->p_vout->change_lock );
520             p_intf->p_vout->b_interface     = !p_intf->p_vout->b_interface;
521             p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
522             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
523         }
524         break;
525     case INTF_KEY_TOGGLE_INFO:                                /* toggle info */
526         if( p_intf->p_vout != NULL )
527         {
528             vlc_mutex_lock( &p_intf->p_vout->change_lock );
529             p_intf->p_vout->b_info     = !p_intf->p_vout->b_info;
530             p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
531             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
532         }
533         break;
534     case INTF_KEY_TOGGLE_SCALING:                          /* toggle scaling */
535         if( p_intf->p_vout != NULL )
536         {
537             vlc_mutex_lock( &p_intf->p_vout->change_lock );
538             p_intf->p_vout->b_scale    = !p_intf->p_vout->b_scale;
539             p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
540             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
541         }
542         break;
543    default:                                                   /* unknown key */
544         return( 1 );
545     }
546
547     return( 0 );
548 }
549
550 /* following functions are local */
551
552 /*****************************************************************************
553  * LoadChannels: load channels description from a file
554  *****************************************************************************
555  * This structe describes all interface-specific data of the main (interface)
556  * thread.
557  * Each line of the file is a semicolon separated list of the following
558  * fields :
559  *      integer         channel number
560  *      string          channel description
561  *      integer         input method (see input.h)
562  *      string          input source
563  *      integer         input port
564  *      integer         input vlan id
565  * The last field must end with a semicolon.
566  * Comments and empty lines are not explicitely allowed, but lines with parsing
567  * errors are ignored without warning.
568  *****************************************************************************/
569 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
570 {
571     FILE *              p_file;                                      /* file */
572     intf_channel_t *    p_channel;                        /* current channel */
573     char                psz_line[INTF_MAX_CMD_SIZE];          /* line buffer */
574     int                 i_index;                   /* channel or field index */
575
576     /* Set default value */
577     p_intf->p_channel = NULL;
578
579     /* FIXME: channels are disabled */
580     //return( 0 );
581
582     /* Open file */
583     p_file = fopen( psz_filename, "r" );
584     if( p_file == NULL )
585     {
586         intf_ErrMsg( "intf error: cannot open %s (%s)",
587                      psz_filename, strerror(errno) );
588         return( 1 );
589     }
590
591     /* First pass: count number of lines */
592     for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
593     {
594         ;
595     }
596
597     if( i_index != 0 )
598     {
599         /* Allocate array and rewind - some of the lines may be invalid, and the
600          * array will probably be larger than the actual number of channels, but
601          * it has no consequence. */
602         p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
603         if( p_intf->p_channel == NULL )
604         {
605             intf_ErrMsg( "intf error: cannot create intf_channel_t (%s)",
606                          strerror(ENOMEM) );
607             fclose( p_file );
608             return( 1 );
609         }
610         p_channel = p_intf->p_channel;
611         rewind( p_file );
612
613         /* Second pass: read channels descriptions */
614         while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
615         {
616             if( !ParseChannel( p_channel, psz_line ) )
617             {
618                 intf_DbgMsg( "channel [%d] %s : method %d (%s:%d vlan id %d)",
619                          p_channel->i_channel, p_channel->psz_description,
620                          p_channel->i_input_method,
621                          p_channel->psz_input_source,
622                          p_channel->i_input_port, p_channel->i_input_vlan_id );
623                 p_channel++;
624             }
625         }
626
627         /* Add marker at the end of the array */
628         p_channel->i_channel = -1;
629     }
630
631     /* Close file */
632     fclose( p_file );
633     return( 0 );
634 }
635
636 /*****************************************************************************
637  * UnloadChannels: unload channels description
638  *****************************************************************************
639  * This function free all resources allocated by LoadChannels, if any.
640  *****************************************************************************/
641 static void UnloadChannels( intf_thread_t *p_intf )
642 {
643     int i_channel;                                          /* channel index */
644
645     if( p_intf->p_channel != NULL )
646     {
647         /* Free allocated strings */
648         for( i_channel = 0;
649              p_intf->p_channel[ i_channel ].i_channel != -1;
650              i_channel++ )
651         {
652             if( p_intf->p_channel[ i_channel ].psz_description != NULL )
653             {
654                 free( p_intf->p_channel[ i_channel ].psz_description );
655             }
656             if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
657             {
658                 free( p_intf->p_channel[ i_channel ].psz_input_source );
659             }
660         }
661
662         /* Free array */
663         free( p_intf->p_channel );
664         p_intf->p_channel = NULL;
665     }
666 }
667
668
669 /*****************************************************************************
670  * ParseChannel: parse a channel description line
671  *****************************************************************************
672  * See LoadChannels. This function return non 0 on parsing error.
673  *****************************************************************************/
674 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
675 {
676     char *      psz_index;                              /* current character */
677     char *      psz_end;                           /* end pointer for strtol */
678     int         i_field;                        /* field number, -1 on error */
679     int         i_field_length;             /* field length, for text fields */
680
681     /* Set some default fields */
682     p_channel->i_channel =              0;
683     p_channel->psz_description =        NULL;
684     p_channel->i_input_method =         0;
685     p_channel->psz_input_source =       NULL;
686     p_channel->i_input_port =           0;
687     p_channel->i_input_vlan_id =        0;
688
689     /* Parse string */
690     i_field = 0;
691     for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
692     {
693         if( *psz_index == ';' )
694         {
695             /* Mark end of field */
696             *psz_index = '\0';
697
698             /* Parse field */
699             switch( i_field++ )
700             {
701             case 0:                                        /* channel number */
702                 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
703                 if( (*psz_str == '\0') || (*psz_end != '\0') )
704                 {
705                     i_field = -1;
706                 }
707                 break;
708             case 1:                                   /* channel description */
709                 i_field_length = strlen( psz_str );
710                 if( i_field_length != 0 )
711                 {
712                     p_channel->psz_description = malloc( i_field_length + 1 );
713                     if( p_channel->psz_description == NULL )
714                     {
715                         intf_ErrMsg( "intf error: cannot create channel "
716                                      "description (%s)", strerror( ENOMEM ) );
717                         i_field = -1;
718                     }
719                     else
720                     {
721                         strcpy( p_channel->psz_description, psz_str );
722                     }
723                 }
724                 break;
725             case 2:                                          /* input method */
726                 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
727                 if( (*psz_str == '\0') || (*psz_end != '\0') )
728                 {
729                     i_field = -1;
730                 }
731                 break;
732             case 3:                                          /* input source */
733                 i_field_length = strlen( psz_str );
734                 if( i_field_length != 0 )
735                 {
736                     p_channel->psz_input_source = malloc( i_field_length + 1 );
737                     if( p_channel->psz_input_source == NULL )
738                     {
739                         intf_ErrMsg( "intf error: cannot create input "
740                                      "source (%s)", strerror( ENOMEM ) );
741                         i_field = -1;
742                     }
743                     else
744                     {
745                         strcpy( p_channel->psz_input_source, psz_str );
746                     }
747                 }
748                 break;
749             case 4:                                            /* input port */
750                 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
751                 if( (*psz_str == '\0') || (*psz_end != '\0') )
752                 {
753                     i_field = -1;
754                 }
755                 break;
756             case 5:                                          /* input vlan id */
757                 p_channel->i_input_vlan_id = strtol( psz_str, &psz_end, 0);
758                 if( (*psz_str == '\0') || (*psz_end != '\0') )
759                 {
760                     i_field = -1;
761                 }
762                 break;
763                 /* ... following fields are ignored */
764             }
765
766             /* Set new beginning of field */
767             psz_str = psz_index + 1;
768         }
769     }
770
771     /* At least the first three fields must be parsed sucessfully for function
772      * success. Other parsing errors are returned using i_field = -1. */
773     if( i_field < 3 )
774     {
775         /* Function fails. Free allocated strings */
776         if( p_channel->psz_description != NULL )
777         {
778             free( p_channel->psz_description );
779         }
780         if( p_channel->psz_input_source != NULL )
781         {
782             free( p_channel->psz_input_source );
783         }
784         return( 1 );
785     }
786
787     /* Return success */
788     return( 0 );
789 }