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 VLC authors and VideoLAN
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
28 * This file contains functions related to interface management
32 /*****************************************************************************
34 *****************************************************************************/
45 #include <vlc_common.h>
46 #include <vlc_modules.h>
47 #include <vlc_interface.h>
50 /*****************************************************************************
52 *****************************************************************************/
53 static void* RunInterface( void * );
54 static int AddIntfCallback( vlc_object_t *, char const *,
55 vlc_value_t , vlc_value_t , void * );
57 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
61 * Create and start an interface.
63 * @param p_this the calling vlc_object_t
64 * @param chain configuration chain string
65 * @return VLC_SUCCESS or an error code
67 int intf_Create( vlc_object_t *p_this, const char *chain )
69 libvlc_int_t *p_libvlc = p_this->p_libvlc;
70 intf_thread_t * p_intf;
72 /* Allocate structure */
73 p_intf = vlc_custom_create( p_libvlc, sizeof( *p_intf ), "interface" );
77 /* Variable used for interface spawning */
78 vlc_value_t val, text;
79 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
80 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
81 text.psz_string = _("Add Interface");
82 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
83 #if !defined(WIN32) && defined(HAVE_ISATTY)
87 val.psz_string = (char *)"rc";
88 text.psz_string = (char *)_("Console");
89 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
91 val.psz_string = (char *)"telnet";
92 text.psz_string = (char *)_("Telnet");
93 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
94 val.psz_string = (char *)"http";
95 text.psz_string = (char *)_("Web");
96 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
97 val.psz_string = (char *)"logger";
98 text.psz_string = (char *)_("Debug logging");
99 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
100 val.psz_string = (char *)"gestures";
101 text.psz_string = (char *)_("Mouse Gestures");
102 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
104 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
106 /* Attach interface to LibVLC */
107 #if defined( __APPLE__ )
108 p_intf->b_should_run_on_first_thread = false;
111 /* Choose the best module */
112 p_intf->p_cfg = NULL;
113 char *psz_parser = *chain == '$'
114 ? var_CreateGetString(p_intf, chain+1)
117 char *psz_tmp = config_ChainCreate( &module, &p_intf->p_cfg,
121 p_intf->p_module = module_need( p_intf, "interface", module, true );
123 if( p_intf->p_module == NULL )
125 msg_Err( p_intf, "no suitable interface module" );
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 libvlc_SetExitHandler( p_libvlc, vlc_object_kill, p_intf );
135 assert( p_intf->pf_run );
136 p_intf->pf_run( p_intf );
137 p_intf->pf_run = NULL;
140 /* Run the interface in a separate thread */
142 && vlc_clone( &p_intf->thread,
143 RunInterface, p_intf, VLC_THREAD_PRIORITY_LOW ) )
145 msg_Err( p_intf, "cannot spawn interface thread" );
149 vlc_mutex_lock( &lock );
150 p_intf->p_next = libvlc_priv( p_libvlc )->p_intf;
151 libvlc_priv( p_libvlc )->p_intf = p_intf;
152 vlc_mutex_unlock( &lock );
157 if( p_intf->p_module )
158 module_unneed( p_intf, p_intf->p_module );
159 config_ChainDestroy( p_intf->p_cfg );
160 vlc_object_release( p_intf );
166 * Stops and destroys all interfaces
167 * @param p_libvlc the LibVLC instance
169 void intf_DestroyAll( libvlc_int_t *p_libvlc )
171 intf_thread_t *p_first;
173 vlc_mutex_lock( &lock );
174 p_first = libvlc_priv( p_libvlc )->p_intf;
176 libvlc_priv( p_libvlc )->p_intf = NULL;
178 vlc_mutex_unlock( &lock );
180 /* Tell the interfaces to die */
181 for( intf_thread_t *p_intf = p_first; p_intf; p_intf = p_intf->p_next )
182 vlc_object_kill( p_intf );
184 /* Cleanup the interfaces */
185 for( intf_thread_t *p_intf = p_first; p_intf != NULL; )
187 intf_thread_t *p_next = p_intf->p_next;
191 vlc_cancel( p_intf->thread );
192 vlc_join( p_intf->thread, NULL );
194 module_unneed( p_intf, p_intf->p_module );
195 config_ChainDestroy( p_intf->p_cfg );
196 vlc_object_release( p_intf );
202 /* Following functions are local */
205 * RunInterface: setups necessary data and give control to the interface
207 * @param p_this: interface object
209 static void* RunInterface( void *p_this )
211 intf_thread_t *p_intf = p_this;
213 p_intf->pf_run( p_intf );
217 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
218 vlc_value_t oldval, vlc_value_t newval, void *p_data )
220 (void)psz_cmd; (void)oldval; (void)p_data;
223 /* Try to create the interface */
224 if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
227 int ret = intf_Create( VLC_OBJECT(p_this->p_libvlc), psz_intf );
230 msg_Err( p_this, "interface \"%s\" initialization failed",