]> git.sesse.net Git - vlc/blob - src/interface/interface.c
* src/video_output/vout_intf.c: new vout_ControlWindow() function.
[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-2004 VideoLAN
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., 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
79     /* Allocate structure */
80     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
81     if( !p_intf )
82     {
83         msg_Err( p_this, "out of memory" );
84         return NULL;
85     }
86     p_intf->pf_request_window = NULL;
87     p_intf->pf_release_window = NULL;
88     p_intf->pf_control_window = NULL;
89
90     /* Choose the best module */
91     p_intf->p_module = module_Need( p_intf, "interface", psz_module, 0 );
92
93     if( p_intf->p_module == NULL )
94     {
95         msg_Err( p_intf, "no suitable intf module" );
96         vlc_object_destroy( p_intf );
97         return NULL;
98     }
99
100     /* Initialize structure */
101     p_intf->b_menu        = VLC_FALSE;
102     p_intf->b_menu_change = VLC_FALSE;
103
104     /* Initialize mutexes */
105     vlc_mutex_init( p_intf, &p_intf->change_lock );
106
107     msg_Dbg( p_intf, "interface initialized" );
108
109     /* Attach interface to its parent object */
110     vlc_object_attach( p_intf, p_this );
111
112     return p_intf;
113 }
114
115 /*****************************************************************************
116  * intf_RunThread: launch the interface thread
117  *****************************************************************************
118  * This function either creates a new thread and runs the interface in it,
119  * or runs the interface in the current thread, depending on b_block.
120  *****************************************************************************/
121 /**
122  * Run the interface thread.
123  *
124  * If b_block is not set, runs the interface in the thread, else,
125  * creates a new thread and runs the interface.
126  * \param p_intf the interface thread
127  * \return VLC_SUCCESS on success, an error number else
128  */
129 int intf_RunThread( intf_thread_t *p_intf )
130 {
131     if( p_intf->b_block )
132     {
133         /* Run a manager thread, launch the interface, kill the manager */
134         if( vlc_thread_create( p_intf, "manager", Manager,
135                                VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
136         {
137             msg_Err( p_intf, "cannot spawn manager thread" );
138             return VLC_EGENERIC;
139         }
140
141         RunInterface( p_intf );
142
143         p_intf->b_die = VLC_TRUE;
144
145         /* Do not join the thread... intf_StopThread will do it for us */
146     }
147     else
148     {
149         /* Run the interface in a separate thread */
150         if( vlc_thread_create( p_intf, "interface", RunInterface,
151                                VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
152         {
153             msg_Err( p_intf, "cannot spawn interface thread" );
154             return VLC_EGENERIC;
155         }
156     }
157
158     return VLC_SUCCESS;
159 }
160
161 /**
162  * Stops the interface thread
163  *
164  * This function asks the interface thread to stop
165  * \param p_intf the interface thread
166  * \return nothing
167  */
168 void intf_StopThread( intf_thread_t *p_intf )
169 {
170     /* Tell the interface to die */
171     if( !p_intf->b_block )
172     {
173         p_intf->b_die = VLC_TRUE;
174     }
175
176     /* Wait for the thread to exit */
177     vlc_thread_join( p_intf );
178 }
179
180 /**
181  * \brief Destroy the interface after the main loop endeed.
182  *
183  * Destroys interfaces and closes output devices
184  * \param p_intf the interface thread
185  * \return nothing
186  */
187 void intf_Destroy( intf_thread_t *p_intf )
188 {
189     /* Unlock module */
190     module_Unneed( p_intf, p_intf->p_module );
191
192     vlc_mutex_destroy( &p_intf->change_lock );
193
194     /* Free structure */
195     vlc_object_destroy( p_intf );
196 }
197
198
199 /* Following functions are local */
200
201 /*****************************************************************************
202  * Manager: helper thread for blocking interfaces
203  *****************************************************************************
204  * If the interface is launched in the main thread, it will not listen to
205  * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
206  * This thread takes care of the matter.
207  *****************************************************************************/
208 /**
209  * \brief Helper thread for blocking interfaces.
210  * \ingroup vlc_interface
211  *
212  * This is a local function
213  * If the interface is launched in the main thread, it will not listen to
214  * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
215  * This thread takes care of the matter.
216  * \see intf_RunThread
217  * \param p_intf an interface thread
218  * \return nothing
219  */
220 static void Manager( intf_thread_t *p_intf )
221 {
222     while( !p_intf->b_die )
223     {
224         msleep( INTF_IDLE_SLEEP );
225
226         if( p_intf->p_vlc->b_die )
227         {
228             p_intf->b_die = VLC_TRUE;
229             return;
230         }
231     }
232 }
233
234 /*****************************************************************************
235  * RunInterface: setups necessary data and give control to the interface
236  *****************************************************************************/
237 static void RunInterface( intf_thread_t *p_intf )
238 {
239     vlc_value_t val, text;
240
241     /* Variable used for interface switching */
242     p_intf->psz_switch_intf = NULL;
243     var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
244                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
245     text.psz_string = _("Switch interface");
246     var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
247
248     val.psz_string = "skins"; text.psz_string = "Skins";
249     var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE, &val, &text );
250     val.psz_string = "skins2"; text.psz_string = "Skins 2";
251     var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE, &val, &text );
252     val.psz_string = "wxwin"; text.psz_string = "wxWindows";
253     var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE, &val, &text );
254
255     var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
256
257     /* Variable used for interface spawning */
258     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
259                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
260     text.psz_string = _("Add Interface");
261     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
262
263     val.psz_string = "rc"; text.psz_string = "Console";
264     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
265     val.psz_string = "telnet"; text.psz_string = "Telnet Interface";
266     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
267     val.psz_string = "http"; text.psz_string = "Web Interface";
268     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
269     val.psz_string = "logger"; text.psz_string = "Debug logging";
270     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
271     val.psz_string = "sap"; text.psz_string = "SAP Playlist";
272     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
273     val.psz_string = "gestures"; text.psz_string = "Mouse Gestures";
274     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
275
276     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
277
278     /* Give control to the interface */
279     p_intf->pf_run( p_intf );
280
281     /* Provide ability to switch the main interface on the fly */
282     while( p_intf->psz_switch_intf )
283     {
284         char *psz_intf = p_intf->psz_switch_intf;
285         p_intf->psz_switch_intf = NULL;
286         p_intf->b_die = VLC_FALSE;
287
288         /* Make sure the old interface is completely uninitialised */
289         module_Unneed( p_intf, p_intf->p_module );
290
291         p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
292         free( psz_intf );
293
294         if( p_intf->p_module )
295         {
296             p_intf->pf_run( p_intf );
297         }
298         else break;
299     }
300 }
301
302 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
303                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
304 {
305     intf_thread_t *p_intf = (intf_thread_t *)p_this;
306
307     p_intf->psz_switch_intf =
308         malloc( strlen(newval.psz_string) + sizeof(",none") );
309     sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
310     p_intf->b_die = VLC_TRUE;
311
312     return VLC_SUCCESS;
313 }
314
315 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
316                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
317 {
318     intf_thread_t *p_intf;
319     char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
320
321     /* Try to create the interface */
322     sprintf( psz_intf, "%s,none", newval.psz_string );
323     p_intf = intf_Create( p_this->p_vlc, psz_intf );
324     free( psz_intf );
325     if( p_intf == NULL )
326     {
327         msg_Err( p_this, "interface \"%s\" initialization failed",
328                  newval.psz_string );
329         return VLC_EGENERIC;
330     }
331
332     /* Try to run the interface */
333     p_intf->b_block = VLC_FALSE;
334     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
335     {
336         vlc_object_detach( p_intf );
337         intf_Destroy( p_intf );
338         return VLC_EGENERIC;
339     }
340
341     return VLC_SUCCESS;
342 }