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