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