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