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 #if defined( __APPLE__ ) || defined( WIN32 )
47 #include "../control/libvlc_internal.h"
51 /*****************************************************************************
53 *****************************************************************************/
54 static void* RunInterface( vlc_object_t *p_this );
55 #if defined( __APPLE__ ) || defined( WIN32 )
56 static void * MonitorLibVLCDeath( vlc_object_t *p_this );
58 static int AddIntfCallback( vlc_object_t *, char const *,
59 vlc_value_t , vlc_value_t , void * );
62 * Destroy the interface after the main loop endeed.
64 * @param p_obj: the interface thread
66 static void intf_Destroy( vlc_object_t *obj )
68 intf_thread_t *p_intf = (intf_thread_t *)obj;
70 free( p_intf->psz_intf );
71 config_ChainDestroy( p_intf->p_cfg );
77 * Create and start an interface.
79 * @param p_this the calling vlc_object_t
80 * @param psz_module a preferred interface module
81 * @return VLC_SUCCESS or an error code
83 int intf_Create( vlc_object_t *p_this, const char *psz_module )
85 intf_thread_t * p_intf;
87 /* Allocate structure */
88 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
92 /* Variable used for interface spawning */
93 vlc_value_t val, text;
94 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
95 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
96 text.psz_string = _("Add Interface");
97 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
99 val.psz_string = (char *)"rc";
100 text.psz_string = (char *)_("Console");
101 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
102 val.psz_string = (char *)"telnet";
103 text.psz_string = (char *)_("Telnet Interface");
104 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
105 val.psz_string = (char *)"http";
106 text.psz_string = (char *)_("Web Interface");
107 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
108 val.psz_string = (char *)"logger";
109 text.psz_string = (char *)_("Debug logging");
110 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
111 val.psz_string = (char *)"gestures";
112 text.psz_string = (char *)_("Mouse Gestures");
113 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
115 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
117 /* Attach interface to its parent object */
118 vlc_object_attach( p_intf, p_this );
119 vlc_object_set_destructor( p_intf, intf_Destroy );
120 #if defined( __APPLE__ ) || defined( WIN32 )
121 p_intf->b_should_run_on_first_thread = false;
124 /* Choose the best module */
125 p_intf->p_cfg = NULL;
126 char *psz_parser = *psz_module == '$'
127 ? var_CreateGetString(p_intf,psz_module+1)
128 : strdup( psz_module );
129 char *psz_tmp = config_ChainCreate( &p_intf->psz_intf, &p_intf->p_cfg,
133 p_intf->p_module = module_need( p_intf, "interface", p_intf->psz_intf, true );
134 if( p_intf->p_module == NULL )
136 msg_Err( p_intf, "no suitable interface module" );
137 vlc_object_release( p_intf );
141 if( p_intf->pf_run == NULL )
144 #if defined( __APPLE__ ) || defined( WIN32 )
145 /* Hack to get Mac OS X Cocoa runtime running
146 * (it needs access to the main thread) */
147 if( p_intf->b_should_run_on_first_thread )
149 if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
150 VLC_THREAD_PRIORITY_LOW ) )
152 msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
153 vlc_object_release( p_intf );
156 p_intf->pf_run( p_intf );
158 /* Make sure our MonitorLibVLCDeath thread exit */
159 vlc_object_kill( p_intf );
160 /* It is monitoring libvlc, not the p_intf */
161 vlc_object_kill( p_intf->p_libvlc );
162 vlc_thread_join( p_intf );
164 vlc_object_detach( p_intf );
165 vlc_object_release( p_intf );
169 /* Run the interface in a separate thread */
170 if( vlc_thread_create( p_intf, "interface", RunInterface,
171 VLC_THREAD_PRIORITY_LOW ) )
173 msg_Err( p_intf, "cannot spawn interface thread" );
174 vlc_object_release( p_intf );
183 * Stops the interface thread
185 * This function asks the interface thread to stop
186 * @param p_intf the interface thread
188 void intf_StopThread( intf_thread_t *p_intf )
190 /* Tell the interface to die */
191 vlc_object_kill( p_intf );
193 vlc_thread_join( p_intf );
195 module_unneed( p_intf, p_intf->p_module );
198 /* Following functions are local */
201 * RunInterface: setups necessary data and give control to the interface
203 * @param p_this: interface object
205 static void* RunInterface( vlc_object_t *p_this )
207 intf_thread_t *p_intf = (intf_thread_t *)p_this;
209 p_intf->pf_run( p_intf );
213 #if defined( __APPLE__ ) || defined( WIN32 )
214 #include "control/libvlc_internal.h" /* libvlc_InternalWait */
216 * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
218 * @param p_this: the interface object
220 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
222 intf_thread_t *p_intf = (intf_thread_t *)p_this;
223 libvlc_int_t * p_libvlc = p_intf->p_libvlc;
224 int canc = vlc_savecancel ();
226 libvlc_InternalWait( p_libvlc );
228 vlc_object_kill( p_intf ); /* Kill the stupid first thread interface */
229 vlc_restorecancel (canc);
234 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
235 vlc_value_t oldval, vlc_value_t newval, void *p_data )
237 (void)psz_cmd; (void)oldval; (void)p_data;
240 /* Try to create the interface */
241 if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
244 int ret = intf_Create( VLC_OBJECT(p_this->p_libvlc), psz_intf );
247 msg_Err( p_this, "interface \"%s\" initialization failed",