]> git.sesse.net Git - vlc/blob - src/interface/interface.c
(new in MAIN)
[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.96 2002/06/04 00:11:12 sam 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  * Preamble
28  *****************************************************************************/
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdlib.h>                                      /* free(), strtol() */
31 #include <stdio.h>                                                   /* FILE */
32 #include <string.h>                                            /* strerror() */
33 #include <sys/types.h>                                              /* off_t */
34
35 #include <vlc/vlc.h>
36
37 #include "stream_control.h"
38 #include "input_ext-intf.h"
39
40 #include "audio_output.h"
41
42 #include "interface.h"
43
44 #include "video.h"
45 #include "video_output.h"
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static void Manager( intf_thread_t *p_intf );
51
52 /*****************************************************************************
53  * intf_Create: prepare interface before main loop
54  *****************************************************************************
55  * This function opens output devices and creates specific interfaces. It sends
56  * its own error messages.
57  *****************************************************************************/
58 intf_thread_t* __intf_Create( vlc_object_t *p_this )
59 {
60     intf_thread_t * p_intf;
61     char *psz_name;
62
63     /* Allocate structure */
64     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
65     if( !p_intf )
66     {
67         msg_Err( p_this, "out of memory" );
68         return NULL;
69     }
70
71     /* Choose the best module */
72     psz_name = config_GetPsz( p_intf, "intf" );
73     p_intf->p_module = module_Need( p_intf, MODULE_CAPABILITY_INTF,
74                                     psz_name, (void *)p_intf );
75
76     if( psz_name ) free( psz_name );
77     if( p_intf->p_module == NULL )
78     {
79         msg_Err( p_intf, "no suitable intf module" );
80         vlc_object_destroy( p_intf );
81         return NULL;
82     }
83
84 #define f p_intf->p_module->p_functions->intf.functions.intf
85     p_intf->pf_open       = f.pf_open;
86     p_intf->pf_close      = f.pf_close;
87     p_intf->pf_run        = f.pf_run;
88 #undef f
89
90     /* Initialize structure */
91     p_intf->b_menu        = 0;
92     p_intf->b_menu_change = 0;
93
94     /* Initialize mutexes */
95     vlc_mutex_init( p_intf, &p_intf->change_lock );
96
97     msg_Dbg( p_intf, "interface initialized" );
98
99     /* Attach interface to its parent object */
100     vlc_object_attach( p_intf, p_this );
101
102     return p_intf;
103 }
104
105 /*****************************************************************************
106  * intf_RunThread: launch the interface thread
107  *****************************************************************************
108  * This function either creates a new thread and runs the interface in it,
109  * or runs the interface in the current thread, depending on b_block.
110  *****************************************************************************/
111 vlc_error_t intf_RunThread( intf_thread_t *p_intf )
112 {
113     if( p_intf->b_block )
114     {
115         /* Run a manager thread, launch the interface, kill the manager */
116         if( vlc_thread_create( p_intf, "manager", Manager, 0 ) )
117         {
118             msg_Err( p_intf, "cannot spawn manager thread" );
119             return VLC_EGENERIC;
120         }
121
122         p_intf->pf_run( p_intf );
123
124         p_intf->b_die = 1;
125
126         /* Do not join the thread... intf_StopThread will do it for us */
127     }
128     else
129     {
130         /* Run the interface in a separate thread */
131         if( vlc_thread_create( p_intf, "interface", p_intf->pf_run, 0 ) )
132         {
133             msg_Err( p_intf, "cannot spawn interface thread" );
134             return VLC_EGENERIC;
135         }
136     }
137
138     return VLC_SUCCESS;
139 }
140
141 /*****************************************************************************
142  * intf_StopThread: end the interface thread
143  *****************************************************************************
144  * This function asks the interface thread to stop.
145  *****************************************************************************/
146 void intf_StopThread( intf_thread_t *p_intf )
147 {
148     /* Tell the interface to die */
149     if( !p_intf->b_block )
150     {
151         p_intf->b_die = 1;
152     }
153
154     /* Wait for the thread to exit */
155     vlc_thread_join( p_intf );
156 }
157
158 /*****************************************************************************
159  * intf_Destroy: clean interface after main loop
160  *****************************************************************************
161  * This function destroys specific interfaces and close output devices.
162  *****************************************************************************/
163 void intf_Destroy( intf_thread_t *p_intf )
164 {
165     /* Destroy interface */
166     p_intf->pf_close( p_intf );
167
168     /* Unlock module */
169     module_Unneed( p_intf->p_module );
170
171     vlc_mutex_destroy( &p_intf->change_lock );
172
173     /* Free structure */
174     vlc_object_destroy( p_intf );
175 }
176
177 /* Following functions are local */
178
179 /*****************************************************************************
180  * Manager: helper thread for blocking interfaces
181  *****************************************************************************
182  * If the interface is launched in the main thread, it will not listen to
183  * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
184  * This thread takes care of the matter.
185  *****************************************************************************/
186 static void Manager( intf_thread_t *p_intf )
187 {
188     while( !p_intf->b_die )
189     {
190         msleep( INTF_IDLE_SLEEP );
191
192         if( p_intf->p_vlc->b_die )
193         {
194             p_intf->b_die = 1;
195             return;
196         }
197     }
198 }
199