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