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 | |
Marcus Meissner | 317af32 | 1999-02-17 13:51:06 +0000 | [diff] [blame^] | 13 | #include "windef.h" |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 14 | #include "console.h" |
| 15 | #include "config.h" |
| 16 | |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 17 | static int pop_driver(char **, char **, int *); |
Joseph Pranevich | 5576838 | 1998-12-09 15:43:03 +0000 | [diff] [blame] | 18 | |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 19 | static int console_initialized = FALSE; |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 20 | |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 21 | int CONSOLE_Init(char *drivers) |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 22 | { |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 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 | |
Joseph Pranevich | 5576838 | 1998-12-09 15:43:03 +0000 | [diff] [blame] | 37 | /* Suitable defaults... */ |
| 38 | driver.console_out = stdout; |
| 39 | driver.console_in = stdin; |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 40 | |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 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(); |
Joseph Pranevich | 5576838 | 1998-12-09 15:43:03 +0000 | [diff] [blame] | 54 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 55 | if (driver.init) |
| 56 | driver.init(); |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 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 | } |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 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 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 83 | if (!console_initialized) |
| 84 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 85 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 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 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 97 | if (!console_initialized) |
| 98 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 99 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 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 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 111 | if (!console_initialized) |
| 112 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 113 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 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 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 126 | if (!console_initialized) |
| 127 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 128 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 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 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 143 | if (!console_initialized) |
| 144 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 145 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 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 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 154 | if (!console_initialized) |
| 155 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 156 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 157 | if (driver.getKeystroke) |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 158 | driver.getKeystroke(scan, ascii); |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void CONSOLE_GetCursorPosition(char *row, char *col) |
| 162 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 163 | if (!console_initialized) |
| 164 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 165 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 166 | if (driver.getCursorPosition) |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 167 | driver.getCursorPosition(row, col); |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void CONSOLE_GetCharacterAtCursor(char *ch, int *fg, int *bg, int *a) |
| 171 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 172 | if (!console_initialized) |
| 173 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 174 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 175 | if (driver.getCharacterAtCursor) |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 176 | driver.getCharacterAtCursor(ch, fg, bg, a); |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void CONSOLE_Refresh() |
| 180 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 181 | if (!console_initialized) |
| 182 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 183 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 184 | if (driver.refresh) |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 185 | driver.refresh(); |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Joseph Pranevich | 9c77b47 | 1999-01-30 12:51:09 +0000 | [diff] [blame] | 188 | int CONSOLE_AllocColor(int color) |
| 189 | { |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 190 | if (!console_initialized) |
| 191 | console_initialized = CONSOLE_Init(driver.driver_list); |
| 192 | |
Joseph Pranevich | 9c77b47 | 1999-01-30 12:51:09 +0000 | [diff] [blame] | 193 | if (driver.allocColor) |
| 194 | return driver.allocColor(color); |
| 195 | else |
| 196 | return 0; |
| 197 | } |
| 198 | |
Joseph Pranevich | ebc0e5e | 1999-02-14 11:15:47 +0000 | [diff] [blame] | 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 | |
Joseph Pranevich | 791cd6a | 1998-12-02 19:58:08 +0000 | [diff] [blame] | 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 | |
Joseph Pranevich | 06591f6 | 1998-12-25 08:48:56 +0000 | [diff] [blame] | 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 | } |