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 );
52 #if defined( __APPLE__ ) || defined( WIN32 )
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 * Destroy the interface after the main loop endeed.
61 * @param p_obj: 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 );
77 * Create the interface, and prepare it for main loop. It opens ouput device
78 * and creates specific interfaces. Sends its own error messages.
80 * @param p_this the calling vlc_object_t
81 * @param psz_module a preferred interface module
82 * @return a pointer to the created interface thread, NULL on error
84 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
86 intf_thread_t * p_intf;
88 /* Allocate structure */
89 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
92 p_intf->b_interaction = false;
93 #if defined( __APPLE__ ) || defined( WIN32 )
94 p_intf->b_should_run_on_first_thread = false;
97 /* Choose the best module */
98 p_intf->psz_intf = strdup( psz_module );
99 p_intf->p_module = module_need( p_intf, "interface", psz_module, true );
101 if( p_intf->p_module == NULL )
103 msg_Err( p_intf, "no suitable interface module" );
104 free( p_intf->psz_intf );
105 vlc_object_release( p_intf );
109 /* Initialize structure */
110 p_intf->b_menu = false;
112 /* Initialize mutexes */
113 vlc_mutex_init( &p_intf->change_lock );
115 /* Attach interface to its parent object */
116 vlc_object_attach( p_intf, p_this );
117 vlc_object_set_destructor( p_intf, intf_Destroy );
124 * Starts and runs the interface thread.
126 * @param p_intf the interface thread
127 * @return VLC_SUCCESS on success, an error number else
129 int intf_RunThread( intf_thread_t *p_intf )
131 #if defined( __APPLE__ ) || defined( WIN32 )
132 /* Hack to get Mac OS X Cocoa runtime running
133 * (it needs access to the main thread) */
134 if( p_intf->b_should_run_on_first_thread )
136 if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
137 VLC_THREAD_PRIORITY_LOW, false ) )
139 msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
142 RunInterface( VLC_OBJECT(p_intf) );
144 /* Make sure our MonitorLibVLCDeath thread exit */
145 vlc_object_kill( p_intf );
146 /* It is monitoring libvlc, not the p_intf */
147 vlc_object_signal( p_intf->p_libvlc );
148 vlc_thread_join( p_intf );
150 vlc_object_detach( p_intf );
151 vlc_object_release( p_intf );
155 /* Run the interface in a separate thread */
156 if( vlc_thread_create( p_intf, "interface", RunInterface,
157 VLC_THREAD_PRIORITY_LOW, false ) )
159 msg_Err( p_intf, "cannot spawn interface thread" );
168 * Stops the interface thread
170 * This function asks the interface thread to stop
171 * @param p_intf the interface thread
173 void intf_StopThread( intf_thread_t *p_intf )
175 /* Tell the interface to die */
176 vlc_object_kill( p_intf );
177 vlc_thread_join( p_intf );
182 /* Following functions are local */
185 * RunInterface: setups necessary data and give control to the interface
187 * @param p_this: interface object
189 static void* RunInterface( vlc_object_t *p_this )
191 intf_thread_t *p_intf = (intf_thread_t *)p_this;
192 vlc_value_t val, text;
193 int canc = vlc_savecancel ();
195 /* Variable used for interface spawning */
196 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
197 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
198 text.psz_string = _("Add Interface");
199 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
201 val.psz_string = (char *)"rc";
202 text.psz_string = (char *)_("Console");
203 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
204 val.psz_string = (char *)"telnet";
205 text.psz_string = (char *)_("Telnet Interface");
206 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
207 val.psz_string = (char *)"http";
208 text.psz_string = (char *)_("Web Interface");
209 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
210 val.psz_string = (char *)"logger";
211 text.psz_string = (char *)_("Debug logging");
212 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
213 val.psz_string = (char *)"gestures";
214 text.psz_string = (char *)_("Mouse Gestures");
215 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
217 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
218 vlc_restorecancel (canc);
220 /* Give control to the interface */
222 p_intf->pf_run( p_intf );
227 #if defined( __APPLE__ ) || defined( WIN32 )
229 * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
231 * @param p_this: the interface object
233 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
235 intf_thread_t *p_intf = (intf_thread_t *)p_this;
236 libvlc_int_t * p_libvlc = p_intf->p_libvlc;
237 int canc = vlc_savecancel ();
239 vlc_object_lock( p_libvlc );
240 while(vlc_object_alive( p_libvlc ) )
244 vlc_object_unlock( p_libvlc );
247 vlc_object_wait( p_libvlc );
249 vlc_object_unlock( p_libvlc );
251 /* Someone killed libvlc */
253 /* Make sure we kill all interface objects, especially
254 * those that are blocking libvlc (running on main thread) */
255 vlc_list_t * p_list = vlc_list_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD );
256 for( int i = 0; i < p_list->i_count; i++ )
258 vlc_object_t * p_intf = p_list->p_values[i].p_object;
259 vlc_object_kill( p_intf );
261 vlc_list_release( p_list );
262 vlc_restorecancel (canc);
267 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
268 vlc_value_t oldval, vlc_value_t newval, void *p_data )
270 (void)psz_cmd; (void)oldval; (void)p_data;
271 intf_thread_t *p_intf;
274 /* Try to create the interface */
275 if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
278 p_intf = intf_Create( p_this->p_libvlc, psz_intf );
282 msg_Err( p_this, "interface \"%s\" initialization failed",
287 /* Try to run the interface */
288 if( intf_RunThread( p_intf ) != VLC_SUCCESS )
290 vlc_object_detach( p_intf );
291 vlc_object_release( p_intf );