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