blob: afe646b7f220750b86b14d8ea59b5e6130ea0183 [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
Marcus Meissner317af321999-02-17 13:51:06 +000013#include "windef.h"
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000014#include "console.h"
15#include "config.h"
16
Joseph Pranevich06591f61998-12-25 08:48:56 +000017static int pop_driver(char **, char **, int *);
Joseph Pranevich55768381998-12-09 15:43:03 +000018
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +000019static int console_initialized = FALSE;
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000020
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +000021int CONSOLE_Init(char *drivers)
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000022{
Joseph Pranevich06591f61998-12-25 08:48:56 +000023 /* 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
Joseph Pranevich55768381998-12-09 15:43:03 +000037 /* Suitable defaults... */
38 driver.console_out = stdout;
39 driver.console_in = stdin;
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000040
Joseph Pranevich06591f61998-12-25 08:48:56 +000041 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();
Joseph Pranevich55768381998-12-09 15:43:03 +000054
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000055 if (driver.init)
56 driver.init();
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +000057
58 /* For now, always return TRUE */
59 return TRUE;
60}
61
62void 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 }
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000073}
74
75void CONSOLE_Close()
76{
77 if (driver.close)
78 driver.close();
79}
80
81void CONSOLE_MoveCursor(char row, char col)
82{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +000083 if (!console_initialized)
84 console_initialized = CONSOLE_Init(driver.driver_list);
85
Joseph Pranevich791cd6a1998-12-02 19:58:08 +000086 if (driver.moveCursor)
87 {
88 driver.moveCursor(row, col);
89 if (!driver.norefresh)
90 CONSOLE_Refresh();
91 }
92}
93
94void CONSOLE_ClearWindow(char row1, char col1, char row2, char col2,
95 int bg_color, int attribute)
96{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +000097 if (!console_initialized)
98 console_initialized = CONSOLE_Init(driver.driver_list);
99
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000100 if (driver.clearWindow)
101 {
102 driver.clearWindow(row1, col1, row2, col2, bg_color, attribute);
103 if (!driver.norefresh)
104 CONSOLE_Refresh();
105 }
106}
107
108void CONSOLE_ScrollUpWindow(char row1, char col1, char row2, char col2,
109 char lines, int bg_color, int attribute)
110{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000111 if (!console_initialized)
112 console_initialized = CONSOLE_Init(driver.driver_list);
113
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000114 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
123void CONSOLE_ScrollDownWindow(char row1, char col1, char row2, char col2,
124 char lines, int bg_color, int attribute)
125{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000126 if (!console_initialized)
127 console_initialized = CONSOLE_Init(driver.driver_list);
128
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000129 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
138int 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{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000143 if (!console_initialized)
144 console_initialized = CONSOLE_Init(driver.driver_list);
145
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000146 if (driver.checkForKeystroke)
147 return driver.checkForKeystroke(scan, ascii);
148 else
149 return FALSE;
150}
151
152void CONSOLE_GetKeystroke(char *scan, char *ascii)
153{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000154 if (!console_initialized)
155 console_initialized = CONSOLE_Init(driver.driver_list);
156
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000157 if (driver.getKeystroke)
Joseph Pranevich06591f61998-12-25 08:48:56 +0000158 driver.getKeystroke(scan, ascii);
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000159}
160
161void CONSOLE_GetCursorPosition(char *row, char *col)
162{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000163 if (!console_initialized)
164 console_initialized = CONSOLE_Init(driver.driver_list);
165
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000166 if (driver.getCursorPosition)
Joseph Pranevich06591f61998-12-25 08:48:56 +0000167 driver.getCursorPosition(row, col);
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000168}
169
170void CONSOLE_GetCharacterAtCursor(char *ch, int *fg, int *bg, int *a)
171{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000172 if (!console_initialized)
173 console_initialized = CONSOLE_Init(driver.driver_list);
174
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000175 if (driver.getCharacterAtCursor)
Joseph Pranevich06591f61998-12-25 08:48:56 +0000176 driver.getCharacterAtCursor(ch, fg, bg, a);
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000177}
178
179void CONSOLE_Refresh()
180{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000181 if (!console_initialized)
182 console_initialized = CONSOLE_Init(driver.driver_list);
183
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000184 if (driver.refresh)
Joseph Pranevich06591f61998-12-25 08:48:56 +0000185 driver.refresh();
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000186}
187
Joseph Pranevich9c77b471999-01-30 12:51:09 +0000188int CONSOLE_AllocColor(int color)
189{
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000190 if (!console_initialized)
191 console_initialized = CONSOLE_Init(driver.driver_list);
192
Joseph Pranevich9c77b471999-01-30 12:51:09 +0000193 if (driver.allocColor)
194 return driver.allocColor(color);
195 else
196 return 0;
197}
198
Joseph Pranevichebc0e5e1999-02-14 11:15:47 +0000199void 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
212char 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
225void 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
234void CONSOLE_NotifyResizeScreen(int x, int y)
235{
236 if (driver.notifyResizeScreen)
237 driver.notifyResizeScreen(x, y);
238}
239
240void 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
249void 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
Joseph Pranevich791cd6a1998-12-02 19:58:08 +0000267/* This function is only at the CONSOLE level. */
268/* Admittably, calling the variable norefresh might be a bit dumb...*/
269void 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. */
278int CONSOLE_GetRefresh()
279{
280 if (driver.norefresh)
281 return FALSE;
282 else
283 return TRUE;
284}
285
Joseph Pranevich06591f61998-12-25 08:48:56 +0000286
287/* Utility functions... */
288
289int 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}