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-2001 VideoLAN
7 * $Id: interface.c,v 1.82 2001/12/07 18:33:08 sam 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"
48 #include "interface.h"
49 #include "intf_playlist.h"
50 #include "intf_channels.h"
51 #include "keystrokes.h"
54 #include "video_output.h"
56 /*****************************************************************************
58 *****************************************************************************/
59 static void intf_Manage( intf_thread_t *p_intf );
61 /*****************************************************************************
62 * intf_Create: prepare interface before main loop
63 *****************************************************************************
64 * This function opens output devices and creates specific interfaces. It sends
65 * its own error messages.
66 *****************************************************************************/
67 intf_thread_t* intf_Create( void )
69 intf_thread_t * p_intf;
71 /* Allocate structure */
72 p_intf = malloc( sizeof( intf_thread_t ) );
75 intf_ErrMsg( "intf error: cannot create interface thread (%s)",
80 /* Choose the best module */
81 p_intf->p_module = module_Need( MODULE_CAPABILITY_INTF, NULL );
83 if( p_intf->p_module == NULL )
85 intf_ErrMsg( "intf error: no suitable intf module" );
90 #define f p_intf->p_module->p_functions->intf.functions.intf
91 p_intf->pf_open = f.pf_open;
92 p_intf->pf_close = f.pf_close;
93 p_intf->pf_run = f.pf_run;
96 /* Initialize callbacks */
97 p_intf->pf_manage = intf_Manage;
99 /* Initialize structure */
102 p_intf->p_input = NULL;
103 p_intf->p_keys = NULL;
105 p_intf->b_menu_change = 0;
107 if( p_intf->pf_open( p_intf ) )
109 intf_ErrMsg("intf error: cannot create interface");
110 module_Unneed( p_intf->p_module );
115 /* Initialize mutexes */
116 vlc_mutex_init( &p_intf->change_lock );
118 /* Load channels - the pointer will be set to NULL on failure. The
119 * return value is ignored since the program can work without
121 intf_LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR,
122 INTF_CHANNELS_DEFAULT ));
124 intf_WarnMsg( 1, "intf: interface initialized");
128 /*****************************************************************************
129 * intf_Manage: manage interface
130 *****************************************************************************
131 * This function has to be called regularly by the interface plugin. It
132 * checks for playlist end, module expiration, message flushing, and a few
133 * other useful things.
134 *****************************************************************************/
135 static void intf_Manage( intf_thread_t *p_intf )
137 /* Flush waiting messages */
140 /* Manage module bank */
141 module_ManageBank( );
143 if( ( p_intf->p_input != NULL ) &&
144 ( p_intf->p_input->b_error || p_intf->p_input->b_eof ) )
146 input_DestroyThread( p_intf->p_input, NULL );
147 p_intf->p_input = NULL;
148 intf_DbgMsg("Input thread destroyed");
151 /* If no stream is being played, try to find one */
152 if( p_intf->p_input == NULL && !p_intf->b_die )
154 // vlc_mutex_lock( &p_main->p_playlist->change_lock );
156 if( !p_main->p_playlist->b_stopped )
158 /* Select the next playlist item */
159 intf_PlaylistNext( p_main->p_playlist );
161 /* don't loop by default: stop at playlist end */
162 if( p_main->p_playlist->i_index == -1 )
164 p_main->p_playlist->b_stopped = 1;
168 p_main->p_playlist->b_stopped = 0;
169 p_main->p_playlist->i_mode = PLAYLIST_FORWARD +
170 main_GetIntVariable( PLAYLIST_LOOP_VAR,
171 PLAYLIST_LOOP_DEFAULT );
173 input_CreateThread( &p_main->p_playlist->current, NULL );
178 /* playing has been stopped: we no longer need outputs */
179 if( p_aout_bank->i_count )
181 /* FIXME kludge that does not work with several outputs */
182 aout_DestroyThread( p_aout_bank->pp_aout[0], NULL );
183 p_aout_bank->i_count--;
185 if( p_vout_bank->i_count )
187 vout_DestroyThread( p_vout_bank->pp_vout[0], NULL );
188 p_vout_bank->i_count--;
192 // vlc_mutex_unlock( &p_main->p_playlist->change_lock );
196 /*****************************************************************************
197 * intf_Destroy: clean interface after main loop
198 *****************************************************************************
199 * This function destroys specific interfaces and close output devices.
200 *****************************************************************************/
201 void intf_Destroy( intf_thread_t *p_intf )
206 /* Unload channels */
207 intf_UnloadChannels( p_intf );
209 /* Destroy interfaces */
210 p_intf->pf_close( p_intf );
212 /* Close input thread, if any (blocking) */
213 if( p_intf->p_input )
215 input_DestroyThread( p_intf->p_input, NULL );
219 p_cur = p_intf->p_keys;
220 while( p_cur != NULL)
222 p_next = p_cur->next;
228 module_Unneed( p_intf->p_module );
230 vlc_mutex_destroy( &p_intf->change_lock );
236 /*****************************************************************************
237 * intf_AssignKey: assign standartkeys *
238 *****************************************************************************
239 * This function fills in the associative array that links the key pressed *
240 * and the key we use internally. Support one extra parameter. *
241 ****************************************************************************/
242 void intf_AssignKey( intf_thread_t *p_intf, int r_key, int f_key, int param)
244 p_intf_key p_cur = p_intf->p_keys;
247 p_cur = (p_intf_key )(malloc ( sizeof( intf_key ) ) );
248 p_cur->received_key = r_key;
249 p_cur->forwarded.key = f_key;
250 p_cur->forwarded.param = param;
252 p_intf->p_keys = p_cur;
256 while( p_cur->next != NULL && p_cur ->received_key != r_key)
260 if( p_cur->next == NULL )
262 p_cur->next = ( p_intf_key )( malloc( sizeof( intf_key ) ) );
265 p_cur->forwarded.param = param;
266 p_cur->received_key = r_key;
268 p_cur->forwarded.key = f_key;
272 /* Basic getKey function... */
273 keyparm intf_GetKey( intf_thread_t *p_intf, int r_key)
277 p_intf_key current = p_intf->p_keys;
278 while(current != NULL && current->received_key != r_key)
280 current = current->next;
283 { /* didn't find any key in the array */
284 reply.key = INTF_KEY_UNKNOWN;
289 reply.key = current->forwarded.key;
290 reply.param = current->forwarded.param;
295 /*****************************************************************************
296 * intf_AssignNormalKeys: used for normal interfaces.
297 *****************************************************************************
298 * This function assign the basic key to the normal keys.
299 *****************************************************************************/
301 void intf_AssignNormalKeys( intf_thread_t *p_intf)
303 p_intf->p_intf_get_key = intf_GetKey;
305 intf_AssignKey( p_intf , 'Q', INTF_KEY_QUIT, 0);
306 intf_AssignKey( p_intf , 'q', INTF_KEY_QUIT, 0);
307 intf_AssignKey( p_intf , 27, INTF_KEY_QUIT, 0);
308 intf_AssignKey( p_intf , 3, INTF_KEY_QUIT, 0);
309 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
310 intf_AssignKey( p_intf , '1', INTF_KEY_SET_CHANNEL, 1);
311 intf_AssignKey( p_intf , '2', INTF_KEY_SET_CHANNEL, 2);
312 intf_AssignKey( p_intf , '3', INTF_KEY_SET_CHANNEL, 3);
313 intf_AssignKey( p_intf , '4', INTF_KEY_SET_CHANNEL, 4);
314 intf_AssignKey( p_intf , '5', INTF_KEY_SET_CHANNEL, 5);
315 intf_AssignKey( p_intf , '6', INTF_KEY_SET_CHANNEL, 6);
316 intf_AssignKey( p_intf , '7', INTF_KEY_SET_CHANNEL, 7);
317 intf_AssignKey( p_intf , '8', INTF_KEY_SET_CHANNEL, 8);
318 intf_AssignKey( p_intf , '9', INTF_KEY_SET_CHANNEL, 9);
319 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
320 intf_AssignKey( p_intf , '+', INTF_KEY_INC_VOLUME, 0);
321 intf_AssignKey( p_intf , '-', INTF_KEY_DEC_VOLUME, 0);
322 intf_AssignKey( p_intf , 'm', INTF_KEY_TOGGLE_VOLUME, 0);
323 intf_AssignKey( p_intf , 'M', INTF_KEY_TOGGLE_VOLUME, 0);
324 intf_AssignKey( p_intf , 'g', INTF_KEY_DEC_GAMMA, 0);
325 intf_AssignKey( p_intf , 'G', INTF_KEY_INC_GAMMA, 0);
326 intf_AssignKey( p_intf , 'c', INTF_KEY_TOGGLE_GRAYSCALE, 0);
327 intf_AssignKey( p_intf , ' ', INTF_KEY_TOGGLE_INTERFACE, 0);
328 intf_AssignKey( p_intf , 'i', INTF_KEY_TOGGLE_INFO, 0);
329 intf_AssignKey( p_intf , 's', INTF_KEY_TOGGLE_SCALING, 0);
330 intf_AssignKey( p_intf , 'd', INTF_KEY_DUMP_STREAM, 0);
333 /*****************************************************************************
334 * intf_ProcessKey: process standard keys
335 *****************************************************************************
336 * This function will process standard keys and return non 0 if the key was
338 *****************************************************************************/
339 int intf_ProcessKey( intf_thread_t *p_intf, int g_key )
344 k_reply = intf_GetKey( p_intf, g_key);
345 switch( k_reply.key )
347 case INTF_KEY_QUIT: /* quit order */
351 case INTF_KEY_SET_CHANNEL:
352 /* Change channel - return code is ignored since SelectChannel displays
353 * its own error messages */
354 /* intf_SelectChannel( p_intf, k_reply.param ); */
355 /* network_ChannelJoin() */
356 /* FIXME : keyboard event is for the time being half handled by the interface
357 * half handled directly by the plugins. We should decide what to do. */
360 case INTF_KEY_INC_VOLUME: /* volume + */
361 vlc_mutex_lock( &p_aout_bank->lock );
362 for( i_index = 0 ; i_index < p_aout_bank->i_count ; i_index++ )
364 if( p_aout_bank->pp_aout[i_index]->i_volume
365 < VOLUME_MAX - VOLUME_STEP )
367 p_aout_bank->pp_aout[i_index]->i_volume += VOLUME_STEP;
371 p_aout_bank->pp_aout[i_index]->i_volume = VOLUME_MAX;
374 vlc_mutex_unlock( &p_aout_bank->lock );
377 case INTF_KEY_DEC_VOLUME: /* volume - */
378 vlc_mutex_lock( &p_aout_bank->lock );
379 for( i_index = 0 ; i_index < p_aout_bank->i_count ; i_index++ )
381 if( p_aout_bank->pp_aout[i_index]->i_volume > VOLUME_STEP )
383 p_aout_bank->pp_aout[i_index]->i_volume -= VOLUME_STEP;
387 p_aout_bank->pp_aout[i_index]->i_volume = 0;
390 vlc_mutex_unlock( &p_aout_bank->lock );
393 case INTF_KEY_TOGGLE_VOLUME: /* toggle mute */
394 /* Start/stop feeding audio data. */
395 if( p_intf->p_input != NULL )
397 input_ToggleMute( p_intf->p_input );
400 /* Start/stop playing sound. */
401 vlc_mutex_lock( &p_aout_bank->lock );
402 for( i_index = 0 ; i_index < p_aout_bank->i_count ; i_index++ )
404 if( p_aout_bank->pp_aout[i_index]->i_savedvolume )
406 p_aout_bank->pp_aout[i_index]->i_volume =
407 p_aout_bank->pp_aout[i_index]->i_savedvolume;
408 p_aout_bank->pp_aout[i_index]->i_savedvolume = 0;
412 p_aout_bank->pp_aout[i_index]->i_savedvolume =
413 p_aout_bank->pp_aout[i_index]->i_volume;
414 p_aout_bank->pp_aout[i_index]->i_volume = 0;
417 vlc_mutex_unlock( &p_aout_bank->lock );
420 /* XXX: fix this later */
422 case INTF_KEY_DEC_GAMMA: /* gamma - */
423 if( (p_main->p_vout != NULL) && (p_main->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
425 /* FIXME: we should lock if called from the interface */
426 p_main->p_vout->f_gamma -= INTF_GAMMA_STEP;
427 p_main->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
430 case INTF_KEY_INC_GAMMA: /* gamma + */
431 if( (p_main->p_vout != NULL) && (p_main->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
433 /* FIXME: we should lock if called from the interface */
434 p_main->p_vout->f_gamma += INTF_GAMMA_STEP;
435 p_main->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
440 case INTF_KEY_DUMP_STREAM:
441 if( p_intf->p_input != NULL )
443 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
444 input_DumpStream( p_intf->p_input );
445 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
449 default: /* unknown key */