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 );
102 p_intf->p_intf_plugin = RequestPlugin( "intf", psz_method );
104 if( !p_intf->p_intf_plugin )
106 intf_ErrMsg( "error: could not open interface plugin intf_%s.so\n", psz_method );
112 p_intf->p_sys_create = GetPluginFunction( p_intf->p_intf_plugin, "intf_SysCreate" );
113 p_intf->p_sys_manage = GetPluginFunction( p_intf->p_intf_plugin, "intf_SysManage" );
114 p_intf->p_sys_destroy = GetPluginFunction( p_intf->p_intf_plugin, "intf_SysDestroy" );
116 /* Initialize structure */
118 p_intf->p_vout = NULL;
119 p_intf->p_input = NULL;
121 /* Load channels - the pointer will be set to NULL on failure. The
122 * return value is ignored since the program can work without
124 LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
126 /* Start interfaces */
127 p_intf->p_console = intf_ConsoleCreate();
128 if( p_intf->p_console == NULL )
130 intf_ErrMsg("error: can't create control console\n");
131 TrashPlugin( p_intf->p_intf_plugin );
135 if( p_intf->p_sys_create( p_intf ) )
137 intf_ErrMsg("error: can't create interface\n");
138 intf_ConsoleDestroy( p_intf->p_console );
139 TrashPlugin( p_intf->p_intf_plugin );
144 intf_Msg("Interface initialized\n");
148 /*****************************************************************************
150 *****************************************************************************
151 * Initialization script and main interface loop.
152 *****************************************************************************/
153 void intf_Run( intf_thread_t *p_intf )
155 /* Execute the initialization script - if a positive number is returned,
156 * the script could be executed but failed */
157 if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
159 intf_ErrMsg("warning: error(s) during startup script\n");
163 while(!p_intf->b_die)
165 /* Flush waiting messages */
168 /* Manage specific interface */
169 p_intf->p_sys_manage( p_intf );
171 /* Check attached threads status */
172 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
174 /* FIXME: add aout error detection ?? */
177 if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
179 input_DestroyThread( p_intf->p_input, NULL );
180 p_intf->p_input = NULL;
181 intf_DbgMsg("Input thread destroyed\n");
184 /* Sleep to avoid using all CPU - since some interfaces needs to access
185 * keyboard events, a 100ms delay is a good compromise */
186 msleep( INTF_IDLE_SLEEP );
190 /*****************************************************************************
191 * intf_Destroy: clean interface after main loop
192 *****************************************************************************
193 * This function destroys specific interfaces and close output devices.
194 *****************************************************************************/
195 void intf_Destroy( intf_thread_t *p_intf )
197 /* Destroy interfaces */
198 p_intf->p_sys_destroy( p_intf );
199 intf_ConsoleDestroy( p_intf->p_console );
201 /* Unload channels */
202 UnloadChannels( p_intf );
205 TrashPlugin( p_intf->p_intf_plugin );
211 /*****************************************************************************
212 * intf_SelectChannel: change channel
213 *****************************************************************************
214 * Kill existing input, if any, and try to open a new one, using an input
215 * configuration table.
216 *****************************************************************************/
217 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
219 intf_channel_t * p_channel; /* channel */
221 /* Look for channel in array */
222 if( p_intf->p_channel != NULL )
224 for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
226 if( p_channel->i_channel == i_channel )
232 /* Kill existing input, if any */
233 if( p_intf->p_input != NULL )
235 input_DestroyThread( p_intf->p_input, NULL );
238 intf_Msg("Channel %d: %s\n", i_channel, p_channel->psz_description );
240 /* Open a new input */
241 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
242 p_channel->i_input_port, p_channel->i_input_vlan,
243 p_intf->p_vout, p_main->p_aout, NULL );
244 return( p_intf->p_input == NULL );
249 /* Channel does not exist */
250 intf_Msg("Channel %d does not exist\n", i_channel );
254 /*****************************************************************************
255 * intf_ProcessKey: process standard keys
256 *****************************************************************************
257 * This function will process standard keys and return non 0 if the key was
259 *****************************************************************************/
260 int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
264 case 'Q': /* quit order */
266 case 27: /* escape key */
270 case '0': /* source change */
280 /* Change channel - return code is ignored since SelectChannel displays
281 * its own error messages */
282 intf_SelectChannel( p_intf, i_key - '0' );
284 case '+': /* volume + */
287 case '-': /* volume - */
290 case 'M': /* toggle mute */
294 case 'g': /* gamma - */
295 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
297 vlc_mutex_lock( &p_intf->p_vout->change_lock );
298 p_intf->p_vout->f_gamma -= INTF_GAMMA_STEP;
299 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
300 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
303 case 'G': /* gamma + */
304 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
306 vlc_mutex_lock( &p_intf->p_vout->change_lock );
307 p_intf->p_vout->f_gamma += INTF_GAMMA_STEP;
308 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
309 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
312 case 'c': /* toggle grayscale */
313 if( p_intf->p_vout != NULL )
315 vlc_mutex_lock( &p_intf->p_vout->change_lock );
316 p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
317 p_intf->p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
318 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
321 case ' ': /* toggle interface */
322 if( p_intf->p_vout != NULL )
324 vlc_mutex_lock( &p_intf->p_vout->change_lock );
325 p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
326 p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
327 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
330 case 'i': /* toggle info */
331 if( p_intf->p_vout != NULL )
333 vlc_mutex_lock( &p_intf->p_vout->change_lock );
334 p_intf->p_vout->b_info = !p_intf->p_vout->b_info;
335 p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
336 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
339 case 's': /* toggle scaling */
340 if( p_intf->p_vout != NULL )
342 vlc_mutex_lock( &p_intf->p_vout->change_lock );
343 p_intf->p_vout->b_scale = !p_intf->p_vout->b_scale;
344 p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
345 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
348 default: /* unknown key */
355 /* following functions are local */
357 /*****************************************************************************
358 * LoadChannels: load channels description from a file
359 *****************************************************************************
360 * This structe describes all interface-specific data of the main (interface)
362 * Each line of the file is a semicolon separated list of the following
364 * integer channel number
365 * string channel description
366 * integer input method (see input.h)
367 * string input source
370 * The last field must end with a semicolon.
371 * Comments and empty lines are not explicitely allowed, but lines with parsing
372 * errors are ignored without warning.
373 *****************************************************************************/
374 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
376 FILE * p_file; /* file */
377 intf_channel_t * p_channel; /* current channel */
378 char psz_line[INTF_MAX_CMD_SIZE]; /* line buffer */
379 int i_index; /* channel or field index */
381 /* Set default value */
382 p_intf->p_channel = NULL;
385 p_file = fopen( psz_filename, "r" );
388 intf_ErrMsg("error: can't open %s (%s)\n", psz_filename, strerror(errno));
392 /* First pass: count number of lines */
393 for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
400 /* Allocate array and rewind - some of the lines may be invalid, and the
401 * array will probably be larger than the actual number of channels, but
402 * it has no consequence. */
403 p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
404 if( p_intf->p_channel == NULL )
406 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
410 p_channel = p_intf->p_channel;
413 /* Second pass: read channels descriptions */
414 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
416 if( !ParseChannel( p_channel, psz_line ) )
418 intf_DbgMsg("channel [%d] %s : method %d (%s:%d vlan %d)\n",
419 p_channel->i_channel, p_channel->psz_description,
420 p_channel->i_input_method, p_channel->psz_input_source,
421 p_channel->i_input_port, p_channel->i_input_vlan );
426 /* Add marker at the end of the array */
427 p_channel->i_channel = -1;
435 /*****************************************************************************
436 * UnloadChannels: unload channels description
437 *****************************************************************************
438 * This function free all resources allocated by LoadChannels, if any.
439 *****************************************************************************/
440 static void UnloadChannels( intf_thread_t *p_intf )
442 int i_channel; /* channel index */
444 if( p_intf->p_channel != NULL )
446 /* Free allocated strings */
448 p_intf->p_channel[ i_channel ].i_channel != -1;
451 if( p_intf->p_channel[ i_channel ].psz_description != NULL )
453 free( p_intf->p_channel[ i_channel ].psz_description );
455 if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
457 free( p_intf->p_channel[ i_channel ].psz_input_source );
462 free( p_intf->p_channel );
463 p_intf->p_channel = NULL;
468 /*****************************************************************************
469 * ParseChannel: parse a channel description line
470 *****************************************************************************
471 * See LoadChannels. This function return non 0 on parsing error.
472 *****************************************************************************/
473 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
475 char * psz_index; /* current character */
476 char * psz_end; /* end pointer for strtol */
477 int i_field; /* field number, -1 on error */
478 int i_field_length; /* field length, for text fields */
480 /* Set some default fields */
481 p_channel->i_channel = 0;
482 p_channel->psz_description = NULL;
483 p_channel->i_input_method = 0;
484 p_channel->psz_input_source = NULL;
485 p_channel->i_input_port = 0;
486 p_channel->i_input_vlan = 0;
490 for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
492 if( *psz_index == ';' )
494 /* Mark end of field */
500 case 0: /* channel number */
501 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
502 if( (*psz_str == '\0') || (*psz_end != '\0') )
507 case 1: /* channel description */
508 i_field_length = strlen( psz_str );
509 if( i_field_length != 0 )
511 p_channel->psz_description = malloc( i_field_length + 1 );
512 if( p_channel->psz_description == NULL )
514 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
519 strcpy( p_channel->psz_description, psz_str );
523 case 2: /* input method */
524 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
525 if( (*psz_str == '\0') || (*psz_end != '\0') )
530 case 3: /* input source */
531 i_field_length = strlen( psz_str );
532 if( i_field_length != 0 )
534 p_channel->psz_input_source = malloc( i_field_length + 1 );
535 if( p_channel->psz_input_source == NULL )
537 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
542 strcpy( p_channel->psz_input_source, psz_str );
546 case 4: /* input port */
547 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
548 if( (*psz_str == '\0') || (*psz_end != '\0') )
553 case 5: /* input vlan */
554 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
555 if( (*psz_str == '\0') || (*psz_end != '\0') )
560 /* ... following fields are ignored */
563 /* Set new beginning of field */
564 psz_str = psz_index + 1;
568 /* At least the first three fields must be parsed sucessfully for function
569 * success. Other parsing errors are returned using i_field = -1. */
572 /* Function fails. Free allocated strings */
573 if( p_channel->psz_description != NULL )
575 free( p_channel->psz_description );
577 if( p_channel->psz_input_source != NULL )
579 free( p_channel->psz_input_source );