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 */
186 while( !p_intf->b_die )
188 /* Select the next playlist item */
189 playlist_Next( p_main->p_playlist );
191 if( p_main->p_playlist->i_index == -1 )
193 /* FIXME: wait for user to add stuff to playlist ? */
199 input_CreateThread( &p_main->p_playlist->current, NULL );
202 while( !p_intf->b_die && (p_intf->p_input != NULL) )
204 /* Flush waiting messages */
207 /* Manage specific interface */
208 p_intf->p_sys_manage( p_intf );
210 /* Manage module bank */
211 module_ManageBank( p_main->p_module_bank );
213 /* Check attached threads status */
214 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
216 /* FIXME: add aout error detection ?? */
220 if( ( p_intf->p_input != NULL ) &&
221 ( p_intf->p_input->b_error || p_intf->p_input->b_eof ) )
223 input_DestroyThread( p_intf->p_input, NULL );
224 p_intf->p_input = NULL;
225 intf_DbgMsg("Input thread destroyed");
228 /* Sleep to avoid using all CPU - since some interfaces needs
229 * to access keyboard events, a 100ms delay is a good compromise */
230 msleep( INTF_IDLE_SLEEP );
235 /*****************************************************************************
236 * intf_Destroy: clean interface after main loop
237 *****************************************************************************
238 * This function destroys specific interfaces and close output devices.
239 *****************************************************************************/
240 void intf_Destroy( intf_thread_t *p_intf )
244 /* Destroy interfaces */
245 p_intf->p_sys_destroy( p_intf );
246 intf_ConsoleDestroy( p_intf->p_console );
248 /* Unload channels */
249 UnloadChannels( p_intf );
252 p_cur = p_intf->p_keys;
253 while( p_cur != NULL)
255 p_next = p_cur->next;
264 /*****************************************************************************
265 * intf_SelectChannel: change channel
266 *****************************************************************************
267 * Kill existing input, if any, and try to open a new one, using an input
268 * configuration table.
269 *****************************************************************************/
270 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
274 intf_channel_t * p_channel; /* channel */
276 /* Look for channel in array */
277 if( p_intf->p_channel != NULL )
279 for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
281 if( p_channel->i_channel == i_channel )
287 /* Kill existing input, if any */
288 if( p_intf->p_input != NULL )
290 input_DestroyThread( p_intf->p_input, NULL );
293 intf_Msg("Channel %d: %s", i_channel, p_channel->psz_description );
295 /* Open a new input */
296 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
297 p_channel->i_input_port, p_channel->i_input_vlan_id,
298 p_intf->p_vout, p_main->p_aout, NULL );
299 return( p_intf->p_input == NULL );
304 /* Channel does not exist */
305 intf_Msg("Channel %d does not exist", i_channel );
310 /*****************************************************************************
311 * intf_AssignKey: assign standartkeys *
312 *****************************************************************************
313 * This function fills in the associative array that links the key pressed *
314 * and the key we use internally. Support one extra parameter. *
315 ****************************************************************************/
316 void intf_AssignKey( intf_thread_t *p_intf, int r_key, int f_key, int param)
318 p_intf_key p_cur = p_intf->p_keys;
321 p_cur = (p_intf_key )(malloc ( sizeof( intf_key ) ) );
322 p_cur->received_key = r_key;
323 p_cur->forwarded.key = f_key;
324 p_cur->forwarded.param = param;
326 p_intf->p_keys = p_cur;
330 while( p_cur->next != NULL && p_cur ->received_key != r_key)
334 if( p_cur->next == NULL )
336 p_cur->next = ( p_intf_key )( malloc( sizeof( intf_key ) ) );
339 p_cur->forwarded.param = param;
340 p_cur->received_key = r_key;
342 p_cur->forwarded.key = f_key;
346 /* Basic getKey function... */
347 keyparm intf_GetKey( intf_thread_t *p_intf, int r_key)
351 p_intf_key current = p_intf->p_keys;
352 while(current != NULL && current->received_key != r_key)
354 current = current->next;
357 { /* didn't find any key in the array */
358 reply.key = INTF_KEY_UNKNOWN;
363 reply.key = current->forwarded.key;
364 reply.param = current->forwarded.param;
369 /*****************************************************************************
370 * intf_AssignNormalKeys: used for normal interfaces.
371 *****************************************************************************
372 * This function assign the basic key to the normal keys.
373 *****************************************************************************/
375 void intf_AssignNormalKeys( intf_thread_t *p_intf)
377 p_intf->p_intf_get_key = intf_GetKey;
379 intf_AssignKey( p_intf , 'Q', INTF_KEY_QUIT, 0);
380 intf_AssignKey( p_intf , 'q', INTF_KEY_QUIT, 0);
381 intf_AssignKey( p_intf , 27, INTF_KEY_QUIT, 0);
382 intf_AssignKey( p_intf , 3, INTF_KEY_QUIT, 0);
383 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
384 intf_AssignKey( p_intf , '1', INTF_KEY_SET_CHANNEL, 1);
385 intf_AssignKey( p_intf , '2', INTF_KEY_SET_CHANNEL, 2);
386 intf_AssignKey( p_intf , '3', INTF_KEY_SET_CHANNEL, 3);
387 intf_AssignKey( p_intf , '4', INTF_KEY_SET_CHANNEL, 4);
388 intf_AssignKey( p_intf , '5', INTF_KEY_SET_CHANNEL, 5);
389 intf_AssignKey( p_intf , '6', INTF_KEY_SET_CHANNEL, 6);
390 intf_AssignKey( p_intf , '7', INTF_KEY_SET_CHANNEL, 7);
391 intf_AssignKey( p_intf , '8', INTF_KEY_SET_CHANNEL, 8);
392 intf_AssignKey( p_intf , '9', INTF_KEY_SET_CHANNEL, 9);
393 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
394 intf_AssignKey( p_intf , '+', INTF_KEY_INC_VOLUME, 0);
395 intf_AssignKey( p_intf , '-', INTF_KEY_DEC_VOLUME, 0);
396 intf_AssignKey( p_intf , 'm', INTF_KEY_TOGGLE_VOLUME, 0);
397 intf_AssignKey( p_intf , 'M', INTF_KEY_TOGGLE_VOLUME, 0);
398 intf_AssignKey( p_intf , 'g', INTF_KEY_DEC_GAMMA, 0);
399 intf_AssignKey( p_intf , 'G', INTF_KEY_INC_GAMMA, 0);
400 intf_AssignKey( p_intf , 'c', INTF_KEY_TOGGLE_GRAYSCALE, 0);
401 intf_AssignKey( p_intf , ' ', INTF_KEY_TOGGLE_INTERFACE, 0);
402 intf_AssignKey( p_intf , 'i', INTF_KEY_TOGGLE_INFO, 0);
403 intf_AssignKey( p_intf , 's', INTF_KEY_TOGGLE_SCALING, 0);
406 /*****************************************************************************
407 * intf_ProcessKey: process standard keys
408 *****************************************************************************
409 * This function will process standard keys and return non 0 if the key was
411 *****************************************************************************/
412 int intf_ProcessKey( intf_thread_t *p_intf, int g_key )
414 static int i_volbackup;
417 k_reply = intf_GetKey( p_intf, g_key);
419 switch( k_reply.key )
421 case INTF_KEY_QUIT: /* quit order */
424 case INTF_KEY_SET_CHANNEL:
425 /* Change channel - return code is ignored since SelectChannel displays
426 * its own error messages */
427 intf_SelectChannel( p_intf, k_reply.param );
429 case INTF_KEY_INC_VOLUME: /* volume + */
430 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol < VOLUME_MAX) )
431 p_main->p_aout->vol += VOLUME_STEP;
433 case INTF_KEY_DEC_VOLUME: /* volume - */
434 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol > VOLUME_STEP) )
435 p_main->p_aout->vol -= VOLUME_STEP;
437 case INTF_KEY_TOGGLE_VOLUME: /* toggle mute */
438 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol))
440 i_volbackup = p_main->p_aout->vol;
441 p_main->p_aout->vol = 0;
443 else if( (p_main->p_aout != NULL) && (!p_main->p_aout->vol))
444 p_main->p_aout->vol = i_volbackup;
446 case INTF_KEY_DEC_GAMMA: /* gamma - */
447 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
449 vlc_mutex_lock( &p_intf->p_vout->change_lock );
450 p_intf->p_vout->f_gamma -= INTF_GAMMA_STEP;
451 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
452 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
455 case INTF_KEY_INC_GAMMA: /* gamma + */
456 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
458 vlc_mutex_lock( &p_intf->p_vout->change_lock );
459 p_intf->p_vout->f_gamma += INTF_GAMMA_STEP;
460 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
461 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
464 case INTF_KEY_TOGGLE_GRAYSCALE: /* toggle grayscale */
465 if( p_intf->p_vout != NULL )
467 vlc_mutex_lock( &p_intf->p_vout->change_lock );
468 p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
469 p_intf->p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
470 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
473 case INTF_KEY_TOGGLE_INTERFACE: /* toggle interface */
474 if( p_intf->p_vout != NULL )
476 vlc_mutex_lock( &p_intf->p_vout->change_lock );
477 p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
478 p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
479 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
482 case INTF_KEY_TOGGLE_INFO: /* toggle info */
483 if( p_intf->p_vout != NULL )
485 vlc_mutex_lock( &p_intf->p_vout->change_lock );
486 p_intf->p_vout->b_info = !p_intf->p_vout->b_info;
487 p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
488 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
491 case INTF_KEY_TOGGLE_SCALING: /* toggle scaling */
492 if( p_intf->p_vout != NULL )
494 vlc_mutex_lock( &p_intf->p_vout->change_lock );
495 p_intf->p_vout->b_scale = !p_intf->p_vout->b_scale;
496 p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
497 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
500 default: /* unknown key */
507 /* following functions are local */
509 /*****************************************************************************
510 * LoadChannels: load channels description from a file
511 *****************************************************************************
512 * This structe describes all interface-specific data of the main (interface)
514 * Each line of the file is a semicolon separated list of the following
516 * integer channel number
517 * string channel description
518 * integer input method (see input.h)
519 * string input source
521 * integer input vlan id
522 * The last field must end with a semicolon.
523 * Comments and empty lines are not explicitely allowed, but lines with parsing
524 * errors are ignored without warning.
525 *****************************************************************************/
526 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
528 FILE * p_file; /* file */
529 intf_channel_t * p_channel; /* current channel */
530 char psz_line[INTF_MAX_CMD_SIZE]; /* line buffer */
531 int i_index; /* channel or field index */
533 /* Set default value */
534 p_intf->p_channel = NULL;
536 /* FIXME: channels are disabled */
540 p_file = fopen( psz_filename, "r" );
543 intf_DbgMsg( "intf warning: cannot open %s (%s)",
544 psz_filename, strerror(errno) );
548 /* First pass: count number of lines */
549 for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
556 /* Allocate array and rewind - some of the lines may be invalid, and the
557 * array will probably be larger than the actual number of channels, but
558 * it has no consequence. */
559 p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
560 if( p_intf->p_channel == NULL )
562 intf_ErrMsg( "intf error: cannot create intf_channel_t (%s)",
567 p_channel = p_intf->p_channel;
570 /* Second pass: read channels descriptions */
571 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
573 if( !ParseChannel( p_channel, psz_line ) )
575 intf_DbgMsg( "channel [%d] %s : method %d (%s:%d vlan id %d)",
576 p_channel->i_channel, p_channel->psz_description,
577 p_channel->i_input_method,
578 p_channel->psz_input_source,
579 p_channel->i_input_port, p_channel->i_input_vlan_id );
584 /* Add marker at the end of the array */
585 p_channel->i_channel = -1;
593 /*****************************************************************************
594 * UnloadChannels: unload channels description
595 *****************************************************************************
596 * This function free all resources allocated by LoadChannels, if any.
597 *****************************************************************************/
598 static void UnloadChannels( intf_thread_t *p_intf )
600 int i_channel; /* channel index */
602 if( p_intf->p_channel != NULL )
604 /* Free allocated strings */
606 p_intf->p_channel[ i_channel ].i_channel != -1;
609 if( p_intf->p_channel[ i_channel ].psz_description != NULL )
611 free( p_intf->p_channel[ i_channel ].psz_description );
613 if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
615 free( p_intf->p_channel[ i_channel ].psz_input_source );
620 free( p_intf->p_channel );
621 p_intf->p_channel = NULL;
626 /*****************************************************************************
627 * ParseChannel: parse a channel description line
628 *****************************************************************************
629 * See LoadChannels. This function return non 0 on parsing error.
630 *****************************************************************************/
631 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
633 char * psz_index; /* current character */
634 char * psz_end; /* end pointer for strtol */
635 int i_field; /* field number, -1 on error */
636 int i_field_length; /* field length, for text fields */
638 /* Set some default fields */
639 p_channel->i_channel = 0;
640 p_channel->psz_description = NULL;
641 p_channel->i_input_method = 0;
642 p_channel->psz_input_source = NULL;
643 p_channel->i_input_port = 0;
644 p_channel->i_input_vlan_id = 0;
648 for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
650 if( *psz_index == ';' )
652 /* Mark end of field */
658 case 0: /* channel number */
659 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
660 if( (*psz_str == '\0') || (*psz_end != '\0') )
665 case 1: /* channel description */
666 i_field_length = strlen( psz_str );
667 if( i_field_length != 0 )
669 p_channel->psz_description = malloc( i_field_length + 1 );
670 if( p_channel->psz_description == NULL )
672 intf_ErrMsg( "intf error: cannot create channel "
673 "description (%s)", strerror( ENOMEM ) );
678 strcpy( p_channel->psz_description, psz_str );
682 case 2: /* input method */
683 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
684 if( (*psz_str == '\0') || (*psz_end != '\0') )
689 case 3: /* input source */
690 i_field_length = strlen( psz_str );
691 if( i_field_length != 0 )
693 p_channel->psz_input_source = malloc( i_field_length + 1 );
694 if( p_channel->psz_input_source == NULL )
696 intf_ErrMsg( "intf error: cannot create input "
697 "source (%s)", strerror( ENOMEM ) );
702 strcpy( p_channel->psz_input_source, psz_str );
706 case 4: /* input port */
707 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
708 if( (*psz_str == '\0') || (*psz_end != '\0') )
713 case 5: /* input vlan id */
714 p_channel->i_input_vlan_id = strtol( psz_str, &psz_end, 0);
715 if( (*psz_str == '\0') || (*psz_end != '\0') )
720 /* ... following fields are ignored */
723 /* Set new beginning of field */
724 psz_str = psz_index + 1;
728 /* At least the first three fields must be parsed sucessfully for function
729 * success. Other parsing errors are returned using i_field = -1. */
732 /* Function fails. Free allocated strings */
733 if( p_channel->psz_description != NULL )
735 free( p_channel->psz_description );
737 if( p_channel->psz_input_source != NULL )
739 free( p_channel->psz_input_source );