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