]> git.sesse.net Git - vlc/blob - src/interface/intf_channels.c
* Coding style fixes here and there.
[vlc] / src / interface / intf_channels.c
1 /*****************************************************************************
2  * intf_channels.c: channel handling functions
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  * $Id: intf_channels.c,v 1.3 2001/04/28 03:36:25 sam Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdlib.h>                                      /* free(), strtol() */
31 #include <stdio.h>                                                   /* FILE */
32 #include <string.h>                                            /* strerror() */
33
34 #include "config.h"
35 #include "common.h"
36 #include "threads.h"
37 #include "mtime.h"
38
39 #include "intf_msg.h"
40 #include "intf_channels.h"
41 #include "interface.h"
42
43 #include "main.h"
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int ParseChannel( intf_channel_t *p_channel, char *psz_str );
49
50 /*****************************************************************************
51  * intf_LoadChannels: load channels description from a file
52  *****************************************************************************
53  * This structe describes all interface-specific data of the main (interface)
54  * thread.
55  * Each line of the file is a semicolon separated list of the following
56  * fields :
57  *      integer         channel number
58  *      string          channel description
59  *      integer         input method (see input.h)
60  *      string          input source
61  *      integer         input port
62  *      integer         input vlan id
63  * The last field must end with a semicolon.
64  * Comments and empty lines are not explicitely allowed, but lines with parsing
65  * errors are ignored without warning.
66  *****************************************************************************/
67 int intf_LoadChannels( intf_thread_t *p_intf, char *psz_filename )
68 {
69     FILE *              p_file;                                      /* file */
70     intf_channel_t *    p_channel;                        /* current channel */
71     char                psz_line[INTF_MAX_CMD_SIZE];          /* line buffer */
72     int                 i_index;                   /* channel or field index */
73
74     /* Set default value */
75     p_intf->p_channel = NULL;
76
77     /* Open file */
78     p_file = fopen( psz_filename, "r" );
79     if( p_file == NULL )
80     {
81         intf_DbgMsg( "intf warning: cannot open %s (%s)",
82                      psz_filename, strerror(errno) );
83         return( 1 );
84     }
85
86     /* First pass: count number of lines */
87     for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL;
88          i_index++ )
89     {
90         ;
91     }
92
93     if( i_index != 0 )
94     {
95         /* Allocate array and rewind - some of the lines may be invalid,
96          * and the array will probably be larger than the actual number of
97          * channels, but it has no consequences. */
98         p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
99         if( p_intf->p_channel == NULL )
100         {
101             intf_ErrMsg( "intf error: cannot create intf_channel_t (%s)",
102                          strerror(ENOMEM) );
103             fclose( p_file );
104             return( 1 );
105         }
106         p_channel = p_intf->p_channel;
107         rewind( p_file );
108
109         /* Second pass: read channels descriptions */
110         while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
111         {
112             if( !ParseChannel( p_channel, psz_line ) )
113             {
114                 intf_DbgMsg( "channel [%d] %s : method %d (%s:%d vlan id %d)",
115                          p_channel->i_channel, p_channel->psz_description,
116                          p_channel->i_input_method,
117                          p_channel->psz_input_source,
118                          p_channel->i_input_port, p_channel->i_input_vlan_id );
119                 p_channel++;
120             }
121         }
122
123         /* Add marker at the end of the array */
124         p_channel->i_channel = -1;
125     }
126
127     /* Close file */
128     fclose( p_file );
129     return( 0 );
130 }
131
132 /*****************************************************************************
133  * intf_UnloadChannels: unload channels description
134  *****************************************************************************
135  * This function free all resources allocated by LoadChannels, if any.
136  *****************************************************************************/
137 void intf_UnloadChannels( intf_thread_t *p_intf )
138 {
139     int i_channel;                                          /* channel index */
140
141     if( p_intf->p_channel != NULL )
142     {
143         /* Free allocated strings */
144         for( i_channel = 0;
145              p_intf->p_channel[ i_channel ].i_channel != -1;
146              i_channel++ )
147         {
148             if( p_intf->p_channel[ i_channel ].psz_description != NULL )
149             {
150                 free( p_intf->p_channel[ i_channel ].psz_description );
151             }
152             if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
153             {
154                 free( p_intf->p_channel[ i_channel ].psz_input_source );
155             }
156         }
157
158         /* Free array */
159         free( p_intf->p_channel );
160         p_intf->p_channel = NULL;
161     }
162 }
163
164 /*****************************************************************************
165  * intf_SelectChannel: change channel
166  *****************************************************************************
167  * Kill existing input, if any, and try to open a new one, using an input
168  * configuration table.
169  *****************************************************************************/
170 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
171 {
172     /* FIXME */
173 #if 0
174     intf_channel_t *    p_channel;                                /* channel */
175
176     /* Look for channel in array */
177     if( p_intf->p_channel != NULL )
178     {
179         for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
180         {
181             if( p_channel->i_channel == i_channel )
182             {
183             /*
184              * Change channel
185              */
186
187             /* Kill existing input, if any */
188             if( p_intf->p_input != NULL )
189             {
190                 input_DestroyThread( p_intf->p_input, NULL );
191             }
192
193             intf_Msg("Channel %d: %s", i_channel, p_channel->psz_description );
194
195             /* Open a new input */
196             p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
197                                   p_channel->i_input_port, p_channel->i_input_vlan_id,
198                                   p_intf->p_vout, p_main->p_aout, NULL );
199             return( p_intf->p_input == NULL );
200             }
201         }
202     }
203
204     /* Channel does not exist */
205     intf_Msg("Channel %d does not exist", i_channel );
206 #endif
207     return( 1 );
208 }
209
210 /* Following functions are local */
211
212 /*****************************************************************************
213  * ParseChannel: parse a channel description line
214  *****************************************************************************
215  * See intf_LoadChannels. This function return non 0 on parsing error.
216  *****************************************************************************/
217 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
218 {
219     char *      psz_index;                              /* current character */
220     char *      psz_end;                           /* end pointer for strtol */
221     int         i_field;                        /* field number, -1 on error */
222     int         i_field_length;             /* field length, for text fields */
223
224     /* Set some default fields */
225     p_channel->i_channel =              0;
226     p_channel->psz_description =        NULL;
227     p_channel->i_input_method =         0;
228     p_channel->psz_input_source =       NULL;
229     p_channel->i_input_port =           0;
230     p_channel->i_input_vlan_id =        0;
231
232     /* Parse string */
233     i_field = 0;
234     for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
235     {
236         if( *psz_index == ';' )
237         {
238             /* Mark end of field */
239             *psz_index = '\0';
240
241             /* Parse field */
242             switch( i_field++ )
243             {
244             case 0:                                        /* channel number */
245                 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
246                 if( (*psz_str == '\0') || (*psz_end != '\0') )
247                 {
248                     i_field = -1;
249                 }
250                 break;
251             case 1:                                   /* channel description */
252                 i_field_length = strlen( psz_str );
253                 if( i_field_length != 0 )
254                 {
255                     p_channel->psz_description = malloc( i_field_length + 1 );
256                     if( p_channel->psz_description == NULL )
257                     {
258                         intf_ErrMsg( "intf error: cannot create channel "
259                                      "description (%s)", strerror( ENOMEM ) );
260                         i_field = -1;
261                     }
262                     else
263                     {
264                         strcpy( p_channel->psz_description, psz_str );
265                     }
266                 }
267                 break;
268             case 2:                                          /* input method */
269                 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
270                 if( (*psz_str == '\0') || (*psz_end != '\0') )
271                 {
272                     i_field = -1;
273                 }
274                 break;
275             case 3:                                          /* input source */
276                 i_field_length = strlen( psz_str );
277                 if( i_field_length != 0 )
278                 {
279                     p_channel->psz_input_source = malloc( i_field_length + 1 );
280                     if( p_channel->psz_input_source == NULL )
281                     {
282                         intf_ErrMsg( "intf error: cannot create input "
283                                      "source (%s)", strerror( ENOMEM ) );
284                         i_field = -1;
285                     }
286                     else
287                     {
288                         strcpy( p_channel->psz_input_source, psz_str );
289                     }
290                 }
291                 break;
292             case 4:                                            /* input port */
293                 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
294                 if( (*psz_str == '\0') || (*psz_end != '\0') )
295                 {
296                     i_field = -1;
297                 }
298                 break;
299             case 5:                                         /* input vlan id */
300                 p_channel->i_input_vlan_id = strtol( psz_str, &psz_end, 0);
301                 if( (*psz_str == '\0') || (*psz_end != '\0') )
302                 {
303                     i_field = -1;
304                 }
305                 break;
306                 /* ... following fields are ignored */
307             }
308
309             /* Set new beginning of field */
310             psz_str = psz_index + 1;
311         }
312     }
313
314     /* At least the first three fields must be parsed sucessfully for function
315      * success. Other parsing errors are returned using i_field = -1. */
316     if( i_field < 3 )
317     {
318         /* Function fails. Free allocated strings */
319         if( p_channel->psz_description != NULL )
320         {
321             free( p_channel->psz_description );
322         }
323         if( p_channel->psz_input_source != NULL )
324         {
325             free( p_channel->psz_input_source );
326         }
327         return( 1 );
328     }
329
330     /* Return success */
331     return( 0 );
332 }
333