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;
73 /* Allocate structure */
74 p_intf = malloc( sizeof( intf_thread_t ) );
77 intf_ErrMsg("error: %s\n", strerror( ENOMEM ) );
81 /* initialize method-dependent functions */
82 psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
84 if( !strcmp(psz_method, "dummy") )
86 p_intf->p_sys_create = intf_DummySysCreate;
87 p_intf->p_sys_manage = intf_DummySysManage;
88 p_intf->p_sys_destroy = intf_DummySysDestroy;
91 else if( !strcmp(psz_method, "x11") )
93 p_intf->p_sys_create = intf_X11SysCreate;
94 p_intf->p_sys_manage = intf_X11SysManage;
95 p_intf->p_sys_destroy = intf_X11SysDestroy;
99 else if( !strcmp(psz_method, "fb") )
101 p_intf->p_sys_create = intf_FBSysCreate;
102 p_intf->p_sys_manage = intf_FBSysManage;
103 p_intf->p_sys_destroy = intf_FBSysDestroy;
107 else if( !strcmp(psz_method, "ggi") )
109 p_intf->p_sys_create = intf_GGISysCreate;
110 p_intf->p_sys_manage = intf_GGISysManage;
111 p_intf->p_sys_destroy = intf_GGISysDestroy;
115 else if( !strcmp(psz_method, "dga") )
117 p_intf->p_sys_create = intf_DGASysCreate;
118 p_intf->p_sys_manage = intf_DGASysManage;
119 p_intf->p_sys_destroy = intf_DGASysDestroy;
123 else if( !strcmp(psz_method, "glide") )
125 p_intf->p_sys_create = intf_GlideSysCreate;
126 p_intf->p_sys_manage = intf_GlideSysManage;
127 p_intf->p_sys_destroy = intf_GlideSysDestroy;
131 else if( !strcmp(psz_method, "beos") )
133 p_intf->p_sys_create = intf_BSysCreate;
134 p_intf->p_sys_manage = intf_BSysManage;
135 p_intf->p_sys_destroy = intf_BSysDestroy;
140 intf_ErrMsg( "error: requested video output method not available\n" );
145 /* Initialize structure */
147 p_intf->p_vout = NULL;
148 p_intf->p_input = NULL;
150 /* Load channels - the pointer will be set to NULL on failure. The
151 * return value is ignored since the program can work without
153 LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
155 /* Start interfaces */
156 p_intf->p_console = intf_ConsoleCreate();
157 if( p_intf->p_console == NULL )
159 intf_ErrMsg("error: can't create control console\n");
163 if( p_intf->p_sys_create( p_intf ) )
165 intf_ErrMsg("error: can't create interface\n");
166 intf_ConsoleDestroy( p_intf->p_console );
171 intf_Msg("Interface initialized\n");
175 /*****************************************************************************
177 *****************************************************************************
178 * Initialization script and main interface loop.
179 *****************************************************************************/
180 void intf_Run( intf_thread_t *p_intf )
182 /* Execute the initialization script - if a positive number is returned,
183 * the script could be executed but failed */
184 if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
186 intf_ErrMsg("warning: error(s) during startup script\n");
190 while(!p_intf->b_die)
192 /* Flush waiting messages */
195 /* Manage specific interface */
196 p_intf->p_sys_manage( p_intf );
198 /* Check attached threads status */
199 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
201 //?? add aout error detection
204 if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
206 input_DestroyThread( p_intf->p_input, NULL );
207 p_intf->p_input = NULL;
208 intf_DbgMsg("Input thread destroyed\n");
211 /* Sleep to avoid using all CPU - since some interfaces needs to access
212 * keyboard events, a 100ms delay is a good compromise */
213 msleep( INTF_IDLE_SLEEP );
217 /*****************************************************************************
218 * intf_Destroy: clean interface after main loop
219 *****************************************************************************
220 * This function destroys specific interfaces and close output devices.
221 *****************************************************************************/
222 void intf_Destroy( intf_thread_t *p_intf )
224 /* Destroy interfaces */
225 p_intf->p_sys_destroy( p_intf );
226 intf_ConsoleDestroy( p_intf->p_console );
228 /* Unload channels */
229 UnloadChannels( p_intf );
235 /*****************************************************************************
236 * intf_SelectChannel: change channel
237 *****************************************************************************
238 * Kill existing input, if any, and try to open a new one, using an input
239 * configuration table.
240 *****************************************************************************/
241 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
243 intf_channel_t * p_channel; /* channel */
245 /* Look for channel in array */
246 if( p_intf->p_channel != NULL )
248 for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
250 if( p_channel->i_channel == i_channel )
256 /* Kill existing input, if any */
257 if( p_intf->p_input != NULL )
259 input_DestroyThread( p_intf->p_input, NULL );
262 intf_Msg("Channel %d: %s\n", i_channel, p_channel->psz_description );
264 /* Open a new input */
265 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
266 p_channel->i_input_port, p_channel->i_input_vlan,
267 p_intf->p_vout, p_main->p_aout, NULL );
268 return( p_intf->p_input == NULL );
273 /* Channel does not exist */
274 intf_Msg("Channel %d does not exist\n", i_channel );
278 /*****************************************************************************
279 * intf_ProcessKey: process standard keys
280 *****************************************************************************
281 * This function will process standard keys and return non 0 if the key was
283 *****************************************************************************/
284 int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
288 case 'Q': /* quit order */
290 case 27: /* escape key */
294 case '0': /* source change */
304 /* Change channel - return code is ignored since SelectChannel displays
305 * its own error messages */
306 intf_SelectChannel( p_intf, i_key - '0' );
308 case '+': /* volume + */
311 case '-': /* volume - */
314 case 'M': /* toggle mute */
318 case 'g': /* gamma - */
319 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
321 vlc_mutex_lock( &p_intf->p_vout->change_lock );
322 p_intf->p_vout->f_gamma -= INTF_GAMMA_STEP;
323 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
324 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
327 case 'G': /* gamma + */
328 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
330 vlc_mutex_lock( &p_intf->p_vout->change_lock );
331 p_intf->p_vout->f_gamma += INTF_GAMMA_STEP;
332 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
333 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
336 case 'c': /* toggle grayscale */
337 if( p_intf->p_vout != NULL )
339 vlc_mutex_lock( &p_intf->p_vout->change_lock );
340 p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
341 p_intf->p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
342 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
345 case ' ': /* toggle interface */
346 if( p_intf->p_vout != NULL )
348 vlc_mutex_lock( &p_intf->p_vout->change_lock );
349 p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
350 p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
351 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
354 case 'i': /* toggle info */
355 if( p_intf->p_vout != NULL )
357 vlc_mutex_lock( &p_intf->p_vout->change_lock );
358 p_intf->p_vout->b_info = !p_intf->p_vout->b_info;
359 p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
360 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
363 case 's': /* toggle scaling */
364 if( p_intf->p_vout != NULL )
366 vlc_mutex_lock( &p_intf->p_vout->change_lock );
367 p_intf->p_vout->b_scale = !p_intf->p_vout->b_scale;
368 p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
369 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
372 default: /* unknown key */
379 /* following functions are local */
381 /*****************************************************************************
382 * LoadChannels: load channels description from a file
383 *****************************************************************************
384 * This structe describes all interface-specific data of the main (interface)
386 * Each line of the file is a semicolon separated list of the following
388 * integer channel number
389 * string channel description
390 * integer input method (see input.h)
391 * string input source
394 * The last field must end with a semicolon.
395 * Comments and empty lines are not explicitely allowed, but lines with parsing
396 * errors are ignored without warning.
397 *****************************************************************************/
398 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
400 FILE * p_file; /* file */
401 intf_channel_t * p_channel; /* current channel */
402 char psz_line[INTF_MAX_CMD_SIZE]; /* line buffer */
403 int i_index; /* channel or field index */
405 /* Set default value */
406 p_intf->p_channel = NULL;
409 p_file = fopen( psz_filename, "r" );
412 intf_ErrMsg("error: can't open %s (%s)\n", psz_filename, strerror(errno));
416 /* First pass: count number of lines */
417 for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
424 /* Allocate array and rewind - some of the lines may be invalid, and the
425 * array will probably be larger than the actual number of channels, but
426 * it has no consequence. */
427 p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
428 if( p_intf->p_channel == NULL )
430 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
434 p_channel = p_intf->p_channel;
437 /* Second pass: read channels descriptions */
438 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
440 if( !ParseChannel( p_channel, psz_line ) )
442 intf_DbgMsg("channel [%d] %s : method %d (%s:%d vlan %d)\n",
443 p_channel->i_channel, p_channel->psz_description,
444 p_channel->i_input_method, p_channel->psz_input_source,
445 p_channel->i_input_port, p_channel->i_input_vlan );
450 /* Add marker at the end of the array */
451 p_channel->i_channel = -1;
459 /*****************************************************************************
460 * UnloadChannels: unload channels description
461 *****************************************************************************
462 * This function free all resources allocated by LoadChannels, if any.
463 *****************************************************************************/
464 static void UnloadChannels( intf_thread_t *p_intf )
466 int i_channel; /* channel index */
468 if( p_intf->p_channel != NULL )
470 /* Free allocated strings */
472 p_intf->p_channel[ i_channel ].i_channel != -1;
475 if( p_intf->p_channel[ i_channel ].psz_description != NULL )
477 free( p_intf->p_channel[ i_channel ].psz_description );
479 if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
481 free( p_intf->p_channel[ i_channel ].psz_input_source );
486 free( p_intf->p_channel );
487 p_intf->p_channel = NULL;
492 /*****************************************************************************
493 * ParseChannel: parse a channel description line
494 *****************************************************************************
495 * See LoadChannels. This function return non 0 on parsing error.
496 *****************************************************************************/
497 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
499 char * psz_index; /* current character */
500 char * psz_end; /* end pointer for strtol */
501 int i_field; /* field number, -1 on error */
502 int i_field_length; /* field length, for text fields */
504 /* Set some default fields */
505 p_channel->i_channel = 0;
506 p_channel->psz_description = NULL;
507 p_channel->i_input_method = 0;
508 p_channel->psz_input_source = NULL;
509 p_channel->i_input_port = 0;
510 p_channel->i_input_vlan = 0;
514 for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
516 if( *psz_index == ';' )
518 /* Mark end of field */
524 case 0: /* channel number */
525 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
526 if( (*psz_str == '\0') || (*psz_end != '\0') )
531 case 1: /* channel description */
532 i_field_length = strlen( psz_str );
533 if( i_field_length != 0 )
535 p_channel->psz_description = malloc( i_field_length + 1 );
536 if( p_channel->psz_description == NULL )
538 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
543 strcpy( p_channel->psz_description, psz_str );
547 case 2: /* input method */
548 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
549 if( (*psz_str == '\0') || (*psz_end != '\0') )
554 case 3: /* input source */
555 i_field_length = strlen( psz_str );
556 if( i_field_length != 0 )
558 p_channel->psz_input_source = malloc( i_field_length + 1 );
559 if( p_channel->psz_input_source == NULL )
561 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
566 strcpy( p_channel->psz_input_source, psz_str );
570 case 4: /* input port */
571 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
572 if( (*psz_str == '\0') || (*psz_end != '\0') )
577 case 5: /* input vlan */
578 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
579 if( (*psz_str == '\0') || (*psz_end != '\0') )
584 /* ... following fields are ignored */
587 /* Set new beginning of field */
588 psz_str = psz_index + 1;
592 /* At least the first three fields must be parsed sucessfully for function
593 * success. Other parsing errors are returned using i_field = -1. */
596 /* Function fails. Free allocated strings */
597 if( p_channel->psz_description != NULL )
599 free( p_channel->psz_description );
601 if( p_channel->psz_input_source != NULL )
603 free( p_channel->psz_input_source );