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
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free(), strtol() */
32 #include <stdio.h> /* FILE */
33 #include <string.h> /* strerror() */
34 #include <sys/types.h> /* on BSD, uio.h needs types.h */
35 #include <sys/uio.h> /* for input.h */
44 #include "audio_output.h"
47 #include "interface.h"
49 #include "intf_console.h"
52 #include "video_output.h"
56 /*****************************************************************************
57 * intf_channel_t: channel description
58 *****************************************************************************
59 * A 'channel' is a descriptor of an input method. It is used to switch easily
60 * from source to source without having to specify the whole input thread
61 * configuration. The channels array, stored in the interface thread object, is
62 * loaded in intf_Create, and unloaded in intf_Destroy.
63 *****************************************************************************/
64 typedef struct intf_channel_s
66 /* Channel description */
67 int i_channel; /* channel number, -1 for end of array */
68 char * psz_description; /* channel description (owned) */
70 /* Input configuration */
71 int i_input_method; /* input method descriptor */
72 char * psz_input_source; /* source string (owned) */
73 int i_input_port; /* port */
74 int i_input_vlan; /* vlan */
77 /*****************************************************************************
79 *****************************************************************************/
80 static int LoadChannels ( intf_thread_t *p_intf, char *psz_filename );
81 static void UnloadChannels ( intf_thread_t *p_intf );
82 static int ParseChannel ( intf_channel_t *p_channel, char *psz_str );
84 /*****************************************************************************
85 * intf_Create: prepare interface before main loop
86 *****************************************************************************
87 * This function opens output devices and creates specific interfaces. It sends
88 * its own error messages.
89 *****************************************************************************/
90 intf_thread_t* intf_Create( void )
92 intf_thread_t *p_intf;
95 /* Allocate structure */
96 p_intf = malloc( sizeof( intf_thread_t ) );
99 intf_ErrMsg("error: %s\n", strerror( ENOMEM ) );
103 /* Request an interface plugin */
104 psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
106 if( RequestPlugin( &p_intf->intf_plugin, "intf", psz_method ) < 0 )
108 intf_ErrMsg( "error: could not open interface plugin intf_%s.so\n", psz_method );
115 = GetPluginFunction( p_intf->intf_plugin, "intf_SysCreate" );
117 = GetPluginFunction( p_intf->intf_plugin, "intf_SysManage" );
118 p_intf->p_sys_destroy
119 = GetPluginFunction( p_intf->intf_plugin, "intf_SysDestroy" );
121 /* Initialize structure */
123 p_intf->p_vout = NULL;
124 p_intf->p_input = NULL;
126 /* Load channels - the pointer will be set to NULL on failure. The
127 * return value is ignored since the program can work without
129 LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
131 /* Start interfaces */
132 p_intf->p_console = intf_ConsoleCreate();
133 if( p_intf->p_console == NULL )
135 intf_ErrMsg("error: can't create control console\n");
136 TrashPlugin( p_intf->intf_plugin );
140 if( p_intf->p_sys_create( p_intf ) )
142 intf_ErrMsg("error: can't create interface\n");
143 intf_ConsoleDestroy( p_intf->p_console );
144 TrashPlugin( p_intf->intf_plugin );
149 intf_Msg("Interface initialized\n");
153 /*****************************************************************************
155 *****************************************************************************
156 * Initialization script and main interface loop.
157 *****************************************************************************/
158 void intf_Run( intf_thread_t *p_intf )
160 if( p_intf->p_playlist )
162 p_intf->p_input = input_CreateThread( INPUT_METHOD_TS_FILE, (void *)p_intf->p_playlist, p_intf->i_list_index, 0, p_main->p_intf->p_vout, p_main->p_aout, NULL );
164 /* Execute the initialization script - if a positive number is returned,
165 * the script could be executed but failed */
166 else if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
168 intf_ErrMsg("warning: error(s) during startup script\n");
172 while(!p_intf->b_die)
174 /* Flush waiting messages */
177 /* Manage specific interface */
178 p_intf->p_sys_manage( p_intf );
180 /* Check attached threads status */
181 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
183 /* FIXME: add aout error detection ?? */
186 if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
188 input_DestroyThread( p_intf->p_input, NULL );
189 p_intf->p_input = NULL;
190 intf_DbgMsg("Input thread destroyed\n");
193 /* Sleep to avoid using all CPU - since some interfaces needs to access
194 * keyboard events, a 100ms delay is a good compromise */
195 msleep( INTF_IDLE_SLEEP );
199 /*****************************************************************************
200 * intf_Destroy: clean interface after main loop
201 *****************************************************************************
202 * This function destroys specific interfaces and close output devices.
203 *****************************************************************************/
204 void intf_Destroy( intf_thread_t *p_intf )
206 /* Destroy interfaces */
207 p_intf->p_sys_destroy( p_intf );
208 intf_ConsoleDestroy( p_intf->p_console );
210 /* Unload channels */
211 UnloadChannels( p_intf );
214 TrashPlugin( p_intf->intf_plugin );
220 /*****************************************************************************
221 * intf_SelectChannel: change channel
222 *****************************************************************************
223 * Kill existing input, if any, and try to open a new one, using an input
224 * configuration table.
225 *****************************************************************************/
226 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
228 intf_channel_t * p_channel; /* channel */
230 /* Look for channel in array */
231 if( p_intf->p_channel != NULL )
233 for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
235 if( p_channel->i_channel == i_channel )
241 /* Kill existing input, if any */
242 if( p_intf->p_input != NULL )
244 input_DestroyThread( p_intf->p_input, NULL );
247 intf_Msg("Channel %d: %s\n", i_channel, p_channel->psz_description );
249 /* Open a new input */
250 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
251 p_channel->i_input_port, p_channel->i_input_vlan,
252 p_intf->p_vout, p_main->p_aout, NULL );
253 return( p_intf->p_input == NULL );
258 /* Channel does not exist */
259 intf_Msg("Channel %d does not exist\n", i_channel );
263 /*****************************************************************************
264 * intf_ProcessKey: process standard keys
265 *****************************************************************************
266 * This function will process standard keys and return non 0 if the key was
268 *****************************************************************************/
269 int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
271 static int i_volbackup;
275 case 'Q': /* quit order */
277 case 27: /* escape key */
281 case '0': /* source change */
291 /* Change channel - return code is ignored since SelectChannel displays
292 * its own error messages */
293 intf_SelectChannel( p_intf, i_key - '0' );
295 case '+': /* volume + */
296 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol < VOLMAX) )
297 p_main->p_aout->vol += VOLSTEP;
299 case '-': /* volume - */
300 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol > VOLSTEP) )
301 p_main->p_aout->vol -= VOLSTEP;
303 case 'M': /* toggle mute */
305 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol))
307 i_volbackup = p_main->p_aout->vol;
308 p_main->p_aout->vol = 0;
310 else if( (p_main->p_aout != NULL) && (!p_main->p_aout->vol))
311 p_main->p_aout->vol = i_volbackup;
313 case 'g': /* gamma - */
314 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
316 vlc_mutex_lock( &p_intf->p_vout->change_lock );
317 p_intf->p_vout->f_gamma -= INTF_GAMMA_STEP;
318 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
319 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
322 case 'G': /* gamma + */
323 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
325 vlc_mutex_lock( &p_intf->p_vout->change_lock );
326 p_intf->p_vout->f_gamma += INTF_GAMMA_STEP;
327 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
328 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
331 case 'c': /* toggle grayscale */
332 if( p_intf->p_vout != NULL )
334 vlc_mutex_lock( &p_intf->p_vout->change_lock );
335 p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
336 p_intf->p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
337 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
340 case ' ': /* toggle interface */
341 if( p_intf->p_vout != NULL )
343 vlc_mutex_lock( &p_intf->p_vout->change_lock );
344 p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
345 p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
346 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
349 case 'i': /* toggle info */
350 if( p_intf->p_vout != NULL )
352 vlc_mutex_lock( &p_intf->p_vout->change_lock );
353 p_intf->p_vout->b_info = !p_intf->p_vout->b_info;
354 p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
355 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
358 case 's': /* toggle scaling */
359 if( p_intf->p_vout != NULL )
361 vlc_mutex_lock( &p_intf->p_vout->change_lock );
362 p_intf->p_vout->b_scale = !p_intf->p_vout->b_scale;
363 p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
364 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
367 default: /* unknown key */
374 /* following functions are local */
376 /*****************************************************************************
377 * LoadChannels: load channels description from a file
378 *****************************************************************************
379 * This structe describes all interface-specific data of the main (interface)
381 * Each line of the file is a semicolon separated list of the following
383 * integer channel number
384 * string channel description
385 * integer input method (see input.h)
386 * string input source
389 * The last field must end with a semicolon.
390 * Comments and empty lines are not explicitely allowed, but lines with parsing
391 * errors are ignored without warning.
392 *****************************************************************************/
393 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
395 FILE * p_file; /* file */
396 intf_channel_t * p_channel; /* current channel */
397 char psz_line[INTF_MAX_CMD_SIZE]; /* line buffer */
398 int i_index; /* channel or field index */
400 /* Set default value */
401 p_intf->p_channel = NULL;
404 p_file = fopen( psz_filename, "r" );
407 intf_ErrMsg("error: can't open %s (%s)\n", psz_filename, strerror(errno));
411 /* First pass: count number of lines */
412 for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
419 /* Allocate array and rewind - some of the lines may be invalid, and the
420 * array will probably be larger than the actual number of channels, but
421 * it has no consequence. */
422 p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
423 if( p_intf->p_channel == NULL )
425 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
429 p_channel = p_intf->p_channel;
432 /* Second pass: read channels descriptions */
433 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
435 if( !ParseChannel( p_channel, psz_line ) )
437 intf_DbgMsg( "channel [%d] %s : method %d (%s:%d vlan %d)\n",
438 p_channel->i_channel, p_channel->psz_description,
439 p_channel->i_input_method,
440 p_channel->psz_input_source,
441 p_channel->i_input_port, p_channel->i_input_vlan );
446 /* Add marker at the end of the array */
447 p_channel->i_channel = -1;
455 /*****************************************************************************
456 * UnloadChannels: unload channels description
457 *****************************************************************************
458 * This function free all resources allocated by LoadChannels, if any.
459 *****************************************************************************/
460 static void UnloadChannels( intf_thread_t *p_intf )
462 int i_channel; /* channel index */
464 if( p_intf->p_channel != NULL )
466 /* Free allocated strings */
468 p_intf->p_channel[ i_channel ].i_channel != -1;
471 if( p_intf->p_channel[ i_channel ].psz_description != NULL )
473 free( p_intf->p_channel[ i_channel ].psz_description );
475 if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
477 free( p_intf->p_channel[ i_channel ].psz_input_source );
482 free( p_intf->p_channel );
483 p_intf->p_channel = NULL;
488 /*****************************************************************************
489 * ParseChannel: parse a channel description line
490 *****************************************************************************
491 * See LoadChannels. This function return non 0 on parsing error.
492 *****************************************************************************/
493 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
495 char * psz_index; /* current character */
496 char * psz_end; /* end pointer for strtol */
497 int i_field; /* field number, -1 on error */
498 int i_field_length; /* field length, for text fields */
500 /* Set some default fields */
501 p_channel->i_channel = 0;
502 p_channel->psz_description = NULL;
503 p_channel->i_input_method = 0;
504 p_channel->psz_input_source = NULL;
505 p_channel->i_input_port = 0;
506 p_channel->i_input_vlan = 0;
510 for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
512 if( *psz_index == ';' )
514 /* Mark end of field */
520 case 0: /* channel number */
521 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
522 if( (*psz_str == '\0') || (*psz_end != '\0') )
527 case 1: /* channel description */
528 i_field_length = strlen( psz_str );
529 if( i_field_length != 0 )
531 p_channel->psz_description = malloc( i_field_length + 1 );
532 if( p_channel->psz_description == NULL )
534 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
539 strcpy( p_channel->psz_description, psz_str );
543 case 2: /* input method */
544 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
545 if( (*psz_str == '\0') || (*psz_end != '\0') )
550 case 3: /* input source */
551 i_field_length = strlen( psz_str );
552 if( i_field_length != 0 )
554 p_channel->psz_input_source = malloc( i_field_length + 1 );
555 if( p_channel->psz_input_source == NULL )
557 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
562 strcpy( p_channel->psz_input_source, psz_str );
566 case 4: /* input port */
567 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
568 if( (*psz_str == '\0') || (*psz_end != '\0') )
573 case 5: /* input vlan */
574 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
575 if( (*psz_str == '\0') || (*psz_end != '\0') )
580 /* ... following fields are ignored */
583 /* Set new beginning of field */
584 psz_str = psz_index + 1;
588 /* At least the first three fields must be parsed sucessfully for function
589 * success. Other parsing errors are returned using i_field = -1. */
592 /* Function fails. Free allocated strings */
593 if( p_channel->psz_description != NULL )
595 free( p_channel->psz_description );
597 if( p_channel->psz_input_source != NULL )
599 free( p_channel->psz_input_source );