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