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