]> git.sesse.net Git - vlc/blob - src/interface/interface.c
* COMPLETE CVS BREAKAGE !! The MAIN branch is going to be a playground
[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.83 2001/12/09 17:01:37 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 "defs.h"
30
31 #include <errno.h>                                                 /* ENOMEM */
32 #include <stdlib.h>                                      /* free(), strtol() */
33 #include <stdio.h>                                                   /* FILE */
34 #include <string.h>                                            /* strerror() */
35 #include <sys/types.h>                                              /* off_t */
36
37 #include "common.h"
38 #include "intf_msg.h"
39 #include "threads.h"
40 #include "mtime.h"
41 #include "modules.h"
42
43 #include "stream_control.h"
44 #include "input_ext-intf.h"
45
46 #include "audio_output.h"
47
48 #include "interface.h"
49 #include "intf_playlist.h"
50 #include "intf_channels.h"
51 #include "keystrokes.h"
52
53 #include "video.h"
54 #include "video_output.h"
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static void intf_Manage( intf_thread_t *p_intf );
60
61 /*****************************************************************************
62  * intf_Create: prepare interface before main loop
63  *****************************************************************************
64  * This function opens output devices and creates specific interfaces. It sends
65  * its own error messages.
66  *****************************************************************************/
67 intf_thread_t* intf_Create( void )
68 {
69     intf_thread_t * p_intf;
70
71     /* Allocate structure */
72     p_intf = malloc( sizeof( intf_thread_t ) );
73     if( !p_intf )
74     {
75         intf_ErrMsg( "intf error: cannot create interface thread (%s)",
76                      strerror( ENOMEM ) );
77         return( NULL );
78     }
79
80     /* Choose the best module */
81     p_intf->p_module = module_Need( MODULE_CAPABILITY_INTF, NULL );
82
83     if( p_intf->p_module == NULL )
84     {
85         intf_ErrMsg( "intf error: no suitable intf module" );
86         free( p_intf );
87         return( NULL );
88     }
89
90 #define f p_intf->p_module->p_functions->intf.functions.intf
91     p_intf->pf_open       = f.pf_open;
92     p_intf->pf_close      = f.pf_close;
93     p_intf->pf_run        = f.pf_run;
94 #undef f
95
96     /* Initialize callbacks */
97     p_intf->pf_manage     = intf_Manage;
98
99     /* Initialize structure */
100     p_intf->b_die         = 0;
101
102     p_intf->p_input       = NULL;
103     p_intf->b_menu        = 0;
104     p_intf->b_menu_change = 0;
105
106     if( p_intf->pf_open( p_intf ) )
107     {
108         intf_ErrMsg("intf error: cannot create interface");
109         module_Unneed( p_intf->p_module );
110         free( p_intf );
111         return( NULL );
112     }
113
114     /* Initialize mutexes */
115     vlc_mutex_init( &p_intf->change_lock );
116
117     /* Load channels - the pointer will be set to NULL on failure. The
118      * return value is ignored since the program can work without
119      * channels */
120     intf_LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR,
121                                                     INTF_CHANNELS_DEFAULT ));
122
123     intf_WarnMsg( 1, "intf: interface initialized");
124     return( p_intf );
125 }
126
127 /*****************************************************************************
128  * intf_Manage: manage interface
129  *****************************************************************************
130  * This function has to be called regularly by the interface plugin. It
131  * checks for playlist end, module expiration, message flushing, and a few
132  * other useful things.
133  *****************************************************************************/
134 static void intf_Manage( intf_thread_t *p_intf )
135 {
136     /* Flush waiting messages */
137     intf_FlushMsg();
138
139     /* Manage module bank */
140     module_ManageBank( );
141
142     if( ( p_intf->p_input != NULL ) &&
143             ( p_intf->p_input->b_error || p_intf->p_input->b_eof ) )
144     {
145         input_DestroyThread( p_intf->p_input, NULL );
146         p_intf->p_input = NULL;
147         intf_DbgMsg("Input thread destroyed");
148     }
149
150     /* If no stream is being played, try to find one */
151     if( p_intf->p_input == NULL && !p_intf->b_die )
152     {
153 //        vlc_mutex_lock( &p_main->p_playlist->change_lock );
154
155         if( !p_main->p_playlist->b_stopped )
156         {
157             /* Select the next playlist item */
158             intf_PlaylistNext( p_main->p_playlist );
159
160             /* don't loop by default: stop at playlist end */
161             if( p_main->p_playlist->i_index == -1 )
162             {
163                 p_main->p_playlist->b_stopped = 1;
164             }
165             else
166             {
167                 p_main->p_playlist->b_stopped = 0;
168                 p_main->p_playlist->i_mode = PLAYLIST_FORWARD + 
169                     main_GetIntVariable( PLAYLIST_LOOP_VAR,
170                                          PLAYLIST_LOOP_DEFAULT );
171                 p_intf->p_input =
172                     input_CreateThread( &p_main->p_playlist->current, NULL );
173             }
174         }
175         else
176         {
177             /* playing has been stopped: we no longer need outputs */
178             if( p_aout_bank->i_count )
179             {
180                 /* FIXME kludge that does not work with several outputs */
181                 aout_DestroyThread( p_aout_bank->pp_aout[0], NULL );
182                 p_aout_bank->i_count--;
183             }
184             if( p_vout_bank->i_count )
185             {
186                 vout_DestroyThread( p_vout_bank->pp_vout[0], NULL );
187                 p_vout_bank->i_count--;
188             }
189         }
190
191 //        vlc_mutex_unlock( &p_main->p_playlist->change_lock );
192     }
193 }
194
195 /*****************************************************************************
196  * intf_Destroy: clean interface after main loop
197  *****************************************************************************
198  * This function destroys specific interfaces and close output devices.
199  *****************************************************************************/
200 void intf_Destroy( intf_thread_t *p_intf )
201 {
202     /* Unload channels */
203     intf_UnloadChannels( p_intf );
204
205     /* Destroy interfaces */
206     p_intf->pf_close( p_intf );
207
208     /* Close input thread, if any (blocking) */
209     if( p_intf->p_input )
210     {   
211         input_DestroyThread( p_intf->p_input, NULL );
212     }
213
214     /* Unlock module */
215     module_Unneed( p_intf->p_module );
216
217     vlc_mutex_destroy( &p_intf->change_lock );
218
219     /* Free structure */
220     free( p_intf );
221 }
222