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-2001 VideoLAN
7 * $Id: interface.c,v 1.106 2003/09/18 17:54:02 zorglub Exp $
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
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.
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.
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 *****************************************************************************/
28 * This file contains functions related to interface management
32 /*****************************************************************************
34 *****************************************************************************/
35 #include <stdlib.h> /* free(), strtol() */
36 #include <stdio.h> /* FILE */
37 #include <string.h> /* strerror() */
41 #include "stream_control.h"
42 #include "input_ext-intf.h"
44 #include "audio_output.h"
46 #include "vlc_interface.h"
48 #include "vlc_video.h"
49 #include "video_output.h"
51 /*****************************************************************************
53 *****************************************************************************/
54 static void Manager( intf_thread_t *p_intf );
56 /*****************************************************************************
57 * intf_Create: prepare interface before main loop
58 *****************************************************************************
59 * This function opens output devices and creates specific interfaces. It sends
60 * its own error messages.
61 *****************************************************************************/
63 * Create the interface, and prepare it for main loop.
65 * \param p_this the calling vlc_object_t
66 * \param psz_module a prefered interface module
67 * \return a pointer to the created interface thread, NULL on error
69 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
71 intf_thread_t * p_intf;
74 /* Allocate structure */
75 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
78 msg_Err( p_this, "out of memory" );
82 /* XXX: workaround for a bug in VLC 0.5.0 where the dvdplay plugin was
83 * registering itself in $interface, which we do not want to happen. */
84 psz_intf = config_GetPsz( p_intf, "intf" );
87 if( !strcasecmp( psz_intf, "dvdplay" ) )
89 config_PutPsz( p_intf, "intf", "" );
94 /* Choose the best module */
95 p_intf->p_module = module_Need( p_intf, "interface", psz_module );
97 if( p_intf->p_module == NULL )
99 msg_Err( p_intf, "no suitable intf module" );
100 vlc_object_destroy( p_intf );
104 /* Initialize structure */
105 p_intf->b_menu = VLC_FALSE;
106 p_intf->b_menu_change = VLC_FALSE;
108 /* Initialize mutexes */
109 vlc_mutex_init( p_intf, &p_intf->change_lock );
111 msg_Dbg( p_intf, "interface initialized" );
113 /* Attach interface to its parent object */
114 vlc_object_attach( p_intf, p_this );
119 /*****************************************************************************
120 * intf_RunThread: launch the interface thread
121 *****************************************************************************
122 * This function either creates a new thread and runs the interface in it,
123 * or runs the interface in the current thread, depending on b_block.
124 *****************************************************************************/
126 * Run the interface thread.
128 * If b_block is not set, runs the interface in the thread, else,
129 * creates a new thread and runs the interface.
130 * \param p_intf the interface thread
131 * \return VLC_SUCCESS on success, an error number else
133 int intf_RunThread( intf_thread_t *p_intf )
135 if( p_intf->b_block )
137 /* Run a manager thread, launch the interface, kill the manager */
138 if( vlc_thread_create( p_intf, "manager", Manager,
139 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
141 msg_Err( p_intf, "cannot spawn manager thread" );
145 p_intf->pf_run( p_intf );
147 p_intf->b_die = VLC_TRUE;
149 /* Do not join the thread... intf_StopThread will do it for us */
153 /* Run the interface in a separate thread */
154 if( vlc_thread_create( p_intf, "interface", p_intf->pf_run,
155 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
157 msg_Err( p_intf, "cannot spawn interface thread" );
165 /*****************************************************************************
166 * intf_StopThread: end the interface thread
167 *****************************************************************************
168 * This function asks the interface thread to stop.
169 *****************************************************************************/
171 * Stops the interface thread
173 * This function asks the interface thread to stop
174 * \param p_intf the interface thread
177 void intf_StopThread( intf_thread_t *p_intf )
179 /* Tell the interface to die */
180 if( !p_intf->b_block )
182 p_intf->b_die = VLC_TRUE;
185 /* Wait for the thread to exit */
186 vlc_thread_join( p_intf );
189 /*****************************************************************************
190 * intf_Destroy: clean interface after main loop
191 *****************************************************************************
192 * This function destroys specific interfaces and close output devices.
193 *****************************************************************************/
195 * \brief Destroy the interface after the main loop endeed.
197 * Destroys interfaces and output devices
198 * \param p_intf the interface thread
201 void intf_Destroy( intf_thread_t *p_intf )
204 module_Unneed( p_intf, p_intf->p_module );
206 vlc_mutex_destroy( &p_intf->change_lock );
209 vlc_object_destroy( p_intf );
212 /* Following functions are local */
216 /*****************************************************************************
217 * Manager: helper thread for blocking interfaces
218 *****************************************************************************
219 * If the interface is launched in the main thread, it will not listen to
220 * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
221 * This thread takes care of the matter.
222 *****************************************************************************/
224 * \brief Helper thread for blocking interfaces.
225 * \ingroup vlc_interface
227 * This is a local function
228 * If the interface is launched in the main thread, it will not listen to
229 * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
230 * This thread takes care of the matter.
231 * \see intf_RunThread
232 * \param p_intf an interface thread
235 static void Manager( intf_thread_t *p_intf )
237 while( !p_intf->b_die )
239 msleep( INTF_IDLE_SLEEP );
241 if( p_intf->p_vlc->b_die )
243 p_intf->b_die = VLC_TRUE;