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"
48 /*****************************************************************************
50 *****************************************************************************/
51 static void* RunInterface( vlc_object_t *p_this );
53 static void * MonitorLibVLCDeath( vlc_object_t *p_this );
55 static int AddIntfCallback( vlc_object_t *, char const *,
56 vlc_value_t , vlc_value_t , void * );
59 * \brief Destroy the interface after the main loop endeed.
61 * \param p_intf the interface thread
64 static void intf_Destroy( vlc_object_t *obj )
66 intf_thread_t *p_intf = (intf_thread_t *)obj;
68 /* Unlock module if present (a switch may have failed) */
69 if( p_intf->p_module )
70 module_Unneed( p_intf, p_intf->p_module );
72 free( p_intf->psz_intf );
73 vlc_mutex_destroy( &p_intf->change_lock );
76 /*****************************************************************************
77 * intf_Create: prepare interface before main loop
78 *****************************************************************************
79 * This function opens output devices and creates specific interfaces. It sends
80 * its own error messages.
81 *****************************************************************************/
83 * Create the interface, and prepare it for main loop.
85 * \param p_this the calling vlc_object_t
86 * \param psz_module a preferred interface module
87 * \return a pointer to the created interface thread, NULL on error
89 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
91 intf_thread_t * p_intf;
93 /* Allocate structure */
94 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
97 p_intf->b_interaction = false;
99 p_intf->b_should_run_on_first_thread = false;
102 /* Choose the best module */
103 p_intf->psz_intf = strdup( psz_module );
104 p_intf->p_module = module_Need( p_intf, "interface", psz_module, false );
106 if( p_intf->p_module == NULL )
108 msg_Err( p_intf, "no suitable interface module" );
109 free( p_intf->psz_intf );
110 vlc_object_release( p_intf );
114 /* Initialize structure */
115 p_intf->b_menu = false;
116 p_intf->b_menu_change = false;
118 /* Initialize mutexes */
119 vlc_mutex_init( &p_intf->change_lock );
121 /* Attach interface to its parent object */
122 vlc_object_attach( p_intf, p_this );
123 vlc_object_set_destructor( p_intf, intf_Destroy );
128 /*****************************************************************************
129 * intf_RunThread: launch the interface thread
130 *****************************************************************************
131 * This function either creates a new thread and runs the interface in it.
132 *****************************************************************************/
134 * Starts and runs the interface thread.
136 * \param p_intf the interface thread
137 * \return VLC_SUCCESS on success, an error number else
139 int intf_RunThread( intf_thread_t *p_intf )
142 /* Hack to get Mac OS X Cocoa runtime running
143 * (it needs access to the main thread) */
144 if( p_intf->b_should_run_on_first_thread )
146 if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
147 VLC_THREAD_PRIORITY_LOW, false ) )
149 msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
152 RunInterface( VLC_OBJECT(p_intf) );
154 /* Make sure our MonitorLibVLCDeath thread exit */
155 vlc_object_kill( p_intf );
156 /* It is monitoring libvlc, not the p_intf */
157 vlc_object_signal( p_intf->p_libvlc );
158 vlc_thread_join( p_intf );
160 vlc_object_detach( p_intf );
161 vlc_object_release( p_intf );
165 /* Run the interface in a separate thread */
166 if( vlc_thread_create( p_intf, "interface", RunInterface,
167 VLC_THREAD_PRIORITY_LOW, false ) )
169 msg_Err( p_intf, "cannot spawn interface thread" );
177 * Stops the interface thread
179 * This function asks the interface thread to stop
180 * \param p_intf the interface thread
183 void intf_StopThread( intf_thread_t *p_intf )
185 /* Tell the interface to die */
186 vlc_object_kill( p_intf );
187 vlc_thread_join( p_intf );
190 /* Following functions are local */
192 /*****************************************************************************
193 * RunInterface: setups necessary data and give control to the interface
194 *****************************************************************************/
195 static void* RunInterface( vlc_object_t *p_this )
197 intf_thread_t *p_intf = (intf_thread_t *)p_this;
198 vlc_value_t val, text;
200 int canc = vlc_savecancel ();
202 /* Variable used for interface spawning */
203 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
204 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
205 text.psz_string = _("Add Interface");
206 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
208 val.psz_string = (char *)"rc";
209 text.psz_string = (char *)_("Console");
210 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
211 val.psz_string = (char *)"telnet";
212 text.psz_string = (char *)_("Telnet Interface");
213 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
214 val.psz_string = (char *)"http";
215 text.psz_string = (char *)_("Web Interface");
216 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
217 val.psz_string = (char *)"logger";
218 text.psz_string = (char *)_("Debug logging");
219 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
220 val.psz_string = (char *)"gestures";
221 text.psz_string = (char *)_("Mouse Gestures");
222 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
224 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
226 /* Give control to the interface */
228 p_intf->pf_run( p_intf );
230 vlc_restorecancel (canc);
235 /*****************************************************************************
236 * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
237 *****************************************************************************/
238 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
240 intf_thread_t *p_intf = (intf_thread_t *)p_this;
241 libvlc_int_t * p_libvlc = p_intf->p_libvlc;
242 int canc = vlc_savecancel ();
244 vlc_object_lock( p_libvlc );
245 while(vlc_object_alive( p_libvlc ) )
249 vlc_object_unlock( p_libvlc );
252 vlc_object_wait( p_libvlc );
254 vlc_object_unlock( p_libvlc );
256 /* Someone killed libvlc */
258 /* Make sure we kill all interface objects, especially
259 * those that are blocking libvlc (running on main thread) */
260 vlc_list_t * p_list = vlc_list_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD );
261 for( int i = 0; i < p_list->i_count; i++ )
263 vlc_object_t * p_intf = p_list->p_values[i].p_object;
264 vlc_object_kill( p_intf );
266 vlc_list_release( p_list );
267 vlc_restorecancel (canc);
272 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
273 vlc_value_t oldval, vlc_value_t newval, void *p_data )
275 intf_thread_t *p_intf;
276 char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
278 (void)psz_cmd; (void)oldval; (void)p_data;
280 /* Try to create the interface */
281 sprintf( psz_intf, "%s,none", newval.psz_string );
282 p_intf = intf_Create( p_this->p_libvlc, psz_intf );
286 msg_Err( p_this, "interface \"%s\" initialization failed",
291 /* Try to run the interface */
292 if( intf_RunThread( p_intf ) != VLC_SUCCESS )
294 vlc_object_detach( p_intf );
295 vlc_object_release( p_intf );