]> git.sesse.net Git - vlc/blob - src/interface/interface.c
Don't include config.h from the headers - refs #297.
[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/vlc.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
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static void RunInterface( intf_thread_t *p_intf );
52
53 static int SwitchIntfCallback( vlc_object_t *, char const *,
54                                vlc_value_t , vlc_value_t , void * );
55 static int AddIntfCallback( vlc_object_t *, char const *,
56                             vlc_value_t , vlc_value_t , void * );
57
58 /*****************************************************************************
59  * intf_Create: prepare interface before main loop
60  *****************************************************************************
61  * This function opens output devices and creates specific interfaces. It sends
62  * its own error messages.
63  *****************************************************************************/
64 /**
65  * Create the interface, and prepare it for main loop.
66  * You can give some additional options to be used for interface initialization
67  *
68  * \param p_this the calling vlc_object_t
69  * \param psz_module a preferred interface module
70  * \param i_options number additional options
71  * \param ppsz_options additional option strings
72  * \return a pointer to the created interface thread, NULL on error
73  */
74 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
75                               int i_options, const char *const *ppsz_options  )
76 {
77     intf_thread_t * p_intf;
78     int i;
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     p_intf->pf_request_window = NULL;
88     p_intf->pf_release_window = NULL;
89     p_intf->pf_control_window = NULL;
90     p_intf->b_play = VLC_FALSE;
91     p_intf->b_interaction = VLC_FALSE;
92     p_intf->b_should_run_on_first_thread = VLC_FALSE;
93
94     for( i = 0 ; i< i_options; i++ )
95     {
96         var_OptionParse( p_intf, ppsz_options[i] );
97     }
98
99     /* Choose the best module */
100     p_intf->psz_intf = strdup( psz_module );
101     p_intf->p_module = module_Need( p_intf, "interface", psz_module, VLC_FALSE );
102
103     if( p_intf->p_module == NULL )
104     {
105         msg_Err( p_intf, "no suitable interface module" );
106         free( p_intf->psz_intf );
107         vlc_object_destroy( p_intf );
108         return NULL;
109     }
110
111     /* Initialize structure */
112     p_intf->b_menu        = VLC_FALSE;
113     p_intf->b_menu_change = VLC_FALSE;
114
115     /* Initialize mutexes */
116     vlc_mutex_init( p_intf, &p_intf->change_lock );
117
118     /* Attach interface to its parent object */
119     vlc_object_attach( p_intf, p_this );
120
121     return p_intf;
122 }
123
124 /*****************************************************************************
125  * intf_RunThread: launch the interface thread
126  *****************************************************************************
127  * This function either creates a new thread and runs the interface in it.
128  *****************************************************************************/
129 /**
130  * Starts and runs the interface thread.
131  *
132  * \param p_intf the interface thread
133  * \return VLC_SUCCESS on success, an error number else
134  */
135 int intf_RunThread( intf_thread_t *p_intf )
136 {
137     /* This interface doesn't need to be run */
138     if( p_intf->pf_run == NULL )
139         return VLC_SUCCESS;
140
141     /* Hack to get Mac OS X Cocoa runtime running
142      * (it needs access to the main thread) */
143     if( p_intf->b_should_run_on_first_thread )
144     {
145         RunInterface( p_intf );
146         return VLC_SUCCESS;
147     }
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     return VLC_SUCCESS;
158 }
159
160 /**
161  * Stops the interface thread
162  *
163  * This function asks the interface thread to stop
164  * \param p_intf the interface thread
165  * \return nothing
166  */
167 void intf_StopThread( intf_thread_t *p_intf )
168 {
169     /* Tell the interface to die */
170     vlc_object_kill( p_intf );
171     if( p_intf->pf_run != NULL )
172     {
173         vlc_cond_signal( &p_intf->object_wait );
174         vlc_thread_join( p_intf );
175     }
176 }
177
178 /**
179  * \brief Destroy the interface after the main loop endeed.
180  *
181  * Destroys interfaces and closes output devices
182  * \param p_intf the interface thread
183  * \return nothing
184  */
185 void intf_Destroy( intf_thread_t *p_intf )
186 {
187     /* Unlock module if present (a switch may have failed) */
188     if( p_intf->p_module )
189     {
190         module_Unneed( p_intf, p_intf->p_module );
191     }
192     free( p_intf->psz_intf );
193
194     vlc_mutex_destroy( &p_intf->change_lock );
195
196     /* Free structure */
197     vlc_object_destroy( p_intf );
198 }
199
200
201 /* Following functions are local */
202
203 /*****************************************************************************
204  * RunInterface: setups necessary data and give control to the interface
205  *****************************************************************************/
206 static void RunInterface( intf_thread_t *p_intf )
207 {
208     static const char *ppsz_interfaces[] =
209     {
210         "skins2", "Skins 2",
211 #ifndef WIN32
212         "wxwidgets", "wxWidgets",
213 #endif
214         NULL, NULL
215     };
216     const char **ppsz_parser;
217
218     vlc_list_t *p_list;
219     int i;
220     vlc_value_t val, text;
221     char *psz_intf;
222
223     /* Variable used for interface switching */
224     p_intf->psz_switch_intf = NULL;
225     var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
226                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
227     text.psz_string = _("Switch interface");
228     var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
229
230     /* Only fill the list with available modules */
231     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
232     for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
233     {
234         for( i = 0; i < p_list->i_count; i++ )
235         {
236             module_t *p_module = (module_t *)p_list->p_values[i].p_object;
237             if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
238             {
239                 val.psz_string = (char *)ppsz_parser[0];
240                 text.psz_string = (char *)_(ppsz_parser[1]);
241                 var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
242                             &val, &text );
243                 break;
244             }
245         }
246     }
247     vlc_list_release( p_list );
248
249     var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
250
251     /* Variable used for interface spawning */
252     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
253                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
254     text.psz_string = _("Add Interface");
255     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
256
257     val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
258     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
259     val.psz_string = (char *)"telnet";
260     text.psz_string = (char *)_("Telnet Interface");
261     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
262     val.psz_string = (char *)"http";
263     text.psz_string = (char *)_("Web Interface");
264     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
265     val.psz_string = (char *)"logger";
266     text.psz_string = (char *)_("Debug logging");
267     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
268     val.psz_string = (char *)"gestures";
269     text.psz_string = (char *)_("Mouse Gestures");
270     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
271
272     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
273
274     do
275     {
276         /* Give control to the interface */
277         p_intf->pf_run( p_intf );
278
279         /* Reset play on start status */
280         p_intf->b_play = VLC_FALSE;
281
282         if( !p_intf->psz_switch_intf )
283         {
284             break;
285         }
286
287         /* Make sure the old interface is completely uninitialized */
288         module_Unneed( p_intf, p_intf->p_module );
289
290         /* Provide ability to switch the main interface on the fly */
291         psz_intf = p_intf->psz_switch_intf;
292         p_intf->psz_switch_intf = NULL;
293
294         vlc_mutex_lock( &p_intf->object_lock );
295         p_intf->b_die = VLC_FALSE; /* FIXME */
296         p_intf->b_dead = VLC_FALSE;
297
298         vlc_mutex_unlock( &p_intf->object_lock );
299
300         p_intf->psz_intf = psz_intf;
301         p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
302     }
303     while( p_intf->p_module );
304 }
305
306 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
307                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
308 {
309     intf_thread_t *p_intf = (intf_thread_t *)p_this;
310     (void)psz_cmd; (void)oldval; (void)p_data;
311
312     p_intf->psz_switch_intf =
313         malloc( strlen(newval.psz_string) + sizeof(",none") );
314     sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
315     vlc_object_kill( p_intf );
316
317     return VLC_SUCCESS;
318 }
319
320 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
321                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
322 {
323     intf_thread_t *p_intf;
324     char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
325
326     (void)psz_cmd; (void)oldval; (void)p_data;
327
328     /* Try to create the interface */
329     sprintf( psz_intf, "%s,none", newval.psz_string );
330     p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
331     free( psz_intf );
332     if( p_intf == NULL )
333     {
334         msg_Err( p_this, "interface \"%s\" initialization failed",
335                  newval.psz_string );
336         return VLC_EGENERIC;
337     }
338
339     /* Try to run the interface */
340     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
341     {
342         vlc_object_detach( p_intf );
343         intf_Destroy( p_intf );
344         return VLC_EGENERIC;
345     }
346
347     return VLC_SUCCESS;
348 }
349