]> git.sesse.net Git - vlc/blob - src/interface/interface.c
5b2d392af373be0de4b5907ef0ac3e1edaeed949
[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 #include "modules/modules.h" // Gruik!
47 #include "libvlc.h"
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static void RunInterface( intf_thread_t *p_intf );
53 #ifdef __APPLE__
54 static void MonitorLibVLCDeath( intf_thread_t *p_intf );
55 #endif
56 static int AddIntfCallback( vlc_object_t *, char const *,
57                             vlc_value_t , vlc_value_t , void * );
58
59 /**
60  * \brief Destroy the interface after the main loop endeed.
61  *
62  * \param p_intf the interface thread
63  * \return nothing
64  */
65 static void intf_Destroy( vlc_object_t *obj )
66 {
67     intf_thread_t *p_intf = (intf_thread_t *)obj;
68
69     /* Unlock module if present (a switch may have failed) */
70     if( p_intf->p_module )
71         module_Unneed( p_intf, p_intf->p_module );
72
73     free( p_intf->psz_intf );
74     vlc_mutex_destroy( &p_intf->change_lock );
75 }
76
77 /*****************************************************************************
78  * intf_Create: prepare interface before main loop
79  *****************************************************************************
80  * This function opens output devices and creates specific interfaces. It sends
81  * its own error messages.
82  *****************************************************************************/
83 /**
84  * Create the interface, and prepare it for main loop.
85  *
86  * \param p_this the calling vlc_object_t
87  * \param psz_module a preferred interface module
88  * \return a pointer to the created interface thread, NULL on error
89  */
90 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
91 {
92     intf_thread_t * p_intf;
93
94     /* Allocate structure */
95     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
96     if( !p_intf )
97         return NULL;
98     p_intf->b_interaction = false;
99 #ifdef __APPLE__
100     p_intf->b_should_run_on_first_thread = false;
101 #endif
102
103     /* Choose the best module */
104     p_intf->psz_intf = strdup( psz_module );
105     p_intf->p_module = module_Need( p_intf, "interface", psz_module, false );
106
107     if( p_intf->p_module == NULL )
108     {
109         msg_Err( p_intf, "no suitable interface module" );
110         free( p_intf->psz_intf );
111         vlc_object_release( p_intf );
112         return NULL;
113     }
114
115     /* Initialize structure */
116     p_intf->b_menu        = false;
117     p_intf->b_menu_change = false;
118
119     /* Initialize mutexes */
120     vlc_mutex_init( &p_intf->change_lock );
121
122     /* Attach interface to its parent object */
123     vlc_object_attach( p_intf, p_this );
124     vlc_object_set_destructor( p_intf, intf_Destroy );
125
126     return p_intf;
127 }
128
129 /*****************************************************************************
130  * intf_RunThread: launch the interface thread
131  *****************************************************************************
132  * This function either creates a new thread and runs the interface in it.
133  *****************************************************************************/
134 /**
135  * Starts and runs the interface thread.
136  *
137  * \param p_intf the interface thread
138  * \return VLC_SUCCESS on success, an error number else
139  */
140 int intf_RunThread( intf_thread_t *p_intf )
141 {
142 #ifdef __APPLE__
143     /* Hack to get Mac OS X Cocoa runtime running
144      * (it needs access to the main thread) */
145     if( p_intf->b_should_run_on_first_thread )
146     {
147         if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
148                                VLC_THREAD_PRIORITY_LOW, false ) )
149         {
150             msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
151             return VLC_EGENERIC;
152         }
153         RunInterface( p_intf );
154
155         /* Make sure our MonitorLibVLCDeath thread exit */
156         vlc_object_kill( p_intf );
157         /* It is monitoring libvlc, not the p_intf */
158         vlc_object_signal( p_intf->p_libvlc );
159         vlc_thread_join( p_intf );
160
161         vlc_object_detach( p_intf );
162         vlc_object_release( p_intf );
163         return VLC_SUCCESS;
164     }
165 #endif
166     /* Run the interface in a separate thread */
167     if( vlc_thread_create( p_intf, "interface", RunInterface,
168                            VLC_THREAD_PRIORITY_LOW, false ) )
169     {
170         msg_Err( p_intf, "cannot spawn interface thread" );
171         return VLC_EGENERIC;
172     }
173
174     return VLC_SUCCESS;
175 }
176
177 /**
178  * Stops the interface thread
179  *
180  * This function asks the interface thread to stop
181  * \param p_intf the interface thread
182  * \return nothing
183  */
184 void intf_StopThread( intf_thread_t *p_intf )
185 {
186     /* Tell the interface to die */
187     vlc_object_kill( p_intf );
188     vlc_thread_join( p_intf );
189 }
190
191 /* Following functions are local */
192
193 /*****************************************************************************
194  * RunInterface: setups necessary data and give control to the interface
195  *****************************************************************************/
196 static void RunInterface( intf_thread_t *p_intf )
197 {
198     vlc_value_t val, text;
199     char *psz_intf;
200
201     /* Variable used for interface spawning */
202     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
203                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
204     text.psz_string = _("Add Interface");
205     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
206
207     val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
208     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
209     val.psz_string = (char *)"telnet";
210     text.psz_string = (char *)_("Telnet Interface");
211     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
212     val.psz_string = (char *)"http";
213     text.psz_string = (char *)_("Web Interface");
214     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
215     val.psz_string = (char *)"logger";
216     text.psz_string = (char *)_("Debug logging");
217     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
218     val.psz_string = (char *)"gestures";
219     text.psz_string = (char *)_("Mouse Gestures");
220     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
221
222     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
223
224     do
225     {
226         /* Give control to the interface */
227         if( p_intf->pf_run )
228             p_intf->pf_run( p_intf );
229         else
230         {
231             vlc_object_lock( p_intf );
232             while( vlc_object_alive( p_intf ) )
233                 vlc_object_wait( p_intf );
234             vlc_object_unlock( p_intf );
235         }
236
237         if( !p_intf->psz_switch_intf )
238         {
239             break;
240         }
241
242         /* Make sure the old interface is completely uninitialized */
243         module_Unneed( p_intf, p_intf->p_module );
244
245         /* Provide ability to switch the main interface on the fly */
246         psz_intf = p_intf->psz_switch_intf;
247         p_intf->psz_switch_intf = NULL;
248
249         vlc_object_lock( p_intf );
250         p_intf->b_die = false; /* FIXME */
251         p_intf->b_dead = false;
252
253         vlc_object_unlock( p_intf );
254
255         p_intf->psz_intf = psz_intf;
256         p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
257     }
258     while( p_intf->p_module );
259 }
260
261 #ifdef __APPLE__
262 /*****************************************************************************
263  * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
264  *****************************************************************************/
265 static void MonitorLibVLCDeath( intf_thread_t *p_intf )
266 {
267     libvlc_int_t * p_libvlc = p_intf->p_libvlc;
268     vlc_object_lock( p_libvlc );
269     while(vlc_object_alive( p_libvlc ) )
270     {
271         if(p_intf->b_die)
272         {
273             vlc_object_unlock( p_libvlc );
274             return;
275         }
276         vlc_object_wait( p_libvlc );
277     }
278     vlc_object_unlock( p_libvlc );
279
280     /* Someone killed libvlc */
281
282     /* Make sure we kill all interface objects, especially
283      * those that are blocking libvlc (running on main thread) */
284     vlc_list_t * p_list = vlc_list_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD );
285     for( int i = 0; i < p_list->i_count; i++ )
286     {
287         vlc_object_t * p_intf = p_list->p_values[i].p_object;
288         vlc_object_kill( p_intf );
289     }
290     vlc_list_release( p_list );
291 }
292 #endif
293
294 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
295                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
296 {
297     intf_thread_t *p_intf;
298     char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
299
300     (void)psz_cmd; (void)oldval; (void)p_data;
301
302     /* Try to create the interface */
303     sprintf( psz_intf, "%s,none", newval.psz_string );
304     p_intf = intf_Create( p_this->p_libvlc, psz_intf );
305     free( psz_intf );
306     if( p_intf == NULL )
307     {
308         msg_Err( p_this, "interface \"%s\" initialization failed",
309                  newval.psz_string );
310         return VLC_EGENERIC;
311     }
312
313     /* Try to run the interface */
314     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
315     {
316         vlc_object_detach( p_intf );
317         vlc_object_release( p_intf );
318         return VLC_EGENERIC;
319     }
320
321     return VLC_SUCCESS;
322 }
323