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
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.
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 GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <stdlib.h> /* free(), strtol() */
31 #include <stdio.h> /* FILE */
32 #include <string.h> /* strerror() */
33 #include <sys/types.h> /* on BSD, uio.h needs types.h */
34 #include <sys/uio.h> /* for input.h */
44 #include "interface.h"
46 #include "intf_console.h"
49 #include "video_output.h"
53 /*****************************************************************************
54 * intf_channel_t: channel description
55 *****************************************************************************
56 * A 'channel' is a descriptor of an input method. It is used to switch easily
57 * from source to source without having to specify the whole input thread
58 * configuration. The channels array, stored in the interface thread object, is
59 * loaded in intf_Create, and unloaded in intf_Destroy.
60 *****************************************************************************/
61 typedef struct intf_channel_s
63 /* Channel description */
64 int i_channel; /* channel number, -1 for end of array */
65 char * psz_description; /* channel description (owned) */
67 /* Input configuration */
68 int i_input_method; /* input method descriptor */
69 char * psz_input_source; /* source string (owned) */
70 int i_input_port; /* port */
71 int i_input_vlan; /* vlan */
74 /*****************************************************************************
76 *****************************************************************************/
77 static int LoadChannels ( intf_thread_t *p_intf, char *psz_filename );
78 static void UnloadChannels ( intf_thread_t *p_intf );
79 static int ParseChannel ( intf_channel_t *p_channel, char *psz_str );
81 /*****************************************************************************
82 * intf_Create: prepare interface before main loop
83 *****************************************************************************
84 * This function opens output devices and creates specific interfaces. It sends
85 * its own error messages.
86 *****************************************************************************/
87 intf_thread_t* intf_Create( void )
89 intf_thread_t *p_intf;
92 /* Allocate structure */
93 p_intf = malloc( sizeof( intf_thread_t ) );
96 intf_ErrMsg("error: %s\n", strerror( ENOMEM ) );
100 /* Request an interface plugin */
101 psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
103 if( RequestPlugin( &p_intf->intf_plugin, "intf", psz_method ) < 0 )
105 intf_ErrMsg( "error: could not open interface plugin intf_%s.so\n", psz_method );
111 p_intf->p_sys_create = GetPluginFunction( p_intf->intf_plugin, "intf_SysCreate" );
112 p_intf->p_sys_manage = GetPluginFunction( p_intf->intf_plugin, "intf_SysManage" );
113 p_intf->p_sys_destroy = GetPluginFunction( p_intf->intf_plugin, "intf_SysDestroy" );
115 /* Initialize structure */
117 p_intf->p_vout = NULL;
118 p_intf->p_input = NULL;
120 /* Load channels - the pointer will be set to NULL on failure. The
121 * return value is ignored since the program can work without
123 LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
125 /* Start interfaces */
126 p_intf->p_console = intf_ConsoleCreate();
127 if( p_intf->p_console == NULL )
129 intf_ErrMsg("error: can't create control console\n");
130 TrashPlugin( p_intf->intf_plugin );
134 if( p_intf->p_sys_create( p_intf ) )
136 intf_ErrMsg("error: can't create interface\n");
137 intf_ConsoleDestroy( p_intf->p_console );
138 TrashPlugin( p_intf->intf_plugin );
143 intf_Msg("Interface initialized\n");
147 /*****************************************************************************
149 *****************************************************************************
150 * Initialization script and main interface loop.
151 *****************************************************************************/
152 void intf_Run( intf_thread_t *p_intf )
154 /* Execute the initialization script - if a positive number is returned,
155 * the script could be executed but failed */
156 if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
158 intf_ErrMsg("warning: error(s) during startup script\n");
162 while(!p_intf->b_die)
164 /* Flush waiting messages */
167 /* Manage specific interface */
168 p_intf->p_sys_manage( p_intf );
170 /* Check attached threads status */
171 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
173 /* FIXME: add aout error detection ?? */
176 if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
178 input_DestroyThread( p_intf->p_input, NULL );
179 p_intf->p_input = NULL;
180 intf_DbgMsg("Input thread destroyed\n");
183 /* Sleep to avoid using all CPU - since some interfaces needs to access
184 * keyboard events, a 100ms delay is a good compromise */
185 msleep( INTF_IDLE_SLEEP );
189 /*****************************************************************************
190 * intf_Destroy: clean interface after main loop
191 *****************************************************************************
192 * This function destroys specific interfaces and close output devices.
193 *****************************************************************************/
194 void intf_Destroy( intf_thread_t *p_intf )
196 /* Destroy interfaces */
197 p_intf->p_sys_destroy( p_intf );
198 intf_ConsoleDestroy( p_intf->p_console );
200 /* Unload channels */
201 UnloadChannels( p_intf );
204 TrashPlugin( p_intf->intf_plugin );
210 /*****************************************************************************
211 * intf_SelectChannel: change channel
212 *****************************************************************************
213 * Kill existing input, if any, and try to open a new one, using an input
214 * configuration table.
215 *****************************************************************************/
216 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
218 intf_channel_t * p_channel; /* channel */
220 /* Look for channel in array */
221 if( p_intf->p_channel != NULL )
223 for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
225 if( p_channel->i_channel == i_channel )
231 /* Kill existing input, if any */
232 if( p_intf->p_input != NULL )
234 input_DestroyThread( p_intf->p_input, NULL );
237 intf_Msg("Channel %d: %s\n", i_channel, p_channel->psz_description );
239 /* Open a new input */
240 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
241 p_channel->i_input_port, p_channel->i_input_vlan,
242 p_intf->p_vout, p_main->p_aout, NULL );
243 return( p_intf->p_input == NULL );
248 /* Channel does not exist */
249 intf_Msg("Channel %d does not exist\n", i_channel );
253 /*****************************************************************************
254 * intf_ProcessKey: process standard keys
255 *****************************************************************************
256 * This function will process standard keys and return non 0 if the key was
258 *****************************************************************************/
259 int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
263 case 'Q': /* quit order */
265 case 27: /* escape key */
269 case '0': /* source change */
279 /* Change channel - return code is ignored since SelectChannel displays
280 * its own error messages */
281 intf_SelectChannel( p_intf, i_key - '0' );
283 case '+': /* volume + */
286 case '-': /* volume - */
289 case 'M': /* toggle mute */
293 case 'g': /* gamma - */
294 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
296 vlc_mutex_lock( &p_intf->p_vout->change_lock );
297 p_intf->p_vout->f_gamma -= INTF_GAMMA_STEP;
298 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
299 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
302 case 'G': /* gamma + */
303 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
305 vlc_mutex_lock( &p_intf->p_vout->change_lock );
306 p_intf->p_vout->f_gamma += INTF_GAMMA_STEP;
307 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
308 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
311 case 'c': /* toggle grayscale */
312 if( p_intf->p_vout != NULL )
314 vlc_mutex_lock( &p_intf->p_vout->change_lock );
315 p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
316 p_intf->p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
317 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
320 case ' ': /* toggle interface */
321 if( p_intf->p_vout != NULL )
323 vlc_mutex_lock( &p_intf->p_vout->change_lock );
324 p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
325 p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
326 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
329 case 'i': /* toggle info */
330 if( p_intf->p_vout != NULL )
332 vlc_mutex_lock( &p_intf->p_vout->change_lock );
333 p_intf->p_vout->b_info = !p_intf->p_vout->b_info;
334 p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
335 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
338 case 's': /* toggle scaling */
339 if( p_intf->p_vout != NULL )
341 vlc_mutex_lock( &p_intf->p_vout->change_lock );
342 p_intf->p_vout->b_scale = !p_intf->p_vout->b_scale;
343 p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
344 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
347 default: /* unknown key */
354 /* following functions are local */
356 /*****************************************************************************
357 * LoadChannels: load channels description from a file
358 *****************************************************************************
359 * This structe describes all interface-specific data of the main (interface)
361 * Each line of the file is a semicolon separated list of the following
363 * integer channel number
364 * string channel description
365 * integer input method (see input.h)
366 * string input source
369 * The last field must end with a semicolon.
370 * Comments and empty lines are not explicitely allowed, but lines with parsing
371 * errors are ignored without warning.
372 *****************************************************************************/
373 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
375 FILE * p_file; /* file */
376 intf_channel_t * p_channel; /* current channel */
377 char psz_line[INTF_MAX_CMD_SIZE]; /* line buffer */
378 int i_index; /* channel or field index */
380 /* Set default value */
381 p_intf->p_channel = NULL;
384 p_file = fopen( psz_filename, "r" );
387 intf_ErrMsg("error: can't open %s (%s)\n", psz_filename, strerror(errno));
391 /* First pass: count number of lines */
392 for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
399 /* Allocate array and rewind - some of the lines may be invalid, and the
400 * array will probably be larger than the actual number of channels, but
401 * it has no consequence. */
402 p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
403 if( p_intf->p_channel == NULL )
405 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
409 p_channel = p_intf->p_channel;
412 /* Second pass: read channels descriptions */
413 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
415 if( !ParseChannel( p_channel, psz_line ) )
417 intf_DbgMsg("channel [%d] %s : method %d (%s:%d vlan %d)\n",
418 p_channel->i_channel, p_channel->psz_description,
419 p_channel->i_input_method, p_channel->psz_input_source,
420 p_channel->i_input_port, p_channel->i_input_vlan );
425 /* Add marker at the end of the array */
426 p_channel->i_channel = -1;
434 /*****************************************************************************
435 * UnloadChannels: unload channels description
436 *****************************************************************************
437 * This function free all resources allocated by LoadChannels, if any.
438 *****************************************************************************/
439 static void UnloadChannels( intf_thread_t *p_intf )
441 int i_channel; /* channel index */
443 if( p_intf->p_channel != NULL )
445 /* Free allocated strings */
447 p_intf->p_channel[ i_channel ].i_channel != -1;
450 if( p_intf->p_channel[ i_channel ].psz_description != NULL )
452 free( p_intf->p_channel[ i_channel ].psz_description );
454 if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
456 free( p_intf->p_channel[ i_channel ].psz_input_source );
461 free( p_intf->p_channel );
462 p_intf->p_channel = NULL;
467 /*****************************************************************************
468 * ParseChannel: parse a channel description line
469 *****************************************************************************
470 * See LoadChannels. This function return non 0 on parsing error.
471 *****************************************************************************/
472 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
474 char * psz_index; /* current character */
475 char * psz_end; /* end pointer for strtol */
476 int i_field; /* field number, -1 on error */
477 int i_field_length; /* field length, for text fields */
479 /* Set some default fields */
480 p_channel->i_channel = 0;
481 p_channel->psz_description = NULL;
482 p_channel->i_input_method = 0;
483 p_channel->psz_input_source = NULL;
484 p_channel->i_input_port = 0;
485 p_channel->i_input_vlan = 0;
489 for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
491 if( *psz_index == ';' )
493 /* Mark end of field */
499 case 0: /* channel number */
500 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
501 if( (*psz_str == '\0') || (*psz_end != '\0') )
506 case 1: /* channel description */
507 i_field_length = strlen( psz_str );
508 if( i_field_length != 0 )
510 p_channel->psz_description = malloc( i_field_length + 1 );
511 if( p_channel->psz_description == NULL )
513 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
518 strcpy( p_channel->psz_description, psz_str );
522 case 2: /* input method */
523 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
524 if( (*psz_str == '\0') || (*psz_end != '\0') )
529 case 3: /* input source */
530 i_field_length = strlen( psz_str );
531 if( i_field_length != 0 )
533 p_channel->psz_input_source = malloc( i_field_length + 1 );
534 if( p_channel->psz_input_source == NULL )
536 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
541 strcpy( p_channel->psz_input_source, psz_str );
545 case 4: /* input port */
546 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
547 if( (*psz_str == '\0') || (*psz_end != '\0') )
552 case 5: /* input vlan */
553 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
554 if( (*psz_str == '\0') || (*psz_end != '\0') )
559 /* ... following fields are ignored */
562 /* Set new beginning of field */
563 psz_str = psz_index + 1;
567 /* At least the first three fields must be parsed sucessfully for function
568 * success. Other parsing errors are returned using i_field = -1. */
571 /* Function fails. Free allocated strings */
572 if( p_channel->psz_description != NULL )
574 free( p_channel->psz_description );
576 if( p_channel->psz_input_source != NULL )
578 free( p_channel->psz_input_source );