]> git.sesse.net Git - vlc/blob - src/interface/interface.c
interaction: safely join the thread
[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 #include "modules/modules.h" // Gruik!
47 #include "libvlc.h"
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static void RunInterface( intf_thread_t *p_intf );
53 static void MonitorLibVLCDeath( intf_thread_t *p_intf );
54
55 static int AddIntfCallback( vlc_object_t *, char const *,
56                             vlc_value_t , vlc_value_t , void * );
57
58 /**
59  * \brief Destroy the interface after the main loop endeed.
60  *
61  * \param p_intf the interface thread
62  * \return nothing
63  */
64 static void intf_Destroy( vlc_object_t *obj )
65 {
66     intf_thread_t *p_intf = (intf_thread_t *)obj;
67
68     /* Unlock module if present (a switch may have failed) */
69     if( p_intf->p_module )
70         module_Unneed( p_intf, p_intf->p_module );
71
72     free( p_intf->psz_intf );
73     vlc_mutex_destroy( &p_intf->change_lock );
74 }
75
76 /*****************************************************************************
77  * intf_Create: prepare interface before main loop
78  *****************************************************************************
79  * This function opens output devices and creates specific interfaces. It sends
80  * its own error messages.
81  *****************************************************************************/
82 /**
83  * Create the interface, and prepare it for main loop.
84  *
85  * \param p_this the calling vlc_object_t
86  * \param psz_module a preferred interface module
87  * \return a pointer to the created interface thread, NULL on error
88  */
89 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
90 {
91     intf_thread_t * p_intf;
92
93     /* Allocate structure */
94     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
95     if( !p_intf )
96         return NULL;
97     p_intf->b_interaction = false;
98     p_intf->b_should_run_on_first_thread = false;
99
100     /* Choose the best module */
101     p_intf->psz_intf = strdup( psz_module );
102     p_intf->p_module = module_Need( p_intf, "interface", psz_module, false );
103
104     if( p_intf->p_module == NULL )
105     {
106         msg_Err( p_intf, "no suitable interface module" );
107         free( p_intf->psz_intf );
108         vlc_object_release( p_intf );
109         return NULL;
110     }
111
112     /* Initialize structure */
113     p_intf->b_menu        = false;
114     p_intf->b_menu_change = false;
115
116     /* Initialize mutexes */
117     vlc_mutex_init( &p_intf->change_lock );
118
119     /* Attach interface to its parent object */
120     vlc_object_attach( p_intf, p_this );
121     vlc_object_set_destructor( p_intf, intf_Destroy );
122
123     return p_intf;
124 }
125
126 /*****************************************************************************
127  * intf_RunThread: launch the interface thread
128  *****************************************************************************
129  * This function either creates a new thread and runs the interface in it.
130  *****************************************************************************/
131 /**
132  * Starts and runs the interface thread.
133  *
134  * \param p_intf the interface thread
135  * \return VLC_SUCCESS on success, an error number else
136  */
137 int intf_RunThread( intf_thread_t *p_intf )
138 {
139     /* Hack to get Mac OS X Cocoa runtime running
140      * (it needs access to the main thread) */
141     if( p_intf->b_should_run_on_first_thread )
142     {
143         if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
144                                VLC_THREAD_PRIORITY_LOW, false ) )
145         {
146             msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
147             return VLC_EGENERIC;
148         }
149         RunInterface( p_intf );
150
151         /* Make sure our MonitorLibVLCDeath thread exit */
152         vlc_object_kill( p_intf );
153         /* It is monitoring libvlc, not the p_intf */
154         vlc_object_signal( p_intf->p_libvlc );
155         vlc_thread_join( p_intf );
156
157         vlc_object_detach( p_intf );
158         vlc_object_release( p_intf );
159         return VLC_SUCCESS;
160     }
161     
162     /* Run the interface in a separate thread */
163     if( vlc_thread_create( p_intf, "interface", RunInterface,
164                            VLC_THREAD_PRIORITY_LOW, false ) )
165     {
166         msg_Err( p_intf, "cannot spawn interface thread" );
167         return VLC_EGENERIC;
168     }
169
170     return VLC_SUCCESS;
171 }
172
173 /**
174  * Stops the interface thread
175  *
176  * This function asks the interface thread to stop
177  * \param p_intf the interface thread
178  * \return nothing
179  */
180 void intf_StopThread( intf_thread_t *p_intf )
181 {
182     /* Tell the interface to die */
183     vlc_object_kill( p_intf );
184     vlc_thread_join( p_intf );
185 }
186
187 /* Following functions are local */
188
189 /*****************************************************************************
190  * RunInterface: setups necessary data and give control to the interface
191  *****************************************************************************/
192 static void RunInterface( intf_thread_t *p_intf )
193 {
194     vlc_value_t val, text;
195     char *psz_intf;
196
197     /* Variable used for interface spawning */
198     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
199                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
200     text.psz_string = _("Add Interface");
201     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
202
203     val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
204     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
205     val.psz_string = (char *)"telnet";
206     text.psz_string = (char *)_("Telnet Interface");
207     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
208     val.psz_string = (char *)"http";
209     text.psz_string = (char *)_("Web Interface");
210     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
211     val.psz_string = (char *)"logger";
212     text.psz_string = (char *)_("Debug logging");
213     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
214     val.psz_string = (char *)"gestures";
215     text.psz_string = (char *)_("Mouse Gestures");
216     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
217
218     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
219
220     do
221     {
222         /* Give control to the interface */
223         if( p_intf->pf_run )
224             p_intf->pf_run( p_intf );
225         else
226         {
227             vlc_object_lock( p_intf );
228             while( vlc_object_alive( p_intf ) )
229                 vlc_object_wait( p_intf );
230             vlc_object_unlock( p_intf );
231         }
232
233         if( !p_intf->psz_switch_intf )
234         {
235             break;
236         }
237
238         /* Make sure the old interface is completely uninitialized */
239         module_Unneed( p_intf, p_intf->p_module );
240
241         /* Provide ability to switch the main interface on the fly */
242         psz_intf = p_intf->psz_switch_intf;
243         p_intf->psz_switch_intf = NULL;
244
245         vlc_object_lock( p_intf );
246         p_intf->b_die = false; /* FIXME */
247         p_intf->b_dead = false;
248
249         vlc_object_unlock( p_intf );
250
251         p_intf->psz_intf = psz_intf;
252         p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
253     }
254     while( p_intf->p_module );
255 }
256
257 /*****************************************************************************
258  * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
259  *****************************************************************************/
260 static void MonitorLibVLCDeath( intf_thread_t *p_intf )
261 {
262     libvlc_int_t * p_libvlc = p_intf->p_libvlc;
263     vlc_object_lock( p_libvlc );
264     while(vlc_object_alive( p_libvlc ) )
265     {
266         if(p_intf->b_die)
267         {
268             vlc_object_unlock( p_libvlc );
269             return;
270         }
271         vlc_object_wait( p_libvlc );
272     }
273     vlc_object_unlock( p_libvlc );
274
275     /* Someone killed libvlc */
276
277     /* Make sure we kill all interface objects, especially
278      * those that are blocking libvlc (running on main thread) */
279     vlc_list_t * p_list = vlc_list_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD );
280     for( int i = 0; i < p_list->i_count; i++ )
281     {
282         vlc_object_t * p_intf = p_list->p_values[i].p_object;
283         vlc_object_kill( p_intf );
284     }
285     vlc_list_release( p_list );
286
287 }
288
289 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
290                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
291 {
292     intf_thread_t *p_intf;
293     char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
294
295     (void)psz_cmd; (void)oldval; (void)p_data;
296
297     /* Try to create the interface */
298     sprintf( psz_intf, "%s,none", newval.psz_string );
299     p_intf = intf_Create( p_this->p_libvlc, psz_intf );
300     free( psz_intf );
301     if( p_intf == NULL )
302     {
303         msg_Err( p_this, "interface \"%s\" initialization failed",
304                  newval.psz_string );
305         return VLC_EGENERIC;
306     }
307
308     /* Try to run the interface */
309     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
310     {
311         vlc_object_detach( p_intf );
312         vlc_object_release( p_intf );
313         return VLC_EGENERIC;
314     }
315
316     return VLC_SUCCESS;
317 }
318