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 *****************************************************************************/
40 #include <vlc_common.h>
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 * );
58 * \brief Destroy the interface after the main loop endeed.
60 * \param p_intf the interface thread
63 static void intf_Destroy( vlc_object_t *obj )
65 intf_thread_t *p_intf = (intf_thread_t *)obj;
67 /* Unlock module if present (a switch may have failed) */
68 if( p_intf->p_module )
69 module_Unneed( p_intf, p_intf->p_module );
71 free( p_intf->psz_intf );
72 vlc_mutex_destroy( &p_intf->change_lock );
75 /*****************************************************************************
76 * intf_Create: prepare interface before main loop
77 *****************************************************************************
78 * This function opens output devices and creates specific interfaces. It sends
79 * its own error messages.
80 *****************************************************************************/
82 * Create the interface, and prepare it for main loop.
84 * \param p_this the calling vlc_object_t
85 * \param psz_module a preferred interface module
86 * \return a pointer to the created interface thread, NULL on error
88 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
90 intf_thread_t * p_intf;
92 /* Allocate structure */
93 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
96 p_intf->b_interaction = false;
97 p_intf->b_should_run_on_first_thread = false;
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, 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_release( p_intf );
111 /* Initialize structure */
112 p_intf->b_menu = false;
113 p_intf->b_menu_change = false;
115 /* Initialize mutexes */
116 vlc_mutex_init( &p_intf->change_lock );
118 /* Attach interface to its parent object */
119 vlc_object_attach( p_intf, p_this );
120 vlc_object_set_destructor( p_intf, intf_Destroy );
125 /*****************************************************************************
126 * intf_RunThread: launch the interface thread
127 *****************************************************************************
128 * This function either creates a new thread and runs the interface in it.
129 *****************************************************************************/
131 * Starts and runs the interface thread.
133 * \param p_intf the interface thread
134 * \return VLC_SUCCESS on success, an error number else
136 int intf_RunThread( intf_thread_t *p_intf )
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 );
143 vlc_object_detach( p_intf );
144 vlc_object_release( p_intf );
148 /* Run the interface in a separate thread */
149 if( vlc_thread_create( p_intf, "interface", RunInterface,
150 VLC_THREAD_PRIORITY_LOW, false ) )
152 msg_Err( p_intf, "cannot spawn interface thread" );
160 * Stops the interface thread
162 * This function asks the interface thread to stop
163 * \param p_intf the interface thread
166 void intf_StopThread( intf_thread_t *p_intf )
168 /* Tell the interface to die */
169 vlc_object_kill( p_intf );
170 vlc_thread_join( p_intf );
173 /* Following functions are local */
175 /*****************************************************************************
176 * RunInterface: setups necessary data and give control to the interface
177 *****************************************************************************/
178 static void RunInterface( intf_thread_t *p_intf )
180 vlc_value_t val, text;
183 /* Variable used for interface spawning */
184 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
185 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
186 text.psz_string = _("Add Interface");
187 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
189 val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
190 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
191 val.psz_string = (char *)"telnet";
192 text.psz_string = (char *)_("Telnet Interface");
193 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
194 val.psz_string = (char *)"http";
195 text.psz_string = (char *)_("Web Interface");
196 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
197 val.psz_string = (char *)"logger";
198 text.psz_string = (char *)_("Debug logging");
199 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
200 val.psz_string = (char *)"gestures";
201 text.psz_string = (char *)_("Mouse Gestures");
202 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
204 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
208 /* Give control to the interface */
210 p_intf->pf_run( p_intf );
213 vlc_object_lock( p_intf );
214 while( vlc_object_alive( p_intf ) )
215 vlc_object_wait( p_intf );
216 vlc_object_unlock( p_intf );
219 if( !p_intf->psz_switch_intf )
224 /* Make sure the old interface is completely uninitialized */
225 module_Unneed( p_intf, p_intf->p_module );
227 /* Provide ability to switch the main interface on the fly */
228 psz_intf = p_intf->psz_switch_intf;
229 p_intf->psz_switch_intf = NULL;
231 vlc_object_lock( p_intf );
232 p_intf->b_die = false; /* FIXME */
233 p_intf->b_dead = false;
235 vlc_object_unlock( p_intf );
237 p_intf->psz_intf = psz_intf;
238 p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
240 while( p_intf->p_module );
243 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
244 vlc_value_t oldval, vlc_value_t newval, void *p_data )
246 intf_thread_t *p_intf;
247 char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
249 (void)psz_cmd; (void)oldval; (void)p_data;
251 /* Try to create the interface */
252 sprintf( psz_intf, "%s,none", newval.psz_string );
253 p_intf = intf_Create( p_this->p_libvlc, psz_intf );
257 msg_Err( p_this, "interface \"%s\" initialization failed",
262 /* Try to run the interface */
263 if( intf_RunThread( p_intf ) != VLC_SUCCESS )
265 vlc_object_detach( p_intf );
266 vlc_object_release( p_intf );