]> git.sesse.net Git - vlc/blob - src/interface/interface.c
update module LIST file.
[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 #include "libvlc.h"
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static void RunInterface( intf_thread_t *p_intf );
53
54 static int AddIntfCallback( vlc_object_t *, char const *,
55                             vlc_value_t , vlc_value_t , void * );
56
57 /*****************************************************************************
58  * intf_Create: prepare interface before main loop
59  *****************************************************************************
60  * This function opens output devices and creates specific interfaces. It sends
61  * its own error messages.
62  *****************************************************************************/
63 /**
64  * Create the interface, and prepare it for main loop.
65  * You can give some additional options to be used for interface initialization
66  *
67  * \param p_this the calling vlc_object_t
68  * \param psz_module a preferred interface module
69  * \param i_options number additional options
70  * \param ppsz_options additional option strings
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                               int i_options, const char *const *ppsz_options  )
75 {
76     intf_thread_t * p_intf;
77     int i;
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     p_intf->b_interaction = VLC_FALSE;
91     p_intf->b_should_run_on_first_thread = VLC_FALSE;
92
93     for( i = 0 ; i< i_options; i++ )
94         var_OptionParse( p_this, ppsz_options[i], true );
95
96     /* Choose the best module */
97     p_intf->psz_intf = strdup( psz_module );
98     p_intf->p_module = module_Need( p_intf, "interface", psz_module, VLC_FALSE );
99
100     if( p_intf->p_module == NULL )
101     {
102         msg_Err( p_intf, "no suitable interface module" );
103         free( p_intf->psz_intf );
104         vlc_object_release( p_intf );
105         return NULL;
106     }
107
108     /* Initialize structure */
109     p_intf->b_menu        = VLC_FALSE;
110     p_intf->b_menu_change = VLC_FALSE;
111
112     /* Initialize mutexes */
113     vlc_mutex_init( p_intf, &p_intf->change_lock );
114
115     /* Attach interface to its parent object */
116     vlc_object_attach( p_intf, p_this );
117
118     return p_intf;
119 }
120
121 /*****************************************************************************
122  * intf_RunThread: launch the interface thread
123  *****************************************************************************
124  * This function either creates a new thread and runs the interface in it.
125  *****************************************************************************/
126 /**
127  * Starts and runs the interface thread.
128  *
129  * \param p_intf the interface thread
130  * \return VLC_SUCCESS on success, an error number else
131  */
132 int intf_RunThread( intf_thread_t *p_intf )
133 {
134     /* This interface doesn't need to be run */
135     if( p_intf->pf_run == NULL )
136         return VLC_SUCCESS;
137
138     /* Hack to get Mac OS X Cocoa runtime running
139      * (it needs access to the main thread) */
140     if( p_intf->b_should_run_on_first_thread )
141     {
142         RunInterface( p_intf );
143         return VLC_SUCCESS;
144     }
145     
146     /* Run the interface in a separate thread */
147     if( vlc_thread_create( p_intf, "interface", RunInterface,
148                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
149     {
150         msg_Err( p_intf, "cannot spawn interface thread" );
151         return VLC_EGENERIC;
152     }
153
154     return VLC_SUCCESS;
155 }
156
157 /**
158  * Stops the interface thread
159  *
160  * This function asks the interface thread to stop
161  * \param p_intf the interface thread
162  * \return nothing
163  */
164 void intf_StopThread( intf_thread_t *p_intf )
165 {
166     /* Tell the interface to die */
167     vlc_object_kill( p_intf );
168     if( p_intf->pf_run != NULL )
169     {
170         vlc_cond_signal( &p_intf->object_wait );
171         vlc_thread_join( p_intf );
172     }
173 }
174
175 /**
176  * \brief Destroy the interface after the main loop endeed.
177  *
178  * Destroys interfaces and closes output devices
179  * \param p_intf the interface thread
180  * \return nothing
181  */
182 void intf_Destroy( intf_thread_t *p_intf )
183 {
184     /* Unlock module if present (a switch may have failed) */
185     if( p_intf->p_module )
186     {
187         module_Unneed( p_intf, p_intf->p_module );
188     }
189     free( p_intf->psz_intf );
190
191     vlc_mutex_destroy( &p_intf->change_lock );
192
193     /* Free structure */
194     vlc_object_release( p_intf );
195 }
196
197
198 /* Following functions are local */
199
200 /*****************************************************************************
201  * RunInterface: setups necessary data and give control to the interface
202  *****************************************************************************/
203 static void RunInterface( intf_thread_t *p_intf )
204 {
205     vlc_value_t val, text;
206     char *psz_intf;
207
208     /* Variable used for interface spawning */
209     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
210                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
211     text.psz_string = _("Add Interface");
212     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
213
214     val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
215     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
216     val.psz_string = (char *)"telnet";
217     text.psz_string = (char *)_("Telnet Interface");
218     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
219     val.psz_string = (char *)"http";
220     text.psz_string = (char *)_("Web Interface");
221     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
222     val.psz_string = (char *)"logger";
223     text.psz_string = (char *)_("Debug logging");
224     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
225     val.psz_string = (char *)"gestures";
226     text.psz_string = (char *)_("Mouse Gestures");
227     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
228
229     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
230
231     do
232     {
233         /* Give control to the interface */
234         p_intf->pf_run( p_intf );
235
236         /* Reset play on start status */
237         p_intf->b_play = VLC_FALSE;
238
239         if( !p_intf->psz_switch_intf )
240         {
241             break;
242         }
243
244         /* Make sure the old interface is completely uninitialized */
245         module_Unneed( p_intf, p_intf->p_module );
246
247         /* Provide ability to switch the main interface on the fly */
248         psz_intf = p_intf->psz_switch_intf;
249         p_intf->psz_switch_intf = NULL;
250
251         vlc_mutex_lock( &p_intf->object_lock );
252         p_intf->b_die = VLC_FALSE; /* FIXME */
253         p_intf->b_dead = VLC_FALSE;
254
255         vlc_mutex_unlock( &p_intf->object_lock );
256
257         p_intf->psz_intf = psz_intf;
258         p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
259     }
260     while( p_intf->p_module );
261 }
262
263 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
264                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
265 {
266     intf_thread_t *p_intf;
267     char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
268
269     (void)psz_cmd; (void)oldval; (void)p_data;
270
271     /* Try to create the interface */
272     sprintf( psz_intf, "%s,none", newval.psz_string );
273     p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
274     free( psz_intf );
275     if( p_intf == NULL )
276     {
277         msg_Err( p_this, "interface \"%s\" initialization failed",
278                  newval.psz_string );
279         return VLC_EGENERIC;
280     }
281
282     /* Try to run the interface */
283     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
284     {
285         vlc_object_detach( p_intf );
286         intf_Destroy( p_intf );
287         return VLC_EGENERIC;
288     }
289
290     return VLC_SUCCESS;
291 }
292