]> git.sesse.net Git - vlc/blob - src/interface/interface.c
A tout kass�.
[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 <errno.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/uio.h>                                        /* for input.h */
16
17 #include "config.h"
18 #include "common.h"
19 #include "mtime.h"
20 #include "vlc_thread.h"
21 #include "input.h"
22 #include "intf_msg.h"
23 #include "interface.h"
24 #include "intf_cmd.h"
25 #include "intf_console.h"
26 #include "main.h"
27
28 #include "intf_sys.h"
29
30
31 /*******************************************************************************
32  * intf_Create: prepare interface before main loop
33  *******************************************************************************
34  * This function opens output devices and create specific interfaces. It send
35  * it's own error messages.
36  *******************************************************************************/
37 intf_thread_t* intf_Create( void )
38 {    
39     intf_thread_t *p_intf;                                        
40
41     /* Allocate structure */
42     p_intf = malloc( sizeof( intf_thread_t ) );
43     if( !p_intf )
44     {
45         errno = ENOMEM;
46         return( NULL );
47     }
48     p_intf->b_die = 0;
49     intf_DbgMsg( "0x%x\n", p_intf );
50
51     /* Initialize structure */
52     p_intf->p_vout = NULL;
53     p_intf->p_input = NULL;   
54
55     /* Start interfaces */
56     p_intf->p_console = intf_ConsoleCreate();
57     if( p_intf->p_console == NULL )
58     {
59         intf_ErrMsg("intf error: can't create control console\n");
60         free( p_intf );
61         return( NULL );
62     }
63     if( intf_SysCreate( p_intf ) )
64     {
65         intf_ErrMsg("intf error: can't create interface\n");
66         intf_ConsoleDestroy( p_intf->p_console );
67         free( p_intf );
68         return( NULL );
69     }   
70
71     return( p_intf );
72 }
73
74 /*******************************************************************************
75  * intf_Run
76  *******************************************************************************
77  * Initialization script and main interface loop.
78  *******************************************************************************/
79 void intf_Run( intf_thread_t *p_intf )
80
81     intf_DbgMsg("0x%x begin\n", p_intf );
82
83     /* Execute the initialization script - if a positive number is returned, 
84      * the script could be executed but failed */
85     if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
86     {
87         intf_ErrMsg("intf error: error during initialization script\n");
88     }
89
90     /* Main loop */
91     while(!p_intf->b_die)
92     {
93         /* Flush waiting messages */
94         intf_FlushMsg();
95
96         /* Manage specific interface */
97         intf_SysManage( p_intf );
98
99         /* Sleep to avoid using all CPU - since some interfaces needs to access 
100          * keyboard events, a 100ms delay is a good compromise */
101         msleep( INTF_IDLE_SLEEP );
102     }
103
104     intf_DbgMsg("0x%x end\n", p_intf );
105 }
106
107 /*******************************************************************************
108  * intf_Destroy: clean interface after main loop
109  *******************************************************************************
110  * This function destroys specific interfaces and close output devices.
111  *******************************************************************************/
112 void intf_Destroy( intf_thread_t *p_intf )
113 {
114     intf_DbgMsg("0x%x\n", p_intf );
115
116     /* Destroy interfaces */
117     intf_SysDestroy( p_intf );
118     intf_ConsoleDestroy( p_intf->p_console );
119
120     /* Free structure */
121     free( p_intf );
122 }
123
124 /*******************************************************************************
125  * intf_SelectInput: change input stream
126  *******************************************************************************
127  * Kill existing input, if any, and try to open a new one. If p_cfg is NULL,
128  * no new input will be openned.
129  *******************************************************************************/
130 int intf_SelectInput( intf_thread_t * p_intf, input_cfg_t *p_cfg )
131 {
132     intf_DbgMsg("0x%x\n", p_intf );
133     
134     /* Kill existing input, if any */
135     if( p_intf->p_input != NULL )
136     {        
137         input_DestroyThread( p_intf->p_input /*??, NULL*/ );
138         p_intf->p_input = NULL;        
139     }
140     
141     /* Open new one */
142     if( p_cfg != NULL )
143     {        
144         p_intf->p_input = input_CreateThread( p_cfg /*??, NULL*/ );    
145     }
146     
147     return( (p_cfg != NULL) && (p_intf->p_input == NULL) );    
148 }
149
150