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 */
45 #include "audio_output.h"
48 #include "interface.h"
50 #include "intf_console.h"
51 #include "keystrokes.h"
54 #include "video_output.h"
58 /*****************************************************************************
59 * intf_channel_t: channel description
60 *****************************************************************************
61 * A 'channel' is a descriptor of an input method. It is used to switch easily
62 * from source to source without having to specify the whole input thread
63 * configuration. The channels array, stored in the interface thread object, is
64 * loaded in intf_Create, and unloaded in intf_Destroy.
65 *****************************************************************************/
66 typedef struct intf_channel_s
68 /* Channel description */
69 int i_channel; /* channel number, -1 for end of array */
70 char * psz_description; /* channel description (owned) */
72 /* Input configuration */
73 int i_input_method; /* input method descriptor */
74 char * psz_input_source; /* source string (owned) */
75 int i_input_port; /* port */
76 int i_input_vlan; /* vlan */
79 /*****************************************************************************
81 *****************************************************************************/
82 static int LoadChannels ( intf_thread_t *p_intf, char *psz_filename );
83 static void UnloadChannels ( intf_thread_t *p_intf );
84 static int ParseChannel ( intf_channel_t *p_channel, char *psz_str );
86 /*****************************************************************************
87 * intf_Create: prepare interface before main loop
88 *****************************************************************************
89 * This function opens output devices and creates specific interfaces. It sends
90 * its own error messages.
91 *****************************************************************************/
92 intf_thread_t* intf_Create( void )
94 intf_thread_t * p_intf;
95 typedef void ( intf_getplugin_t ) ( intf_thread_t * p_intf );
97 int i_best_index = 0, i_best_score = 0;
99 /* Allocate structure */
100 p_intf = malloc( sizeof( intf_thread_t ) );
103 intf_ErrMsg("error: %s\n", strerror( ENOMEM ) );
107 /* Get a suitable interface plugin */
108 for( i_index = 0 ; i_index < p_main->p_bank->i_plugin_count ; i_index++ )
110 /* If there's a plugin in p_info ... */
111 if( p_main->p_bank->p_info[ i_index ] != NULL )
113 /* ... and if this plugin provides the functions we want ... */
114 if( p_main->p_bank->p_info[ i_index ]->intf_GetPlugin != NULL )
116 /* ... and if this plugin has a good score ... */
117 if( p_main->p_bank->p_info[ i_index ]->i_score > i_best_score )
119 /* ... then take it */
120 i_best_score = p_main->p_bank->p_info[ i_index ]->i_score;
121 i_best_index = i_index;
127 if( i_best_score == 0 )
133 /* Get the plugin functions */
134 ( (intf_getplugin_t *)
135 p_main->p_bank->p_info[ i_best_index ]->intf_GetPlugin )( p_intf );
137 /* Initialize structure */
139 p_intf->p_vout = NULL;
140 p_intf->p_input = NULL;
141 p_intf->p_keys = NULL;
143 /* Load channels - the pointer will be set to NULL on failure. The
144 * return value is ignored since the program can work without
146 LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
148 /* Start interfaces */
149 p_intf->p_console = intf_ConsoleCreate();
150 if( p_intf->p_console == NULL )
152 intf_ErrMsg("error: can't create control console\n");
156 if( p_intf->p_sys_create( p_intf ) )
158 intf_ErrMsg("error: can't create interface\n");
159 intf_ConsoleDestroy( p_intf->p_console );
164 intf_Msg("Interface initialized\n");
168 /*****************************************************************************
170 *****************************************************************************
171 * Initialization script and main interface loop.
172 *****************************************************************************/
173 void intf_Run( intf_thread_t *p_intf )
175 if( p_main->p_playlist->p_list )
177 p_intf->p_input = input_CreateThread( INPUT_METHOD_TS_FILE, NULL, 0, 0, p_main->p_intf->p_vout, p_main->p_aout, NULL );
179 /* Execute the initialization script - if a positive number is returned,
180 * the script could be executed but failed */
181 else if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
183 intf_ErrMsg("warning: error(s) during startup script\n");
187 while(!p_intf->b_die)
189 /* Flush waiting messages */
192 /* Manage specific interface */
193 p_intf->p_sys_manage( p_intf );
195 /* Check attached threads status */
196 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
198 /* FIXME: add aout error detection ?? */
201 if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
203 input_DestroyThread( p_intf->p_input, NULL );
204 p_intf->p_input = NULL;
205 intf_DbgMsg("Input thread destroyed\n");
208 /* Sleep to avoid using all CPU - since some interfaces needs to access
209 * keyboard events, a 100ms delay is a good compromise */
210 msleep( INTF_IDLE_SLEEP );
214 /*****************************************************************************
215 * intf_Destroy: clean interface after main loop
216 *****************************************************************************
217 * This function destroys specific interfaces and close output devices.
218 *****************************************************************************/
219 void intf_Destroy( intf_thread_t *p_intf )
223 /* Destroy interfaces */
224 p_intf->p_sys_destroy( p_intf );
225 intf_ConsoleDestroy( p_intf->p_console );
227 /* Unload channels */
228 UnloadChannels( p_intf );
232 p_cur = p_intf->p_keys;
233 while( p_cur != NULL)
235 p_next = p_cur->next;
245 /*****************************************************************************
246 * intf_SelectChannel: change channel
247 *****************************************************************************
248 * Kill existing input, if any, and try to open a new one, using an input
249 * configuration table.
250 *****************************************************************************/
251 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
253 intf_channel_t * p_channel; /* channel */
255 /* Look for channel in array */
256 if( p_intf->p_channel != NULL )
258 for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
260 if( p_channel->i_channel == i_channel )
266 /* Kill existing input, if any */
267 if( p_intf->p_input != NULL )
269 input_DestroyThread( p_intf->p_input, NULL );
272 intf_Msg("Channel %d: %s\n", i_channel, p_channel->psz_description );
274 /* Open a new input */
275 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
276 p_channel->i_input_port, p_channel->i_input_vlan,
277 p_intf->p_vout, p_main->p_aout, NULL );
278 return( p_intf->p_input == NULL );
283 /* Channel does not exist */
284 intf_Msg("Channel %d does not exist\n", i_channel );
288 /*****************************************************************************
289 * intf_AssignKey: assign standartkeys *
290 *****************************************************************************
291 * This function fills in the associative array that links the key pressed *
292 * and the key we use internally. Support one extra parameter. *
293 ****************************************************************************/
295 void intf_AssignKey( intf_thread_t *p_intf, int r_key, int f_key, int param)
297 p_intf_key p_cur = p_intf->p_keys;
300 p_cur = (p_intf_key )(malloc ( sizeof( intf_key ) ) );
301 p_cur->received_key = r_key;
302 p_cur->forwarded.key = f_key;
303 p_cur->forwarded.param = param;
305 p_intf->p_keys = p_cur;
309 while( p_cur->next != NULL && p_cur ->received_key != r_key)
313 if( p_cur->next == NULL )
315 p_cur->next = ( p_intf_key )( malloc( sizeof( intf_key ) ) );
318 p_cur->forwarded.param = param;
319 p_cur->received_key = r_key;
321 p_cur->forwarded.key = f_key;
325 /* Basic getKey function... */
326 keyparm intf_getKey( intf_thread_t *p_intf, int r_key)
330 p_intf_key current = p_intf->p_keys;
331 while(current != NULL && current->received_key != r_key)
333 current = current->next;
336 { /* didn't find any key in the array */
337 reply.key=INTF_KEY_UNKNOWN;
342 reply.key = current->forwarded.key;
343 reply.param = current->forwarded.param;
348 /*****************************************************************************
349 * intf_AssignNormalKeys: used for normal interfaces.
350 *****************************************************************************
351 * This function assign the basic key to the normal keys.
352 *****************************************************************************/
354 void intf_AssignNormalKeys( intf_thread_t *p_intf)
356 p_intf->p_intf_getKey = intf_getKey;
358 intf_AssignKey( p_intf , 'Q', INTF_KEY_QUIT, 0);
359 intf_AssignKey( p_intf , 'q', INTF_KEY_QUIT, 0);
360 intf_AssignKey( p_intf , 27, INTF_KEY_QUIT, 0);
361 intf_AssignKey( p_intf , 3, INTF_KEY_QUIT, 0);
362 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
363 intf_AssignKey( p_intf , '1', INTF_KEY_SET_CHANNEL, 1);
364 intf_AssignKey( p_intf , '2', INTF_KEY_SET_CHANNEL, 2);
365 intf_AssignKey( p_intf , '3', INTF_KEY_SET_CHANNEL, 3);
366 intf_AssignKey( p_intf , '4', INTF_KEY_SET_CHANNEL, 4);
367 intf_AssignKey( p_intf , '5', INTF_KEY_SET_CHANNEL, 5);
368 intf_AssignKey( p_intf , '6', INTF_KEY_SET_CHANNEL, 6);
369 intf_AssignKey( p_intf , '7', INTF_KEY_SET_CHANNEL, 7);
370 intf_AssignKey( p_intf , '8', INTF_KEY_SET_CHANNEL, 8);
371 intf_AssignKey( p_intf , '9', INTF_KEY_SET_CHANNEL, 9);
372 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
373 intf_AssignKey( p_intf , '+', INTF_KEY_INC_VOLUME, 0);
374 intf_AssignKey( p_intf , '-', INTF_KEY_DEC_VOLUME, 0);
375 intf_AssignKey( p_intf , 'm', INTF_KEY_TOGGLE_VOLUME, 0);
376 intf_AssignKey( p_intf , 'M', INTF_KEY_TOGGLE_VOLUME, 0);
377 intf_AssignKey( p_intf , 'g', INTF_KEY_DEC_GAMMA, 0);
378 intf_AssignKey( p_intf , 'G', INTF_KEY_INC_GAMMA, 0);
379 intf_AssignKey( p_intf , 'c', INTF_KEY_TOGGLE_GRAYSCALE, 0);
380 intf_AssignKey( p_intf , ' ', INTF_KEY_TOGGLE_INTERFACE, 0);
381 intf_AssignKey( p_intf , 'i', INTF_KEY_TOGGLE_INFO, 0);
382 intf_AssignKey( p_intf , 's', INTF_KEY_TOGGLE_SCALING, 0);
385 /*****************************************************************************
386 * intf_ProcessKey: process standard keys
387 *****************************************************************************
388 * This function will process standard keys and return non 0 if the key was
390 *****************************************************************************/
391 int intf_ProcessKey( intf_thread_t *p_intf, int g_key )
393 static int i_volbackup;
396 k_reply = intf_getKey( p_intf, g_key);
398 switch( k_reply.key )
400 case INTF_KEY_QUIT: /* quit order */
403 case INTF_KEY_SET_CHANNEL:
404 /* Change channel - return code is ignored since SelectChannel displays
405 * its own error messages */
406 intf_SelectChannel( p_intf, k_reply.param - '0' );
408 case INTF_KEY_INC_VOLUME: /* volume + */
409 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol < VOLMAX) )
410 p_main->p_aout->vol += VOLSTEP;
412 case INTF_KEY_DEC_VOLUME: /* volume - */
413 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol > VOLSTEP) )
414 p_main->p_aout->vol -= VOLSTEP;
416 case INTF_KEY_TOGGLE_VOLUME: /* toggle mute */
417 if( (p_main->p_aout != NULL) && (p_main->p_aout->vol))
419 i_volbackup = p_main->p_aout->vol;
420 p_main->p_aout->vol = 0;
422 else if( (p_main->p_aout != NULL) && (!p_main->p_aout->vol))
423 p_main->p_aout->vol = i_volbackup;
425 case INTF_KEY_DEC_GAMMA: /* gamma - */
426 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
428 vlc_mutex_lock( &p_intf->p_vout->change_lock );
429 p_intf->p_vout->f_gamma -= INTF_GAMMA_STEP;
430 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
431 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
434 case INTF_KEY_INC_GAMMA: /* gamma + */
435 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
437 vlc_mutex_lock( &p_intf->p_vout->change_lock );
438 p_intf->p_vout->f_gamma += INTF_GAMMA_STEP;
439 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
440 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
443 case INTF_KEY_TOGGLE_GRAYSCALE: /* toggle grayscale */
444 if( p_intf->p_vout != NULL )
446 vlc_mutex_lock( &p_intf->p_vout->change_lock );
447 p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
448 p_intf->p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
449 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
452 case INTF_KEY_TOGGLE_INTERFACE: /* toggle interface */
453 if( p_intf->p_vout != NULL )
455 vlc_mutex_lock( &p_intf->p_vout->change_lock );
456 p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
457 p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
458 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
461 case INTF_KEY_TOGGLE_INFO: /* toggle info */
462 if( p_intf->p_vout != NULL )
464 vlc_mutex_lock( &p_intf->p_vout->change_lock );
465 p_intf->p_vout->b_info = !p_intf->p_vout->b_info;
466 p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
467 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
470 case INTF_KEY_TOGGLE_SCALING: /* toggle scaling */
471 if( p_intf->p_vout != NULL )
473 vlc_mutex_lock( &p_intf->p_vout->change_lock );
474 p_intf->p_vout->b_scale = !p_intf->p_vout->b_scale;
475 p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
476 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
479 default: /* unknown key */
486 /* following functions are local */
488 /*****************************************************************************
489 * LoadChannels: load channels description from a file
490 *****************************************************************************
491 * This structe describes all interface-specific data of the main (interface)
493 * Each line of the file is a semicolon separated list of the following
495 * integer channel number
496 * string channel description
497 * integer input method (see input.h)
498 * string input source
501 * The last field must end with a semicolon.
502 * Comments and empty lines are not explicitely allowed, but lines with parsing
503 * errors are ignored without warning.
504 *****************************************************************************/
505 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
507 FILE * p_file; /* file */
508 intf_channel_t * p_channel; /* current channel */
509 char psz_line[INTF_MAX_CMD_SIZE]; /* line buffer */
510 int i_index; /* channel or field index */
512 /* Set default value */
513 p_intf->p_channel = NULL;
515 /* FIXME: channels are disabled */
519 p_file = fopen( psz_filename, "r" );
522 intf_ErrMsg("error: can't open %s (%s)\n", psz_filename, strerror(errno));
526 /* First pass: count number of lines */
527 for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
534 /* Allocate array and rewind - some of the lines may be invalid, and the
535 * array will probably be larger than the actual number of channels, but
536 * it has no consequence. */
537 p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
538 if( p_intf->p_channel == NULL )
540 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
544 p_channel = p_intf->p_channel;
547 /* Second pass: read channels descriptions */
548 while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
550 if( !ParseChannel( p_channel, psz_line ) )
552 intf_DbgMsg( "channel [%d] %s : method %d (%s:%d vlan %d)\n",
553 p_channel->i_channel, p_channel->psz_description,
554 p_channel->i_input_method,
555 p_channel->psz_input_source,
556 p_channel->i_input_port, p_channel->i_input_vlan );
561 /* Add marker at the end of the array */
562 p_channel->i_channel = -1;
570 /*****************************************************************************
571 * UnloadChannels: unload channels description
572 *****************************************************************************
573 * This function free all resources allocated by LoadChannels, if any.
574 *****************************************************************************/
575 static void UnloadChannels( intf_thread_t *p_intf )
577 int i_channel; /* channel index */
579 if( p_intf->p_channel != NULL )
581 /* Free allocated strings */
583 p_intf->p_channel[ i_channel ].i_channel != -1;
586 if( p_intf->p_channel[ i_channel ].psz_description != NULL )
588 free( p_intf->p_channel[ i_channel ].psz_description );
590 if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
592 free( p_intf->p_channel[ i_channel ].psz_input_source );
597 free( p_intf->p_channel );
598 p_intf->p_channel = NULL;
603 /*****************************************************************************
604 * ParseChannel: parse a channel description line
605 *****************************************************************************
606 * See LoadChannels. This function return non 0 on parsing error.
607 *****************************************************************************/
608 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
610 char * psz_index; /* current character */
611 char * psz_end; /* end pointer for strtol */
612 int i_field; /* field number, -1 on error */
613 int i_field_length; /* field length, for text fields */
615 /* Set some default fields */
616 p_channel->i_channel = 0;
617 p_channel->psz_description = NULL;
618 p_channel->i_input_method = 0;
619 p_channel->psz_input_source = NULL;
620 p_channel->i_input_port = 0;
621 p_channel->i_input_vlan = 0;
625 for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
627 if( *psz_index == ';' )
629 /* Mark end of field */
635 case 0: /* channel number */
636 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
637 if( (*psz_str == '\0') || (*psz_end != '\0') )
642 case 1: /* channel description */
643 i_field_length = strlen( psz_str );
644 if( i_field_length != 0 )
646 p_channel->psz_description = malloc( i_field_length + 1 );
647 if( p_channel->psz_description == NULL )
649 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
654 strcpy( p_channel->psz_description, psz_str );
658 case 2: /* input method */
659 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
660 if( (*psz_str == '\0') || (*psz_end != '\0') )
665 case 3: /* input source */
666 i_field_length = strlen( psz_str );
667 if( i_field_length != 0 )
669 p_channel->psz_input_source = malloc( i_field_length + 1 );
670 if( p_channel->psz_input_source == NULL )
672 intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
677 strcpy( p_channel->psz_input_source, psz_str );
681 case 4: /* input port */
682 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
683 if( (*psz_str == '\0') || (*psz_end != '\0') )
688 case 5: /* input vlan */
689 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
690 if( (*psz_str == '\0') || (*psz_end != '\0') )
695 /* ... following fields are ignored */
698 /* Set new beginning of field */
699 psz_str = psz_index + 1;
703 /* At least the first three fields must be parsed sucessfully for function
704 * success. Other parsing errors are returned using i_field = -1. */
707 /* Function fails. Free allocated strings */
708 if( p_channel->psz_description != NULL )
710 free( p_channel->psz_description );
712 if( p_channel->psz_input_source != NULL )
714 free( p_channel->psz_input_source );