]> git.sesse.net Git - vlc/blob - src/interface/interface.c
Unload interface plugin in StopThread rather than destroy
[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 <vlc_common.h>
41
42 #include <vlc_aout.h>
43 #include <vlc_vout.h>
44
45 #include "vlc_interface.h"
46 #if defined( __APPLE__ ) || defined( WIN32 )
47 #include "../control/libvlc_internal.h"
48 #endif
49 #include "libvlc.h"
50
51 /*****************************************************************************
52  * Local prototypes
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 );
57 #endif
58 static int AddIntfCallback( vlc_object_t *, char const *,
59                             vlc_value_t , vlc_value_t , void * );
60
61 /**
62  * Destroy the interface after the main loop endeed.
63  *
64  * @param p_obj: the interface thread
65  */
66 static void intf_Destroy( vlc_object_t *obj )
67 {
68     intf_thread_t *p_intf = (intf_thread_t *)obj;
69
70     free( p_intf->psz_intf );
71     config_ChainDestroy( p_intf->p_cfg );
72     vlc_mutex_destroy( &p_intf->change_lock );
73 }
74
75
76 /**
77  * Create the interface, and prepare it for main loop. It opens ouput device
78  * and creates specific interfaces. Sends its own error messages.
79  *
80  * @param p_this the calling vlc_object_t
81  * @param psz_module a preferred interface module
82  * @return a pointer to the created interface thread, NULL on error
83  */
84 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
85 {
86     intf_thread_t * p_intf;
87
88     /* Allocate structure */
89     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
90     if( !p_intf )
91         return NULL;
92 #if defined( __APPLE__ ) || defined( WIN32 )
93     p_intf->b_should_run_on_first_thread = false;
94 #endif
95
96     /* Choose the best module */
97     p_intf->p_cfg = NULL;
98     char *psz_parser = *psz_module == '$'
99                      ? var_CreateGetString(p_intf,psz_module+1)
100                      : strdup( psz_module );
101     char *psz_tmp = config_ChainCreate( &p_intf->psz_intf, &p_intf->p_cfg,
102                                         psz_parser );
103     free( psz_tmp );
104     free( psz_parser );
105     p_intf->p_module = module_need( p_intf, "interface", p_intf->psz_intf, true );
106
107     if( p_intf->p_module == NULL )
108     {
109         msg_Err( p_intf, "no suitable interface module" );
110         free( p_intf->psz_intf );
111         vlc_object_release( p_intf );
112         return NULL;
113     }
114
115     /* Initialize mutexes */
116     vlc_mutex_init( &p_intf->change_lock );
117
118     /* Attach interface to its parent object */
119     vlc_object_attach( p_intf, p_this );
120     vlc_object_set_destructor( p_intf, intf_Destroy );
121
122     return p_intf;
123 }
124
125
126 /**
127  * Starts and runs the interface thread.
128  *
129  * @param p_intf the interface thread
130  * @return VLC_SUCCESS on success, an error number else
131  */
132 int intf_RunThread( intf_thread_t *p_intf )
133 {
134 #if defined( __APPLE__ ) || defined( WIN32 )
135     /* Hack to get Mac OS X Cocoa runtime running
136      * (it needs access to the main thread) */
137     if( p_intf->b_should_run_on_first_thread )
138     {
139         if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
140                                VLC_THREAD_PRIORITY_LOW ) )
141         {
142             msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
143             return VLC_EGENERIC;
144         }
145         RunInterface( VLC_OBJECT(p_intf) );
146
147         /* Make sure our MonitorLibVLCDeath thread exit */
148         vlc_object_kill( p_intf );
149         /* It is monitoring libvlc, not the p_intf */
150         vlc_object_kill( p_intf->p_libvlc );
151         vlc_thread_join( p_intf );
152
153         vlc_object_detach( p_intf );
154         vlc_object_release( p_intf );
155         return VLC_SUCCESS;
156     }
157 #endif
158     /* Run the interface in a separate thread */
159     if( vlc_thread_create( p_intf, "interface", RunInterface,
160                            VLC_THREAD_PRIORITY_LOW ) )
161     {
162         msg_Err( p_intf, "cannot spawn interface thread" );
163         return VLC_EGENERIC;
164     }
165
166     return VLC_SUCCESS;
167 }
168
169
170 /**
171  * Stops the interface thread
172  *
173  * This function asks the interface thread to stop
174  * @param p_intf the interface thread
175  */
176 void intf_StopThread( intf_thread_t *p_intf )
177 {
178     /* Tell the interface to die */
179     vlc_object_kill( p_intf );
180     vlc_thread_join( p_intf );
181
182     module_unneed( p_intf, p_intf->p_module );
183 }
184
185
186
187 /* Following functions are local */
188
189 /**
190  * RunInterface: setups necessary data and give control to the interface
191  *
192  * @param p_this: interface object
193  */
194 static void* RunInterface( vlc_object_t *p_this )
195 {
196     intf_thread_t *p_intf = (intf_thread_t *)p_this;
197     vlc_value_t val, text;
198     int canc = vlc_savecancel ();
199
200     /* Variable used for interface spawning */
201     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
202                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
203     text.psz_string = _("Add Interface");
204     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
205
206     val.psz_string = (char *)"rc";
207     text.psz_string = (char *)_("Console");
208     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
209     val.psz_string = (char *)"telnet";
210     text.psz_string = (char *)_("Telnet Interface");
211     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
212     val.psz_string = (char *)"http";
213     text.psz_string = (char *)_("Web Interface");
214     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
215     val.psz_string = (char *)"logger";
216     text.psz_string = (char *)_("Debug logging");
217     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
218     val.psz_string = (char *)"gestures";
219     text.psz_string = (char *)_("Mouse Gestures");
220     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
221
222     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
223     vlc_restorecancel (canc);
224
225     /* Give control to the interface */
226     if( p_intf->pf_run )
227         p_intf->pf_run( p_intf );
228
229     return NULL;
230 }
231
232 #if defined( __APPLE__ ) || defined( WIN32 )
233 #include "control/libvlc_internal.h" /* libvlc_InternalWait */
234 /**
235  * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
236  *
237  * @param p_this: the interface object
238  */
239 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
240 {
241     intf_thread_t *p_intf = (intf_thread_t *)p_this;
242     libvlc_int_t * p_libvlc = p_intf->p_libvlc;
243     int canc = vlc_savecancel ();
244
245     libvlc_InternalWait( p_libvlc );
246
247     vlc_object_kill( p_intf ); /* Kill the stupid first thread interface */
248     vlc_restorecancel (canc);
249     return NULL;
250 }
251 #endif
252
253 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
254                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
255 {
256     (void)psz_cmd; (void)oldval; (void)p_data;
257     intf_thread_t *p_intf;
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     p_intf = intf_Create( p_this->p_libvlc, psz_intf );
265     free( psz_intf );
266     if( p_intf == NULL )
267     {
268         msg_Err( p_this, "interface \"%s\" initialization failed",
269                  newval.psz_string );
270         return VLC_EGENERIC;
271     }
272
273     /* Try to run the interface */
274     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
275     {
276         vlc_object_detach( p_intf );
277         vlc_object_release( p_intf );
278         return VLC_EGENERIC;
279     }
280
281     return VLC_SUCCESS;
282 }
283