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