1 /*****************************************************************************
2 * interface.c: interface access for other threads
4 *****************************************************************************
5 * This library provides basic functions for threads to interact with user
6 * interface, such as command line.
7 *****************************************************************************/
9 /*****************************************************************************
11 *****************************************************************************/
16 #include <sys/types.h>
17 #include <sys/uio.h> /* for input.h */
22 #include "vlc_thread.h"
25 #include "interface.h"
27 #include "intf_console.h"
30 #include "video_output.h"
34 /*****************************************************************************
35 * intf_channel_t: channel description
36 *****************************************************************************
37 * A 'channel' is a descriptor of an input method. It is used to switch easily
38 * from source to source without having to specify the whole input thread
39 * configuration. The channels array, stored in the interface thread object, is
40 * loaded in intf_Create, and unloaded in intf_Destroy.
41 *****************************************************************************/
42 typedef struct intf_channel_s
44 /* Channel description */
45 int i_channel; /* channel number, -1 for end of array */
46 char * psz_description; /* channel description (owned) */
48 /* Input configuration */
49 int i_input_method; /* input method descriptor */
50 char * psz_input_source; /* source string (owned) */
51 int i_input_port; /* port */
52 int i_input_vlan; /* vlan */
55 /*****************************************************************************
57 *****************************************************************************/
58 static int LoadChannels ( intf_thread_t *p_intf, char *psz_filename );
59 static void UnloadChannels ( intf_thread_t *p_intf );
60 static int ParseChannel ( intf_channel_t *p_channel, char *psz_str );
62 /*****************************************************************************
63 * intf_Create: prepare interface before main loop
64 *****************************************************************************
65 * This function opens output devices and create specific interfaces. It send
66 * it's own error messages.
67 *****************************************************************************/
68 intf_thread_t* intf_Create( void )
70 intf_thread_t *p_intf;
72 /* Allocate structure */
73 p_intf = malloc( sizeof( intf_thread_t ) );
76 intf_ErrMsg("error: %s\n", strerror( ENOMEM ) );
80 /* Initialize structure */
82 p_intf->p_vout = NULL;
83 p_intf->p_input = NULL;
85 /* Load channels - the pointer will be set to NULL on failure. The
86 * return value is ignored since the program can work without
88 LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
90 /* Start interfaces */
91 p_intf->p_console = intf_ConsoleCreate();
92 if( p_intf->p_console == NULL )
94 intf_ErrMsg("error: can't create control console\n");
98 if( intf_SysCreate( p_intf ) )
100 intf_ErrMsg("error: can't create interface\n");
101 intf_ConsoleDestroy( p_intf->p_console );
106 intf_Msg("Interface initialized\n");
110 /*****************************************************************************
112 *****************************************************************************
113 * Initialization script and main interface loop.
114 *****************************************************************************/
115 void intf_Run( intf_thread_t *p_intf )
117 /* Execute the initialization script - if a positive number is returned,
118 * the script could be executed but failed */
119 if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
121 intf_ErrMsg("warning: error(s) during startup script\n");
125 while(!p_intf->b_die)
127 /* Flush waiting messages */
130 /* Manage specific interface */
131 intf_SysManage( p_intf );
133 /* Check attached threads status */
134 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
136 //?? add aout error detection
139 if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
141 input_DestroyThread( p_intf->p_input, NULL );
142 p_intf->p_input = NULL;
143 intf_DbgMsg("Input thread destroyed\n");
146 /* Sleep to avoid using all CPU - since some interfaces needs to access
147 * keyboard events, a 100ms delay is a good compromise */
148 msleep( INTF_IDLE_SLEEP );
152 /*****************************************************************************
153 * intf_Destroy: clean interface after main loop
154 *****************************************************************************
155 * This function destroys specific interfaces and close output devices.
156 *****************************************************************************/
157 void intf_Destroy( intf_thread_t *p_intf )
159 /* Destroy interfaces */
160 intf_SysDestroy( p_intf );
161 intf_ConsoleDestroy( p_intf->p_console );
163 /* Unload channels */
164 UnloadChannels( p_intf );
170 /*****************************************************************************
171 * intf_SelectChannel: change channel
172 *****************************************************************************
173 * Kill existing input, if any, and try to open a new one, using an input
174 * configuration table.
175 *****************************************************************************/
176 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
178 intf_channel_t * p_channel; /* channel */
180 /* Look for channel in array */
181 if( p_intf->p_channel != NULL )
183 for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
185 if( p_channel->i_channel == i_channel )
191 /* Kill existing input, if any */
192 if( p_intf->p_input != NULL )
194 input_DestroyThread( p_intf->p_input, NULL );
197 intf_Msg("Channel %d: %s\n", i_channel, p_channel->psz_description );
199 /* Open a new input */
200 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
201 p_channel->i_input_port, p_channel->i_input_vlan,
202 p_intf->p_vout, p_main->p_aout, NULL );
203 return( p_intf->p_input == NULL );
208 /* Channel does not exist */
209 intf_Msg("Channel %d does not exist\n", i_channel );
213 /*****************************************************************************
214 * intf_ProcessKey: process standard keys
215 *****************************************************************************
216 * This function will process standard keys and return non 0 if the key was
218 *****************************************************************************/
219 int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
223 case 'Q': /* quit order */
225 case 27: /* escape key */
229 case '0': /* source change */
239 /* Change channel - return code is ignored since SelectChannel displays
240 * its own error messages */
241 intf_SelectChannel( p_intf, i_key - '0' );
243 case '+': /* volume + */
246 case '-': /* volume - */
249 case 'M': /* toggle mute */
253 case 'g': /* gamma - */
254 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
256 vlc_mutex_lock( &p_intf->p_vout->change_lock );
257 p_intf->p_vout->f_gamma -= INTF_GAMMA_STEP;
258 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
259 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
262 case 'G': /* gamma + */
263 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
265 vlc_mutex_lock( &p_intf->p_vout->change_lock );
266 p_intf->p_vout->f_gamma += INTF_GAMMA_STEP;
267 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
268 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
271 case 'c': /* toggle grayscale */
272 if( p_intf->p_vout != NULL )
274 vlc_mutex_lock( &p_intf->p_vout->change_lock );
275 p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
276 p_intf->p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
277 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
280 case ' ': /* toggle interface */
281 if( p_intf->p_vout != NULL )
283 vlc_mutex_lock( &p_intf->p_vout->change_lock );
284 p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
285 p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
286 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
289 case 'i': /* toggle info */
290 if( p_intf->p_vout != NULL )
292 vlc_mutex_lock( &p_intf->p_vout->change_lock );
293 p_intf->p_vout->b_info = !p_intf->p_vout->b_info;
294 p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
295 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
298 case 's': /* toggle scaling */
299 if( p_intf->p_vout != NULL )
301 vlc_mutex_lock( &p_intf->p_vout->change_lock );
302 p_intf->p_vout->b_scale = !p_intf->p_vout->b_scale;
303 p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
304 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
307 default: /* unknown key */
314 /* following functions are local */
316 /*****************************************************************************
317 * LoadChannels: load channels description from a file
318 *****************************************************************************
319 * This structe describes all interface-specific data of the main (interface)
321 * Each line of the file is a semicolon separated list of the following
323 * integer channel number
324 * string channel description
325 * integer input method (see input.h)
326 * string input source
329 * The last field must end with a semicolon.
330 * Comments and empty lines are not explicitely allowed, but lines with parsing
331 * errors are ignored without warning.
332 *****************************************************************************/
333 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
335 FILE * p_file; /* file */
336 intf_channel_t * p_channel; /* current channel */
337 char psz_line[INTF_MAX_CMD_SIZE]; /* line buffer */
338 int i_index; /* channel or field index */
340 /* Set default value */
341 p_intf->p_channel = NULL;
344 p_file = fopen( psz_filename, "r" );
347 intf_ErrMsg("error: can't open %s (%s)\n", psz_filename, strerror(errno));
351 /* First pass: count number of lines */
352 for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
359 /* Allocate array and rewind - some of the lines may be invalid, and the
360 * array will probably be larger than the actual number of channels, but
361 * it has no consequence. */
362 p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
363 if( p_intf->p_channel == NULL )
365 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
369 p_channel = p_intf->p_channel;
372 /* Second pass: read channels descriptions */
373 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
375 if( !ParseChannel( p_channel, psz_line ) )
377 intf_DbgMsg("channel [%d] %s : method %d (%s:%d vlan %d)\n",
378 p_channel->i_channel, p_channel->psz_description,
379 p_channel->i_input_method, p_channel->psz_input_source,
380 p_channel->i_input_port, p_channel->i_input_vlan );
385 /* Add marker at the end of the array */
386 p_channel->i_channel = -1;
394 /*****************************************************************************
395 * UnloadChannels: unload channels description
396 *****************************************************************************
397 * This function free all resources allocated by LoadChannels, if any.
398 *****************************************************************************/
399 static void UnloadChannels( intf_thread_t *p_intf )
401 int i_channel; /* channel index */
403 if( p_intf->p_channel != NULL )
405 /* Free allocated strings */
407 p_intf->p_channel[ i_channel ].i_channel != -1;
410 if( p_intf->p_channel[ i_channel ].psz_description != NULL )
412 free( p_intf->p_channel[ i_channel ].psz_description );
414 if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
416 free( p_intf->p_channel[ i_channel ].psz_input_source );
421 free( p_intf->p_channel );
422 p_intf->p_channel = NULL;
427 /*****************************************************************************
428 * ParseChannel: parse a channel description line
429 *****************************************************************************
430 * See LoadChannels. This function return non 0 on parsing error.
431 *****************************************************************************/
432 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
434 char * psz_index; /* current character */
435 char * psz_end; /* end pointer for strtol */
436 int i_field; /* field number, -1 on error */
437 int i_field_length; /* field length, for text fields */
439 /* Set some default fields */
440 p_channel->i_channel = 0;
441 p_channel->psz_description = NULL;
442 p_channel->i_input_method = 0;
443 p_channel->psz_input_source = NULL;
444 p_channel->i_input_port = 0;
445 p_channel->i_input_vlan = 0;
449 for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
451 if( *psz_index == ';' )
453 /* Mark end of field */
459 case 0: /* channel number */
460 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
461 if( (*psz_str == '\0') || (*psz_end != '\0') )
466 case 1: /* channel description */
467 i_field_length = strlen( psz_str );
468 if( i_field_length != 0 )
470 p_channel->psz_description = malloc( i_field_length + 1 );
471 if( p_channel->psz_description == NULL )
473 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
478 strcpy( p_channel->psz_description, psz_str );
482 case 2: /* input method */
483 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
484 if( (*psz_str == '\0') || (*psz_end != '\0') )
489 case 3: /* input source */
490 i_field_length = strlen( psz_str );
491 if( i_field_length != 0 )
493 p_channel->psz_input_source = malloc( i_field_length + 1 );
494 if( p_channel->psz_input_source == NULL )
496 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
501 strcpy( p_channel->psz_input_source, psz_str );
505 case 4: /* input port */
506 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
507 if( (*psz_str == '\0') || (*psz_end != '\0') )
512 case 5: /* input vlan */
513 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
514 if( (*psz_str == '\0') || (*psz_end != '\0') )
519 /* ... following fields are ignored */
522 /* Set new beginning of field */
523 psz_str = psz_index + 1;
527 /* At least the first three fields must be parsed sucessfully for function
528 * success. Other parsing errors are returned using i_field = -1. */
531 /* Function fails. Free allocated strings */
532 if( p_channel->psz_description != NULL )
534 free( p_channel->psz_description );
536 if( p_channel->psz_input_source != NULL )
538 free( p_channel->psz_input_source );