Rearrange winver detection code and cache the winver value we
[wine.git] / console / interface.c
blob80bc1a872cfe2f30ec219545e217acf0d7f883d0
1 /* interface.c */
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. */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
13 #include "config.h"
14 #include "windef.h"
15 #include "console.h"
16 #include "options.h"
18 CONSOLE_device driver;
20 static int pop_driver(char **, char **, int *);
22 static int console_initialized = FALSE;
24 int CONSOLE_Init(char *drivers)
26 /* When this function is called drivers should be a string
27 that consists of driver names followed by plus (+) signs
28 to denote additions.
30 For example:
31 drivers = tty Load just the tty driver
32 drivers = ncurses+xterm Load ncurses then xterm
34 The "default" value is just tty.
37 char *single;
38 int length;
39 char initial_rows[5];
40 char initial_columns[5];
42 /* Suitable defaults... */
43 driver.console_out = stdout;
44 driver.console_in = stdin;
46 while (pop_driver(&drivers, &single, &length))
48 if (!strncmp(single, "tty", length))
49 TTY_Start();
50 #ifdef WINE_NCURSES
51 else if (!strncmp(single, "ncurses", length))
52 NCURSES_Start();
53 #endif /* WINE_NCURSES */
54 else if (!strncmp(single, "xterm", length))
55 XTERM_Start();
58 /* Read in generic configuration */
59 /* This is primarily to work around a limitation in nxterm where
60 this information is not correctly passed to the ncurses layer
61 through the terminal. At least, I'm not doing it correctly if there
62 is a way. But this serves as a generic way to do the same anyway. */
64 /* We are setting this to 80x25 here which is *not* the default for
65 most xterm variants... It is however the standard VGA resolution */
67 /* FIXME: We need to be able to be able to specify that the window's
68 dimensions should be used. This is required for correct emulation
69 of Win32's console and Win32's DOS emulation */
71 PROFILE_GetWineIniString("console", "InitialRows",
72 "24", initial_rows, 5);
73 PROFILE_GetWineIniString("console", "InitialColumns",
74 "80", initial_columns, 5);
76 sscanf(initial_rows, "%d", &driver.y_res);
77 sscanf(initial_columns, "%d", &driver.x_res);
79 GENERIC_Start();
81 if (driver.init)
82 driver.init();
84 /* Not all terminals let our programs know the proper resolution
85 if the resolution is set on the command-line... */
86 CONSOLE_NotifyResizeScreen(driver.x_res, driver.y_res);
88 /* For now, always return TRUE */
89 return TRUE;
92 void CONSOLE_Write(char out, int fg_color, int bg_color, int attribute)
94 if (!console_initialized)
95 console_initialized = CONSOLE_Init(driver.driver_list);
97 if (driver.write)
99 driver.write(out, fg_color, bg_color, attribute);
100 if (!driver.norefresh)
101 CONSOLE_Refresh();
105 void CONSOLE_Close()
107 if (driver.close)
108 driver.close();
111 void CONSOLE_MoveCursor(char row, char col)
113 if (!console_initialized)
114 console_initialized = CONSOLE_Init(driver.driver_list);
116 if (driver.moveCursor)
118 driver.moveCursor(row, col);
119 if (!driver.norefresh)
120 CONSOLE_Refresh();
124 void CONSOLE_ClearWindow(char row1, char col1, char row2, char col2,
125 int bg_color, int attribute)
127 if (!console_initialized)
128 console_initialized = CONSOLE_Init(driver.driver_list);
130 if (driver.clearWindow)
132 driver.clearWindow(row1, col1, row2, col2, bg_color, attribute);
133 if (!driver.norefresh)
134 CONSOLE_Refresh();
138 void CONSOLE_ScrollUpWindow(char row1, char col1, char row2, char col2,
139 char lines, int bg_color, int attribute)
141 if (!console_initialized)
142 console_initialized = CONSOLE_Init(driver.driver_list);
144 if (driver.scrollUpWindow)
146 driver.scrollUpWindow(row1, col1, row2, col2, lines, bg_color,
147 attribute);
148 if (!driver.norefresh)
149 CONSOLE_Refresh();
153 void CONSOLE_ScrollDownWindow(char row1, char col1, char row2, char col2,
154 char lines, int bg_color, int attribute)
156 if (!console_initialized)
157 console_initialized = CONSOLE_Init(driver.driver_list);
159 if (driver.scrollDownWindow)
161 driver.scrollDownWindow(row1, col1, row2, col2, lines, bg_color,
162 attribute);
163 if (!driver.norefresh)
164 CONSOLE_Refresh();
168 int CONSOLE_CheckForKeystroke(char *scan, char *ascii)
169 /* These functions need to go through a conversion layer. Scancodes
170 should *not* be determined by the driver, rather they should have
171 a conv_* function in int16.c. Yuck. */
173 if (!console_initialized)
174 console_initialized = CONSOLE_Init(driver.driver_list);
176 if (driver.checkForKeystroke)
177 return driver.checkForKeystroke(scan, ascii);
178 else
179 return FALSE;
182 void CONSOLE_GetKeystroke(char *scan, char *ascii)
184 if (!console_initialized)
185 console_initialized = CONSOLE_Init(driver.driver_list);
187 if (driver.getKeystroke)
188 driver.getKeystroke(scan, ascii);
191 void CONSOLE_GetCursorPosition(char *row, char *col)
193 if (!console_initialized)
194 console_initialized = CONSOLE_Init(driver.driver_list);
196 if (driver.getCursorPosition)
197 driver.getCursorPosition(row, col);
200 void CONSOLE_GetCharacterAtCursor(char *ch, int *fg, int *bg, int *a)
202 if (!console_initialized)
203 console_initialized = CONSOLE_Init(driver.driver_list);
205 if (driver.getCharacterAtCursor)
206 driver.getCharacterAtCursor(ch, fg, bg, a);
209 void CONSOLE_Refresh()
211 if (!console_initialized)
212 console_initialized = CONSOLE_Init(driver.driver_list);
214 if (driver.refresh)
215 driver.refresh();
218 int CONSOLE_AllocColor(int color)
220 if (!console_initialized)
221 console_initialized = CONSOLE_Init(driver.driver_list);
223 if (driver.allocColor)
224 return driver.allocColor(color);
225 else
226 return 0;
229 void CONSOLE_ClearScreen()
231 if (!console_initialized)
232 console_initialized = CONSOLE_Init(driver.driver_list);
234 if (driver.clearScreen)
236 driver.clearScreen();
237 if (!driver.norefresh)
238 CONSOLE_Refresh();
242 char CONSOLE_GetCharacter()
244 if (!console_initialized)
245 console_initialized = CONSOLE_Init(driver.driver_list);
247 /* I'm not sure if we need this really. This is a function that can be
248 accelerated that returns the next *non extended* keystroke */
249 if (driver.getCharacter)
250 return driver.getCharacter();
251 else
252 return (char) 0; /* Sure, this will probably break programs... */
255 void CONSOLE_ResizeScreen(int x, int y)
257 if (!console_initialized)
258 console_initialized = CONSOLE_Init(driver.driver_list);
260 if (driver.resizeScreen)
261 driver.resizeScreen(x, y);
264 void CONSOLE_NotifyResizeScreen(int x, int y)
266 if (driver.notifyResizeScreen)
267 driver.notifyResizeScreen(x, y);
270 void CONSOLE_SetBackgroundColor(int fg, int bg)
272 if (!console_initialized)
273 console_initialized = CONSOLE_Init(driver.driver_list);
275 if (driver.setBackgroundColor)
276 driver.setBackgroundColor(fg, bg);
279 void CONSOLE_GetBackgroundColor(int *fg, int *bg)
281 if (!console_initialized)
282 console_initialized = CONSOLE_Init(driver.driver_list);
284 if (driver.getBackgroundColor)
285 driver.getBackgroundColor(fg, bg);
288 void CONSOLE_WriteRawString(char *str)
290 if (!console_initialized)
291 console_initialized = CONSOLE_Init(driver.driver_list);
293 /* This is a special function that is only for internal use and
294 does not actually call any of the console drivers. It's
295 primary purpose is to provide a way for higher-level drivers
296 to write directly to the underlying terminal without worry that
297 there will be any retranslation done by the assorted drivers. Care
298 should be taken to ensure that this only gets called when the thing
299 written does not actually produce any output or a CONSOLE_Redraw()
300 is called immediately afterwards.
301 CONSOLE_Redraw() is not yet implemented.
303 fprintf(driver.console_out, "%s", str);
306 /* This function is only at the CONSOLE level. */
307 /* Admittably, calling the variable norefresh might be a bit dumb...*/
308 void CONSOLE_SetRefresh(int setting)
310 if (setting)
311 driver.norefresh = FALSE;
312 else
313 driver.norefresh = TRUE;
316 /* This function is only at the CONSOLE level. */
317 int CONSOLE_GetRefresh()
319 if (driver.norefresh)
320 return FALSE;
321 else
322 return TRUE;
326 /* Utility functions... */
328 int pop_driver(char **drivers, char **single, int *length)
330 /* Take the string in drivers and extract the first "driver" entry */
331 /* Advance the pointer in drivers to the next entry, put the origional
332 pointer in single, and put the length in length. */
333 /* Return TRUE if we found one */
335 if (!*drivers)
336 return FALSE;
338 *single = *drivers;
339 *length = 0;
341 while ((*drivers[0] != NULL) && (*drivers[0] != '+'))
343 (*drivers)++;
344 (*length)++;
347 while (*drivers[0] == '+')
348 (*drivers)++;
350 if (*length)
351 return TRUE;
352 else
353 return FALSE;