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 */
19 #include <dlfcn.h> /* plugins */
24 #include "vlc_thread.h"
27 #include "interface.h"
29 #include "intf_console.h"
32 #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 creates specific interfaces. It sends
66 * its own error messages.
67 *****************************************************************************/
68 intf_thread_t* intf_Create( void )
70 intf_thread_t *p_intf;
74 /* Allocate structure */
75 p_intf = malloc( sizeof( intf_thread_t ) );
78 intf_ErrMsg("error: %s\n", strerror( ENOMEM ) );
82 /* Initialize method-dependent functions */
83 psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
85 psz_plugin = malloc( sizeof("./interface/intf_.so") + strlen(psz_method) );
86 sprintf( psz_plugin, "./interface/intf_%s.so", psz_method );
88 p_intf->p_intf_plugin = dlopen( psz_plugin, RTLD_NOW | RTLD_GLOBAL );
90 if( p_intf->p_intf_plugin == NULL )
92 intf_ErrMsg( "error: could not open interface plugin %s\n", psz_plugin );
100 p_intf->p_sys_create = dlsym(p_intf->p_intf_plugin, "intf_SysCreate");
101 p_intf->p_sys_manage = dlsym(p_intf->p_intf_plugin, "intf_SysManage");
102 p_intf->p_sys_destroy = dlsym(p_intf->p_intf_plugin, "intf_SysDestroy");
104 /* Initialize structure */
106 p_intf->p_vout = NULL;
107 p_intf->p_input = NULL;
109 /* Load channels - the pointer will be set to NULL on failure. The
110 * return value is ignored since the program can work without
112 LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
114 /* Start interfaces */
115 p_intf->p_console = intf_ConsoleCreate();
116 if( p_intf->p_console == NULL )
118 intf_ErrMsg("error: can't create control console\n");
119 dlclose( p_intf->p_intf_plugin );
123 if( p_intf->p_sys_create( p_intf ) )
125 intf_ErrMsg("error: can't create interface\n");
126 intf_ConsoleDestroy( p_intf->p_console );
127 dlclose( p_intf->p_intf_plugin );
132 intf_Msg("Interface initialized\n");
136 /*****************************************************************************
138 *****************************************************************************
139 * Initialization script and main interface loop.
140 *****************************************************************************/
141 void intf_Run( intf_thread_t *p_intf )
143 /* Execute the initialization script - if a positive number is returned,
144 * the script could be executed but failed */
145 if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
147 intf_ErrMsg("warning: error(s) during startup script\n");
151 while(!p_intf->b_die)
153 /* Flush waiting messages */
156 /* Manage specific interface */
157 p_intf->p_sys_manage( p_intf );
159 /* Check attached threads status */
160 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
162 /* FIXME: add aout error detection ?? */
165 if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
167 input_DestroyThread( p_intf->p_input, NULL );
168 p_intf->p_input = NULL;
169 intf_DbgMsg("Input thread destroyed\n");
172 /* Sleep to avoid using all CPU - since some interfaces needs to access
173 * keyboard events, a 100ms delay is a good compromise */
174 msleep( INTF_IDLE_SLEEP );
178 /*****************************************************************************
179 * intf_Destroy: clean interface after main loop
180 *****************************************************************************
181 * This function destroys specific interfaces and close output devices.
182 *****************************************************************************/
183 void intf_Destroy( intf_thread_t *p_intf )
185 /* Destroy interfaces */
186 p_intf->p_sys_destroy( p_intf );
187 intf_ConsoleDestroy( p_intf->p_console );
189 /* Unload channels */
190 UnloadChannels( p_intf );
193 dlclose( p_intf->p_intf_plugin );
199 /*****************************************************************************
200 * intf_SelectChannel: change channel
201 *****************************************************************************
202 * Kill existing input, if any, and try to open a new one, using an input
203 * configuration table.
204 *****************************************************************************/
205 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
207 intf_channel_t * p_channel; /* channel */
209 /* Look for channel in array */
210 if( p_intf->p_channel != NULL )
212 for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
214 if( p_channel->i_channel == i_channel )
220 /* Kill existing input, if any */
221 if( p_intf->p_input != NULL )
223 input_DestroyThread( p_intf->p_input, NULL );
226 intf_Msg("Channel %d: %s\n", i_channel, p_channel->psz_description );
228 /* Open a new input */
229 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
230 p_channel->i_input_port, p_channel->i_input_vlan,
231 p_intf->p_vout, p_main->p_aout, NULL );
232 return( p_intf->p_input == NULL );
237 /* Channel does not exist */
238 intf_Msg("Channel %d does not exist\n", i_channel );
242 /*****************************************************************************
243 * intf_ProcessKey: process standard keys
244 *****************************************************************************
245 * This function will process standard keys and return non 0 if the key was
247 *****************************************************************************/
248 int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
252 case 'Q': /* quit order */
254 case 27: /* escape key */
258 case '0': /* source change */
268 /* Change channel - return code is ignored since SelectChannel displays
269 * its own error messages */
270 intf_SelectChannel( p_intf, i_key - '0' );
272 case '+': /* volume + */
275 case '-': /* volume - */
278 case 'M': /* toggle mute */
282 case 'g': /* gamma - */
283 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
285 vlc_mutex_lock( &p_intf->p_vout->change_lock );
286 p_intf->p_vout->f_gamma -= INTF_GAMMA_STEP;
287 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
288 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
291 case 'G': /* gamma + */
292 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
294 vlc_mutex_lock( &p_intf->p_vout->change_lock );
295 p_intf->p_vout->f_gamma += INTF_GAMMA_STEP;
296 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
297 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
300 case 'c': /* toggle grayscale */
301 if( p_intf->p_vout != NULL )
303 vlc_mutex_lock( &p_intf->p_vout->change_lock );
304 p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
305 p_intf->p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
306 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
309 case ' ': /* toggle interface */
310 if( p_intf->p_vout != NULL )
312 vlc_mutex_lock( &p_intf->p_vout->change_lock );
313 p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
314 p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
315 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
318 case 'i': /* toggle info */
319 if( p_intf->p_vout != NULL )
321 vlc_mutex_lock( &p_intf->p_vout->change_lock );
322 p_intf->p_vout->b_info = !p_intf->p_vout->b_info;
323 p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
324 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
327 case 's': /* toggle scaling */
328 if( p_intf->p_vout != NULL )
330 vlc_mutex_lock( &p_intf->p_vout->change_lock );
331 p_intf->p_vout->b_scale = !p_intf->p_vout->b_scale;
332 p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
333 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
336 default: /* unknown key */
343 /* following functions are local */
345 /*****************************************************************************
346 * LoadChannels: load channels description from a file
347 *****************************************************************************
348 * This structe describes all interface-specific data of the main (interface)
350 * Each line of the file is a semicolon separated list of the following
352 * integer channel number
353 * string channel description
354 * integer input method (see input.h)
355 * string input source
358 * The last field must end with a semicolon.
359 * Comments and empty lines are not explicitely allowed, but lines with parsing
360 * errors are ignored without warning.
361 *****************************************************************************/
362 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
364 FILE * p_file; /* file */
365 intf_channel_t * p_channel; /* current channel */
366 char psz_line[INTF_MAX_CMD_SIZE]; /* line buffer */
367 int i_index; /* channel or field index */
369 /* Set default value */
370 p_intf->p_channel = NULL;
373 p_file = fopen( psz_filename, "r" );
376 intf_ErrMsg("error: can't open %s (%s)\n", psz_filename, strerror(errno));
380 /* First pass: count number of lines */
381 for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
388 /* Allocate array and rewind - some of the lines may be invalid, and the
389 * array will probably be larger than the actual number of channels, but
390 * it has no consequence. */
391 p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
392 if( p_intf->p_channel == NULL )
394 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
398 p_channel = p_intf->p_channel;
401 /* Second pass: read channels descriptions */
402 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
404 if( !ParseChannel( p_channel, psz_line ) )
406 intf_DbgMsg("channel [%d] %s : method %d (%s:%d vlan %d)\n",
407 p_channel->i_channel, p_channel->psz_description,
408 p_channel->i_input_method, p_channel->psz_input_source,
409 p_channel->i_input_port, p_channel->i_input_vlan );
414 /* Add marker at the end of the array */
415 p_channel->i_channel = -1;
423 /*****************************************************************************
424 * UnloadChannels: unload channels description
425 *****************************************************************************
426 * This function free all resources allocated by LoadChannels, if any.
427 *****************************************************************************/
428 static void UnloadChannels( intf_thread_t *p_intf )
430 int i_channel; /* channel index */
432 if( p_intf->p_channel != NULL )
434 /* Free allocated strings */
436 p_intf->p_channel[ i_channel ].i_channel != -1;
439 if( p_intf->p_channel[ i_channel ].psz_description != NULL )
441 free( p_intf->p_channel[ i_channel ].psz_description );
443 if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
445 free( p_intf->p_channel[ i_channel ].psz_input_source );
450 free( p_intf->p_channel );
451 p_intf->p_channel = NULL;
456 /*****************************************************************************
457 * ParseChannel: parse a channel description line
458 *****************************************************************************
459 * See LoadChannels. This function return non 0 on parsing error.
460 *****************************************************************************/
461 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
463 char * psz_index; /* current character */
464 char * psz_end; /* end pointer for strtol */
465 int i_field; /* field number, -1 on error */
466 int i_field_length; /* field length, for text fields */
468 /* Set some default fields */
469 p_channel->i_channel = 0;
470 p_channel->psz_description = NULL;
471 p_channel->i_input_method = 0;
472 p_channel->psz_input_source = NULL;
473 p_channel->i_input_port = 0;
474 p_channel->i_input_vlan = 0;
478 for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
480 if( *psz_index == ';' )
482 /* Mark end of field */
488 case 0: /* channel number */
489 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
490 if( (*psz_str == '\0') || (*psz_end != '\0') )
495 case 1: /* channel description */
496 i_field_length = strlen( psz_str );
497 if( i_field_length != 0 )
499 p_channel->psz_description = malloc( i_field_length + 1 );
500 if( p_channel->psz_description == NULL )
502 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
507 strcpy( p_channel->psz_description, psz_str );
511 case 2: /* input method */
512 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
513 if( (*psz_str == '\0') || (*psz_end != '\0') )
518 case 3: /* input source */
519 i_field_length = strlen( psz_str );
520 if( i_field_length != 0 )
522 p_channel->psz_input_source = malloc( i_field_length + 1 );
523 if( p_channel->psz_input_source == NULL )
525 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
530 strcpy( p_channel->psz_input_source, psz_str );
534 case 4: /* input port */
535 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
536 if( (*psz_str == '\0') || (*psz_end != '\0') )
541 case 5: /* input vlan */
542 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
543 if( (*psz_str == '\0') || (*psz_end != '\0') )
548 /* ... following fields are ignored */
551 /* Set new beginning of field */
552 psz_str = psz_index + 1;
556 /* At least the first three fields must be parsed sucessfully for function
557 * success. Other parsing errors are returned using i_field = -1. */
560 /* Function fails. Free allocated strings */
561 if( p_channel->psz_description != NULL )
563 free( p_channel->psz_description );
565 if( p_channel->psz_input_source != NULL )
567 free( p_channel->psz_input_source );