]> git.sesse.net Git - vlc/blob - src/interface/interface.c
Merge commit 'origin/1.0-bugfix'
[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 <assert.h>
41 #include <vlc_common.h>
42
43 #include <vlc_aout.h>
44 #include <vlc_vout.h>
45
46 #include "vlc_interface.h"
47 #if defined( __APPLE__ ) || defined( WIN32 )
48 #include "../control/libvlc_internal.h"
49 #endif
50 #include "libvlc.h"
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static void* RunInterface( vlc_object_t *p_this );
56 #if defined( __APPLE__ ) || defined( WIN32 )
57 static void * MonitorLibVLCDeath( vlc_object_t *p_this );
58 #endif
59 static int AddIntfCallback( vlc_object_t *, char const *,
60                             vlc_value_t , vlc_value_t , void * );
61
62 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
63
64 #undef intf_Create
65 /**
66  * Create and start an interface.
67  *
68  * @param p_this the calling vlc_object_t
69  * @param psz_module a preferred interface module
70  * @return VLC_SUCCESS or an error code
71  */
72 int intf_Create( vlc_object_t *p_this, const char *psz_module )
73 {
74     libvlc_int_t *p_libvlc = p_this->p_libvlc;
75     intf_thread_t * p_intf;
76     static const char psz_type[] = "interface";
77
78     /* Allocate structure */
79     p_intf = vlc_custom_create( p_libvlc, sizeof( *p_intf ),
80                                 VLC_OBJECT_GENERIC, psz_type );
81     if( !p_intf )
82         return VLC_ENOMEM;
83
84     /* Variable used for interface spawning */
85     vlc_value_t val, text;
86     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
87                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
88     text.psz_string = _("Add Interface");
89     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
90
91     val.psz_string = (char *)"rc";
92     text.psz_string = (char *)_("Console");
93     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
94     val.psz_string = (char *)"telnet";
95     text.psz_string = (char *)_("Telnet Interface");
96     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
97     val.psz_string = (char *)"http";
98     text.psz_string = (char *)_("Web Interface");
99     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
100     val.psz_string = (char *)"logger";
101     text.psz_string = (char *)_("Debug logging");
102     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
103     val.psz_string = (char *)"gestures";
104     text.psz_string = (char *)_("Mouse Gestures");
105     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
106
107     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
108
109     /* Attach interface to LibVLC */
110     vlc_object_attach( p_intf, p_libvlc );
111 #if defined( __APPLE__ ) || defined( WIN32 )
112     p_intf->b_should_run_on_first_thread = false;
113 #endif
114
115     /* Choose the best module */
116     p_intf->p_cfg = NULL;
117     char *psz_parser = *psz_module == '$'
118                      ? var_CreateGetString(p_intf,psz_module+1)
119                      : strdup( psz_module );
120     char *psz_tmp = config_ChainCreate( &p_intf->psz_intf, &p_intf->p_cfg,
121                                         psz_parser );
122     free( psz_tmp );
123     free( psz_parser );
124     p_intf->p_module = module_need( p_intf, "interface", p_intf->psz_intf, true );
125     if( p_intf->p_module == NULL )
126     {
127         msg_Err( p_intf, "no suitable interface module" );
128         goto error;
129     }
130
131     vlc_mutex_lock( &lock );
132     if( !vlc_object_alive( p_libvlc ) )
133     {
134         vlc_mutex_unlock( &lock );
135         goto error; /* Too late! */
136     }
137 #if defined( __APPLE__ ) || defined( WIN32 )
138     /* Hack to get Mac OS X Cocoa runtime running
139      * (it needs access to the main thread) */
140     if( p_intf->b_should_run_on_first_thread )
141     {
142         if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
143                                VLC_THREAD_PRIORITY_LOW ) )
144         {
145             msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
146             vlc_mutex_unlock( &lock );
147             goto error;
148         }
149         assert( p_intf->pf_run );
150         p_intf->pf_run( p_intf );
151
152         /* It is monitoring libvlc, not the p_intf */
153         vlc_object_kill( p_intf->p_libvlc );
154     }
155     else
156 #endif
157     /* Run the interface in a separate thread */
158     if( p_intf->pf_run
159      && vlc_thread_create( p_intf, "interface", RunInterface,
160                            VLC_THREAD_PRIORITY_LOW ) )
161     {
162         msg_Err( p_intf, "cannot spawn interface thread" );
163         vlc_mutex_unlock( &lock );
164         goto error;
165     }
166
167     p_intf->p_next = libvlc_priv( p_libvlc )->p_intf;
168     libvlc_priv( p_libvlc )->p_intf = p_intf;
169     vlc_mutex_unlock( &lock );
170
171     return VLC_SUCCESS;
172
173 error:
174     if( p_intf->p_module )
175         module_unneed( p_intf, p_intf->p_module );
176     config_ChainDestroy( p_intf->p_cfg );
177     free( p_intf->psz_intf );
178     vlc_object_release( p_intf );
179     return VLC_EGENERIC;
180 }
181
182
183 /**
184  * Stops and destroys all interfaces
185  * @param p_libvlc the LibVLC instance
186  */
187 void intf_DestroyAll( libvlc_int_t *p_libvlc )
188 {
189     intf_thread_t *p_first;
190
191     assert( !vlc_object_alive( p_libvlc ) );
192
193     vlc_mutex_lock( &lock );
194     p_first = libvlc_priv( p_libvlc )->p_intf;
195 #ifndef NDEBUG
196     libvlc_priv( p_libvlc )->p_intf = NULL;
197 #endif
198     vlc_mutex_unlock( &lock );
199
200     /* Tell the interfaces to die */
201     for( intf_thread_t *p_intf = p_first; p_intf; p_intf = p_intf->p_next )
202         vlc_object_kill( p_intf );
203
204     /* Cleanup the interfaces */
205     for( intf_thread_t *p_intf = p_first; p_intf != NULL; )
206     {
207         intf_thread_t *p_next = p_intf->p_next;
208
209         if( p_intf->pf_run )
210             vlc_thread_join( p_intf );
211         module_unneed( p_intf, p_intf->p_module );
212         free( p_intf->psz_intf );
213         config_ChainDestroy( p_intf->p_cfg );
214         vlc_object_release( p_intf );
215
216         p_intf = p_next;
217     }
218 }
219
220 /* Following functions are local */
221
222 /**
223  * RunInterface: setups necessary data and give control to the interface
224  *
225  * @param p_this: interface object
226  */
227 static void* RunInterface( vlc_object_t *p_this )
228 {
229     intf_thread_t *p_intf = (intf_thread_t *)p_this;
230
231     p_intf->pf_run( p_intf );
232     return NULL;
233 }
234
235 #if defined( __APPLE__ ) || defined( WIN32 )
236 #include "control/libvlc_internal.h" /* libvlc_InternalWait */
237 /**
238  * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
239  *
240  * @param p_this: the interface object
241  */
242 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
243 {
244     intf_thread_t *p_intf = (intf_thread_t *)p_this;
245     libvlc_int_t * p_libvlc = p_intf->p_libvlc;
246     int canc = vlc_savecancel ();
247
248     libvlc_InternalWait( p_libvlc );
249
250     vlc_object_kill( p_intf ); /* Kill the stupid first thread interface */
251     vlc_restorecancel (canc);
252     return NULL;
253 }
254 #endif
255
256 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
257                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
258 {
259     (void)psz_cmd; (void)oldval; (void)p_data;
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     int ret = intf_Create( VLC_OBJECT(p_this->p_libvlc), psz_intf );
267     free( psz_intf );
268     if( ret )
269         msg_Err( p_this, "interface \"%s\" initialization failed",
270                  newval.psz_string );
271     return ret;
272 }