]> git.sesse.net Git - vlc/blob - src/interface/interface.c
* La DCT et la PCM sont dans audio_math.c ;
[vlc] / src / interface / interface.c
1 /*******************************************************************************
2  * interface.c: interface access for other threads
3  * (c)1998 VideoLAN
4  *******************************************************************************
5  * This library provides basic functions for threads to interact with user
6  * interface, such as command line.
7  *******************************************************************************/
8
9 /*******************************************************************************
10  * Preamble
11  *******************************************************************************/
12 #include <pthread.h>
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <netinet/in.h>
16 #include <sys/soundcard.h>
17 #include <sys/uio.h>
18 #include <X11/Xlib.h>
19 #include <X11/extensions/XShm.h>
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24
25 #include "config.h"
26 #include "common.h"
27 #include "mtime.h"
28 #include "thread.h"
29
30 #include "input.h"
31 #include "input_vlan.h"
32 #include "decoder_fifo.h"
33
34 #include "audio_output.h"
35 #include "audio_decoder.h"
36
37 #include "video.h"
38 #include "video_output.h"
39 #include "video_decoder.h"
40
41 #include "xconsole.h"
42 #include "interface.h"
43 #include "intf_msg.h"
44 #include "intf_cmd.h"
45
46 #include "pgm_data.h"
47 /* ?? remove useless headers */
48
49 /*
50  * Local prototypes
51  */
52 static int  StartInterface  ( intf_thread_t *p_intf );
53 static void EndInterface    ( intf_thread_t *p_intf );
54
55 /*******************************************************************************
56  * intf_Run
57  *******************************************************************************
58  * what it does:
59  *     - Create an X11 console
60  *     - wait for a command and try to execute it
61  *     - interpret the order returned after the command execution
62  *     - print the messages of the message queue (intf_FlushMsg)
63  * return value: 0 if successful, < 0 otherwise
64  *******************************************************************************/
65 int intf_Run( intf_thread_t *p_intf )
66 {
67     /* When it is started, interface won't die immediatly */
68     p_intf->b_die = 0;
69     if( StartInterface( p_intf ) )                                    /* error */
70     {
71         return( 1 );
72     }
73     
74     /* Main loop */
75     while(!p_intf->b_die)
76     {
77         /* Flush waiting messages */
78         intf_FlushMsg();
79
80         /* Manage specific interfaces */
81         intf_ManageXConsole( &p_intf->xconsole );               /* X11 console */
82
83         /* Sleep to avoid using all CPU - since some interfaces needs to access 
84          * keyboard events, a 100ms delay is a good compromise */
85         msleep( INTF_IDLE_SLEEP );
86     }
87
88     /* End of interface thread - the main() function will close all remaining
89      * output threads */
90     EndInterface( p_intf );
91     return ( 0 );
92 }
93
94 /* following functions are local */
95
96 /*******************************************************************************
97  * StartInterface: prepare interface before main loop
98  *******************************************************************************
99  * This function opens output devices and create specific interfaces. It send
100  * it's own error messages.
101  *******************************************************************************/
102 static int StartInterface( intf_thread_t *p_intf )
103 {
104     int i_thread;                                              /* thread index */
105 #ifdef AUTO_SPAWN
106     int fd;
107 #endif
108
109     /* Empty all threads array */
110     for( i_thread = 0; i_thread < VOUT_MAX_THREADS; i_thread++ )
111     {
112         p_intf->pp_vout[i_thread] = NULL;        
113     }
114     for( i_thread = 0; i_thread < INPUT_MAX_THREADS; i_thread++ )
115     {
116         p_intf->pp_input[i_thread] = NULL;        
117     }    
118
119     /* Start X11 Console*/
120     if( intf_OpenXConsole( &p_intf->xconsole ) )
121     {
122         intf_ErrMsg("intf error: can't open X11 console\n");
123         return( 1 );
124     }
125
126 #ifdef AUTO_SPAWN
127     /* Execute the initialization script (typically spawn an input thread) */
128     if ( (fd = open( INIT_SCRIPT, O_RDONLY )) != -1 )
129     {
130         /* Startup script does exist */
131         close( fd );
132         intf_ExecScript( "vlc.init" );
133     }
134 #endif
135
136     return( 0 );
137 }
138
139 /*******************************************************************************
140  * EndInterface: clean interface after main loop
141  *******************************************************************************
142  * This function destroys specific interfaces and close output devices.
143  *******************************************************************************/
144 static void EndInterface( intf_thread_t *p_intf )
145 {
146     int         i_thread;                                      /* thread index */
147     boolean_t   b_thread;                          /* flag for remaing threads */
148     int         pi_vout_status[VOUT_MAX_THREADS];       /* vout threads status */
149     
150     
151     
152     /* Close X11 console */
153     intf_CloseXConsole( &p_intf->xconsole );        
154
155     /* Destroy all remaining input threads */
156     for( i_thread = 0; i_thread < INPUT_MAX_THREADS; i_thread++ )
157     {
158         if( p_intf->pp_input[i_thread] != NULL )
159         {
160             input_DestroyThread( p_intf->pp_input[i_thread] );
161         }        
162     }
163
164     /* Destroy all remaining video output threads - all destruction orders are send,
165      * then all THREAD_OVER status are received */
166     for( i_thread = 0, b_thread = 0; i_thread < VOUT_MAX_THREADS; i_thread++ )
167     {
168         if( p_intf->pp_vout[i_thread] != NULL )
169         {
170             vout_DestroyThread( p_intf->pp_vout[i_thread], &pi_vout_status[i_thread] );
171             b_thread = 1;            
172         }
173     }
174     while( b_thread )
175     {
176         msleep( INTF_IDLE_SLEEP );        
177         b_thread = 0;        
178         for( i_thread = 0; i_thread < VOUT_MAX_THREADS; i_thread++ )
179         {
180             if( (p_intf->pp_vout[i_thread] != NULL) 
181                 && (pi_vout_status[i_thread] != THREAD_OVER) )
182             {
183                 b_thread = 1;
184             }     
185         }
186     }
187     
188
189 }