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