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 * );
63 * Create and start an interface.
65 * @param p_this the calling vlc_object_t
66 * @param psz_module a preferred interface module
67 * @return VLC_SUCCESS or an error code
69 int intf_Create( vlc_object_t *p_this, const char *psz_module )
71 intf_thread_t * p_intf;
73 /* Allocate structure */
74 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
78 /* Variable used for interface spawning */
79 vlc_value_t val, text;
80 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
81 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
82 text.psz_string = _("Add Interface");
83 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
85 val.psz_string = (char *)"rc";
86 text.psz_string = (char *)_("Console");
87 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
88 val.psz_string = (char *)"telnet";
89 text.psz_string = (char *)_("Telnet Interface");
90 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
91 val.psz_string = (char *)"http";
92 text.psz_string = (char *)_("Web Interface");
93 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
94 val.psz_string = (char *)"logger";
95 text.psz_string = (char *)_("Debug logging");
96 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
97 val.psz_string = (char *)"gestures";
98 text.psz_string = (char *)_("Mouse Gestures");
99 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
101 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
103 /* Attach interface to its parent object */
104 vlc_object_attach( p_intf, p_this );
105 #if defined( __APPLE__ ) || defined( WIN32 )
106 p_intf->b_should_run_on_first_thread = false;
109 /* Choose the best module */
110 p_intf->p_cfg = NULL;
111 char *psz_parser = *psz_module == '$'
112 ? var_CreateGetString(p_intf,psz_module+1)
113 : strdup( psz_module );
114 char *psz_tmp = config_ChainCreate( &p_intf->psz_intf, &p_intf->p_cfg,
118 p_intf->p_module = module_need( p_intf, "interface", p_intf->psz_intf, true );
119 if( p_intf->p_module == NULL )
121 msg_Err( p_intf, "no suitable interface module" );
125 if( p_intf->pf_run == NULL )
128 #if defined( __APPLE__ ) || defined( WIN32 )
129 /* Hack to get Mac OS X Cocoa runtime running
130 * (it needs access to the main thread) */
131 if( p_intf->b_should_run_on_first_thread )
133 if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
134 VLC_THREAD_PRIORITY_LOW ) )
136 msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
139 p_intf->pf_run( p_intf );
141 /* It is monitoring libvlc, not the p_intf */
142 vlc_object_kill( p_intf->p_libvlc );
146 /* Run the interface in a separate thread */
147 if( vlc_thread_create( p_intf, "interface", RunInterface,
148 VLC_THREAD_PRIORITY_LOW ) )
150 msg_Err( p_intf, "cannot spawn interface thread" );
157 if( p_intf->p_module )
158 module_unneed( p_intf, p_intf->p_module );
159 config_ChainDestroy( p_intf->p_cfg );
160 free( p_intf->psz_intf );
161 vlc_object_release( p_intf );
167 * Stops and destroys all interfaces
168 * @param p_libvlc the LibVLC instance
170 void intf_DestroyAll( libvlc_int_t *p_libvlc )
172 vlc_list_t *l = vlc_list_find( VLC_OBJECT(p_libvlc), VLC_OBJECT_INTF, FIND_CHILD );
174 /* Tell the interfaces to die */
175 for( int i = 0; i < l->i_count; i++ )
176 vlc_object_kill( l->p_values[i].p_object );
178 /* Cleanup the interfaces */
179 for( int i = 0; i < l->i_count; i++ )
181 intf_thread_t *p_intf = (intf_thread_t *)l->p_values[i].p_object;
184 vlc_thread_join( p_intf );
185 module_unneed( p_intf, p_intf->p_module );
186 free( p_intf->psz_intf );
187 config_ChainDestroy( p_intf->p_cfg );
190 /* Destroy objects */
191 for( int i = 0; i < l->i_count; i++ )
192 vlc_object_release( l->p_values[i].p_object ); /* for intf_Create() */
193 vlc_list_release( l );
196 /* Following functions are local */
199 * RunInterface: setups necessary data and give control to the interface
201 * @param p_this: interface object
203 static void* RunInterface( vlc_object_t *p_this )
205 intf_thread_t *p_intf = (intf_thread_t *)p_this;
207 p_intf->pf_run( p_intf );
211 #if defined( __APPLE__ ) || defined( WIN32 )
212 #include "control/libvlc_internal.h" /* libvlc_InternalWait */
214 * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
216 * @param p_this: the interface object
218 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
220 intf_thread_t *p_intf = (intf_thread_t *)p_this;
221 libvlc_int_t * p_libvlc = p_intf->p_libvlc;
222 int canc = vlc_savecancel ();
224 libvlc_InternalWait( p_libvlc );
226 vlc_object_kill( p_intf ); /* Kill the stupid first thread interface */
227 vlc_restorecancel (canc);
232 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
233 vlc_value_t oldval, vlc_value_t newval, void *p_data )
235 (void)psz_cmd; (void)oldval; (void)p_data;
238 /* Try to create the interface */
239 if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
242 int ret = intf_Create( VLC_OBJECT(p_this->p_libvlc), psz_intf );
245 msg_Err( p_this, "interface \"%s\" initialization failed",