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