]> git.sesse.net Git - vlc/blob - src/interface/interface.c
Merge intf_Create() and intf_RunThread()
[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 }
73
74
75 #undef intf_Create
76 /**
77  * Create and start an interface.
78  *
79  * @param p_this the calling vlc_object_t
80  * @param psz_module a preferred interface module
81  * @return VLC_SUCCESS or an error code
82  */
83 int intf_Create( vlc_object_t *p_this, const char *psz_module )
84 {
85     intf_thread_t * p_intf;
86
87     /* Allocate structure */
88     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
89     if( !p_intf )
90         return VLC_ENOMEM;
91
92     /* Attach interface to its parent object */
93     vlc_object_attach( p_intf, p_this );
94     vlc_object_set_destructor( p_intf, intf_Destroy );
95 #if defined( __APPLE__ ) || defined( WIN32 )
96     p_intf->b_should_run_on_first_thread = false;
97 #endif
98
99     /* Choose the best module */
100     p_intf->p_cfg = NULL;
101     char *psz_parser = *psz_module == '$'
102                      ? var_CreateGetString(p_intf,psz_module+1)
103                      : strdup( psz_module );
104     char *psz_tmp = config_ChainCreate( &p_intf->psz_intf, &p_intf->p_cfg,
105                                         psz_parser );
106     free( psz_tmp );
107     free( psz_parser );
108     p_intf->p_module = module_need( p_intf, "interface", p_intf->psz_intf, true );
109     if( p_intf->p_module == NULL )
110     {
111         msg_Err( p_intf, "no suitable interface module" );
112         vlc_object_release( p_intf );
113         return VLC_EGENERIC;
114     }
115
116 #if defined( __APPLE__ ) || defined( WIN32 )
117     /* Hack to get Mac OS X Cocoa runtime running
118      * (it needs access to the main thread) */
119     if( p_intf->b_should_run_on_first_thread )
120     {
121         if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
122                                VLC_THREAD_PRIORITY_LOW ) )
123         {
124             msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
125             vlc_object_release( p_intf );
126             return VLC_ENOMEM;
127         }
128         RunInterface( VLC_OBJECT(p_intf) );
129
130         /* Make sure our MonitorLibVLCDeath thread exit */
131         vlc_object_kill( p_intf );
132         /* It is monitoring libvlc, not the p_intf */
133         vlc_object_kill( p_intf->p_libvlc );
134         vlc_thread_join( p_intf );
135
136         vlc_object_detach( p_intf );
137         vlc_object_release( p_intf );
138         return VLC_SUCCESS;
139     }
140 #endif
141     /* Run the interface in a separate thread */
142     if( vlc_thread_create( p_intf, "interface", RunInterface,
143                            VLC_THREAD_PRIORITY_LOW ) )
144     {
145         msg_Err( p_intf, "cannot spawn interface thread" );
146         vlc_object_release( p_intf );
147         return VLC_EGENERIC;
148     }
149
150     return VLC_SUCCESS;
151 }
152
153
154 /**
155  * Stops the interface thread
156  *
157  * This function asks the interface thread to stop
158  * @param p_intf the interface thread
159  */
160 void intf_StopThread( intf_thread_t *p_intf )
161 {
162     /* Tell the interface to die */
163     vlc_object_kill( p_intf );
164     vlc_thread_join( p_intf );
165
166     module_unneed( p_intf, p_intf->p_module );
167 }
168
169
170
171 /* Following functions are local */
172
173 /**
174  * RunInterface: setups necessary data and give control to the interface
175  *
176  * @param p_this: interface object
177  */
178 static void* RunInterface( vlc_object_t *p_this )
179 {
180     intf_thread_t *p_intf = (intf_thread_t *)p_this;
181     vlc_value_t val, text;
182     int canc = vlc_savecancel ();
183
184     /* Variable used for interface spawning */
185     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
186                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
187     text.psz_string = _("Add Interface");
188     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
189
190     val.psz_string = (char *)"rc";
191     text.psz_string = (char *)_("Console");
192     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
193     val.psz_string = (char *)"telnet";
194     text.psz_string = (char *)_("Telnet Interface");
195     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
196     val.psz_string = (char *)"http";
197     text.psz_string = (char *)_("Web Interface");
198     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
199     val.psz_string = (char *)"logger";
200     text.psz_string = (char *)_("Debug logging");
201     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
202     val.psz_string = (char *)"gestures";
203     text.psz_string = (char *)_("Mouse Gestures");
204     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
205
206     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
207     vlc_restorecancel (canc);
208
209     /* Give control to the interface */
210     if( p_intf->pf_run )
211         p_intf->pf_run( p_intf );
212
213     return NULL;
214 }
215
216 #if defined( __APPLE__ ) || defined( WIN32 )
217 #include "control/libvlc_internal.h" /* libvlc_InternalWait */
218 /**
219  * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
220  *
221  * @param p_this: the interface object
222  */
223 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
224 {
225     intf_thread_t *p_intf = (intf_thread_t *)p_this;
226     libvlc_int_t * p_libvlc = p_intf->p_libvlc;
227     int canc = vlc_savecancel ();
228
229     libvlc_InternalWait( p_libvlc );
230
231     vlc_object_kill( p_intf ); /* Kill the stupid first thread interface */
232     vlc_restorecancel (canc);
233     return NULL;
234 }
235 #endif
236
237 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
238                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
239 {
240     (void)psz_cmd; (void)oldval; (void)p_data;
241     char* psz_intf;
242
243     /* Try to create the interface */
244     if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
245         return VLC_ENOMEM;
246
247     int ret = intf_Create( p_this->p_libvlc, psz_intf );
248     free( psz_intf );
249     if( ret )
250         msg_Err( p_this, "interface \"%s\" initialization failed",
251                  newval.psz_string );
252     return ret;
253 }