Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 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 | |
Joseph Pranevich | 5576838 | 1998-12-09 15:43:03 +0000 | [diff] [blame] | 9 | #include <stdio.h> |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
Joseph Pranevich | 5576838 | 1998-12-09 15:43:03 +0000 | [diff] [blame] | 12 | |
Joseph Pranevich | 019693b | 1999-02-20 16:42:49 +0000 | [diff] [blame] | 13 | #include "config.h" |
Marcus Meissner | 317af32 | 1999-02-17 13:51:06 +0000 | [diff] [blame] | 14 | #include "windef.h" |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 15 | #include "console.h" |
Joseph Pranevich | 019693b | 1999-02-20 16:42:49 +0000 | [diff] [blame] | 16 | #include "options.h" |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 17 | |
Marcus Meissner | 69289f6 | 1999-04-18 09:25:27 +0000 | [diff] [blame] | 18 | CONSOLE_device driver; |
| 19 | |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 20 | static int pop_driver(char **, char **, int *); |
Joseph Pranevich | 5576838 | 1998-12-09 15:43:03 +0000 | [diff] [blame] | 21 | |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 22 | static int console_initialized = FALSE; |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 23 | |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 24 | int CONSOLE_Init(char *drivers) |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 25 | { |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 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. |
| 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 Pranevich | 019693b | 1999-02-20 16:42:49 +0000 | [diff] [blame] | 39 | char initial_rows[5]; |
| 40 | char initial_columns[5]; |
| 41 | |
Joseph Pranevich | 5576838 | 1998-12-09 15:43:03 +0000 | [diff] [blame] | 42 | /* Suitable defaults... */ |
| 43 | driver.console_out = stdout; |
| 44 | driver.console_in = stdin; |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 45 | |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 46 | 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 Pranevich | 019693b | 1999-02-20 16:42:49 +0000 | [diff] [blame] | 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. */ |
| 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 Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 79 | GENERIC_Start(); |
Joseph Pranevich | 5576838 | 1998-12-09 15:43:03 +0000 | [diff] [blame] | 80 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 81 | if (driver.init) |
| 82 | driver.init(); |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 83 | |
Joseph Pranevich | 019693b | 1999-02-20 16:42:49 +0000 | [diff] [blame] | 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); |
| 87 | |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 88 | /* For now, always return TRUE */ |
| 89 | return TRUE; |
| 90 | } |
| 91 | |
| 92 | void 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 Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void CONSOLE_Close() |
| 106 | { |
| 107 | if (driver.close) |
| 108 | driver.close(); |
| 109 | } |
| 110 | |
| 111 | void CONSOLE_MoveCursor(char row, char col) |
| 112 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 113 | if (!console_initialized) |
| 114 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 115 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 116 | if (driver.moveCursor) |
| 117 | { |
| 118 | driver.moveCursor(row, col); |
| 119 | if (!driver.norefresh) |
| 120 | CONSOLE_Refresh(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void CONSOLE_ClearWindow(char row1, char col1, char row2, char col2, |
| 125 | int bg_color, int attribute) |
| 126 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 127 | if (!console_initialized) |
| 128 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 129 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 130 | if (driver.clearWindow) |
| 131 | { |
| 132 | driver.clearWindow(row1, col1, row2, col2, bg_color, attribute); |
| 133 | if (!driver.norefresh) |
| 134 | CONSOLE_Refresh(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void CONSOLE_ScrollUpWindow(char row1, char col1, char row2, char col2, |
| 139 | char lines, int bg_color, int attribute) |
| 140 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 141 | if (!console_initialized) |
| 142 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 143 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 144 | 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 | |
| 153 | void CONSOLE_ScrollDownWindow(char row1, char col1, char row2, char col2, |
| 154 | char lines, int bg_color, int attribute) |
| 155 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 156 | if (!console_initialized) |
| 157 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 158 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 159 | 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 | |
| 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. */ |
| 172 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 173 | if (!console_initialized) |
| 174 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 175 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 176 | if (driver.checkForKeystroke) |
| 177 | return driver.checkForKeystroke(scan, ascii); |
| 178 | else |
| 179 | return FALSE; |
| 180 | } |
| 181 | |
| 182 | void CONSOLE_GetKeystroke(char *scan, char *ascii) |
| 183 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 184 | if (!console_initialized) |
| 185 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 186 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 187 | if (driver.getKeystroke) |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 188 | driver.getKeystroke(scan, ascii); |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void CONSOLE_GetCursorPosition(char *row, char *col) |
| 192 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 193 | if (!console_initialized) |
| 194 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 195 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 196 | if (driver.getCursorPosition) |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 197 | driver.getCursorPosition(row, col); |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void CONSOLE_GetCharacterAtCursor(char *ch, int *fg, int *bg, int *a) |
| 201 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 202 | if (!console_initialized) |
| 203 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 204 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 205 | if (driver.getCharacterAtCursor) |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 206 | driver.getCharacterAtCursor(ch, fg, bg, a); |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void CONSOLE_Refresh() |
| 210 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 211 | if (!console_initialized) |
| 212 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 213 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 214 | if (driver.refresh) |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 215 | driver.refresh(); |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Joseph Pranevich | 9c77b47 | 1999-01-30 12:51:09 +0000 | [diff] [blame] | 218 | int CONSOLE_AllocColor(int color) |
| 219 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 220 | if (!console_initialized) |
| 221 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 222 | |
Joseph Pranevich | 9c77b47 | 1999-01-30 12:51:09 +0000 | [diff] [blame] | 223 | if (driver.allocColor) |
| 224 | return driver.allocColor(color); |
| 225 | else |
| 226 | return 0; |
| 227 | } |
| 228 | |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 229 | void 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 | |
| 242 | char 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 | |
| 255 | void 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 | |
| 264 | void CONSOLE_NotifyResizeScreen(int x, int y) |
| 265 | { |
| 266 | if (driver.notifyResizeScreen) |
| 267 | driver.notifyResizeScreen(x, y); |
| 268 | } |
| 269 | |
| 270 | void 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 Pranevich | 93a2ce6 | 1999-02-24 11:08:29 +0000 | [diff] [blame] | 279 | void 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 Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 288 | void 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 Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 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) |
| 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. */ |
| 317 | int CONSOLE_GetRefresh() |
| 318 | { |
| 319 | if (driver.norefresh) |
| 320 | return FALSE; |
| 321 | else |
| 322 | return TRUE; |
| 323 | } |
| 324 | |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 325 | |
| 326 | /* Utility functions... */ |
| 327 | |
| 328 | int 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 | } |