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!
49 /*****************************************************************************
51 *****************************************************************************/
52 static void RunInterface( intf_thread_t *p_intf );
54 static int AddIntfCallback( vlc_object_t *, char const *,
55 vlc_value_t , vlc_value_t , void * );
57 /*****************************************************************************
58 * intf_Create: prepare interface before main loop
59 *****************************************************************************
60 * This function opens output devices and creates specific interfaces. It sends
61 * its own error messages.
62 *****************************************************************************/
64 * Create the interface, and prepare it for main loop.
65 * You can give some additional options to be used for interface initialization
67 * \param p_this the calling vlc_object_t
68 * \param psz_module a preferred interface module
69 * \param i_options number additional options
70 * \param ppsz_options additional option strings
71 * \return a pointer to the created interface thread, NULL on error
73 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
74 int i_options, const char *const *ppsz_options )
76 intf_thread_t * p_intf;
79 /* Allocate structure */
80 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
83 msg_Err( p_this, "out of memory" );
86 p_intf->pf_request_window = NULL;
87 p_intf->pf_release_window = NULL;
88 p_intf->pf_control_window = NULL;
89 p_intf->b_play = VLC_FALSE;
90 p_intf->b_interaction = VLC_FALSE;
91 p_intf->b_should_run_on_first_thread = VLC_FALSE;
93 for( i = 0 ; i< i_options; i++ )
94 var_OptionParse( p_this, ppsz_options[i], true );
96 /* Choose the best module */
97 p_intf->psz_intf = strdup( psz_module );
98 p_intf->p_module = module_Need( p_intf, "interface", psz_module, VLC_FALSE );
100 if( p_intf->p_module == NULL )
102 msg_Err( p_intf, "no suitable interface module" );
103 free( p_intf->psz_intf );
104 vlc_object_release( p_intf );
108 /* Initialize structure */
109 p_intf->b_menu = VLC_FALSE;
110 p_intf->b_menu_change = VLC_FALSE;
112 /* Initialize mutexes */
113 vlc_mutex_init( p_intf, &p_intf->change_lock );
115 /* Attach interface to its parent object */
116 vlc_object_attach( p_intf, p_this );
121 /*****************************************************************************
122 * intf_RunThread: launch the interface thread
123 *****************************************************************************
124 * This function either creates a new thread and runs the interface in it.
125 *****************************************************************************/
127 * Starts and runs the interface thread.
129 * \param p_intf the interface thread
130 * \return VLC_SUCCESS on success, an error number else
132 int intf_RunThread( intf_thread_t *p_intf )
134 /* This interface doesn't need to be run */
135 if( p_intf->pf_run == NULL )
138 /* Hack to get Mac OS X Cocoa runtime running
139 * (it needs access to the main thread) */
140 if( p_intf->b_should_run_on_first_thread )
142 RunInterface( p_intf );
146 /* Run the interface in a separate thread */
147 if( vlc_thread_create( p_intf, "interface", RunInterface,
148 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
150 msg_Err( p_intf, "cannot spawn interface thread" );
158 * Stops the interface thread
160 * This function asks the interface thread to stop
161 * \param p_intf the interface thread
164 void intf_StopThread( intf_thread_t *p_intf )
166 /* Tell the interface to die */
167 vlc_object_kill( p_intf );
168 if( p_intf->pf_run != NULL )
170 vlc_cond_signal( &p_intf->object_wait );
171 vlc_thread_join( p_intf );
176 * \brief Destroy the interface after the main loop endeed.
178 * Destroys interfaces and closes output devices
179 * \param p_intf the interface thread
182 void intf_Destroy( intf_thread_t *p_intf )
184 /* Unlock module if present (a switch may have failed) */
185 if( p_intf->p_module )
187 module_Unneed( p_intf, p_intf->p_module );
189 free( p_intf->psz_intf );
191 vlc_mutex_destroy( &p_intf->change_lock );
194 vlc_object_release( p_intf );
198 /* Following functions are local */
200 /*****************************************************************************
201 * RunInterface: setups necessary data and give control to the interface
202 *****************************************************************************/
203 static void RunInterface( intf_thread_t *p_intf )
205 vlc_value_t val, text;
208 /* Variable used for interface spawning */
209 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
210 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
211 text.psz_string = _("Add Interface");
212 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
214 val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
215 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
216 val.psz_string = (char *)"telnet";
217 text.psz_string = (char *)_("Telnet Interface");
218 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
219 val.psz_string = (char *)"http";
220 text.psz_string = (char *)_("Web Interface");
221 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
222 val.psz_string = (char *)"logger";
223 text.psz_string = (char *)_("Debug logging");
224 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
225 val.psz_string = (char *)"gestures";
226 text.psz_string = (char *)_("Mouse Gestures");
227 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
229 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
233 /* Give control to the interface */
234 p_intf->pf_run( p_intf );
236 /* Reset play on start status */
237 p_intf->b_play = VLC_FALSE;
239 if( !p_intf->psz_switch_intf )
244 /* Make sure the old interface is completely uninitialized */
245 module_Unneed( p_intf, p_intf->p_module );
247 /* Provide ability to switch the main interface on the fly */
248 psz_intf = p_intf->psz_switch_intf;
249 p_intf->psz_switch_intf = NULL;
251 vlc_mutex_lock( &p_intf->object_lock );
252 p_intf->b_die = VLC_FALSE; /* FIXME */
253 p_intf->b_dead = VLC_FALSE;
255 vlc_mutex_unlock( &p_intf->object_lock );
257 p_intf->psz_intf = psz_intf;
258 p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
260 while( p_intf->p_module );
263 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
264 vlc_value_t oldval, vlc_value_t newval, void *p_data )
266 intf_thread_t *p_intf;
267 char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
269 (void)psz_cmd; (void)oldval; (void)p_data;
271 /* Try to create the interface */
272 sprintf( psz_intf, "%s,none", newval.psz_string );
273 p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
277 msg_Err( p_this, "interface \"%s\" initialization failed",
282 /* Try to run the interface */
283 if( intf_RunThread( p_intf ) != VLC_SUCCESS )
285 vlc_object_detach( p_intf );
286 intf_Destroy( p_intf );