]> git.sesse.net Git - vlc/blob - src/interface/interface.c
. Added a missing mutex_unlock in video_output.c
[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("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     /* Or if a file was specified */
206     else if( p_main->p_playlist->p_list != NULL )
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_FILE;
216             p_input_config->p_source = p_main->p_playlist->p_list[0]; /* FIXME ??? */
217             p_input_config->p_default_aout = p_main->p_aout;
218             p_input_config->p_default_vout = p_intf->p_vout;
219
220             p_intf->p_input = input_CreateThread( p_input_config, NULL );
221         }
222     }
223     /* Execute the initialization script - if a positive number is returned,
224      * the script could be executed but failed */
225     else if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
226     {
227         intf_ErrMsg( "intf error: errors occured during startup script" );
228     }
229
230     /* Main loop */
231     while(!p_intf->b_die)
232     {
233         /* Flush waiting messages */
234         intf_FlushMsg();
235
236         /* Manage specific interface */
237         p_intf->p_sys_manage( p_intf );
238
239         /* Manage module bank */
240         module_ManageBank( p_main->p_module_bank );
241
242         /* Check attached threads status */
243         if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
244         {
245             /* FIXME: add aout error detection ?? */
246             p_intf->b_die = 1;
247         }
248         if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
249         {
250             input_DestroyThread( p_intf->p_input, NULL );
251             p_intf->p_input = NULL;
252             intf_DbgMsg("Input thread destroyed");
253         }
254
255         /* Sleep to avoid using all CPU - since some interfaces needs to access
256          * keyboard events, a 100ms delay is a good compromise */
257         msleep( INTF_IDLE_SLEEP );
258     }
259 }
260
261 /*****************************************************************************
262  * intf_Destroy: clean interface after main loop
263  *****************************************************************************
264  * This function destroys specific interfaces and close output devices.
265  *****************************************************************************/
266 void intf_Destroy( intf_thread_t *p_intf )
267 {
268     p_intf_key  p_cur;
269     p_intf_key  p_next;
270     /* Destroy interfaces */
271     p_intf->p_sys_destroy( p_intf );
272     intf_ConsoleDestroy( p_intf->p_console );
273
274     /* Unload channels */
275     UnloadChannels( p_intf );
276
277     /* Destroy keymap */
278     p_cur = p_intf->p_keys;
279     while( p_cur != NULL)
280     {
281         p_next = p_cur->next;
282         free(p_cur);
283         p_cur = p_next;
284     }
285          
286     /* Free structure */
287     free( p_intf );
288 }
289
290 /*****************************************************************************
291  * intf_SelectChannel: change channel
292  *****************************************************************************
293  * Kill existing input, if any, and try to open a new one, using an input
294  * configuration table.
295  *****************************************************************************/
296 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
297 {
298     /* FIXME */
299 #if 0
300     intf_channel_t *    p_channel;                                /* channel */
301
302     /* Look for channel in array */
303     if( p_intf->p_channel != NULL )
304     {
305         for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
306         {
307             if( p_channel->i_channel == i_channel )
308             {
309             /*
310              * Change channel
311              */
312
313             /* Kill existing input, if any */
314             if( p_intf->p_input != NULL )
315             {
316                 input_DestroyThread( p_intf->p_input, NULL );
317             }
318
319             intf_Msg("Channel %d: %s", i_channel, p_channel->psz_description );
320
321             /* Open a new input */
322             p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
323                                   p_channel->i_input_port, p_channel->i_input_vlan_id,
324                                   p_intf->p_vout, p_main->p_aout, NULL );
325             return( p_intf->p_input == NULL );
326             }
327         }
328     }
329
330     /* Channel does not exist */
331     intf_Msg("Channel %d does not exist", i_channel );
332 #endif
333     return( 1 );
334 }
335
336 /*****************************************************************************
337  * intf_AssignKey: assign standartkeys                                       *
338  *****************************************************************************
339  * This function fills in the associative array that links the key pressed   *
340  * and the key we use internally. Support one extra parameter.               *
341  ****************************************************************************/
342 void intf_AssignKey( intf_thread_t *p_intf, int r_key, int f_key, int param)
343 {
344     p_intf_key  p_cur =  p_intf->p_keys;
345     if( p_cur == NULL )
346     {
347         p_cur = (p_intf_key )(malloc ( sizeof( intf_key ) ) );
348         p_cur->received_key = r_key;
349         p_cur->forwarded.key = f_key;
350         p_cur->forwarded.param = param; 
351         p_cur->next = NULL;
352         p_intf->p_keys = p_cur;
353     } 
354     else 
355     {
356         while( p_cur->next != NULL && p_cur ->received_key != r_key)
357         {
358             p_cur = p_cur->next;
359         }
360         if( p_cur->next == NULL )
361         {   
362             p_cur->next  = ( p_intf_key )( malloc( sizeof( intf_key ) ) );
363             p_cur = p_cur->next;
364             p_cur->next = NULL;
365             p_cur->forwarded.param = param; 
366             p_cur->received_key = r_key;
367         }
368         p_cur->forwarded.key = f_key;
369     }        
370 }
371
372 /* Basic getKey function... */
373 keyparm intf_GetKey( intf_thread_t *p_intf, int r_key)
374 {   
375     keyparm reply;
376     
377     p_intf_key current = p_intf->p_keys;
378     while(current != NULL && current->received_key != r_key)
379     {    
380         current = current->next;
381     }
382     if(current == NULL)
383     {   /* didn't find any key in the array */ 
384         reply.key = INTF_KEY_UNKNOWN;
385         reply.param = 0;
386     }
387     else
388     {
389         reply.key = current->forwarded.key;
390         reply.param = current->forwarded.param;
391     }
392     return reply;
393 }
394
395 /*****************************************************************************
396 * intf_AssignNormalKeys: used for normal interfaces.
397 *****************************************************************************
398 * This function assign the basic key to the normal keys.
399 *****************************************************************************/
400
401 void intf_AssignNormalKeys( intf_thread_t *p_intf)
402 {
403     p_intf->p_intf_get_key = intf_GetKey;
404
405     intf_AssignKey( p_intf , 'Q', INTF_KEY_QUIT, 0);
406     intf_AssignKey( p_intf , 'q', INTF_KEY_QUIT, 0);
407     intf_AssignKey( p_intf ,  27, INTF_KEY_QUIT, 0);
408     intf_AssignKey( p_intf ,   3, INTF_KEY_QUIT, 0);
409     intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
410     intf_AssignKey( p_intf , '1', INTF_KEY_SET_CHANNEL, 1);
411     intf_AssignKey( p_intf , '2', INTF_KEY_SET_CHANNEL, 2);
412     intf_AssignKey( p_intf , '3', INTF_KEY_SET_CHANNEL, 3);
413     intf_AssignKey( p_intf , '4', INTF_KEY_SET_CHANNEL, 4);
414     intf_AssignKey( p_intf , '5', INTF_KEY_SET_CHANNEL, 5);
415     intf_AssignKey( p_intf , '6', INTF_KEY_SET_CHANNEL, 6);
416     intf_AssignKey( p_intf , '7', INTF_KEY_SET_CHANNEL, 7);
417     intf_AssignKey( p_intf , '8', INTF_KEY_SET_CHANNEL, 8);
418     intf_AssignKey( p_intf , '9', INTF_KEY_SET_CHANNEL, 9);
419     intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
420     intf_AssignKey( p_intf , '+', INTF_KEY_INC_VOLUME, 0);
421     intf_AssignKey( p_intf , '-', INTF_KEY_DEC_VOLUME, 0);
422     intf_AssignKey( p_intf , 'm', INTF_KEY_TOGGLE_VOLUME, 0);
423     intf_AssignKey( p_intf , 'M', INTF_KEY_TOGGLE_VOLUME, 0);
424     intf_AssignKey( p_intf , 'g', INTF_KEY_DEC_GAMMA, 0);
425     intf_AssignKey( p_intf , 'G', INTF_KEY_INC_GAMMA, 0);
426     intf_AssignKey( p_intf , 'c', INTF_KEY_TOGGLE_GRAYSCALE, 0);
427     intf_AssignKey( p_intf , ' ', INTF_KEY_TOGGLE_INTERFACE, 0);
428     intf_AssignKey( p_intf , 'i', INTF_KEY_TOGGLE_INFO, 0);
429     intf_AssignKey( p_intf , 's', INTF_KEY_TOGGLE_SCALING, 0);
430 }   
431
432 /*****************************************************************************
433  * intf_ProcessKey: process standard keys
434  *****************************************************************************
435  * This function will process standard keys and return non 0 if the key was
436  * unknown.
437  *****************************************************************************/
438 int intf_ProcessKey( intf_thread_t *p_intf, int g_key )
439 {
440     static int i_volbackup;
441     keyparm k_reply;
442     
443     k_reply = intf_GetKey( p_intf, g_key); 
444     
445     switch( k_reply.key )
446     {
447     case INTF_KEY_QUIT:                                                  /* quit order */
448         p_intf->b_die = 1;
449         break;
450     case INTF_KEY_SET_CHANNEL:
451         /* Change channel - return code is ignored since SelectChannel displays
452          * its own error messages */
453         intf_SelectChannel( p_intf, k_reply.param );
454         break;
455     case INTF_KEY_INC_VOLUME:                                                    /* volume + */
456         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol < VOLUME_MAX) )
457             p_main->p_aout->vol += VOLUME_STEP;
458         break;
459     case INTF_KEY_DEC_VOLUME:                                                    /* volume - */
460         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol > VOLUME_STEP) )
461             p_main->p_aout->vol -= VOLUME_STEP;
462         break;
463     case INTF_KEY_TOGGLE_VOLUME:                                                 /* toggle mute */
464         if( (p_main->p_aout != NULL) && (p_main->p_aout->vol))
465         {
466             i_volbackup = p_main->p_aout->vol;
467             p_main->p_aout->vol = 0;
468         }
469         else if( (p_main->p_aout != NULL) && (!p_main->p_aout->vol))
470             p_main->p_aout->vol = i_volbackup;
471         break;
472     case INTF_KEY_DEC_GAMMA:                                                     /* gamma - */
473         if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
474         {
475             vlc_mutex_lock( &p_intf->p_vout->change_lock );
476             p_intf->p_vout->f_gamma   -= INTF_GAMMA_STEP;
477             p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
478             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
479         }
480         break;
481     case INTF_KEY_INC_GAMMA:                                                     /* gamma + */
482         if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
483         {
484             vlc_mutex_lock( &p_intf->p_vout->change_lock );
485             p_intf->p_vout->f_gamma   += INTF_GAMMA_STEP;
486             p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
487             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
488         }
489         break;
490     case INTF_KEY_TOGGLE_GRAYSCALE:                                            /* toggle grayscale */
491         if( p_intf->p_vout != NULL )
492         {
493             vlc_mutex_lock( &p_intf->p_vout->change_lock );
494             p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
495             p_intf->p_vout->i_changes  |= VOUT_GRAYSCALE_CHANGE;
496             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
497         }
498         break;
499     case INTF_KEY_TOGGLE_INTERFACE:                                            /* toggle interface */
500         if( p_intf->p_vout != NULL )
501         {
502             vlc_mutex_lock( &p_intf->p_vout->change_lock );
503             p_intf->p_vout->b_interface     = !p_intf->p_vout->b_interface;
504             p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
505             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
506         }
507         break;
508     case INTF_KEY_TOGGLE_INFO:                                                 /* toggle info */
509         if( p_intf->p_vout != NULL )
510         {
511             vlc_mutex_lock( &p_intf->p_vout->change_lock );
512             p_intf->p_vout->b_info     = !p_intf->p_vout->b_info;
513             p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
514             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
515         }
516         break;
517     case INTF_KEY_TOGGLE_SCALING:                                              /* toggle scaling */
518         if( p_intf->p_vout != NULL )
519         {
520             vlc_mutex_lock( &p_intf->p_vout->change_lock );
521             p_intf->p_vout->b_scale    = !p_intf->p_vout->b_scale;
522             p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
523             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
524         }
525         break;
526    default:                                                   /* unknown key */
527         return( 1 );
528     }
529
530     return( 0 );
531 }
532
533 /* following functions are local */
534
535 /*****************************************************************************
536  * LoadChannels: load channels description from a file
537  *****************************************************************************
538  * This structe describes all interface-specific data of the main (interface)
539  * thread.
540  * Each line of the file is a semicolon separated list of the following
541  * fields :
542  *      integer         channel number
543  *      string          channel description
544  *      integer         input method (see input.h)
545  *      string          input source
546  *      integer         input port
547  *      integer         input vlan id
548  * The last field must end with a semicolon.
549  * Comments and empty lines are not explicitely allowed, but lines with parsing
550  * errors are ignored without warning.
551  *****************************************************************************/
552 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
553 {
554     FILE *              p_file;                                      /* file */
555     intf_channel_t *    p_channel;                        /* current channel */
556     char                psz_line[INTF_MAX_CMD_SIZE];          /* line buffer */
557     int                 i_index;                   /* channel or field index */
558
559     /* Set default value */
560     p_intf->p_channel = NULL;
561
562     /* FIXME: channels are disabled */
563     //return( 0 );
564
565     /* Open file */
566     p_file = fopen( psz_filename, "r" );
567     if( p_file == NULL )
568     {
569         intf_ErrMsg( "intf error: cannot open %s (%s)",
570                      psz_filename, strerror(errno) );
571         return( 1 );
572     }
573
574     /* First pass: count number of lines */
575     for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
576     {
577         ;
578     }
579
580     if( i_index != 0 )
581     {
582         /* Allocate array and rewind - some of the lines may be invalid, and the
583          * array will probably be larger than the actual number of channels, but
584          * it has no consequence. */
585         p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
586         if( p_intf->p_channel == NULL )
587         {
588             intf_ErrMsg( "intf error: cannot create intf_channel_t (%s)",
589                          strerror(ENOMEM) );
590             fclose( p_file );
591             return( 1 );
592         }
593         p_channel = p_intf->p_channel;
594         rewind( p_file );
595
596         /* Second pass: read channels descriptions */
597         while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
598         {
599             if( !ParseChannel( p_channel, psz_line ) )
600             {
601                 intf_DbgMsg( "channel [%d] %s : method %d (%s:%d vlan id %d)",
602                          p_channel->i_channel, p_channel->psz_description,
603                          p_channel->i_input_method,
604                          p_channel->psz_input_source,
605                          p_channel->i_input_port, p_channel->i_input_vlan_id );
606                 p_channel++;
607             }
608         }
609
610         /* Add marker at the end of the array */
611         p_channel->i_channel = -1;
612     }
613
614     /* Close file */
615     fclose( p_file );
616     return( 0 );
617 }
618
619 /*****************************************************************************
620  * UnloadChannels: unload channels description
621  *****************************************************************************
622  * This function free all resources allocated by LoadChannels, if any.
623  *****************************************************************************/
624 static void UnloadChannels( intf_thread_t *p_intf )
625 {
626     int i_channel;                                          /* channel index */
627
628     if( p_intf->p_channel != NULL )
629     {
630         /* Free allocated strings */
631         for( i_channel = 0;
632              p_intf->p_channel[ i_channel ].i_channel != -1;
633              i_channel++ )
634         {
635             if( p_intf->p_channel[ i_channel ].psz_description != NULL )
636             {
637                 free( p_intf->p_channel[ i_channel ].psz_description );
638             }
639             if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
640             {
641                 free( p_intf->p_channel[ i_channel ].psz_input_source );
642             }
643         }
644
645         /* Free array */
646         free( p_intf->p_channel );
647         p_intf->p_channel = NULL;
648     }
649 }
650
651
652 /*****************************************************************************
653  * ParseChannel: parse a channel description line
654  *****************************************************************************
655  * See LoadChannels. This function return non 0 on parsing error.
656  *****************************************************************************/
657 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
658 {
659     char *      psz_index;                              /* current character */
660     char *      psz_end;                           /* end pointer for strtol */
661     int         i_field;                        /* field number, -1 on error */
662     int         i_field_length;             /* field length, for text fields */
663
664     /* Set some default fields */
665     p_channel->i_channel =              0;
666     p_channel->psz_description =        NULL;
667     p_channel->i_input_method =         0;
668     p_channel->psz_input_source =       NULL;
669     p_channel->i_input_port =           0;
670     p_channel->i_input_vlan_id =        0;
671
672     /* Parse string */
673     i_field = 0;
674     for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
675     {
676         if( *psz_index == ';' )
677         {
678             /* Mark end of field */
679             *psz_index = '\0';
680
681             /* Parse field */
682             switch( i_field++ )
683             {
684             case 0:                                        /* channel number */
685                 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
686                 if( (*psz_str == '\0') || (*psz_end != '\0') )
687                 {
688                     i_field = -1;
689                 }
690                 break;
691             case 1:                                   /* channel description */
692                 i_field_length = strlen( psz_str );
693                 if( i_field_length != 0 )
694                 {
695                     p_channel->psz_description = malloc( i_field_length + 1 );
696                     if( p_channel->psz_description == NULL )
697                     {
698                         intf_ErrMsg( "intf error: cannot create channel "
699                                      "description (%s)", strerror( ENOMEM ) );
700                         i_field = -1;
701                     }
702                     else
703                     {
704                         strcpy( p_channel->psz_description, psz_str );
705                     }
706                 }
707                 break;
708             case 2:                                          /* input method */
709                 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
710                 if( (*psz_str == '\0') || (*psz_end != '\0') )
711                 {
712                     i_field = -1;
713                 }
714                 break;
715             case 3:                                          /* input source */
716                 i_field_length = strlen( psz_str );
717                 if( i_field_length != 0 )
718                 {
719                     p_channel->psz_input_source = malloc( i_field_length + 1 );
720                     if( p_channel->psz_input_source == NULL )
721                     {
722                         intf_ErrMsg( "intf error: cannot create input "
723                                      "source (%s)", strerror( ENOMEM ) );
724                         i_field = -1;
725                     }
726                     else
727                     {
728                         strcpy( p_channel->psz_input_source, psz_str );
729                     }
730                 }
731                 break;
732             case 4:                                            /* input port */
733                 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
734                 if( (*psz_str == '\0') || (*psz_end != '\0') )
735                 {
736                     i_field = -1;
737                 }
738                 break;
739             case 5:                                          /* input vlan id */
740                 p_channel->i_input_vlan_id = strtol( psz_str, &psz_end, 0);
741                 if( (*psz_str == '\0') || (*psz_end != '\0') )
742                 {
743                     i_field = -1;
744                 }
745                 break;
746                 /* ... following fields are ignored */
747             }
748
749             /* Set new beginning of field */
750             psz_str = psz_index + 1;
751         }
752     }
753
754     /* At least the first three fields must be parsed sucessfully for function
755      * success. Other parsing errors are returned using i_field = -1. */
756     if( i_field < 3 )
757     {
758         /* Function fails. Free allocated strings */
759         if( p_channel->psz_description != NULL )
760         {
761             free( p_channel->psz_description );
762         }
763         if( p_channel->psz_input_source != NULL )
764         {
765             free( p_channel->psz_input_source );
766         }
767         return( 1 );
768     }
769
770     /* Return success */
771     return( 0 );
772 }