]> git.sesse.net Git - vlc/blob - src/interface/interface.c
Keep a thread-safe list of interfaces
[vlc] / src / interface / interface.c
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
7  * $Id$
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /**
27  *   \file
28  *   This file contains functions related to interface management
29  */
30
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <assert.h>
41 #include <vlc_common.h>
42
43 #include <vlc_aout.h>
44 #include <vlc_vout.h>
45
46 #include "vlc_interface.h"
47 #if defined( __APPLE__ ) || defined( WIN32 )
48 #include "../control/libvlc_internal.h"
49 #endif
50 #include "libvlc.h"
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static void* RunInterface( vlc_object_t *p_this );
56 #if defined( __APPLE__ ) || defined( WIN32 )
57 static void * MonitorLibVLCDeath( vlc_object_t *p_this );
58 #endif
59 static int AddIntfCallback( vlc_object_t *, char const *,
60                             vlc_value_t , vlc_value_t , void * );
61
62 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
63
64 #undef intf_Create
65 /**
66  * Create and start an interface.
67  *
68  * @param p_this the calling vlc_object_t
69  * @param psz_module a preferred interface module
70  * @return VLC_SUCCESS or an error code
71  */
72 int intf_Create( vlc_object_t *p_this, const char *psz_module )
73 {
74     libvlc_int_t *p_libvlc = p_this->p_libvlc;
75     intf_thread_t * p_intf;
76
77     /* Allocate structure */
78     p_intf = vlc_object_create( p_libvlc, VLC_OBJECT_INTF );
79     if( !p_intf )
80         return VLC_ENOMEM;
81
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 );
88
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 Interface");
94     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
95     val.psz_string = (char *)"http";
96     text.psz_string = (char *)_("Web Interface");
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 );
104
105     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
106
107     /* Attach interface to LibVLC */
108     vlc_object_attach( p_intf, p_libvlc );
109 #if defined( __APPLE__ ) || defined( WIN32 )
110     p_intf->b_should_run_on_first_thread = false;
111 #endif
112
113     /* Choose the best module */
114     p_intf->p_cfg = NULL;
115     char *psz_parser = *psz_module == '$'
116                      ? var_CreateGetString(p_intf,psz_module+1)
117                      : strdup( psz_module );
118     char *psz_tmp = config_ChainCreate( &p_intf->psz_intf, &p_intf->p_cfg,
119                                         psz_parser );
120     free( psz_tmp );
121     free( psz_parser );
122     p_intf->p_module = module_need( p_intf, "interface", p_intf->psz_intf, true );
123     if( p_intf->p_module == NULL )
124     {
125         msg_Err( p_intf, "no suitable interface module" );
126         goto error;
127     }
128
129     vlc_mutex_lock( &lock );
130     if( !vlc_object_alive( p_libvlc ) )
131     {
132         vlc_mutex_unlock( &lock );
133         goto error; /* Too late! */
134     }
135 #if defined( __APPLE__ ) || defined( WIN32 )
136     /* Hack to get Mac OS X Cocoa runtime running
137      * (it needs access to the main thread) */
138     if( p_intf->b_should_run_on_first_thread )
139     {
140         if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
141                                VLC_THREAD_PRIORITY_LOW ) )
142         {
143             msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
144             vlc_mutex_unlock( &lock );
145             goto error;
146         }
147         assert( p_intf->pf_run );
148         p_intf->pf_run( p_intf );
149
150         /* It is monitoring libvlc, not the p_intf */
151         vlc_object_kill( p_intf->p_libvlc );
152     }
153     else
154 #endif
155     /* Run the interface in a separate thread */
156     if( p_intf->pf_run
157      && vlc_thread_create( p_intf, "interface", RunInterface,
158                            VLC_THREAD_PRIORITY_LOW ) )
159     {
160         msg_Err( p_intf, "cannot spawn interface thread" );
161         vlc_mutex_unlock( &lock );
162         goto error;
163     }
164
165     p_intf->p_next = libvlc_priv( p_libvlc )->p_intf;
166     libvlc_priv( p_libvlc )->p_intf = p_intf;
167     vlc_mutex_unlock( &lock );
168
169     return VLC_SUCCESS;
170
171 error:
172     if( p_intf->p_module )
173         module_unneed( p_intf, p_intf->p_module );
174     config_ChainDestroy( p_intf->p_cfg );
175     free( p_intf->psz_intf );
176     vlc_object_release( p_intf );
177     return VLC_EGENERIC;
178 }
179
180
181 /**
182  * Stops and destroys all interfaces
183  * @param p_libvlc the LibVLC instance
184  */
185 void intf_DestroyAll( libvlc_int_t *p_libvlc )
186 {
187     intf_thread_t *p_first;
188
189     assert( !vlc_object_alive( p_libvlc ) );
190
191     vlc_mutex_lock( &lock );
192     p_first = libvlc_priv( p_libvlc )->p_intf;
193 #ifndef NDEBUG
194     libvlc_priv( p_libvlc )->p_intf = NULL;
195 #endif
196     vlc_mutex_unlock( &lock );
197
198     /* Tell the interfaces to die */
199     for( intf_thread_t *p_intf = p_first; p_intf; p_intf = p_intf->p_next )
200         vlc_object_kill( p_intf );
201
202     /* Cleanup the interfaces */
203     for( intf_thread_t *p_intf = p_first; p_intf != NULL; )
204     {
205         intf_thread_t *p_next = p_intf->p_next;
206
207         if( p_intf->pf_run )
208             vlc_thread_join( p_intf );
209         module_unneed( p_intf, p_intf->p_module );
210         free( p_intf->psz_intf );
211         config_ChainDestroy( p_intf->p_cfg );
212         vlc_object_release( p_intf );
213
214         p_intf = p_next;
215     }
216 }
217
218 /* Following functions are local */
219
220 /**
221  * RunInterface: setups necessary data and give control to the interface
222  *
223  * @param p_this: interface object
224  */
225 static void* RunInterface( vlc_object_t *p_this )
226 {
227     intf_thread_t *p_intf = (intf_thread_t *)p_this;
228
229     p_intf->pf_run( p_intf );
230     return NULL;
231 }
232
233 #if defined( __APPLE__ ) || defined( WIN32 )
234 #include "control/libvlc_internal.h" /* libvlc_InternalWait */
235 /**
236  * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
237  *
238  * @param p_this: the interface object
239  */
240 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
241 {
242     intf_thread_t *p_intf = (intf_thread_t *)p_this;
243     libvlc_int_t * p_libvlc = p_intf->p_libvlc;
244     int canc = vlc_savecancel ();
245
246     libvlc_InternalWait( p_libvlc );
247
248     vlc_object_kill( p_intf ); /* Kill the stupid first thread interface */
249     vlc_restorecancel (canc);
250     return NULL;
251 }
252 #endif
253
254 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
255                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
256 {
257     (void)psz_cmd; (void)oldval; (void)p_data;
258     char* psz_intf;
259
260     /* Try to create the interface */
261     if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
262         return VLC_ENOMEM;
263
264     int ret = intf_Create( VLC_OBJECT(p_this->p_libvlc), psz_intf );
265     free( psz_intf );
266     if( ret )
267         msg_Err( p_this, "interface \"%s\" initialization failed",
268                  newval.psz_string );
269     return ret;
270 }