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