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() */
42 #include "stream_control.h"
43 #include "input_ext-intf.h"
45 #include "audio_output.h"
48 #include "interface.h"
50 #include "intf_console.h"
51 #include "intf_plst.h"
52 #include "keystrokes.h"
55 #include "video_output.h"
59 /*****************************************************************************
60 * intf_channel_t: channel description
61 *****************************************************************************
62 * A 'channel' is a descriptor of an input method. It is used to switch easily
63 * from source to source without having to specify the whole input thread
64 * configuration. The channels array, stored in the interface thread object, is
65 * loaded in intf_Create, and unloaded in intf_Destroy.
66 *****************************************************************************/
67 typedef struct intf_channel_s
69 /* Channel description */
70 int i_channel; /* channel number, -1 for end of array */
71 char * psz_description; /* channel description (owned) */
73 /* Input configuration */
74 int i_input_method; /* input method descriptor */
75 char * psz_input_source; /* source string (owned) */
76 int i_input_port; /* port */
77 int i_input_vlan_id; /* vlan id */
80 /*****************************************************************************
82 *****************************************************************************/
83 static int LoadChannels ( intf_thread_t *p_intf, char *psz_filename );
84 static void UnloadChannels ( intf_thread_t *p_intf );
85 static int ParseChannel ( intf_channel_t *p_channel, char *psz_str );
87 /*****************************************************************************
88 * intf_Create: prepare interface before main loop
89 *****************************************************************************
90 * This function opens output devices and creates specific interfaces. It sends
91 * its own error messages.
92 *****************************************************************************/
93 intf_thread_t* intf_Create( void )
95 intf_thread_t * p_intf;
96 typedef void ( intf_getplugin_t ) ( intf_thread_t * p_intf );
98 int i_best_index = 0, i_best_score = 0;
100 /* Allocate structure */
101 p_intf = malloc( sizeof( intf_thread_t ) );
104 intf_ErrMsg( "intf error: cannot create interface thread (%s)",
105 strerror( ENOMEM ) );
109 /* Get a suitable interface plugin */
110 for( i_index = 0 ; i_index < p_main->p_bank->i_plugin_count ; i_index++ )
112 /* If there's a plugin in p_info ... */
113 if( p_main->p_bank->p_info[ i_index ] != NULL )
115 /* ... and if this plugin provides the functions we want ... */
116 if( p_main->p_bank->p_info[ i_index ]->intf_GetPlugin != NULL )
118 /* ... and if this plugin has a good score ... */
119 if( p_main->p_bank->p_info[ i_index ]->i_score > i_best_score )
121 /* ... then take it */
122 i_best_score = p_main->p_bank->p_info[ i_index ]->i_score;
123 i_best_index = i_index;
129 if( i_best_score == 0 )
132 intf_ErrMsg( "intf error: no suitable plugin" );
136 /* Get the plugin functions */
137 ( (intf_getplugin_t *)
138 p_main->p_bank->p_info[ i_best_index ]->intf_GetPlugin )( p_intf );
140 /* Initialize structure */
142 p_intf->p_vout = NULL;
143 p_intf->p_input = NULL;
144 p_intf->p_keys = NULL;
146 /* Warning level initialisation */
147 p_intf->i_warning_level = main_GetIntVariable( INTF_WARNING_VAR, INTF_WARNING_DEFAULT );
149 /* Load channels - the pointer will be set to NULL on failure. The
150 * return value is ignored since the program can work without
152 LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
154 /* Start interfaces */
155 p_intf->p_console = intf_ConsoleCreate();
156 if( p_intf->p_console == NULL )
158 intf_ErrMsg( "intf error: cannot create control console" );
163 if( p_intf->p_sys_create( p_intf ) )
165 intf_ErrMsg("intf error: cannot create interface");
166 intf_ConsoleDestroy( p_intf->p_console );
171 intf_Msg("intf: interface initialized");
175 /*****************************************************************************
177 *****************************************************************************
178 * Initialization script and main interface loop.
179 *****************************************************************************/
180 void intf_Run( intf_thread_t *p_intf )
182 /* Flush messages before spawning input */
185 p_intf->p_input = input_CreateThread( NULL );
188 while(!p_intf->b_die && (p_intf->p_input != NULL) )
190 /* Flush waiting messages */
193 /* Manage specific interface */
194 p_intf->p_sys_manage( p_intf );
196 /* Manage module bank */
197 module_ManageBank( p_main->p_module_bank );
199 /* Check attached threads status */
200 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
202 /* FIXME: add aout error detection ?? */
205 if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
207 input_DestroyThread( p_intf->p_input, NULL );
208 p_intf->p_input = NULL;
209 intf_DbgMsg("Input thread destroyed");
212 /* Sleep to avoid using all CPU - since some interfaces needs to access
213 * keyboard events, a 100ms delay is a good compromise */
214 msleep( INTF_IDLE_SLEEP );
218 /*****************************************************************************
219 * intf_Destroy: clean interface after main loop
220 *****************************************************************************
221 * This function destroys specific interfaces and close output devices.
222 *****************************************************************************/
223 void intf_Destroy( intf_thread_t *p_intf )
227 /* Destroy interfaces */
228 p_intf->p_sys_destroy( p_intf );
229 intf_ConsoleDestroy( p_intf->p_console );
231 /* Unload channels */
232 UnloadChannels( p_intf );
235 p_cur = p_intf->p_keys;
236 while( p_cur != NULL)
238 p_next = p_cur->next;
247 /*****************************************************************************
248 * intf_SelectChannel: change channel
249 *****************************************************************************
250 * Kill existing input, if any, and try to open a new one, using an input
251 * configuration table.
252 *****************************************************************************/
253 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
257 intf_channel_t * p_channel; /* channel */
259 /* Look for channel in array */
260 if( p_intf->p_channel != NULL )
262 for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
264 if( p_channel->i_channel == i_channel )
270 /* Kill existing input, if any */
271 if( p_intf->p_input != NULL )
273 input_DestroyThread( p_intf->p_input, NULL );
276 intf_Msg("Channel %d: %s", i_channel, p_channel->psz_description );
278 /* Open a new input */
279 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
280 p_channel->i_input_port, p_channel->i_input_vlan_id,
281 p_intf->p_vout, p_main->p_aout, NULL );
282 return( p_intf->p_input == NULL );
287 /* Channel does not exist */
288 intf_Msg("Channel %d does not exist", i_channel );
293 /*****************************************************************************
294 * intf_AssignKey: assign standartkeys *
295 *****************************************************************************
296 * This function fills in the associative array that links the key pressed *
297 * and the key we use internally. Support one extra parameter. *
298 ****************************************************************************/
299 void intf_AssignKey( intf_thread_t *p_intf, int r_key, int f_key, int param)
301 p_intf_key p_cur = p_intf->p_keys;
304 p_cur = (p_intf_key )(malloc ( sizeof( intf_key ) ) );
305 p_cur->received_key = r_key;
306 p_cur->forwarded.key = f_key;
307 p_cur->forwarded.param = param;
309 p_intf->p_keys = p_cur;
313 while( p_cur->next != NULL && p_cur ->received_key != r_key)
317 if( p_cur->next == NULL )
319 p_cur->next = ( p_intf_key )( malloc( sizeof( intf_key ) ) );
322 p_cur->forwarded.param = param;
323 p_cur->received_key = r_key;
325 p_cur->forwarded.key = f_key;
329 /* Basic getKey function... */
330 keyparm intf_GetKey( intf_thread_t *p_intf, int r_key)
334 p_intf_key current = p_intf->p_keys;
335 while(current != NULL && current->received_key != r_key)
337 current = current->next;
340 { /* didn't find any key in the array */
341 reply.key = INTF_KEY_UNKNOWN;
346 reply.key = current->forwarded.key;
347 reply.param = current->forwarded.param;
352 /*****************************************************************************
353 * intf_AssignNormalKeys: used for normal interfaces.
354 *****************************************************************************
355 * This function assign the basic key to the normal keys.
356 *****************************************************************************/
358 void intf_AssignNormalKeys( intf_thread_t *p_intf)
360 p_intf->p_intf_get_key = intf_GetKey;
362 intf_AssignKey( p_intf , 'Q', INTF_KEY_QUIT, 0);
363 intf_AssignKey( p_intf , 'q', INTF_KEY_QUIT, 0);
364 intf_AssignKey( p_intf , 27, INTF_KEY_QUIT, 0);
365 intf_AssignKey( p_intf , 3, INTF_KEY_QUIT, 0);
366 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
367 intf_AssignKey( p_intf , '1', INTF_KEY_SET_CHANNEL, 1);
368 intf_AssignKey( p_intf , '2', INTF_KEY_SET_CHANNEL, 2);
369 intf_AssignKey( p_intf , '3', INTF_KEY_SET_CHANNEL, 3);
370 intf_AssignKey( p_intf , '4', INTF_KEY_SET_CHANNEL, 4);
371 intf_AssignKey( p_intf , '5', INTF_KEY_SET_CHANNEL, 5);
372 intf_AssignKey( p_intf , '6', INTF_KEY_SET_CHANNEL, 6);
373 intf_AssignKey( p_intf , '7', INTF_KEY_SET_CHANNEL, 7);
374 intf_AssignKey( p_intf , '8', INTF_KEY_SET_CHANNEL, 8);
375 intf_AssignKey( p_intf , '9', INTF_KEY_SET_CHANNEL, 9);
376 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
377 intf_AssignKey( p_intf , '+', INTF_KEY_INC_VOLUME, 0);
378 intf_AssignKey( p_intf , '-', INTF_KEY_DEC_VOLUME, 0);
379 intf_AssignKey( p_intf , 'm', INTF_KEY_TOGGLE_VOLUME, 0);
380 intf_AssignKey( p_intf , 'M', INTF_KEY_TOGGLE_VOLUME, 0);
381 intf_AssignKey( p_intf , 'g', INTF_KEY_DEC_GAMMA, 0);
382 intf_AssignKey( p_intf , 'G', INTF_KEY_INC_GAMMA, 0);
383 intf_AssignKey( p_intf , 'c', INTF_KEY_TOGGLE_GRAYSCALE, 0);
384 intf_AssignKey( p_intf , ' ', INTF_KEY_TOGGLE_INTERFACE, 0);
385 intf_AssignKey( p_intf , 'i', INTF_KEY_TOGGLE_INFO, 0);
386 intf_AssignKey( p_intf , 's', INTF_KEY_TOGGLE_SCALING, 0);
389 /*****************************************************************************
390 * intf_ProcessKey: process standard keys
391 *****************************************************************************
392 * This function will process standard keys and return non 0 if the key was
394 *****************************************************************************/
395 int intf_ProcessKey( intf_thread_t *p_intf, int g_key )
397 static int i_volbackup;
400 k_reply = intf_GetKey( p_intf, g_key);
402 switch( k_reply.key )
404 case INTF_KEY_QUIT: /* quit order */
407 case INTF_KEY_SET_CHANNEL:
408 /* Change channel - return code is ignored since SelectChannel displays
409 * its own error messages */
410 intf_SelectChannel( p_intf, k_reply.param );
412 case INTF_KEY_INC_VOLUME: /* volume + */
413 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol < VOLUME_MAX) )
414 p_main->p_aout->vol += VOLUME_STEP;
416 case INTF_KEY_DEC_VOLUME: /* volume - */
417 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol > VOLUME_STEP) )
418 p_main->p_aout->vol -= VOLUME_STEP;
420 case INTF_KEY_TOGGLE_VOLUME: /* toggle mute */
421 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol))
423 i_volbackup = p_main->p_aout->vol;
424 p_main->p_aout->vol = 0;
426 else if( (p_main->p_aout != NULL) && (!p_main->p_aout->vol))
427 p_main->p_aout->vol = i_volbackup;
429 case INTF_KEY_DEC_GAMMA: /* gamma - */
430 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
432 vlc_mutex_lock( &p_intf->p_vout->change_lock );
433 p_intf->p_vout->f_gamma -= INTF_GAMMA_STEP;
434 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
435 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
438 case INTF_KEY_INC_GAMMA: /* gamma + */
439 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
441 vlc_mutex_lock( &p_intf->p_vout->change_lock );
442 p_intf->p_vout->f_gamma += INTF_GAMMA_STEP;
443 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
444 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
447 case INTF_KEY_TOGGLE_GRAYSCALE: /* toggle grayscale */
448 if( p_intf->p_vout != NULL )
450 vlc_mutex_lock( &p_intf->p_vout->change_lock );
451 p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
452 p_intf->p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
453 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
456 case INTF_KEY_TOGGLE_INTERFACE: /* toggle interface */
457 if( p_intf->p_vout != NULL )
459 vlc_mutex_lock( &p_intf->p_vout->change_lock );
460 p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
461 p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
462 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
465 case INTF_KEY_TOGGLE_INFO: /* toggle info */
466 if( p_intf->p_vout != NULL )
468 vlc_mutex_lock( &p_intf->p_vout->change_lock );
469 p_intf->p_vout->b_info = !p_intf->p_vout->b_info;
470 p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
471 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
474 case INTF_KEY_TOGGLE_SCALING: /* toggle scaling */
475 if( p_intf->p_vout != NULL )
477 vlc_mutex_lock( &p_intf->p_vout->change_lock );
478 p_intf->p_vout->b_scale = !p_intf->p_vout->b_scale;
479 p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
480 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
483 default: /* unknown key */
490 /* following functions are local */
492 /*****************************************************************************
493 * LoadChannels: load channels description from a file
494 *****************************************************************************
495 * This structe describes all interface-specific data of the main (interface)
497 * Each line of the file is a semicolon separated list of the following
499 * integer channel number
500 * string channel description
501 * integer input method (see input.h)
502 * string input source
504 * integer input vlan id
505 * The last field must end with a semicolon.
506 * Comments and empty lines are not explicitely allowed, but lines with parsing
507 * errors are ignored without warning.
508 *****************************************************************************/
509 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
511 FILE * p_file; /* file */
512 intf_channel_t * p_channel; /* current channel */
513 char psz_line[INTF_MAX_CMD_SIZE]; /* line buffer */
514 int i_index; /* channel or field index */
516 /* Set default value */
517 p_intf->p_channel = NULL;
519 /* FIXME: channels are disabled */
523 p_file = fopen( psz_filename, "r" );
526 intf_DbgMsg( "intf warning: cannot open %s (%s)",
527 psz_filename, strerror(errno) );
531 /* First pass: count number of lines */
532 for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
539 /* Allocate array and rewind - some of the lines may be invalid, and the
540 * array will probably be larger than the actual number of channels, but
541 * it has no consequence. */
542 p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
543 if( p_intf->p_channel == NULL )
545 intf_ErrMsg( "intf error: cannot create intf_channel_t (%s)",
550 p_channel = p_intf->p_channel;
553 /* Second pass: read channels descriptions */
554 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
556 if( !ParseChannel( p_channel, psz_line ) )
558 intf_DbgMsg( "channel [%d] %s : method %d (%s:%d vlan id %d)",
559 p_channel->i_channel, p_channel->psz_description,
560 p_channel->i_input_method,
561 p_channel->psz_input_source,
562 p_channel->i_input_port, p_channel->i_input_vlan_id );
567 /* Add marker at the end of the array */
568 p_channel->i_channel = -1;
576 /*****************************************************************************
577 * UnloadChannels: unload channels description
578 *****************************************************************************
579 * This function free all resources allocated by LoadChannels, if any.
580 *****************************************************************************/
581 static void UnloadChannels( intf_thread_t *p_intf )
583 int i_channel; /* channel index */
585 if( p_intf->p_channel != NULL )
587 /* Free allocated strings */
589 p_intf->p_channel[ i_channel ].i_channel != -1;
592 if( p_intf->p_channel[ i_channel ].psz_description != NULL )
594 free( p_intf->p_channel[ i_channel ].psz_description );
596 if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
598 free( p_intf->p_channel[ i_channel ].psz_input_source );
603 free( p_intf->p_channel );
604 p_intf->p_channel = NULL;
609 /*****************************************************************************
610 * ParseChannel: parse a channel description line
611 *****************************************************************************
612 * See LoadChannels. This function return non 0 on parsing error.
613 *****************************************************************************/
614 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
616 char * psz_index; /* current character */
617 char * psz_end; /* end pointer for strtol */
618 int i_field; /* field number, -1 on error */
619 int i_field_length; /* field length, for text fields */
621 /* Set some default fields */
622 p_channel->i_channel = 0;
623 p_channel->psz_description = NULL;
624 p_channel->i_input_method = 0;
625 p_channel->psz_input_source = NULL;
626 p_channel->i_input_port = 0;
627 p_channel->i_input_vlan_id = 0;
631 for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
633 if( *psz_index == ';' )
635 /* Mark end of field */
641 case 0: /* channel number */
642 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
643 if( (*psz_str == '\0') || (*psz_end != '\0') )
648 case 1: /* channel description */
649 i_field_length = strlen( psz_str );
650 if( i_field_length != 0 )
652 p_channel->psz_description = malloc( i_field_length + 1 );
653 if( p_channel->psz_description == NULL )
655 intf_ErrMsg( "intf error: cannot create channel "
656 "description (%s)", strerror( ENOMEM ) );
661 strcpy( p_channel->psz_description, psz_str );
665 case 2: /* input method */
666 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
667 if( (*psz_str == '\0') || (*psz_end != '\0') )
672 case 3: /* input source */
673 i_field_length = strlen( psz_str );
674 if( i_field_length != 0 )
676 p_channel->psz_input_source = malloc( i_field_length + 1 );
677 if( p_channel->psz_input_source == NULL )
679 intf_ErrMsg( "intf error: cannot create input "
680 "source (%s)", strerror( ENOMEM ) );
685 strcpy( p_channel->psz_input_source, psz_str );
689 case 4: /* input port */
690 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
691 if( (*psz_str == '\0') || (*psz_end != '\0') )
696 case 5: /* input vlan id */
697 p_channel->i_input_vlan_id = strtol( psz_str, &psz_end, 0);
698 if( (*psz_str == '\0') || (*psz_end != '\0') )
703 /* ... following fields are ignored */
706 /* Set new beginning of field */
707 psz_str = psz_index + 1;
711 /* At least the first three fields must be parsed sucessfully for function
712 * success. Other parsing errors are returned using i_field = -1. */
715 /* Function fails. Free allocated strings */
716 if( p_channel->psz_description != NULL )
718 free( p_channel->psz_description );
720 if( p_channel->psz_input_source != NULL )
722 free( p_channel->psz_input_source );