]> git.sesse.net Git - vlc/blob - src/interface/interface.c
* ./include/modules_inner.h: replaced _X with __VLC_SYMBOL because _X was
[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.87 2002/01/09 02:01:14 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 <videolan/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 #include "intf_playlist.h"
44
45 #include "video.h"
46 #include "video_output.h"
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static void intf_Manage( intf_thread_t *p_intf );
52
53 /*****************************************************************************
54  * intf_Create: prepare interface before main loop
55  *****************************************************************************
56  * This function opens output devices and creates specific interfaces. It sends
57  * its own error messages.
58  *****************************************************************************/
59 intf_thread_t* intf_Create( void )
60 {
61     intf_thread_t * p_intf;
62
63     /* Allocate structure */
64     p_intf = malloc( sizeof( intf_thread_t ) );
65     if( !p_intf )
66     {
67         intf_ErrMsg( "intf error: cannot create interface thread (%s)",
68                      strerror( ENOMEM ) );
69         return( NULL );
70     }
71
72     /* Choose the best module */
73     p_intf->p_module = module_Need( MODULE_CAPABILITY_INTF,
74                            main_GetPszVariable( INTF_METHOD_VAR, NULL ),
75                            NULL );
76
77     if( p_intf->p_module == NULL )
78     {
79         intf_ErrMsg( "intf error: no suitable intf module" );
80         free( 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 callbacks */
91     p_intf->pf_manage     = intf_Manage;
92
93     /* Initialize structure */
94     p_intf->b_die         = 0;
95
96     p_intf->b_menu        = 0;
97     p_intf->b_menu_change = 0;
98
99     if( p_intf->pf_open( p_intf ) )
100     {
101         intf_ErrMsg("intf error: cannot create interface");
102         module_Unneed( p_intf->p_module );
103         free( p_intf );
104         return( NULL );
105     }
106
107     /* Initialize mutexes */
108     vlc_mutex_init( &p_intf->change_lock );
109
110     intf_WarnMsg( 1, "intf: interface initialized");
111     return( p_intf );
112 }
113
114 /*****************************************************************************
115  * intf_Manage: manage interface
116  *****************************************************************************
117  * This function has to be called regularly by the interface plugin. It
118  * checks for playlist end, module expiration, message flushing, and a few
119  * other useful things.
120  *****************************************************************************/
121 static void intf_Manage( intf_thread_t *p_intf )
122 {
123     /* Flush waiting messages */
124     intf_FlushMsg();
125
126     /* Manage module bank */
127     module_ManageBank( );
128
129     vlc_mutex_lock( &p_input_bank->lock );
130
131     if( p_input_bank->i_count )
132     {
133         int i_input;
134         input_thread_t *p_input;
135
136         for( i_input = 0; i_input < p_input_bank->i_count; i_input++ )
137         {
138             p_input = p_input_bank->pp_input[i_input];
139             
140             if( p_input->i_status == THREAD_OVER )
141             {
142                 /* XXX: completely stupid ! */
143                 input_DestroyThread( p_input );
144                 p_input_bank->pp_input[i_input] = NULL;
145                 p_input_bank->i_count--;
146             }
147             else if( ( p_input->i_status == THREAD_READY
148                         || p_input->i_status == THREAD_ERROR )
149                      && ( p_input->b_error || p_input->b_eof ) )
150             {
151                 input_StopThread( p_input, NULL );
152             }
153
154         }
155     }
156     /* If no stream is being played, try to find one */
157     else
158     {
159 //        vlc_mutex_lock( &p_main->p_playlist->change_lock );
160
161         if( !p_main->p_playlist->b_stopped )
162         {
163             /* Select the next playlist item */
164             intf_PlaylistNext( p_main->p_playlist );
165
166             /* don't loop by default: stop at playlist end */
167             if( p_main->p_playlist->i_index == -1 )
168             {
169                 p_main->p_playlist->b_stopped = 1;
170             }
171             else
172             {
173                 input_thread_t *p_input;
174
175                 p_main->p_playlist->b_stopped = 0;
176                 p_main->p_playlist->i_mode = PLAYLIST_FORWARD + 
177                     main_GetIntVariable( PLAYLIST_LOOP_VAR,
178                                          PLAYLIST_LOOP_DEFAULT );
179                 intf_WarnMsg( 3, "intf: creating new input thread" );
180                 p_input = input_CreateThread( &p_main->p_playlist->current,
181                                               NULL );
182                 if( p_input != NULL )
183                 {
184                     p_input_bank->pp_input[ p_input_bank->i_count ] = p_input;
185                     p_input_bank->i_count++;
186                 }
187             }
188         }
189         else
190         {
191             /* playing has been stopped: we no longer need outputs */
192             if( p_aout_bank->i_count )
193             {
194                 /* FIXME kludge that does not work with several outputs */
195                 aout_DestroyThread( p_aout_bank->pp_aout[0], NULL );
196                 p_aout_bank->i_count--;
197             }
198             if( p_vout_bank->i_count )
199             {
200                 vout_DestroyThread( p_vout_bank->pp_vout[0], NULL );
201                 p_vout_bank->i_count--;
202             }
203         }
204
205 //        vlc_mutex_unlock( &p_main->p_playlist->change_lock );
206     }
207
208     vlc_mutex_unlock( &p_input_bank->lock );
209 }
210
211 /*****************************************************************************
212  * intf_Destroy: clean interface after main loop
213  *****************************************************************************
214  * This function destroys specific interfaces and close output devices.
215  *****************************************************************************/
216 void intf_Destroy( intf_thread_t *p_intf )
217 {
218     /* Destroy interfaces */
219     p_intf->pf_close( p_intf );
220
221 #if 0
222     /* Close input thread, if any (blocking) */
223     if( p_intf->p_input )
224     {   
225         input_DestroyThread( p_intf->p_input, NULL );
226     }
227 #endif
228
229     /* Unlock module */
230     module_Unneed( p_intf->p_module );
231
232     vlc_mutex_destroy( &p_intf->change_lock );
233
234     /* Free structure */
235     free( p_intf );
236 }
237