]> git.sesse.net Git - vlc/blob - src/interface/interface.c
Added some Doxygen doc
[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-2001 VideoLAN
7  * $Id: interface.c,v 1.106 2003/09/18 17:54:02 zorglub Exp $
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
41 #include "stream_control.h"
42 #include "input_ext-intf.h"
43
44 #include "audio_output.h"
45
46 #include "vlc_interface.h"
47
48 #include "vlc_video.h"
49 #include "video_output.h"
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static void Manager( intf_thread_t *p_intf );
55
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  *****************************************************************************/
62 /**
63  * Create the interface, and prepare it for main loop.
64  * 
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
68  */
69 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
70 {
71     intf_thread_t * p_intf;
72     char *psz_intf;
73
74     /* Allocate structure */
75     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
76     if( !p_intf )
77     {
78         msg_Err( p_this, "out of memory" );
79         return NULL;
80     }
81
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" );
85     if( psz_intf )
86     {
87         if( !strcasecmp( psz_intf, "dvdplay" ) )
88         {
89             config_PutPsz( p_intf, "intf", "" );
90         }
91         free( psz_intf );
92     }
93
94     /* Choose the best module */
95     p_intf->p_module = module_Need( p_intf, "interface", psz_module );
96
97     if( p_intf->p_module == NULL )
98     {
99         msg_Err( p_intf, "no suitable intf module" );
100         vlc_object_destroy( p_intf );
101         return NULL;
102     }
103
104     /* Initialize structure */
105     p_intf->b_menu        = VLC_FALSE;
106     p_intf->b_menu_change = VLC_FALSE;
107
108     /* Initialize mutexes */
109     vlc_mutex_init( p_intf, &p_intf->change_lock );
110
111     msg_Dbg( p_intf, "interface initialized" );
112
113     /* Attach interface to its parent object */
114     vlc_object_attach( p_intf, p_this );
115
116     return p_intf;
117 }
118
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  *****************************************************************************/
125 /**
126  * Run the interface thread.
127  *
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
132  */
133 int intf_RunThread( intf_thread_t *p_intf )
134 {
135     if( p_intf->b_block )
136     {
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 ) )
140         {
141             msg_Err( p_intf, "cannot spawn manager thread" );
142             return VLC_EGENERIC;
143         }
144
145         p_intf->pf_run( p_intf );
146
147         p_intf->b_die = VLC_TRUE;
148
149         /* Do not join the thread... intf_StopThread will do it for us */
150     }
151     else
152     {
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 ) )
156         {
157             msg_Err( p_intf, "cannot spawn interface thread" );
158             return VLC_EGENERIC;
159         }
160     }
161
162     return VLC_SUCCESS;
163 }
164
165 /*****************************************************************************
166  * intf_StopThread: end the interface thread
167  *****************************************************************************
168  * This function asks the interface thread to stop.
169  *****************************************************************************/
170 /**
171  * Stops the interface thread
172  *
173  * This function asks the interface thread to stop
174  * \param p_intf the interface thread
175  * \return nothing
176  */
177 void intf_StopThread( intf_thread_t *p_intf )
178 {
179     /* Tell the interface to die */
180     if( !p_intf->b_block )
181     {
182         p_intf->b_die = VLC_TRUE;
183     }
184
185     /* Wait for the thread to exit */
186     vlc_thread_join( p_intf );
187 }
188
189 /*****************************************************************************
190  * intf_Destroy: clean interface after main loop
191  *****************************************************************************
192  * This function destroys specific interfaces and close output devices.
193  *****************************************************************************/
194 /**
195  * \brief Destroy the interface after the main loop endeed.
196  * 
197  * Destroys interfaces and output devices
198  * \param p_intf the interface thread
199  * \return nothing
200  */
201 void intf_Destroy( intf_thread_t *p_intf )
202 {
203     /* Unlock module */
204     module_Unneed( p_intf, p_intf->p_module );
205
206     vlc_mutex_destroy( &p_intf->change_lock );
207
208     /* Free structure */
209     vlc_object_destroy( p_intf );
210 }
211
212 /* Following functions are local */
213
214
215
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  *****************************************************************************/
223 /**
224  * \brief Helper thread for blocking interfaces.
225  * \ingroup vlc_interface
226  *
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
233  * \return nothing
234  */
235 static void Manager( intf_thread_t *p_intf )
236 {
237     while( !p_intf->b_die )
238     {
239         msleep( INTF_IDLE_SLEEP );
240
241         if( p_intf->p_vlc->b_die )
242         {
243             p_intf->b_die = VLC_TRUE;
244             return;
245         }
246     }
247 }
248