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
7 * $Id: interface.c,v 1.77 2001/05/07 03:14:09 stef Exp $
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, 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> /* off_t */
43 #include "stream_control.h"
44 #include "input_ext-intf.h"
46 #include "audio_output.h"
49 #include "interface.h"
50 #include "intf_playlist.h"
51 #include "intf_channels.h"
52 #include "keystrokes.h"
55 #include "video_output.h"
59 /*****************************************************************************
61 *****************************************************************************/
62 static void intf_Manage( intf_thread_t *p_intf );
64 /*****************************************************************************
65 * intf_Create: prepare interface before main loop
66 *****************************************************************************
67 * This function opens output devices and creates specific interfaces. It sends
68 * its own error messages.
69 *****************************************************************************/
70 intf_thread_t* intf_Create( void )
72 intf_thread_t * p_intf;
74 /* Allocate structure */
75 p_intf = malloc( sizeof( intf_thread_t ) );
78 intf_ErrMsg( "intf error: cannot create interface thread (%s)",
83 /* Choose the best module */
84 p_intf->p_module = module_Need( MODULE_CAPABILITY_INTF, NULL );
86 if( p_intf->p_module == NULL )
88 intf_ErrMsg( "intf error: no suitable intf module" );
93 #define f p_intf->p_module->p_functions->intf.functions.intf
94 p_intf->pf_open = f.pf_open;
95 p_intf->pf_close = f.pf_close;
96 p_intf->pf_run = f.pf_run;
99 /* Initialize callbacks */
100 p_intf->pf_manage = intf_Manage;
102 /* Initialize structure */
105 p_intf->p_input = NULL;
106 p_intf->p_keys = NULL;
108 p_intf->b_menu_change = 0;
110 if( p_intf->pf_open( p_intf ) )
112 intf_ErrMsg("intf error: cannot create interface");
113 module_Unneed( p_intf->p_module );
118 /* Initialize mutexes */
119 vlc_mutex_init( &p_intf->change_lock );
121 /* Load channels - the pointer will be set to NULL on failure. The
122 * return value is ignored since the program can work without
124 intf_LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR,
125 INTF_CHANNELS_DEFAULT ));
127 intf_WarnMsg( 1, "intf: interface initialized");
131 /*****************************************************************************
132 * intf_Manage: manage interface
133 *****************************************************************************
134 * This function has to be called regularly by the interface plugin. It
135 * checks for playlist end, module expiration, message flushing, and a few
136 * other useful things.
137 *****************************************************************************/
138 static void intf_Manage( intf_thread_t *p_intf )
140 /* Flush waiting messages */
143 /* Manage module bank */
144 module_ManageBank( );
146 if( ( p_intf->p_input != NULL ) &&
147 ( p_intf->p_input->b_error || p_intf->p_input->b_eof ) )
149 input_DestroyThread( p_intf->p_input, NULL );
150 p_intf->p_input = NULL;
151 intf_DbgMsg("Input thread destroyed");
154 /* If no stream is being played, try to find one */
155 if( p_intf->p_input == NULL && !p_intf->b_die )
157 /* Select the next playlist item */
158 intf_PlaylistNext( p_main->p_playlist );
160 if( p_main->p_playlist->i_index == -1 )
162 /* FIXME: wait for user to add stuff to playlist ? */
170 input_CreateThread( &p_main->p_playlist->current, NULL );
175 /*****************************************************************************
176 * intf_Destroy: clean interface after main loop
177 *****************************************************************************
178 * This function destroys specific interfaces and close output devices.
179 *****************************************************************************/
180 void intf_Destroy( intf_thread_t *p_intf )
185 /* Unload channels */
186 intf_UnloadChannels( p_intf );
188 /* Destroy interfaces */
189 p_intf->pf_close( p_intf );
191 /* Close input thread, if any (blocking) */
192 if( p_intf->p_input )
194 input_DestroyThread( p_intf->p_input, NULL );
198 p_cur = p_intf->p_keys;
199 while( p_cur != NULL)
201 p_next = p_cur->next;
207 module_Unneed( p_intf->p_module );
209 vlc_mutex_destroy( &p_intf->change_lock );
215 /*****************************************************************************
216 * intf_AssignKey: assign standartkeys *
217 *****************************************************************************
218 * This function fills in the associative array that links the key pressed *
219 * and the key we use internally. Support one extra parameter. *
220 ****************************************************************************/
221 void intf_AssignKey( intf_thread_t *p_intf, int r_key, int f_key, int param)
223 p_intf_key p_cur = p_intf->p_keys;
226 p_cur = (p_intf_key )(malloc ( sizeof( intf_key ) ) );
227 p_cur->received_key = r_key;
228 p_cur->forwarded.key = f_key;
229 p_cur->forwarded.param = param;
231 p_intf->p_keys = p_cur;
235 while( p_cur->next != NULL && p_cur ->received_key != r_key)
239 if( p_cur->next == NULL )
241 p_cur->next = ( p_intf_key )( malloc( sizeof( intf_key ) ) );
244 p_cur->forwarded.param = param;
245 p_cur->received_key = r_key;
247 p_cur->forwarded.key = f_key;
251 /* Basic getKey function... */
252 keyparm intf_GetKey( intf_thread_t *p_intf, int r_key)
256 p_intf_key current = p_intf->p_keys;
257 while(current != NULL && current->received_key != r_key)
259 current = current->next;
262 { /* didn't find any key in the array */
263 reply.key = INTF_KEY_UNKNOWN;
268 reply.key = current->forwarded.key;
269 reply.param = current->forwarded.param;
274 /*****************************************************************************
275 * intf_AssignNormalKeys: used for normal interfaces.
276 *****************************************************************************
277 * This function assign the basic key to the normal keys.
278 *****************************************************************************/
280 void intf_AssignNormalKeys( intf_thread_t *p_intf)
282 p_intf->p_intf_get_key = intf_GetKey;
284 intf_AssignKey( p_intf , 'Q', INTF_KEY_QUIT, 0);
285 intf_AssignKey( p_intf , 'q', INTF_KEY_QUIT, 0);
286 intf_AssignKey( p_intf , 27, INTF_KEY_QUIT, 0);
287 intf_AssignKey( p_intf , 3, INTF_KEY_QUIT, 0);
288 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
289 intf_AssignKey( p_intf , '1', INTF_KEY_SET_CHANNEL, 1);
290 intf_AssignKey( p_intf , '2', INTF_KEY_SET_CHANNEL, 2);
291 intf_AssignKey( p_intf , '3', INTF_KEY_SET_CHANNEL, 3);
292 intf_AssignKey( p_intf , '4', INTF_KEY_SET_CHANNEL, 4);
293 intf_AssignKey( p_intf , '5', INTF_KEY_SET_CHANNEL, 5);
294 intf_AssignKey( p_intf , '6', INTF_KEY_SET_CHANNEL, 6);
295 intf_AssignKey( p_intf , '7', INTF_KEY_SET_CHANNEL, 7);
296 intf_AssignKey( p_intf , '8', INTF_KEY_SET_CHANNEL, 8);
297 intf_AssignKey( p_intf , '9', INTF_KEY_SET_CHANNEL, 9);
298 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
299 intf_AssignKey( p_intf , '+', INTF_KEY_INC_VOLUME, 0);
300 intf_AssignKey( p_intf , '-', INTF_KEY_DEC_VOLUME, 0);
301 intf_AssignKey( p_intf , 'm', INTF_KEY_TOGGLE_VOLUME, 0);
302 intf_AssignKey( p_intf , 'M', INTF_KEY_TOGGLE_VOLUME, 0);
303 intf_AssignKey( p_intf , 'g', INTF_KEY_DEC_GAMMA, 0);
304 intf_AssignKey( p_intf , 'G', INTF_KEY_INC_GAMMA, 0);
305 intf_AssignKey( p_intf , 'c', INTF_KEY_TOGGLE_GRAYSCALE, 0);
306 intf_AssignKey( p_intf , ' ', INTF_KEY_TOGGLE_INTERFACE, 0);
307 intf_AssignKey( p_intf , 'i', INTF_KEY_TOGGLE_INFO, 0);
308 intf_AssignKey( p_intf , 's', INTF_KEY_TOGGLE_SCALING, 0);
309 intf_AssignKey( p_intf , 'd', INTF_KEY_DUMP_STREAM, 0);
312 /*****************************************************************************
313 * intf_ProcessKey: process standard keys
314 *****************************************************************************
315 * This function will process standard keys and return non 0 if the key was
317 *****************************************************************************/
318 int intf_ProcessKey( intf_thread_t *p_intf, int g_key )
323 k_reply = intf_GetKey( p_intf, g_key);
324 switch( k_reply.key )
326 case INTF_KEY_QUIT: /* quit order */
330 case INTF_KEY_SET_CHANNEL:
331 /* Change channel - return code is ignored since SelectChannel displays
332 * its own error messages */
333 /* intf_SelectChannel( p_intf, k_reply.param ); */
334 /* network_ChannelJoin() */
335 /* FIXME : keyboard event is for the time being half handled by the interface
336 * half handled directly by the plugins. We should decide what to do. */
339 case INTF_KEY_INC_VOLUME: /* volume + */
340 vlc_mutex_lock( &p_aout_bank->lock );
341 for( i_index = 0 ; i_index < p_aout_bank->i_count ; i_index++ )
343 if( p_aout_bank->pp_aout[i_index]->i_volume
344 < VOLUME_MAX - VOLUME_STEP )
346 p_aout_bank->pp_aout[i_index]->i_volume += VOLUME_STEP;
350 p_aout_bank->pp_aout[i_index]->i_volume = VOLUME_MAX;
353 vlc_mutex_unlock( &p_aout_bank->lock );
356 case INTF_KEY_DEC_VOLUME: /* volume - */
357 vlc_mutex_lock( &p_aout_bank->lock );
358 for( i_index = 0 ; i_index < p_aout_bank->i_count ; i_index++ )
360 if( p_aout_bank->pp_aout[i_index]->i_volume > VOLUME_STEP )
362 p_aout_bank->pp_aout[i_index]->i_volume -= VOLUME_STEP;
366 p_aout_bank->pp_aout[i_index]->i_volume = 0;
369 vlc_mutex_unlock( &p_aout_bank->lock );
372 case INTF_KEY_TOGGLE_VOLUME: /* toggle mute */
373 vlc_mutex_lock( &p_aout_bank->lock );
374 for( i_index = 0 ; i_index < p_aout_bank->i_count ; i_index++ )
376 if( p_aout_bank->pp_aout[i_index]->i_savedvolume )
378 p_aout_bank->pp_aout[i_index]->i_volume =
379 p_aout_bank->pp_aout[i_index]->i_savedvolume;
380 p_aout_bank->pp_aout[i_index]->i_savedvolume = 0;
384 p_aout_bank->pp_aout[i_index]->i_savedvolume =
385 p_aout_bank->pp_aout[i_index]->i_volume;
386 p_aout_bank->pp_aout[i_index]->i_volume = 0;
389 vlc_mutex_unlock( &p_aout_bank->lock );
392 /* XXX: fix this later */
394 case INTF_KEY_DEC_GAMMA: /* gamma - */
395 if( (p_main->p_vout != NULL) && (p_main->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
397 /* FIXME: we should lock if called from the interface */
398 p_main->p_vout->f_gamma -= INTF_GAMMA_STEP;
399 p_main->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
402 case INTF_KEY_INC_GAMMA: /* gamma + */
403 if( (p_main->p_vout != NULL) && (p_main->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
405 /* FIXME: we should lock if called from the interface */
406 p_main->p_vout->f_gamma += INTF_GAMMA_STEP;
407 p_main->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
412 case INTF_KEY_DUMP_STREAM:
413 if( p_intf->p_input != NULL )
415 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
416 input_DumpStream( p_intf->p_input );
417 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
421 default: /* unknown key */