1 /*******************************************************************************
2 * interface.c: interface access for other threads
4 *******************************************************************************
5 * This library provides basic functions for threads to interact with user
6 * interface, such as command line.
7 *******************************************************************************/
9 /*******************************************************************************
11 *******************************************************************************/
16 #include <sys/types.h>
17 #include <sys/uio.h> /* for input.h */
22 #include "vlc_thread.h"
25 #include "interface.h"
27 #include "intf_console.h"
30 #include "video_output.h"
34 /*******************************************************************************
36 *******************************************************************************/
38 /* INTF_INPUT_CFG: pre-configured inputs */
39 #define INTF_MAX_INPUT_CFG 10
40 static const input_cfg_t INTF_INPUT_CFG[] =
43 * file host ip port vlan */
45 /* Local input (unicast) */
46 { INPUT_CFG_METHOD | INPUT_CFG_IP, INPUT_METHOD_TS_UCAST,
47 NULL, NULL, "127.0.0.1", 0, 0 },
50 { INPUT_CFG_METHOD | INPUT_CFG_VLAN, INPUT_METHOD_TS_VLAN_BCAST,
51 NULL, NULL, NULL, 0, 0 },
52 { INPUT_CFG_METHOD | INPUT_CFG_VLAN, INPUT_METHOD_TS_VLAN_BCAST,
53 NULL, NULL, NULL, 0, 1 },
54 { INPUT_CFG_METHOD | INPUT_CFG_VLAN, INPUT_METHOD_TS_VLAN_BCAST,
55 NULL, NULL, NULL, 0, 2 },
56 { INPUT_CFG_METHOD | INPUT_CFG_VLAN, INPUT_METHOD_TS_VLAN_BCAST,
57 NULL, NULL, NULL, 0, 3 },
58 { INPUT_CFG_METHOD | INPUT_CFG_VLAN, INPUT_METHOD_TS_VLAN_BCAST,
59 NULL, NULL, NULL, 0, 4 },
60 { INPUT_CFG_METHOD | INPUT_CFG_VLAN, INPUT_METHOD_TS_VLAN_BCAST,
61 NULL, NULL, NULL, 0, 5 },
62 { INPUT_CFG_METHOD | INPUT_CFG_VLAN, INPUT_METHOD_TS_VLAN_BCAST,
63 NULL, NULL, NULL, 0, 6 },
64 { INPUT_CFG_METHOD | INPUT_CFG_VLAN, INPUT_METHOD_TS_VLAN_BCAST,
65 NULL, NULL, NULL, 0, 7 },
66 { INPUT_CFG_METHOD | INPUT_CFG_VLAN, INPUT_METHOD_TS_VLAN_BCAST,
67 NULL, NULL, NULL, 0, 8 }
70 /*******************************************************************************
71 * intf_Create: prepare interface before main loop
72 *******************************************************************************
73 * This function opens output devices and create specific interfaces. It send
74 * it's own error messages.
75 *******************************************************************************/
76 intf_thread_t* intf_Create( void )
78 intf_thread_t *p_intf;
80 /* Allocate structure */
81 p_intf = malloc( sizeof( intf_thread_t ) );
84 intf_ErrMsg("error: %s\n", strerror( ENOMEM ) );
88 /* Initialize structure */
90 p_intf->p_vout = NULL;
91 p_intf->p_input = NULL;
93 /* Start interfaces */
94 p_intf->p_console = intf_ConsoleCreate();
95 if( p_intf->p_console == NULL )
97 intf_ErrMsg("error: can't create control console\n");
101 if( intf_SysCreate( p_intf ) )
103 intf_ErrMsg("error: can't create interface\n");
104 intf_ConsoleDestroy( p_intf->p_console );
109 intf_Msg("Interface initialized\n");
113 /*******************************************************************************
115 *******************************************************************************
116 * Initialization script and main interface loop.
117 *******************************************************************************/
118 void intf_Run( intf_thread_t *p_intf )
120 /* Execute the initialization script - if a positive number is returned,
121 * the script could be executed but failed */
122 if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
124 intf_ErrMsg("warning: error(s) during startup script\n");
128 while(!p_intf->b_die)
130 /* Flush waiting messages */
133 /* Manage specific interface */
134 intf_SysManage( p_intf );
136 /* Check attached threads status */
137 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
139 //?? add aout error detection
142 if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
144 input_DestroyThread( p_intf->p_input /*, NULL */ );
145 p_intf->p_input = NULL;
146 intf_DbgMsg("Input thread destroyed\n");
149 /* Sleep to avoid using all CPU - since some interfaces needs to access
150 * keyboard events, a 100ms delay is a good compromise */
151 msleep( INTF_IDLE_SLEEP );
155 /*******************************************************************************
156 * intf_Destroy: clean interface after main loop
157 *******************************************************************************
158 * This function destroys specific interfaces and close output devices.
159 *******************************************************************************/
160 void intf_Destroy( intf_thread_t *p_intf )
162 /* Destroy interfaces */
163 intf_SysDestroy( p_intf );
164 intf_ConsoleDestroy( p_intf->p_console );
170 /*******************************************************************************
171 * intf_SelectInput: change input stream
172 *******************************************************************************
173 * Kill existing input, if any, and try to open a new one, using an input
174 * configuration table.
175 *******************************************************************************/
176 int intf_SelectInput( intf_thread_t * p_intf, int i_index )
178 intf_DbgMsg("0x%x\n", p_intf );
180 /* Kill existing input, if any */
181 if( p_intf->p_input != NULL )
183 input_DestroyThread( p_intf->p_input /*??, NULL*/ );
186 /* Check that input index is valid */
187 if( (i_index < 0) || (INTF_MAX_INPUT_CFG < i_index) )
189 p_intf->p_input = NULL;
193 /* Open a new input */
194 p_intf->p_input = input_CreateThread( &INTF_INPUT_CFG[ i_index ] /*??, NULL*/ );
195 return( p_intf->p_input == NULL );
198 /*******************************************************************************
199 * intf_ProcessKey: process standard keys
200 *******************************************************************************
201 * This function will process standard keys and return non 0 if the key was
203 *******************************************************************************/
204 int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
208 case 'Q': /* quit order */
213 case '0': /* source change */
223 if( intf_SelectInput( p_intf, i_key - '0' ) )
225 intf_ErrMsg("error: can not open channel %d\n", i_key - '0');
228 case '+': /* volume + */
231 case '-': /* volume - */
234 case 'M': /* toggle mute */
238 case 'g': /* gamma - */
239 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
241 vlc_mutex_lock( &p_intf->p_vout->change_lock );
242 p_intf->p_vout->f_gamma -= INTF_GAMMA_STEP;
243 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
244 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
247 case 'G': /* gamma + */
248 if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
250 vlc_mutex_lock( &p_intf->p_vout->change_lock );
251 p_intf->p_vout->f_gamma += INTF_GAMMA_STEP;
252 p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
253 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
256 case 'c': /* toggle grayscale */
257 if( p_intf->p_vout != NULL )
259 vlc_mutex_lock( &p_intf->p_vout->change_lock );
260 p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
261 p_intf->p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
262 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
265 case ' ': /* toggle info */
266 if( p_intf->p_vout != NULL )
268 vlc_mutex_lock( &p_intf->p_vout->change_lock );
269 p_intf->p_vout->b_info = !p_intf->p_vout->b_info;
270 p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
271 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
274 default: /* unknown key */