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-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
28 * This file contains functions related to interface management
32 /*****************************************************************************
34 *****************************************************************************/
41 #include "vlc_interface.h"
42 #include "modules/modules.h" // Gruik!
44 /*****************************************************************************
46 *****************************************************************************/
47 static void RunInterface( intf_thread_t *p_intf );
49 static int SwitchIntfCallback( vlc_object_t *, char const *,
50 vlc_value_t , vlc_value_t , void * );
51 static int AddIntfCallback( vlc_object_t *, char const *,
52 vlc_value_t , vlc_value_t , void * );
54 /*****************************************************************************
55 * intf_Create: prepare interface before main loop
56 *****************************************************************************
57 * This function opens output devices and creates specific interfaces. It sends
58 * its own error messages.
59 *****************************************************************************/
61 * Create the interface, and prepare it for main loop.
62 * You can give some additional options to be used for interface initialization
64 * \param p_this the calling vlc_object_t
65 * \param psz_module a preferred interface module
66 * \param i_options number additional options
67 * \param ppsz_options additional option strings
68 * \return a pointer to the created interface thread, NULL on error
70 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
71 int i_options, const char *const *ppsz_options )
73 intf_thread_t * p_intf;
76 /* Allocate structure */
77 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
80 msg_Err( p_this, "out of memory" );
83 p_intf->pf_request_window = NULL;
84 p_intf->pf_release_window = NULL;
85 p_intf->pf_control_window = NULL;
86 p_intf->b_play = VLC_FALSE;
87 p_intf->b_interaction = VLC_FALSE;
89 for( i = 0 ; i< i_options; i++ )
91 var_OptionParse( p_intf, ppsz_options[i] );
94 /* Choose the best module */
95 p_intf->p_module = module_Need( p_intf, "interface", psz_module, VLC_FALSE );
97 if( p_intf->p_module == NULL )
99 msg_Err( p_intf, "no suitable interface module" );
100 vlc_object_destroy( p_intf );
104 /* Initialize structure */
105 p_intf->b_menu = VLC_FALSE;
106 p_intf->b_menu_change = VLC_FALSE;
108 /* Initialize mutexes */
109 vlc_mutex_init( p_intf, &p_intf->change_lock );
111 /* Attach interface to its parent object */
112 vlc_object_attach( p_intf, p_this );
117 /*****************************************************************************
118 * intf_RunThread: launch the interface thread
119 *****************************************************************************
120 * This function either creates a new thread and runs the interface in it.
121 *****************************************************************************/
123 * Starts and runs the interface thread.
125 * \param p_intf the interface thread
126 * \return VLC_SUCCESS on success, an error number else
128 int intf_RunThread( intf_thread_t *p_intf )
130 /* This interface doesn't need to be run */
131 if( p_intf->pf_run == NULL )
134 /* Hack to get Mac OS X Cocoa runtime,
135 * which need access to the main thread */
136 if( p_intf->b_should_run_on_first_thread )
138 RunInterface( p_intf );
142 /* Run the interface in a separate thread */
143 if( vlc_thread_create( p_intf, "interface", RunInterface,
144 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
146 msg_Err( p_intf, "cannot spawn interface thread" );
154 * Stops the interface thread
156 * This function asks the interface thread to stop
157 * \param p_intf the interface thread
160 void intf_StopThread( intf_thread_t *p_intf )
162 /* Tell the interface to die */
163 vlc_object_kill( p_intf );
164 if( p_intf->pf_run != NULL )
166 vlc_cond_signal( &p_intf->object_wait );
167 vlc_thread_join( p_intf );
172 * \brief Destroy the interface after the main loop endeed.
174 * Destroys interfaces and closes output devices
175 * \param p_intf the interface thread
178 void intf_Destroy( intf_thread_t *p_intf )
180 /* Unlock module if present (a switch may have failed) */
181 if( p_intf->p_module )
183 module_Unneed( p_intf, p_intf->p_module );
186 vlc_mutex_destroy( &p_intf->change_lock );
189 vlc_object_destroy( p_intf );
193 /* Following functions are local */
195 /*****************************************************************************
196 * RunInterface: setups necessary data and give control to the interface
197 *****************************************************************************/
198 static void RunInterface( intf_thread_t *p_intf )
200 static const char *ppsz_interfaces[] =
204 "wxwidgets", "wxWidgets",
208 const char **ppsz_parser;
212 vlc_value_t val, text;
215 /* Variable used for interface switching */
216 p_intf->psz_switch_intf = NULL;
217 var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
218 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
219 text.psz_string = _("Switch interface");
220 var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
222 /* Only fill the list with available modules */
223 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
224 for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
226 for( i = 0; i < p_list->i_count; i++ )
228 module_t *p_module = (module_t *)p_list->p_values[i].p_object;
229 if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
231 val.psz_string = (char *)ppsz_parser[0];
232 text.psz_string = (char *)_(ppsz_parser[1]);
233 var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
239 vlc_list_release( p_list );
241 var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
243 /* Variable used for interface spawning */
244 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
245 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
246 text.psz_string = _("Add Interface");
247 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
249 val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
250 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
251 val.psz_string = (char *)"telnet";
252 text.psz_string = (char *)_("Telnet Interface");
253 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
254 val.psz_string = (char *)"http";
255 text.psz_string = (char *)_("Web Interface");
256 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
257 val.psz_string = (char *)"logger";
258 text.psz_string = (char *)_("Debug logging");
259 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
260 val.psz_string = (char *)"gestures";
261 text.psz_string = (char *)_("Mouse Gestures");
262 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
264 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
268 /* Give control to the interface */
269 p_intf->pf_run( p_intf );
271 /* Reset play on start status */
272 p_intf->b_play = VLC_FALSE;
274 if( !p_intf->psz_switch_intf )
279 /* Make sure the old interface is completely uninitialized */
280 module_Unneed( p_intf, p_intf->p_module );
282 /* Provide ability to switch the main interface on the fly */
283 psz_intf = p_intf->psz_switch_intf;
284 p_intf->psz_switch_intf = NULL;
286 vlc_mutex_lock( &p_intf->object_lock );
287 p_intf->b_die = VLC_FALSE; /* FIXME */
288 p_intf->b_dead = VLC_FALSE;
289 vlc_mutex_unlock( &p_intf->object_lock );
291 p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
294 while( p_intf->p_module );
297 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
298 vlc_value_t oldval, vlc_value_t newval, void *p_data )
300 intf_thread_t *p_intf = (intf_thread_t *)p_this;
301 (void)psz_cmd; (void)oldval; (void)p_data;
303 p_intf->psz_switch_intf =
304 malloc( strlen(newval.psz_string) + sizeof(",none") );
305 sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
306 vlc_object_kill( p_intf );
311 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
312 vlc_value_t oldval, vlc_value_t newval, void *p_data )
314 intf_thread_t *p_intf;
315 char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
317 (void)psz_cmd; (void)oldval; (void)p_data;
319 /* Try to create the interface */
320 sprintf( psz_intf, "%s,none", newval.psz_string );
321 p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
325 msg_Err( p_this, "interface \"%s\" initialization failed",
330 /* Try to run the interface */
331 if( intf_RunThread( p_intf ) != VLC_SUCCESS )
333 vlc_object_detach( p_intf );
334 intf_Destroy( p_intf );