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