]> git.sesse.net Git - vlc/blob - src/interface/interface.c
* src/interface/interface.c: interfaces added on the fly are attached to p_vlc instea...
[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-2001 VideoLAN
7  * $Id: interface.c,v 1.108 2003/10/14 23:09:03 gbazin Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /**
27  *   \file
28  *   This file contains functions related to interface management
29  */
30
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #include <stdlib.h>                                      /* free(), strtol() */
36 #include <stdio.h>                                                   /* FILE */
37 #include <string.h>                                            /* strerror() */
38
39 #include <vlc/vlc.h>
40
41 #include "stream_control.h"
42 #include "input_ext-intf.h"
43
44 #include "audio_output.h"
45
46 #include "vlc_interface.h"
47
48 #include "vlc_video.h"
49 #include "video_output.h"
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static void Manager( intf_thread_t *p_intf );
55 static void RunInterface( intf_thread_t *p_intf );
56
57 static int SwitchIntfCallback( vlc_object_t *, char const *,
58                                vlc_value_t , vlc_value_t , void * );
59 static int AddIntfCallback( vlc_object_t *, char const *,
60                             vlc_value_t , vlc_value_t , void * );
61
62 /*****************************************************************************
63  * intf_Create: prepare interface before main loop
64  *****************************************************************************
65  * This function opens output devices and creates specific interfaces. It sends
66  * its own error messages.
67  *****************************************************************************/
68 /**
69  * Create the interface, and prepare it for main loop.
70  * 
71  * \param p_this the calling vlc_object_t
72  * \param psz_module a prefered interface module
73  * \return a pointer to the created interface thread, NULL on error
74  */
75 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
76 {
77     intf_thread_t * p_intf;
78     char *psz_intf;
79
80     /* Allocate structure */
81     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
82     if( !p_intf )
83     {
84         msg_Err( p_this, "out of memory" );
85         return NULL;
86     }
87
88     /* XXX: workaround for a bug in VLC 0.5.0 where the dvdplay plugin was
89      * registering itself in $interface, which we do not want to happen. */
90     psz_intf = config_GetPsz( p_intf, "intf" );
91     if( psz_intf )
92     {
93         if( !strcasecmp( psz_intf, "dvdplay" ) )
94         {
95             config_PutPsz( p_intf, "intf", "" );
96         }
97         free( psz_intf );
98     }
99
100     /* Choose the best module */
101     p_intf->p_module = module_Need( p_intf, "interface", psz_module );
102
103     if( p_intf->p_module == NULL )
104     {
105         msg_Err( p_intf, "no suitable intf module" );
106         vlc_object_destroy( p_intf );
107         return NULL;
108     }
109
110     /* Initialize structure */
111     p_intf->b_menu        = VLC_FALSE;
112     p_intf->b_menu_change = VLC_FALSE;
113
114     /* Initialize mutexes */
115     vlc_mutex_init( p_intf, &p_intf->change_lock );
116
117     msg_Dbg( p_intf, "interface initialized" );
118
119     /* Attach interface to its parent object */
120     vlc_object_attach( p_intf, p_this );
121
122     return p_intf;
123 }
124
125 /*****************************************************************************
126  * intf_RunThread: launch the interface thread
127  *****************************************************************************
128  * This function either creates a new thread and runs the interface in it,
129  * or runs the interface in the current thread, depending on b_block.
130  *****************************************************************************/
131 /**
132  * Run the interface thread.
133  *
134  * If b_block is not set, runs the interface in the thread, else, 
135  * creates a new thread and runs the interface.
136  * \param p_intf the interface thread
137  * \return VLC_SUCCESS on success, an error number else
138  */
139 int intf_RunThread( intf_thread_t *p_intf )
140 {
141     if( p_intf->b_block )
142     {
143         /* Run a manager thread, launch the interface, kill the manager */
144         if( vlc_thread_create( p_intf, "manager", Manager,
145                                VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
146         {
147             msg_Err( p_intf, "cannot spawn manager thread" );
148             return VLC_EGENERIC;
149         }
150
151         RunInterface( p_intf );
152
153         p_intf->b_die = VLC_TRUE;
154
155         /* Do not join the thread... intf_StopThread will do it for us */
156     }
157     else
158     {
159         /* Run the interface in a separate thread */
160         if( vlc_thread_create( p_intf, "interface", RunInterface,
161                                VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
162         {
163             msg_Err( p_intf, "cannot spawn interface thread" );
164             return VLC_EGENERIC;
165         }
166     }
167
168     return VLC_SUCCESS;
169 }
170
171 /*****************************************************************************
172  * intf_StopThread: end the interface thread
173  *****************************************************************************
174  * This function asks the interface thread to stop.
175  *****************************************************************************/
176 /**
177  * Stops the interface thread
178  *
179  * This function asks the interface thread to stop
180  * \param p_intf the interface thread
181  * \return nothing
182  */
183 void intf_StopThread( intf_thread_t *p_intf )
184 {
185     /* Tell the interface to die */
186     if( !p_intf->b_block )
187     {
188         p_intf->b_die = VLC_TRUE;
189     }
190
191     /* Wait for the thread to exit */
192     vlc_thread_join( p_intf );
193 }
194
195 /*****************************************************************************
196  * intf_Destroy: clean interface after main loop
197  *****************************************************************************
198  * This function destroys specific interfaces and close output devices.
199  *****************************************************************************/
200 /**
201  * \brief Destroy the interface after the main loop endeed.
202  * 
203  * Destroys interfaces and output devices
204  * \param p_intf the interface thread
205  * \return nothing
206  */
207 void intf_Destroy( intf_thread_t *p_intf )
208 {
209     /* Unlock module */
210     module_Unneed( p_intf, p_intf->p_module );
211
212     vlc_mutex_destroy( &p_intf->change_lock );
213
214     /* Free structure */
215     vlc_object_destroy( p_intf );
216 }
217
218
219 /* Following functions are local */
220
221 /*****************************************************************************
222  * Manager: helper thread for blocking interfaces
223  *****************************************************************************
224  * If the interface is launched in the main thread, it will not listen to
225  * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
226  * This thread takes care of the matter.
227  *****************************************************************************/
228 /**
229  * \brief Helper thread for blocking interfaces.
230  * \ingroup vlc_interface
231  *
232  * This is a local function
233  * If the interface is launched in the main thread, it will not listen to
234  * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
235  * This thread takes care of the matter.
236  * \see intf_RunThread
237  * \param p_intf an interface thread
238  * \return nothing
239  */
240 static void Manager( intf_thread_t *p_intf )
241 {
242     while( !p_intf->b_die )
243     {
244         msleep( INTF_IDLE_SLEEP );
245
246         if( p_intf->p_vlc->b_die )
247         {
248             p_intf->b_die = VLC_TRUE;
249             return;
250         }
251     }
252 }
253
254 /*****************************************************************************
255  * RunInterface: setups necessary data and give control to the interface
256  *****************************************************************************/
257 static void RunInterface( intf_thread_t *p_intf )
258 {
259     vlc_value_t val, text;
260
261     /* Variable used for interface switching */
262     p_intf->psz_switch_intf = NULL;
263     var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
264                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
265     text.psz_string = _("Switch interface");
266     var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
267
268     val.psz_string = "skins"; text.psz_string = "Skins";
269     var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE, &val, &text );
270     val.psz_string = "wxwin"; text.psz_string = "wxWindows";
271     var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE, &val, &text );
272
273     var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
274
275     /* Variable used for interface spawning */
276     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
277                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
278     text.psz_string = _("Add interface");
279     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
280
281     val.psz_string = "rc"; text.psz_string = "Console";
282     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
283     val.psz_string = "logger"; text.psz_string = "Debug logging";
284     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
285     val.psz_string = "http"; text.psz_string = "HTTP remote control";
286     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
287
288     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
289
290     /* Give control to the interface */
291     p_intf->pf_run( p_intf );
292
293     /* Provide ability to switch the main interface on the fly */
294     while( p_intf->psz_switch_intf )
295     {
296         char *psz_intf = p_intf->psz_switch_intf;
297         p_intf->psz_switch_intf = NULL;
298         p_intf->b_die = VLC_FALSE;
299
300         /* Make sure the old interface is completely uninitialised */
301         module_Unneed( p_intf, p_intf->p_module );
302
303         p_intf->p_module = module_Need( p_intf, "interface", psz_intf );
304         free( psz_intf );
305
306         if( p_intf->p_module )
307         {
308             p_intf->pf_run( p_intf );
309         }
310         else break;
311     }
312 }
313
314 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
315                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
316 {
317     intf_thread_t *p_intf = (intf_thread_t *)p_this;
318
319     p_intf->psz_switch_intf =
320         malloc( strlen(newval.psz_string) + sizeof(",none") );
321     sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
322     p_intf->b_die = VLC_TRUE;
323
324     return VLC_SUCCESS;
325 }
326
327 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
328                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
329 {
330     intf_thread_t *p_intf;
331     char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
332
333     /* Try to create the interface */
334     sprintf( psz_intf, "%s,none", newval.psz_string );
335     p_intf = intf_Create( p_this->p_vlc, psz_intf );
336     free( psz_intf );
337     if( p_intf == NULL )
338     {
339         msg_Err( p_this, "interface \"%s\" initialization failed",
340                  newval.psz_string );
341         return VLC_EGENERIC;
342     }
343
344     /* Try to run the interface */
345     p_intf->b_block = VLC_FALSE;
346     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
347     {
348         vlc_object_detach( p_intf );
349         intf_Destroy( p_intf );
350         return VLC_EGENERIC;
351     }
352
353     return VLC_SUCCESS;
354 }