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 *****************************************************************************/
45 #include "vlc_interface.h"
46 #include "modules/modules.h" // Gruik!
48 /*****************************************************************************
50 *****************************************************************************/
51 static void RunInterface( intf_thread_t *p_intf );
53 static int SwitchIntfCallback( vlc_object_t *, char const *,
54 vlc_value_t , vlc_value_t , void * );
55 static int AddIntfCallback( vlc_object_t *, char const *,
56 vlc_value_t , vlc_value_t , void * );
58 /*****************************************************************************
59 * intf_Create: prepare interface before main loop
60 *****************************************************************************
61 * This function opens output devices and creates specific interfaces. It sends
62 * its own error messages.
63 *****************************************************************************/
65 * Create the interface, and prepare it for main loop.
66 * You can give some additional options to be used for interface initialization
68 * \param p_this the calling vlc_object_t
69 * \param psz_module a preferred interface module
70 * \param i_options number additional options
71 * \param ppsz_options additional option strings
72 * \return a pointer to the created interface thread, NULL on error
74 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
75 int i_options, const char *const *ppsz_options )
77 intf_thread_t * p_intf;
80 /* Allocate structure */
81 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
84 msg_Err( p_this, "out of memory" );
87 p_intf->pf_request_window = NULL;
88 p_intf->pf_release_window = NULL;
89 p_intf->pf_control_window = NULL;
90 p_intf->b_play = VLC_FALSE;
91 p_intf->b_interaction = VLC_FALSE;
92 p_intf->b_should_run_on_first_thread = VLC_FALSE;
94 for( i = 0 ; i< i_options; i++ )
96 var_OptionParse( p_intf, ppsz_options[i] );
99 /* Choose the best module */
100 p_intf->psz_intf = strdup( psz_module );
101 p_intf->p_module = module_Need( p_intf, "interface", psz_module, VLC_FALSE );
103 if( p_intf->p_module == NULL )
105 msg_Err( p_intf, "no suitable interface module" );
106 free( p_intf->psz_intf );
107 vlc_object_destroy( p_intf );
111 /* Initialize structure */
112 p_intf->b_menu = VLC_FALSE;
113 p_intf->b_menu_change = VLC_FALSE;
115 /* Initialize mutexes */
116 vlc_mutex_init( p_intf, &p_intf->change_lock );
118 /* Attach interface to its parent object */
119 vlc_object_attach( p_intf, p_this );
124 /*****************************************************************************
125 * intf_RunThread: launch the interface thread
126 *****************************************************************************
127 * This function either creates a new thread and runs the interface in it.
128 *****************************************************************************/
130 * Starts and runs the interface thread.
132 * \param p_intf the interface thread
133 * \return VLC_SUCCESS on success, an error number else
135 int intf_RunThread( intf_thread_t *p_intf )
137 /* This interface doesn't need to be run */
138 if( p_intf->pf_run == NULL )
141 /* Hack to get Mac OS X Cocoa runtime running
142 * (it needs access to the main thread) */
143 if( p_intf->b_should_run_on_first_thread )
145 RunInterface( p_intf );
149 /* Run the interface in a separate thread */
150 if( vlc_thread_create( p_intf, "interface", RunInterface,
151 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
153 msg_Err( p_intf, "cannot spawn interface thread" );
161 * Stops the interface thread
163 * This function asks the interface thread to stop
164 * \param p_intf the interface thread
167 void intf_StopThread( intf_thread_t *p_intf )
169 /* Tell the interface to die */
170 vlc_object_kill( p_intf );
171 if( p_intf->pf_run != NULL )
173 vlc_cond_signal( &p_intf->object_wait );
174 vlc_thread_join( p_intf );
179 * \brief Destroy the interface after the main loop endeed.
181 * Destroys interfaces and closes output devices
182 * \param p_intf the interface thread
185 void intf_Destroy( intf_thread_t *p_intf )
187 /* Unlock module if present (a switch may have failed) */
188 if( p_intf->p_module )
190 module_Unneed( p_intf, p_intf->p_module );
192 free( p_intf->psz_intf );
194 vlc_mutex_destroy( &p_intf->change_lock );
197 vlc_object_destroy( p_intf );
201 /* Following functions are local */
203 /*****************************************************************************
204 * RunInterface: setups necessary data and give control to the interface
205 *****************************************************************************/
206 static void RunInterface( intf_thread_t *p_intf )
208 static const char *ppsz_interfaces[] =
212 "wxwidgets", "wxWidgets",
216 const char **ppsz_parser;
220 vlc_value_t val, text;
223 /* Variable used for interface switching */
224 p_intf->psz_switch_intf = NULL;
225 var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
226 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
227 text.psz_string = _("Switch interface");
228 var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
230 /* Only fill the list with available modules */
231 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
232 for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
234 for( i = 0; i < p_list->i_count; i++ )
236 module_t *p_module = (module_t *)p_list->p_values[i].p_object;
237 if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
239 val.psz_string = (char *)ppsz_parser[0];
240 text.psz_string = (char *)_(ppsz_parser[1]);
241 var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
247 vlc_list_release( p_list );
249 var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
251 /* Variable used for interface spawning */
252 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
253 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
254 text.psz_string = _("Add Interface");
255 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
257 val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
258 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
259 val.psz_string = (char *)"telnet";
260 text.psz_string = (char *)_("Telnet Interface");
261 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
262 val.psz_string = (char *)"http";
263 text.psz_string = (char *)_("Web Interface");
264 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
265 val.psz_string = (char *)"logger";
266 text.psz_string = (char *)_("Debug logging");
267 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
268 val.psz_string = (char *)"gestures";
269 text.psz_string = (char *)_("Mouse Gestures");
270 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
272 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
276 /* Give control to the interface */
277 p_intf->pf_run( p_intf );
279 /* Reset play on start status */
280 p_intf->b_play = VLC_FALSE;
282 if( !p_intf->psz_switch_intf )
287 /* Make sure the old interface is completely uninitialized */
288 module_Unneed( p_intf, p_intf->p_module );
290 /* Provide ability to switch the main interface on the fly */
291 psz_intf = p_intf->psz_switch_intf;
292 p_intf->psz_switch_intf = NULL;
294 vlc_mutex_lock( &p_intf->object_lock );
295 p_intf->b_die = VLC_FALSE; /* FIXME */
296 p_intf->b_dead = VLC_FALSE;
298 vlc_mutex_unlock( &p_intf->object_lock );
300 p_intf->psz_intf = psz_intf;
301 p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
303 while( p_intf->p_module );
306 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
307 vlc_value_t oldval, vlc_value_t newval, void *p_data )
309 intf_thread_t *p_intf = (intf_thread_t *)p_this;
310 (void)psz_cmd; (void)oldval; (void)p_data;
312 p_intf->psz_switch_intf =
313 malloc( strlen(newval.psz_string) + sizeof(",none") );
314 sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
315 vlc_object_kill( p_intf );
320 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
321 vlc_value_t oldval, vlc_value_t newval, void *p_data )
323 intf_thread_t *p_intf;
324 char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
326 (void)psz_cmd; (void)oldval; (void)p_data;
328 /* Try to create the interface */
329 sprintf( psz_intf, "%s,none", newval.psz_string );
330 p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
334 msg_Err( p_this, "interface \"%s\" initialization failed",
339 /* Try to run the interface */
340 if( intf_RunThread( p_intf ) != VLC_SUCCESS )
342 vlc_object_detach( p_intf );
343 intf_Destroy( p_intf );