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