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 *****************************************************************************/
41 #include <vlc_common.h>
42 #include <vlc_modules.h>
43 #include <vlc_interface.h>
45 #if defined( __APPLE__ ) || defined( WIN32 )
46 #include "../control/libvlc_internal.h"
50 /*****************************************************************************
52 *****************************************************************************/
53 static void* RunInterface( void * );
54 #if defined( __APPLE__)
55 static void * MonitorLibVLCDeath( vlc_object_t *p_this );
57 static int AddIntfCallback( vlc_object_t *, char const *,
58 vlc_value_t , vlc_value_t , void * );
60 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
64 * Create and start an interface.
66 * @param p_this the calling vlc_object_t
67 * @param psz_module a preferred interface module
68 * @return VLC_SUCCESS or an error code
70 int intf_Create( vlc_object_t *p_this, const char *psz_module )
72 libvlc_int_t *p_libvlc = p_this->p_libvlc;
73 intf_thread_t * p_intf;
74 static const char psz_type[] = "interface";
76 /* Allocate structure */
77 p_intf = vlc_custom_create( p_libvlc, sizeof( *p_intf ),
78 VLC_OBJECT_GENERIC, psz_type );
82 /* Variable used for interface spawning */
83 vlc_value_t val, text;
84 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
85 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
86 text.psz_string = _("Add Interface");
87 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
89 val.psz_string = (char *)"rc";
90 text.psz_string = (char *)_("Console");
91 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
92 val.psz_string = (char *)"telnet";
93 text.psz_string = (char *)_("Telnet");
94 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
95 val.psz_string = (char *)"http";
96 text.psz_string = (char *)_("Web");
97 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
98 val.psz_string = (char *)"logger";
99 text.psz_string = (char *)_("Debug logging");
100 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
101 val.psz_string = (char *)"gestures";
102 text.psz_string = (char *)_("Mouse Gestures");
103 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
105 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
107 /* Attach interface to LibVLC */
108 #if defined( __APPLE__ )
109 p_intf->b_should_run_on_first_thread = false;
112 /* Choose the best module */
113 p_intf->p_cfg = NULL;
114 char *psz_parser = *psz_module == '$'
115 ? var_CreateGetString(p_intf,psz_module+1)
116 : strdup( psz_module );
117 char *psz_tmp = config_ChainCreate( &p_intf->psz_intf, &p_intf->p_cfg,
121 p_intf->p_module = module_need( p_intf, "interface", p_intf->psz_intf, true );
122 if( p_intf->p_module == NULL )
124 msg_Err( p_intf, "no suitable interface module" );
128 vlc_mutex_lock( &lock );
129 #if defined( __APPLE__ )
130 /* Hack to get Mac OS X Cocoa runtime running
131 * (it needs access to the main thread) */
132 if( p_intf->b_should_run_on_first_thread )
134 if( vlc_clone( &p_intf->thread,
135 MonitorLibVLCDeath, p_intf, VLC_THREAD_PRIORITY_LOW ) )
137 msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
138 vlc_mutex_unlock( &lock );
141 assert( p_intf->pf_run );
142 p_intf->pf_run( p_intf );
144 /* It is monitoring libvlc, not the p_intf */
145 vlc_object_kill( p_intf->p_libvlc );
147 vlc_join( p_intf->thread, NULL );
151 /* Run the interface in a separate thread */
153 && vlc_clone( &p_intf->thread,
154 RunInterface, p_intf, VLC_THREAD_PRIORITY_LOW ) )
156 msg_Err( p_intf, "cannot spawn interface thread" );
157 vlc_mutex_unlock( &lock );
161 p_intf->p_next = libvlc_priv( p_libvlc )->p_intf;
162 libvlc_priv( p_libvlc )->p_intf = p_intf;
163 vlc_mutex_unlock( &lock );
168 if( p_intf->p_module )
169 module_unneed( p_intf, p_intf->p_module );
170 config_ChainDestroy( p_intf->p_cfg );
171 free( p_intf->psz_intf );
172 vlc_object_release( p_intf );
178 * Stops and destroys all interfaces
179 * @param p_libvlc the LibVLC instance
181 void intf_DestroyAll( libvlc_int_t *p_libvlc )
183 intf_thread_t *p_first;
185 vlc_mutex_lock( &lock );
186 p_first = libvlc_priv( p_libvlc )->p_intf;
188 libvlc_priv( p_libvlc )->p_intf = NULL;
190 vlc_mutex_unlock( &lock );
192 /* Tell the interfaces to die */
193 for( intf_thread_t *p_intf = p_first; p_intf; p_intf = p_intf->p_next )
194 vlc_object_kill( p_intf );
196 /* Cleanup the interfaces */
197 for( intf_thread_t *p_intf = p_first; p_intf != NULL; )
199 intf_thread_t *p_next = p_intf->p_next;
202 vlc_join( p_intf->thread, NULL );
203 module_unneed( p_intf, p_intf->p_module );
204 free( p_intf->psz_intf );
205 config_ChainDestroy( p_intf->p_cfg );
206 vlc_object_release( p_intf );
212 /* Following functions are local */
215 * RunInterface: setups necessary data and give control to the interface
217 * @param p_this: interface object
219 static void* RunInterface( void *p_this )
221 intf_thread_t *p_intf = p_this;
223 p_intf->pf_run( p_intf );
227 #if defined( __APPLE__ )
228 #include "control/libvlc_internal.h" /* libvlc_InternalWait */
230 * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
232 * @param p_this: the interface object
234 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
236 intf_thread_t *p_intf = (intf_thread_t *)p_this;
237 libvlc_int_t * p_libvlc = p_intf->p_libvlc;
238 int canc = vlc_savecancel ();
240 libvlc_InternalWait( p_libvlc );
242 vlc_object_kill( p_intf ); /* Kill the stupid first thread interface */
243 vlc_restorecancel (canc);
248 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
249 vlc_value_t oldval, vlc_value_t newval, void *p_data )
251 (void)psz_cmd; (void)oldval; (void)p_data;
254 /* Try to create the interface */
255 if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
258 int ret = intf_Create( VLC_OBJECT(p_this->p_libvlc), psz_intf );
261 msg_Err( p_this, "interface \"%s\" initialization failed",