blob: 80bc1a872cfe2f30ec219545e217acf0d7f883d0 [file] [log] [blame]
Joseph Pranevich791cd6a1998-12-02 19:58:08 +00001/* 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
Joseph Pranevich55768381998-12-09 15:43:03 +00009#include <stdio.h>
Joseph Pranevich06591f61998-12-25 08:48:56 +000010#include <stdlib.h>
11#include <string.h>
Joseph Pranevich55768381998-12-09 15:43:03 +000012
Joseph Pranevich019693b1999-02-20 16:42:49 +000013#include "config.h"
Marcus Meissner317af321999-02-17 13:51:06 +000014#include "windef.h"
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000015#include "console.h"
Joseph Pranevich019693b1999-02-20 16:42:49 +000016#include "options.h"
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000017
Marcus Meissner69289f61999-04-18 09:25:27 +000018CONSOLE_device driver;
19
Joseph Pranevich06591f61998-12-25 08:48:56 +000020static int pop_driver(char **, char **, int *);
Joseph Pranevich55768381998-12-09 15:43:03 +000021
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +000022static int console_initialized = FALSE;
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000023
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +000024int CONSOLE_Init(char *drivers)
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000025{
Joseph Pranevich06591f61998-12-25 08:48:56 +000026 /* When this function is called drivers should be a string
27 that consists of driver names followed by plus (+) signs
28 to denote additions.
29
30 For example:
31 drivers = tty Load just the tty driver
32 drivers = ncurses+xterm Load ncurses then xterm
33
34 The "default" value is just tty.
35 */
36
37 char *single;
38 int length;
Joseph Pranevich019693b1999-02-20 16:42:49 +000039 char initial_rows[5];
40 char initial_columns[5];
41
Joseph Pranevich55768381998-12-09 15:43:03 +000042 /* Suitable defaults... */
43 driver.console_out = stdout;
44 driver.console_in = stdin;
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000045
Joseph Pranevich06591f61998-12-25 08:48:56 +000046 while (pop_driver(&drivers, &single, &length))
47 {
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();
56 }
57
Joseph Pranevich019693b1999-02-20 16:42:49 +000058 /* 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. */
63
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 */
66
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 */
70
71 PROFILE_GetWineIniString("console", "InitialRows",
72 "24", initial_rows, 5);
73 PROFILE_GetWineIniString("console", "InitialColumns",
74 "80", initial_columns, 5);
75
76 sscanf(initial_rows, "%d", &driver.y_res);
77 sscanf(initial_columns, "%d", &driver.x_res);
78
Joseph Pranevich06591f61998-12-25 08:48:56 +000079 GENERIC_Start();
Joseph Pranevich55768381998-12-09 15:43:03 +000080
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000081 if (driver.init)
82 driver.init();
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +000083
Joseph Pranevich019693b1999-02-20 16:42:49 +000084 /* 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);
87
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +000088 /* For now, always return TRUE */
89 return TRUE;
90}
91
92void CONSOLE_Write(char out, int fg_color, int bg_color, int attribute)
93{
94 if (!console_initialized)
95 console_initialized = CONSOLE_Init(driver.driver_list);
96
97 if (driver.write)
98 {
99 driver.write(out, fg_color, bg_color, attribute);
100 if (!driver.norefresh)
101 CONSOLE_Refresh();
102 }
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000103}
104
105void CONSOLE_Close()
106{
107 if (driver.close)
108 driver.close();
109}
110
111void CONSOLE_MoveCursor(char row, char col)
112{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000113 if (!console_initialized)
114 console_initialized = CONSOLE_Init(driver.driver_list);
115
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000116 if (driver.moveCursor)
117 {
118 driver.moveCursor(row, col);
119 if (!driver.norefresh)
120 CONSOLE_Refresh();
121 }
122}
123
124void CONSOLE_ClearWindow(char row1, char col1, char row2, char col2,
125 int bg_color, int attribute)
126{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000127 if (!console_initialized)
128 console_initialized = CONSOLE_Init(driver.driver_list);
129
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000130 if (driver.clearWindow)
131 {
132 driver.clearWindow(row1, col1, row2, col2, bg_color, attribute);
133 if (!driver.norefresh)
134 CONSOLE_Refresh();
135 }
136}
137
138void CONSOLE_ScrollUpWindow(char row1, char col1, char row2, char col2,
139 char lines, int bg_color, int attribute)
140{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000141 if (!console_initialized)
142 console_initialized = CONSOLE_Init(driver.driver_list);
143
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000144 if (driver.scrollUpWindow)
145 {
146 driver.scrollUpWindow(row1, col1, row2, col2, lines, bg_color,
147 attribute);
148 if (!driver.norefresh)
149 CONSOLE_Refresh();
150 }
151}
152
153void CONSOLE_ScrollDownWindow(char row1, char col1, char row2, char col2,
154 char lines, int bg_color, int attribute)
155{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000156 if (!console_initialized)
157 console_initialized = CONSOLE_Init(driver.driver_list);
158
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000159 if (driver.scrollDownWindow)
160 {
161 driver.scrollDownWindow(row1, col1, row2, col2, lines, bg_color,
162 attribute);
163 if (!driver.norefresh)
164 CONSOLE_Refresh();
165 }
166}
167
168int 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. */
172{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000173 if (!console_initialized)
174 console_initialized = CONSOLE_Init(driver.driver_list);
175
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000176 if (driver.checkForKeystroke)
177 return driver.checkForKeystroke(scan, ascii);
178 else
179 return FALSE;
180}
181
182void CONSOLE_GetKeystroke(char *scan, char *ascii)
183{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000184 if (!console_initialized)
185 console_initialized = CONSOLE_Init(driver.driver_list);
186
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000187 if (driver.getKeystroke)
Joseph Pranevich06591f61998-12-25 08:48:56 +0000188 driver.getKeystroke(scan, ascii);
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000189}
190
191void CONSOLE_GetCursorPosition(char *row, char *col)
192{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000193 if (!console_initialized)
194 console_initialized = CONSOLE_Init(driver.driver_list);
195
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000196 if (driver.getCursorPosition)
Joseph Pranevich06591f61998-12-25 08:48:56 +0000197 driver.getCursorPosition(row, col);
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000198}
199
200void CONSOLE_GetCharacterAtCursor(char *ch, int *fg, int *bg, int *a)
201{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000202 if (!console_initialized)
203 console_initialized = CONSOLE_Init(driver.driver_list);
204
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000205 if (driver.getCharacterAtCursor)
Joseph Pranevich06591f61998-12-25 08:48:56 +0000206 driver.getCharacterAtCursor(ch, fg, bg, a);
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000207}
208
209void CONSOLE_Refresh()
210{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000211 if (!console_initialized)
212 console_initialized = CONSOLE_Init(driver.driver_list);
213
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000214 if (driver.refresh)
Joseph Pranevich06591f61998-12-25 08:48:56 +0000215 driver.refresh();
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000216}
217
Joseph Pranevich9c77b471999-01-30 12:51:09 +0000218int CONSOLE_AllocColor(int color)
219{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000220 if (!console_initialized)
221 console_initialized = CONSOLE_Init(driver.driver_list);
222
Joseph Pranevich9c77b471999-01-30 12:51:09 +0000223 if (driver.allocColor)
224 return driver.allocColor(color);
225 else
226 return 0;
227}
228
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000229void CONSOLE_ClearScreen()
230{
231 if (!console_initialized)
232 console_initialized = CONSOLE_Init(driver.driver_list);
233
234 if (driver.clearScreen)
235 {
236 driver.clearScreen();
237 if (!driver.norefresh)
238 CONSOLE_Refresh();
239 }
240}
241
242char CONSOLE_GetCharacter()
243{
244 if (!console_initialized)
245 console_initialized = CONSOLE_Init(driver.driver_list);
246
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... */
253}
254
255void CONSOLE_ResizeScreen(int x, int y)
256{
257 if (!console_initialized)
258 console_initialized = CONSOLE_Init(driver.driver_list);
259
260 if (driver.resizeScreen)
261 driver.resizeScreen(x, y);
262}
263
264void CONSOLE_NotifyResizeScreen(int x, int y)
265{
266 if (driver.notifyResizeScreen)
267 driver.notifyResizeScreen(x, y);
268}
269
270void CONSOLE_SetBackgroundColor(int fg, int bg)
271{
272 if (!console_initialized)
273 console_initialized = CONSOLE_Init(driver.driver_list);
274
275 if (driver.setBackgroundColor)
276 driver.setBackgroundColor(fg, bg);
277}
278
Joseph Pranevich93a2ce61999-02-24 11:08:29 +0000279void CONSOLE_GetBackgroundColor(int *fg, int *bg)
280{
281 if (!console_initialized)
282 console_initialized = CONSOLE_Init(driver.driver_list);
283
284 if (driver.getBackgroundColor)
285 driver.getBackgroundColor(fg, bg);
286}
287
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000288void CONSOLE_WriteRawString(char *str)
289{
290 if (!console_initialized)
291 console_initialized = CONSOLE_Init(driver.driver_list);
292
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.
302 */
303 fprintf(driver.console_out, "%s", str);
304}
305
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000306/* This function is only at the CONSOLE level. */
307/* Admittably, calling the variable norefresh might be a bit dumb...*/
308void CONSOLE_SetRefresh(int setting)
309{
310 if (setting)
311 driver.norefresh = FALSE;
312 else
313 driver.norefresh = TRUE;
314}
315
316/* This function is only at the CONSOLE level. */
317int CONSOLE_GetRefresh()
318{
319 if (driver.norefresh)
320 return FALSE;
321 else
322 return TRUE;
323}
324
Joseph Pranevich06591f61998-12-25 08:48:56 +0000325
326/* Utility functions... */
327
328int pop_driver(char **drivers, char **single, int *length)
329{
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 */
334
335 if (!*drivers)
336 return FALSE;
337
338 *single = *drivers;
339 *length = 0;
340
341 while ((*drivers[0] != NULL) && (*drivers[0] != '+'))
342 {
343 (*drivers)++;
344 (*length)++;
345 }
346
347 while (*drivers[0] == '+')
348 (*drivers)++;
349
350 if (*length)
351 return TRUE;
352 else
353 return FALSE;
354
355}