- New implementation of SendMessage, ReceiveMessage, ReplyMessage functions
[wine] / console / interface.c
1 /* interface.c */
2
3 /* The primary purpose of this function is to provide CONSOLE_*
4    reotines that immediately call the appropiate driver handler.
5    This cleans up code in the individual modules considerably.
6    This could be done using a macro, but additional functionality
7    may be provided here in the future. */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "windef.h"
14 #include "console.h"
15 #include "config.h"
16
17 static int pop_driver(char **, char **, int *);
18
19 static int console_initialized = FALSE;
20
21 int CONSOLE_Init(char *drivers)
22 {
23       /* When this function is called drivers should be a string
24          that consists of driver names followed by plus (+) signs
25          to denote additions. 
26
27          For example:
28             drivers = tty                Load just the tty driver
29             drivers = ncurses+xterm      Load ncurses then xterm
30
31          The "default" value is just tty.
32       */
33       
34       char *single;
35       int length;
36       
37       /* Suitable defaults... */
38       driver.console_out = stdout;
39       driver.console_in = stdin;
40
41       while (pop_driver(&drivers, &single, &length))
42       {
43          if (!strncmp(single, "tty", length))
44             TTY_Start();
45 #ifdef WINE_NCURSES
46          else if (!strncmp(single, "ncurses", length))
47             NCURSES_Start();
48 #endif /* WINE_NCURSES */
49          else if (!strncmp(single, "xterm", length))
50             XTERM_Start();
51       }
52
53    GENERIC_Start();
54
55    if (driver.init)
56       driver.init();
57
58    /* For now, always return TRUE */
59    return TRUE;
60 }
61
62 void CONSOLE_Write(char out, int fg_color, int bg_color, int attribute)
63 {
64    if (!console_initialized)
65       console_initialized = CONSOLE_Init(driver.driver_list);
66       
67    if (driver.write)
68    {
69       driver.write(out, fg_color, bg_color, attribute);
70       if (!driver.norefresh)
71          CONSOLE_Refresh();
72    }
73 }
74
75 void CONSOLE_Close()
76 {
77    if (driver.close)
78       driver.close();
79 }
80
81 void CONSOLE_MoveCursor(char row, char col)
82 {
83    if (!console_initialized)
84       console_initialized = CONSOLE_Init(driver.driver_list);
85       
86    if (driver.moveCursor)
87    {
88       driver.moveCursor(row, col);
89       if (!driver.norefresh)
90          CONSOLE_Refresh();
91    }
92 }
93
94 void CONSOLE_ClearWindow(char row1, char col1, char row2, char col2, 
95    int bg_color, int attribute)
96 {
97    if (!console_initialized)
98       console_initialized = CONSOLE_Init(driver.driver_list);
99       
100    if (driver.clearWindow)
101    {
102       driver.clearWindow(row1, col1, row2, col2, bg_color, attribute);
103       if (!driver.norefresh)
104          CONSOLE_Refresh();
105    }
106 }
107
108 void CONSOLE_ScrollUpWindow(char row1, char col1, char row2, char col2, 
109    char lines, int bg_color, int attribute)
110 {
111    if (!console_initialized)
112       console_initialized = CONSOLE_Init(driver.driver_list);
113       
114    if (driver.scrollUpWindow)
115    {
116       driver.scrollUpWindow(row1, col1, row2, col2, lines, bg_color, 
117          attribute);
118       if (!driver.norefresh)
119          CONSOLE_Refresh();
120    }
121 }
122
123 void CONSOLE_ScrollDownWindow(char row1, char col1, char row2, char col2, 
124    char lines, int bg_color, int attribute)
125 {
126    if (!console_initialized)
127       console_initialized = CONSOLE_Init(driver.driver_list);
128       
129    if (driver.scrollDownWindow)
130    {
131       driver.scrollDownWindow(row1, col1, row2, col2, lines, bg_color, 
132          attribute);
133       if (!driver.norefresh)
134          CONSOLE_Refresh();
135    }
136 }
137
138 int CONSOLE_CheckForKeystroke(char *scan, char *ascii)
139 /* These functions need to go through a conversion layer. Scancodes
140    should *not* be determined by the driver, rather they should have
141    a conv_* function in int16.c. Yuck. */
142 {
143    if (!console_initialized)
144       console_initialized = CONSOLE_Init(driver.driver_list);
145       
146    if (driver.checkForKeystroke)
147       return driver.checkForKeystroke(scan, ascii);
148    else
149       return FALSE;
150 }
151
152 void CONSOLE_GetKeystroke(char *scan, char *ascii)
153 {
154    if (!console_initialized)
155       console_initialized = CONSOLE_Init(driver.driver_list);
156       
157    if (driver.getKeystroke)
158       driver.getKeystroke(scan, ascii);
159 }
160
161 void CONSOLE_GetCursorPosition(char *row, char *col)
162 {
163    if (!console_initialized)
164       console_initialized = CONSOLE_Init(driver.driver_list);
165       
166    if (driver.getCursorPosition)
167       driver.getCursorPosition(row, col);
168 }
169
170 void CONSOLE_GetCharacterAtCursor(char *ch, int *fg, int *bg, int *a)
171 {
172    if (!console_initialized)
173       console_initialized = CONSOLE_Init(driver.driver_list);
174       
175    if (driver.getCharacterAtCursor)
176       driver.getCharacterAtCursor(ch, fg, bg, a);
177 }
178
179 void CONSOLE_Refresh()
180 {
181    if (!console_initialized)
182       console_initialized = CONSOLE_Init(driver.driver_list);
183       
184    if (driver.refresh)
185       driver.refresh();
186 }
187
188 int CONSOLE_AllocColor(int color)
189 {
190    if (!console_initialized)
191       console_initialized = CONSOLE_Init(driver.driver_list);
192       
193    if (driver.allocColor)
194       return driver.allocColor(color);
195    else 
196       return 0;
197 }
198
199 void CONSOLE_ClearScreen()
200 {
201    if (!console_initialized)
202       console_initialized = CONSOLE_Init(driver.driver_list);
203       
204    if (driver.clearScreen)
205    {
206       driver.clearScreen();
207       if (!driver.norefresh)
208          CONSOLE_Refresh();
209    }
210 }
211
212 char CONSOLE_GetCharacter()
213 {
214    if (!console_initialized)
215       console_initialized = CONSOLE_Init(driver.driver_list);
216       
217    /* I'm not sure if we need this really. This is a function that can be
218       accelerated that returns the next *non extended* keystroke */
219    if (driver.getCharacter)
220       return driver.getCharacter();
221    else
222       return (char) 0; /* Sure, this will probably break programs... */
223 }
224
225 void CONSOLE_ResizeScreen(int x, int y)
226 {
227    if (!console_initialized)
228       console_initialized = CONSOLE_Init(driver.driver_list);
229       
230    if (driver.resizeScreen)
231       driver.resizeScreen(x, y);
232 }
233
234 void CONSOLE_NotifyResizeScreen(int x, int y)
235 {
236    if (driver.notifyResizeScreen)
237       driver.notifyResizeScreen(x, y);
238 }
239
240 void CONSOLE_SetBackgroundColor(int fg, int bg)
241 {
242    if (!console_initialized)
243       console_initialized = CONSOLE_Init(driver.driver_list);
244       
245    if (driver.setBackgroundColor)
246       driver.setBackgroundColor(fg, bg);
247 }
248
249 void CONSOLE_WriteRawString(char *str)
250 {
251    if (!console_initialized)
252       console_initialized = CONSOLE_Init(driver.driver_list);
253       
254    /* This is a special function that is only for internal use and 
255       does not actually call any of the console drivers. It's 
256       primary purpose is to provide a way for higher-level drivers
257       to write directly to the underlying terminal without worry that
258       there will be any retranslation done by the assorted drivers. Care
259       should be taken to ensure that this only gets called when the thing
260       written does not actually produce any output or a CONSOLE_Redraw()
261       is called immediately afterwards.
262       CONSOLE_Redraw() is not yet implemented.
263    */
264    fprintf(driver.console_out, "%s", str);
265 }
266
267 /* This function is only at the CONSOLE level. */
268 /* Admittably, calling the variable norefresh might be a bit dumb...*/
269 void CONSOLE_SetRefresh(int setting)
270 {
271    if (setting)
272       driver.norefresh = FALSE;
273    else
274       driver.norefresh = TRUE;
275 }
276
277 /* This function is only at the CONSOLE level. */
278 int CONSOLE_GetRefresh()
279 {
280    if (driver.norefresh)
281       return FALSE;
282    else 
283       return TRUE;
284 }
285
286
287 /* Utility functions... */
288
289 int pop_driver(char **drivers, char **single, int *length)
290 {
291    /* Take the string in drivers and extract the first "driver" entry */
292    /* Advance the pointer in drivers to the next entry, put the origional
293       pointer in single, and put the length in length. */
294    /* Return TRUE if we found one */
295
296    if (!*drivers)
297       return FALSE;
298
299    *single = *drivers;
300    *length = 0;
301
302    while ((*drivers[0] != NULL) && (*drivers[0] != '+'))
303    {
304       (*drivers)++;
305       (*length)++;
306    }
307    
308    while (*drivers[0] == '+')
309       (*drivers)++;
310
311    if (*length)
312       return TRUE;
313    else
314       return FALSE;
315       
316 }