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 *****************************************************************************/
31 #include <errno.h> /* ENOMEM */
32 #include <stdlib.h> /* free(), strtol() */
33 #include <stdio.h> /* FILE */
34 #include <string.h> /* strerror() */
35 #include <sys/types.h> /* on BSD, uio.h needs types.h */
36 #include <sys/uio.h> /* for input.h */
45 #include "audio_output.h"
48 #include "interface.h"
50 #include "intf_console.h"
53 #include "video_output.h"
57 /*****************************************************************************
58 * intf_channel_t: channel description
59 *****************************************************************************
60 * A 'channel' is a descriptor of an input method. It is used to switch easily
61 * from source to source without having to specify the whole input thread
62 * configuration. The channels array, stored in the interface thread object, is
63 * loaded in intf_Create, and unloaded in intf_Destroy.
64 *****************************************************************************/
65 typedef struct intf_channel_s
67 /* Channel description */
68 int i_channel; /* channel number, -1 for end of array */
69 char * psz_description; /* channel description (owned) */
71 /* Input configuration */
72 int i_input_method; /* input method descriptor */
73 char * psz_input_source; /* source string (owned) */
74 int i_input_port; /* port */
75 int i_input_vlan; /* vlan */
78 /*****************************************************************************
80 *****************************************************************************/
81 static int LoadChannels ( intf_thread_t *p_intf, char *psz_filename );
82 static void UnloadChannels ( intf_thread_t *p_intf );
83 static int ParseChannel ( intf_channel_t *p_channel, char *psz_str );
85 /*****************************************************************************
86 * intf_Create: prepare interface before main loop
87 *****************************************************************************
88 * This function opens output devices and creates specific interfaces. It sends
89 * its own error messages.
90 *****************************************************************************/
91 intf_thread_t* intf_Create( void )
93 intf_thread_t *p_intf;
96 /* Allocate structure */
97 p_intf = malloc( sizeof( intf_thread_t ) );
100 intf_ErrMsg("error: %s\n", strerror( ENOMEM ) );
104 /* Request an interface plugin */
105 psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
107 if( RequestPlugin( &p_intf->intf_plugin, "intf", psz_method ) < 0 )
109 intf_ErrMsg( "error: could not open interface plugin intf_%s.so\n", psz_method );
116 = GetPluginFunction( p_intf->intf_plugin, "intf_SysCreate" );
118 = GetPluginFunction( p_intf->intf_plugin, "intf_SysManage" );
119 p_intf->p_sys_destroy
120 = GetPluginFunction( p_intf->intf_plugin, "intf_SysDestroy" );
122 /* Initialize structure */
124 p_intf->p_vout = NULL;
125 p_intf->p_input = NULL;
127 /* Load channels - the pointer will be set to NULL on failure. The
128 * return value is ignored since the program can work without
130 LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
132 /* Start interfaces */
133 p_intf->p_console = intf_ConsoleCreate();
134 if( p_intf->p_console == NULL )
136 intf_ErrMsg("error: can't create control console\n");
137 TrashPlugin( p_intf->intf_plugin );
141 if( p_intf->p_sys_create( p_intf ) )
143 intf_ErrMsg("error: can't create interface\n");
144 intf_ConsoleDestroy( p_intf->p_console );
145 TrashPlugin( p_intf->intf_plugin );
150 intf_Msg("Interface initialized\n");
154 /*****************************************************************************
156 *****************************************************************************
157 * Initialization script and main interface loop.
158 *****************************************************************************/
159 void intf_Run( intf_thread_t *p_intf )
161 /* Execute the initialization script - if a positive number is returned,
162 * the script could be executed but failed */
163 if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
165 intf_ErrMsg("warning: error(s) during startup script\n");
169 while(!p_intf->b_die)
171 /* Flush waiting messages */
174 /* Manage specific interface */
175 p_intf->p_sys_manage( p_intf );
177 /* Check attached threads status */
178 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
180 /* FIXME: add aout error detection ?? */
183 if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
185 input_DestroyThread( p_intf->p_input, NULL );
186 p_intf->p_input = NULL;
187 intf_DbgMsg("Input thread destroyed\n");
190 /* Sleep to avoid using all CPU - since some interfaces needs to access
191 * keyboard events, a 100ms delay is a good compromise */
192 msleep( INTF_IDLE_SLEEP );
196 /*****************************************************************************
197 * intf_Destroy: clean interface after main loop
198 *****************************************************************************
199 * This function destroys specific interfaces and close output devices.
200 *****************************************************************************/
201 void intf_Destroy( intf_thread_t *p_intf )
203 /* Destroy interfaces */
204 p_intf->p_sys_destroy( p_intf );
205 intf_ConsoleDestroy( p_intf->p_console );
207 /* Unload channels */
208 UnloadChannels( p_intf );
211 TrashPlugin( p_intf->intf_plugin );
217 /*****************************************************************************
218 * intf_SelectChannel: change channel
219 *****************************************************************************
220 * Kill existing input, if any, and try to open a new one, using an input
221 * configuration table.
222 *****************************************************************************/
223 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
225 intf_channel_t * p_channel; /* channel */
227 /* Look for channel in array */
228 if( p_intf->p_channel != NULL )
230 for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
232 if( p_channel->i_channel == i_channel )
238 /* Kill existing input, if any */
239 if( p_intf->p_input != NULL )
241 input_DestroyThread( p_intf->p_input, NULL );
244 intf_Msg("Channel %d: %s\n", i_channel, p_channel->psz_description );
246 /* Open a new input */
247 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
248 p_channel->i_input_port, p_channel->i_input_vlan,
249 p_intf->p_vout, p_main->p_aout, NULL );
250 return( p_intf->p_input == NULL );
255 /* Channel does not exist */
256 intf_Msg("Channel %d does not exist\n", i_channel );
260 /*****************************************************************************
261 * intf_ProcessKey: process standard keys
262 *****************************************************************************
263 * This function will process standard keys and return non 0 if the key was
265 *****************************************************************************/
266 int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
268 static int i_volbackup;
272 case 'Q': /* quit order */
274 case 27: /* escape key */
278 case '0': /* source change */
288 /* Change channel - return code is ignored since SelectChannel displays
289 * its own error messages */
290 intf_SelectChannel( p_intf, i_key - '0' );
292 case '+': /* volume + */
293 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol < VOLMAX) )
294 p_main->p_aout->vol += VOLSTEP;
296 case '-': /* volume - */
297 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol > VOLSTEP) )
298 p_main->p_aout->vol -= VOLSTEP;
300 case 'M': /* toggle mute */
302 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol))
304 i_volbackup = p_main->p_aout->vol;
305 p_main->p_aout->vol = 0;
307 else if( (p_main->p_aout != NULL) && (!p_main->p_aout->vol))
308 p_main->p_aout->vol = i_volbackup;
310 case 'g': /* gamma - */
311 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
313 vlc_mutex_lock( &p_intf->p_vout->change_lock );
314 p_intf->p_vout->f_gamma -= INTF_GAMMA_STEP;
315 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
316 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
319 case 'G': /* gamma + */
320 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
322 vlc_mutex_lock( &p_intf->p_vout->change_lock );
323 p_intf->p_vout->f_gamma += INTF_GAMMA_STEP;
324 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
325 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
328 case 'c': /* toggle grayscale */
329 if( p_intf->p_vout != NULL )
331 vlc_mutex_lock( &p_intf->p_vout->change_lock );
332 p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
333 p_intf->p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
334 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
337 case ' ': /* toggle interface */
338 if( p_intf->p_vout != NULL )
340 vlc_mutex_lock( &p_intf->p_vout->change_lock );
341 p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
342 p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
343 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
346 case 'i': /* toggle info */
347 if( p_intf->p_vout != NULL )
349 vlc_mutex_lock( &p_intf->p_vout->change_lock );
350 p_intf->p_vout->b_info = !p_intf->p_vout->b_info;
351 p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
352 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
355 case 's': /* toggle scaling */
356 if( p_intf->p_vout != NULL )
358 vlc_mutex_lock( &p_intf->p_vout->change_lock );
359 p_intf->p_vout->b_scale = !p_intf->p_vout->b_scale;
360 p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
361 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
364 default: /* unknown key */
371 /* following functions are local */
373 /*****************************************************************************
374 * LoadChannels: load channels description from a file
375 *****************************************************************************
376 * This structe describes all interface-specific data of the main (interface)
378 * Each line of the file is a semicolon separated list of the following
380 * integer channel number
381 * string channel description
382 * integer input method (see input.h)
383 * string input source
386 * The last field must end with a semicolon.
387 * Comments and empty lines are not explicitely allowed, but lines with parsing
388 * errors are ignored without warning.
389 *****************************************************************************/
390 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
392 FILE * p_file; /* file */
393 intf_channel_t * p_channel; /* current channel */
394 char psz_line[INTF_MAX_CMD_SIZE]; /* line buffer */
395 int i_index; /* channel or field index */
397 /* Set default value */
398 p_intf->p_channel = NULL;
401 p_file = fopen( psz_filename, "r" );
404 intf_ErrMsg("error: can't open %s (%s)\n", psz_filename, strerror(errno));
408 /* First pass: count number of lines */
409 for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
416 /* Allocate array and rewind - some of the lines may be invalid, and the
417 * array will probably be larger than the actual number of channels, but
418 * it has no consequence. */
419 p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
420 if( p_intf->p_channel == NULL )
422 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
426 p_channel = p_intf->p_channel;
429 /* Second pass: read channels descriptions */
430 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
432 if( !ParseChannel( p_channel, psz_line ) )
434 intf_DbgMsg( "channel [%d] %s : method %d (%s:%d vlan %d)\n",
435 p_channel->i_channel, p_channel->psz_description,
436 p_channel->i_input_method,
437 p_channel->psz_input_source,
438 p_channel->i_input_port, p_channel->i_input_vlan );
443 /* Add marker at the end of the array */
444 p_channel->i_channel = -1;
452 /*****************************************************************************
453 * UnloadChannels: unload channels description
454 *****************************************************************************
455 * This function free all resources allocated by LoadChannels, if any.
456 *****************************************************************************/
457 static void UnloadChannels( intf_thread_t *p_intf )
459 int i_channel; /* channel index */
461 if( p_intf->p_channel != NULL )
463 /* Free allocated strings */
465 p_intf->p_channel[ i_channel ].i_channel != -1;
468 if( p_intf->p_channel[ i_channel ].psz_description != NULL )
470 free( p_intf->p_channel[ i_channel ].psz_description );
472 if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
474 free( p_intf->p_channel[ i_channel ].psz_input_source );
479 free( p_intf->p_channel );
480 p_intf->p_channel = NULL;
485 /*****************************************************************************
486 * ParseChannel: parse a channel description line
487 *****************************************************************************
488 * See LoadChannels. This function return non 0 on parsing error.
489 *****************************************************************************/
490 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
492 char * psz_index; /* current character */
493 char * psz_end; /* end pointer for strtol */
494 int i_field; /* field number, -1 on error */
495 int i_field_length; /* field length, for text fields */
497 /* Set some default fields */
498 p_channel->i_channel = 0;
499 p_channel->psz_description = NULL;
500 p_channel->i_input_method = 0;
501 p_channel->psz_input_source = NULL;
502 p_channel->i_input_port = 0;
503 p_channel->i_input_vlan = 0;
507 for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
509 if( *psz_index == ';' )
511 /* Mark end of field */
517 case 0: /* channel number */
518 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
519 if( (*psz_str == '\0') || (*psz_end != '\0') )
524 case 1: /* channel description */
525 i_field_length = strlen( psz_str );
526 if( i_field_length != 0 )
528 p_channel->psz_description = malloc( i_field_length + 1 );
529 if( p_channel->psz_description == NULL )
531 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
536 strcpy( p_channel->psz_description, psz_str );
540 case 2: /* input method */
541 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
542 if( (*psz_str == '\0') || (*psz_end != '\0') )
547 case 3: /* input source */
548 i_field_length = strlen( psz_str );
549 if( i_field_length != 0 )
551 p_channel->psz_input_source = malloc( i_field_length + 1 );
552 if( p_channel->psz_input_source == NULL )
554 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
559 strcpy( p_channel->psz_input_source, psz_str );
563 case 4: /* input port */
564 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
565 if( (*psz_str == '\0') || (*psz_end != '\0') )
570 case 5: /* input vlan */
571 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
572 if( (*psz_str == '\0') || (*psz_end != '\0') )
577 /* ... following fields are ignored */
580 /* Set new beginning of field */
581 psz_str = psz_index + 1;
585 /* At least the first three fields must be parsed sucessfully for function
586 * success. Other parsing errors are returned using i_field = -1. */
589 /* Function fails. Free allocated strings */
590 if( p_channel->psz_description != NULL )
592 free( p_channel->psz_description );
594 if( p_channel->psz_input_source != NULL )
596 free( p_channel->psz_input_source );