]> git.sesse.net Git - vlc/blob - src/interface/interface.c
interface: fix building on MacOS X
[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     /* Unlock module if present (a switch may have failed) */
71     if( p_intf->p_module )
72         module_unneed( p_intf, p_intf->p_module );
73
74     free( p_intf->psz_intf );
75     config_ChainDestroy( p_intf->p_cfg );
76     vlc_mutex_destroy( &p_intf->change_lock );
77 }
78
79
80 /**
81  * Create the interface, and prepare it for main loop. It opens ouput device
82  * and creates specific interfaces. Sends its own error messages.
83  *
84  * @param p_this the calling vlc_object_t
85  * @param psz_module a preferred interface module
86  * @return a pointer to the created interface thread, NULL on error
87  */
88 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
89 {
90     intf_thread_t * p_intf;
91
92     /* Allocate structure */
93     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
94     if( !p_intf )
95         return NULL;
96 #if defined( __APPLE__ ) || defined( WIN32 )
97     p_intf->b_should_run_on_first_thread = false;
98 #endif
99
100     /* Choose the best module */
101     p_intf->p_cfg = NULL;
102     char *psz_parser = *psz_module == '$'
103                      ? var_CreateGetString(p_intf,psz_module+1)
104                      : strdup( psz_module );
105     char *psz_tmp = config_ChainCreate( &p_intf->psz_intf, &p_intf->p_cfg,
106                                         psz_parser );
107     free( psz_tmp );
108     free( psz_parser );
109     p_intf->p_module = module_need( p_intf, "interface", p_intf->psz_intf, true );
110
111     if( p_intf->p_module == NULL )
112     {
113         msg_Err( p_intf, "no suitable interface module" );
114         free( p_intf->psz_intf );
115         vlc_object_release( p_intf );
116         return NULL;
117     }
118
119     /* Initialize mutexes */
120     vlc_mutex_init( &p_intf->change_lock );
121
122     /* Attach interface to its parent object */
123     vlc_object_attach( p_intf, p_this );
124     vlc_object_set_destructor( p_intf, intf_Destroy );
125
126     return p_intf;
127 }
128
129
130 /**
131  * Starts and runs the interface thread.
132  *
133  * @param p_intf the interface thread
134  * @return VLC_SUCCESS on success, an error number else
135  */
136 int intf_RunThread( intf_thread_t *p_intf )
137 {
138 #if defined( __APPLE__ ) || defined( WIN32 )
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 ) )
145         {
146             msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
147             return VLC_EGENERIC;
148         }
149         RunInterface( VLC_OBJECT(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_kill( 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 #endif
162     /* Run the interface in a separate thread */
163     if( vlc_thread_create( p_intf, "interface", RunInterface,
164                            VLC_THREAD_PRIORITY_LOW ) )
165     {
166         msg_Err( p_intf, "cannot spawn interface thread" );
167         return VLC_EGENERIC;
168     }
169
170     return VLC_SUCCESS;
171 }
172
173
174 /**
175  * Stops the interface thread
176  *
177  * This function asks the interface thread to stop
178  * @param p_intf the interface thread
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
188
189 /* Following functions are local */
190
191 /**
192  * RunInterface: setups necessary data and give control to the interface
193  *
194  * @param p_this: interface object
195  */
196 static void* RunInterface( vlc_object_t *p_this )
197 {
198     intf_thread_t *p_intf = (intf_thread_t *)p_this;
199     vlc_value_t val, text;
200     int canc = vlc_savecancel ();
201
202     /* Variable used for interface spawning */
203     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
204                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
205     text.psz_string = _("Add Interface");
206     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
207
208     val.psz_string = (char *)"rc";
209     text.psz_string = (char *)_("Console");
210     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
211     val.psz_string = (char *)"telnet";
212     text.psz_string = (char *)_("Telnet Interface");
213     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
214     val.psz_string = (char *)"http";
215     text.psz_string = (char *)_("Web Interface");
216     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
217     val.psz_string = (char *)"logger";
218     text.psz_string = (char *)_("Debug logging");
219     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
220     val.psz_string = (char *)"gestures";
221     text.psz_string = (char *)_("Mouse Gestures");
222     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
223
224     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
225     vlc_restorecancel (canc);
226
227     /* Give control to the interface */
228     if( p_intf->pf_run )
229         p_intf->pf_run( p_intf );
230
231     return NULL;
232 }
233
234 #if defined( __APPLE__ ) || defined( WIN32 )
235 #include "control/libvlc_internal.h" /* libvlc_InternalWait */
236 /**
237  * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
238  *
239  * @param p_this: the interface object
240  */
241 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
242 {
243     intf_thread_t *p_intf = (intf_thread_t *)p_this;
244     libvlc_int_t * p_libvlc = p_intf->p_libvlc;
245     int canc = vlc_savecancel ();
246
247     libvlc_InternalWait( p_libvlc );
248
249     vlc_object_kill( p_intf ); /* Kill the stupid first thread interface */
250     vlc_restorecancel (canc);
251     return NULL;
252 }
253 #endif
254
255 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
256                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
257 {
258     (void)psz_cmd; (void)oldval; (void)p_data;
259     intf_thread_t *p_intf;
260     char* psz_intf;
261
262     /* Try to create the interface */
263     if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
264         return VLC_ENOMEM;
265
266     p_intf = intf_Create( p_this->p_libvlc, psz_intf );
267     free( psz_intf );
268     if( p_intf == NULL )
269     {
270         msg_Err( p_this, "interface \"%s\" initialization failed",
271                  newval.psz_string );
272         return VLC_EGENERIC;
273     }
274
275     /* Try to run the interface */
276     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
277     {
278         vlc_object_detach( p_intf );
279         vlc_object_release( p_intf );
280         return VLC_EGENERIC;
281     }
282
283     return VLC_SUCCESS;
284 }
285