| 1 | /****************************************************************************** |
| 2 | * * |
| 3 | * Copyright (C) 2006-2009 by Tor Andersson. * |
| 4 | * Copyright (C) 2010-2011 by Ben Cressey. * |
| 5 | * * |
| 6 | * This file is part of Gargoyle. * |
| 7 | * * |
| 8 | * Gargoyle is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * Gargoyle is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with Gargoyle; if not, write to the Free Software * |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
| 21 | * * |
| 22 | *****************************************************************************/ |
| 23 | |
| 24 | /* osglk.c -- glk os adapter */ |
| 25 | |
| 26 | #include <assert.h> |
| 27 | |
| 28 | #include "os.h" |
| 29 | #include "glk.h" |
| 30 | |
| 31 | /* for version strings */ |
| 32 | #include "trd.h" |
| 33 | #include "vmvsn.h" |
| 34 | |
| 35 | #ifndef gestalt_GarglkText |
| 36 | // If this isn't defined by the Glk header, then it's very unlikely to be |
| 37 | // supported, but defining it here makes the code below a bit nicer. |
| 38 | # define gestalt_GarglkText 0x1100 |
| 39 | #endif |
| 40 | |
| 41 | #ifndef gestalt_Stylehints |
| 42 | // preliminary, likely to change later. |
| 43 | # define gestalt_Stylehints 0x1101 |
| 44 | #endif |
| 45 | |
| 46 | static void redraw_windows(void); |
| 47 | static void os_status_redraw(void); |
| 48 | extern void os_banners_redraw(void); |
| 49 | |
| 50 | static char lbuf[256], rbuf[256]; |
| 51 | static int curwin = 0; |
| 52 | static int curattr = 0; |
| 53 | |
| 54 | winid_t mainwin; |
| 55 | winid_t statuswin; |
| 56 | |
| 57 | glui32 mainfg; |
| 58 | glui32 mainbg; |
| 59 | |
| 60 | glui32 statusfg; |
| 61 | glui32 statusbg; |
| 62 | |
| 63 | static int use_more_text_styling = 0; |
| 64 | |
| 65 | /* ------------------------------------------------------------------------ */ |
| 66 | /* |
| 67 | * Get system information. 'code' is a SYSINFO_xxx code, which |
| 68 | * specifies what type of information to get. The 'param' argument's |
| 69 | * meaning depends on which code is selected. 'result' is a pointer to |
| 70 | * an integer that is to be filled in with the result value. If the |
| 71 | * code is not known, this function should return FALSE. If the code is |
| 72 | * known, the function should fill in *result and return TRUE. |
| 73 | */ |
| 74 | int os_get_sysinfo(int code, void *param, long *result) |
| 75 | { |
| 76 | switch (code) |
| 77 | { |
| 78 | case SYSINFO_TEXT_HILITE: |
| 79 | *result = 1; |
| 80 | return TRUE; |
| 81 | case SYSINFO_BANNERS: |
| 82 | *result = 1; |
| 83 | return TRUE; |
| 84 | case SYSINFO_TEXT_COLORS: |
| 85 | *result = SYSINFO_TXC_NONE; |
| 86 | return TRUE; |
| 87 | |
| 88 | #ifdef USE_HTML |
| 89 | case SYSINFO_INTERP_CLASS: |
| 90 | *result = SYSINFO_ICLASS_HTML; |
| 91 | return TRUE; |
| 92 | case SYSINFO_HTML: |
| 93 | *result = 1; |
| 94 | return TRUE; |
| 95 | #else |
| 96 | case SYSINFO_INTERP_CLASS: |
| 97 | *result = SYSINFO_ICLASS_TEXTGUI; |
| 98 | return TRUE; |
| 99 | case SYSINFO_HTML: |
| 100 | *result = 0; |
| 101 | return TRUE; |
| 102 | #endif |
| 103 | |
| 104 | case SYSINFO_JPEG: |
| 105 | case SYSINFO_PNG: |
| 106 | case SYSINFO_WAV: |
| 107 | case SYSINFO_MIDI: |
| 108 | case SYSINFO_WAV_MIDI_OVL: |
| 109 | case SYSINFO_WAV_OVL: |
| 110 | case SYSINFO_PREF_IMAGES: |
| 111 | case SYSINFO_PREF_SOUNDS: |
| 112 | case SYSINFO_PREF_MUSIC: |
| 113 | case SYSINFO_PREF_LINKS: |
| 114 | case SYSINFO_MPEG: |
| 115 | case SYSINFO_MPEG1: |
| 116 | case SYSINFO_MPEG2: |
| 117 | case SYSINFO_MPEG3: |
| 118 | case SYSINFO_LINKS_HTTP: |
| 119 | case SYSINFO_LINKS_FTP: |
| 120 | case SYSINFO_LINKS_NEWS: |
| 121 | case SYSINFO_LINKS_MAILTO: |
| 122 | case SYSINFO_LINKS_TELNET: |
| 123 | case SYSINFO_PNG_TRANS: |
| 124 | case SYSINFO_PNG_ALPHA: |
| 125 | case SYSINFO_OGG: |
| 126 | *result = 0; |
| 127 | return TRUE; |
| 128 | |
| 129 | default: |
| 130 | return FALSE; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* ------------------------------------------------------------------------ */ |
| 136 | |
| 137 | /* |
| 138 | * Initialize. This should be called during program startup to |
| 139 | * initialize the OS layer and check OS-specific command-line arguments. |
| 140 | * |
| 141 | * If 'prompt' and 'buf' are non-null, and there are no arguments on the |
| 142 | * given command line, the OS code can use the prompt to ask the user to |
| 143 | * supply a filename, then store the filename in 'buf' and set up |
| 144 | * argc/argv to give a one-argument command string. (This mechanism for |
| 145 | * prompting for a filename is obsolescent, and is retained for |
| 146 | * compatibility with a small number of existing implementations only; |
| 147 | * new implementations should ignore this mechanism and leave the |
| 148 | * argc/argv values unchanged.) |
| 149 | */ |
| 150 | int os_init(int *argc, char *argv[], const char *prompt, |
| 151 | char *buf, int bufsiz) |
| 152 | { |
| 153 | // If stylehints are supported, set styles to enable us to have all combinations of bold, italic, and monospaced |
| 154 | // Some of these might be redundant, but set them just to be safe |
| 155 | if (glk_gestalt(gestalt_GarglkText, 0) || glk_gestalt(gestalt_Stylehints, 0)) { |
| 156 | use_more_text_styling = 1; |
| 157 | // Use Normal for no formatting |
| 158 | glk_stylehint_set(wintype_TextBuffer, style_Normal, stylehint_Weight, 0); |
| 159 | glk_stylehint_set(wintype_TextBuffer, style_Normal, stylehint_Oblique, 0); |
| 160 | glk_stylehint_set(wintype_TextBuffer, style_Normal, stylehint_Proportional, 1); |
| 161 | // Use Subheader for bold |
| 162 | glk_stylehint_set(wintype_TextBuffer, style_Subheader, stylehint_Weight, 1); |
| 163 | glk_stylehint_set(wintype_TextBuffer, style_Subheader, stylehint_Oblique, 0); |
| 164 | glk_stylehint_set(wintype_TextBuffer, style_Subheader, stylehint_Proportional, 1); |
| 165 | // Use Emphasized for italic |
| 166 | glk_stylehint_set(wintype_TextBuffer, style_Emphasized, stylehint_Weight, 0); |
| 167 | glk_stylehint_set(wintype_TextBuffer, style_Emphasized, stylehint_Oblique, 1); |
| 168 | glk_stylehint_set(wintype_TextBuffer, style_Emphasized, stylehint_Proportional, 1); |
| 169 | // Use Alert for bold+italic |
| 170 | glk_stylehint_set(wintype_TextBuffer, style_Alert, stylehint_Weight, 1); |
| 171 | glk_stylehint_set(wintype_TextBuffer, style_Alert, stylehint_Oblique, 1); |
| 172 | glk_stylehint_set(wintype_TextBuffer, style_Alert, stylehint_Proportional, 1); |
| 173 | // Use Preformatted for monospace |
| 174 | glk_stylehint_set(wintype_TextBuffer, style_Preformatted, stylehint_Weight, 0); |
| 175 | glk_stylehint_set(wintype_TextBuffer, style_Preformatted, stylehint_Oblique, 0); |
| 176 | glk_stylehint_set(wintype_TextBuffer, style_Preformatted, stylehint_Proportional, 0); |
| 177 | // Use User1 for monospace+bold |
| 178 | glk_stylehint_set(wintype_TextBuffer, style_User1, stylehint_Weight, 1); |
| 179 | glk_stylehint_set(wintype_TextBuffer, style_User1, stylehint_Oblique, 0); |
| 180 | glk_stylehint_set(wintype_TextBuffer, style_User1, stylehint_Proportional, 0); |
| 181 | // Use User2 for monospace+italic |
| 182 | glk_stylehint_set(wintype_TextBuffer, style_User2, stylehint_Weight, 0); |
| 183 | glk_stylehint_set(wintype_TextBuffer, style_User2, stylehint_Oblique, 1); |
| 184 | glk_stylehint_set(wintype_TextBuffer, style_User2, stylehint_Proportional, 0); |
| 185 | // Use Note for monospace+italic+bold |
| 186 | glk_stylehint_set(wintype_TextBuffer, style_Note, stylehint_Weight, 1); |
| 187 | glk_stylehint_set(wintype_TextBuffer, style_Note, stylehint_Oblique, 1); |
| 188 | glk_stylehint_set(wintype_TextBuffer, style_Note, stylehint_Proportional, 0); |
| 189 | } |
| 190 | |
| 191 | mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 0); |
| 192 | |
| 193 | if (!mainwin) |
| 194 | { |
| 195 | fprintf(stderr, "fatal: could not open window!\n"); |
| 196 | glk_exit(); |
| 197 | } |
| 198 | |
| 199 | /* get default colors for main window */ |
| 200 | if (!glk_style_measure(mainwin, style_Normal, stylehint_TextColor, &mainfg)) |
| 201 | mainfg = 0; |
| 202 | |
| 203 | if (!glk_style_measure(mainwin, style_Normal, stylehint_BackColor, &mainbg)) |
| 204 | mainbg = 0xFFFFFFFF; |
| 205 | |
| 206 | /* get default colors for status window */ |
| 207 | statuswin = glk_window_open(mainwin, |
| 208 | winmethod_Above | winmethod_Fixed, 1, |
| 209 | wintype_TextGrid, 0); |
| 210 | |
| 211 | if (!glk_style_measure(statuswin, style_Normal, stylehint_TextColor, &statusfg)) |
| 212 | statusfg = 0; |
| 213 | |
| 214 | if (!glk_style_measure(statuswin, style_Normal, stylehint_BackColor, &statusbg)) |
| 215 | statusbg = 0; |
| 216 | |
| 217 | /* close statuswin; reopened on request */ |
| 218 | glk_window_close(statuswin, 0); |
| 219 | |
| 220 | statuswin = NULL; |
| 221 | |
| 222 | glk_set_window(mainwin); |
| 223 | |
| 224 | strcpy(rbuf, ""); |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Uninitialize. This is called prior to progam termination to reverse |
| 231 | * the effect of any changes made in os_init(). For example, if |
| 232 | * os_init() put the terminal in raw mode, this should restore the |
| 233 | * previous terminal mode. This routine should not terminate the |
| 234 | * program (so don't call exit() here) - the caller might have more |
| 235 | * processing to perform after this routine returns. |
| 236 | */ |
| 237 | void os_uninit(void) |
| 238 | { |
| 239 | } |
| 240 | |
| 241 | void os_term(int status) |
| 242 | { |
| 243 | glk_exit(); |
| 244 | } |
| 245 | |
| 246 | /* ------------------------------------------------------------------------ */ |
| 247 | /* |
| 248 | * Display routines. |
| 249 | * |
| 250 | * Our display model is a simple stdio-style character stream. |
| 251 | * |
| 252 | * In addition, we provide an optional "status line," which is a |
| 253 | * non-scrolling area where a line of text can be displayed. If the status |
| 254 | * line is supported, text should only be displayed in this area when |
| 255 | * os_status() is used to enter status-line mode (mode 1); while in status |
| 256 | * line mode, text is written to the status line area, otherwise (mode 0) |
| 257 | * it's written to the normal main text area. The status line is normally |
| 258 | * shown in a different color to set it off from the rest of the text. |
| 259 | * |
| 260 | * The OS layer can provide its own formatting (word wrapping in |
| 261 | * particular) if it wants, in which case it should also provide pagination |
| 262 | * using os_more_prompt(). |
| 263 | */ |
| 264 | |
| 265 | /* |
| 266 | * Print a string on the console. These routines come in two varieties: |
| 267 | * |
| 268 | * os_printz - write a NULL-TERMINATED string |
| 269 | *. os_print - write a COUNTED-LENGTH string, which may not end with a null |
| 270 | * |
| 271 | * These two routines are identical except that os_printz() takes a string |
| 272 | * which is terminated by a null byte, and os_print() instead takes an |
| 273 | * explicit length, and a string that may not end with a null byte. |
| 274 | * |
| 275 | * os_printz(str) may be implemented as simply os_print(str, strlen(str)). |
| 276 | * |
| 277 | * The string is written in one of three ways, depending on the status mode |
| 278 | * set by os_status(): |
| 279 | * |
| 280 | * status mode == 0 -> write to main text window |
| 281 | *. status mode == 1 -> write to status line |
| 282 | *. anything else -> do not display the text at all |
| 283 | * |
| 284 | * Implementations are free to omit any status line support, in which case |
| 285 | * they should simply suppress all output when the status mode is anything |
| 286 | * other than zero. |
| 287 | * |
| 288 | * The following special characters must be recognized in the displayed |
| 289 | * text: |
| 290 | * |
| 291 | * '\n' - newline: end the current line and move the cursor to the start of |
| 292 | * the next line. If the status line is supported, and the current status |
| 293 | * mode is 1 (i.e., displaying in the status line), then two special rules |
| 294 | * apply to newline handling: newlines preceding any other text should be |
| 295 | * ignored, and a newline following any other text should set the status |
| 296 | * mode to 2, so that all subsequent output is suppressed until the status |
| 297 | * mode is changed with an explicit call by the client program to |
| 298 | * os_status(). |
| 299 | * |
| 300 | * '\r' - carriage return: end the current line and move the cursor back to |
| 301 | * the beginning of the current line. Subsequent output is expected to |
| 302 | * overwrite the text previously on this same line. The implementation |
| 303 | * may, if desired, IMMEDIATELY clear the previous text when the '\r' is |
| 304 | * written, rather than waiting for subsequent text to be displayed. |
| 305 | * |
| 306 | * All other characters may be assumed to be ordinary printing characters. |
| 307 | * The routine need not check for any other special characters. |
| 308 | * |
| 309 | */ |
| 310 | |
| 311 | void os_printz(const char *str) |
| 312 | { |
| 313 | os_print(str, strlen(str)); |
| 314 | } |
| 315 | |
| 316 | void os_print(const char *str, size_t len) |
| 317 | { |
| 318 | if (curwin == 0 && str) |
| 319 | os_put_buffer((unsigned char *)str, len); |
| 320 | |
| 321 | if (curwin == 1) |
| 322 | { |
| 323 | const char *p; |
| 324 | size_t rem, max; |
| 325 | |
| 326 | /* The string requires some fiddling for the status window */ |
| 327 | for (p = str, rem = len ; rem != 0 && *p == '\n'; p++, --rem) |
| 328 | ; |
| 329 | if (rem != 0 && p[rem-1] == '\n') |
| 330 | --rem; |
| 331 | |
| 332 | /* if that leaves anything, update the statusline */ |
| 333 | if (rem != 0) |
| 334 | { |
| 335 | max = sizeof(lbuf) - strlen(lbuf) - 1; |
| 336 | strncat(lbuf, p, rem > max ? max : rem); |
| 337 | os_status_redraw(); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /* |
| 344 | * Set the status line mode. There are three possible settings: |
| 345 | * |
| 346 | * 0 -> main text mode. In this mode, all subsequent text written with |
| 347 | * os_print() and os_printz() is to be displayed to the main text area. |
| 348 | * This is the normal mode that should be in effect initially. This mode |
| 349 | * stays in effect until an explicit call to os_status(). |
| 350 | * |
| 351 | * 1 -> statusline mode. In this mode, text written with os_print() and |
| 352 | * os_printz() is written to the status line, which is usually rendered as |
| 353 | * a one-line area across the top of the terminal screen or application |
| 354 | * window. In statusline mode, leading newlines ('\n' characters) are to |
| 355 | * be ignored, and any newline following any other character must change |
| 356 | * the mode to 2, as though os_status(2) had been called. |
| 357 | * |
| 358 | * 2 -> suppress mode. In this mode, all text written with os_print() and |
| 359 | * os_printz() must simply be ignored, and not displayed at all. This mode |
| 360 | * stays in effect until an explicit call to os_status(). |
| 361 | */ |
| 362 | |
| 363 | void os_status(int stat) |
| 364 | { |
| 365 | curwin = stat; |
| 366 | |
| 367 | if (stat == 1) |
| 368 | { |
| 369 | if (statuswin == NULL) |
| 370 | { |
| 371 | glk_stylehint_set(wintype_TextGrid, style_User1, stylehint_ReverseColor, 1); |
| 372 | statuswin = glk_window_open(mainwin, |
| 373 | winmethod_Above | winmethod_Fixed, 1, |
| 374 | wintype_TextGrid, 0); |
| 375 | } |
| 376 | strcpy(lbuf, ""); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /* get the status line mode */ |
| 381 | int os_get_status() |
| 382 | { |
| 383 | return curwin; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Set the score value. This displays the given score and turn counts on |
| 388 | * the status line. In most cases, these values are displayed at the right |
| 389 | * edge of the status line, in the format "score/turns", but the format is |
| 390 | * up to the implementation to determine. In most cases, this can simply |
| 391 | * be implemented as follows: |
| 392 | * |
| 393 | */ |
| 394 | void os_score(int score, int turncount) |
| 395 | { |
| 396 | char buf[40]; |
| 397 | sprintf(buf, "%d/%d", score, turncount); |
| 398 | os_strsc(buf); |
| 399 | } |
| 400 | |
| 401 | /* display a string in the score area in the status line */ |
| 402 | void os_strsc(const char *p) |
| 403 | { |
| 404 | snprintf(rbuf, sizeof rbuf, "%s", p); |
| 405 | os_status_redraw(); |
| 406 | } |
| 407 | |
| 408 | static void os_status_redraw(void) |
| 409 | { |
| 410 | char fmt[32]; |
| 411 | char buf[256]; |
| 412 | glui32 wid; |
| 413 | glui32 div; |
| 414 | int i; |
| 415 | |
| 416 | if (!statuswin) |
| 417 | return; |
| 418 | |
| 419 | glk_window_get_size(statuswin, &wid, NULL); |
| 420 | div = wid - strlen(rbuf) - 3; |
| 421 | |
| 422 | sprintf(fmt, " %%%ds %%s ", - (int)div); |
| 423 | sprintf(buf, fmt, lbuf, rbuf); |
| 424 | |
| 425 | glk_window_clear(statuswin); |
| 426 | glk_set_window(statuswin); |
| 427 | glk_set_style(style_User1); |
| 428 | os_put_buffer((unsigned char *)buf, strlen(buf)); |
| 429 | glk_set_window(mainwin); |
| 430 | } |
| 431 | |
| 432 | static void redraw_windows(void) |
| 433 | { |
| 434 | os_status_redraw(); |
| 435 | os_banners_redraw(); |
| 436 | } |
| 437 | |
| 438 | /* clear the screen */ |
| 439 | void oscls(void) |
| 440 | { |
| 441 | glk_window_clear(mainwin); |
| 442 | } |
| 443 | |
| 444 | // mapping of text attributes to Glk styles |
| 445 | static const glui32 attr_to_style[] = { |
| 446 | style_Normal, // nothing |
| 447 | style_Subheader, // bold |
| 448 | style_Emphasized, // italic |
| 449 | style_Alert, // italic + bold |
| 450 | style_Preformatted, // everything with monospace bit set maps to monospace |
| 451 | style_Preformatted, |
| 452 | style_Preformatted, |
| 453 | style_Preformatted |
| 454 | }; |
| 455 | static const glui32 attr_to_style_ext[] = { |
| 456 | style_Normal, // nothing |
| 457 | style_Subheader, // bold |
| 458 | style_Emphasized, // italic |
| 459 | style_Alert, // italic + bold |
| 460 | style_Preformatted, // monospace |
| 461 | style_User1, // monospace + bold |
| 462 | style_User2, // monospace + italic |
| 463 | style_Note // monospace + bold + italic |
| 464 | }; |
| 465 | |
| 466 | /* ------------------------------------------------------------------------ */ |
| 467 | /* |
| 468 | * Set text attributes. Text subsequently displayed through os_print() and |
| 469 | * os_printz() are to be displayed with the given attributes. |
| 470 | * |
| 471 | * 'attr' is a (bitwise-OR'd) combination of OS_ATTR_xxx values. A value |
| 472 | * of zero indicates normal text, with no extra attributes. |
| 473 | */ |
| 474 | void os_set_text_attr(int attr) |
| 475 | { |
| 476 | // If anyone adds more style attributes in the future, our array lookup |
| 477 | // will blow up, so... |
| 478 | assert(attr < 8); |
| 479 | curattr = attr; |
| 480 | if (use_more_text_styling) |
| 481 | glk_set_style(attr_to_style_ext[curattr]); |
| 482 | else |
| 483 | glk_set_style(attr_to_style[curattr]); |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Set the text foreground and background colors. This sets the text |
| 488 | * color for subsequent os_printf() and os_vprintf() calls. |
| 489 | * |
| 490 | * The background color can be OS_COLOR_TRANSPARENT, in which case the |
| 491 | * background color is "inherited" from the current screen background. |
| 492 | * Note that if the platform is capable of keeping old text for |
| 493 | * "scrollback," then the transparency should be a permanent attribute of |
| 494 | * the character - in other words, it should not be mapped to the current |
| 495 | * screen color in the scrollback buffer, because doing so would keep the |
| 496 | * current screen color even if the screen color changes in the future. |
| 497 | * |
| 498 | * Text color support is optional. If the platform doesn't support text |
| 499 | * colors, this can simply do nothing. If the platform supports text |
| 500 | * colors, but the requested color or attributes cannot be displayed, the |
| 501 | * implementation should use the best available approximation. |
| 502 | */ |
| 503 | void os_set_text_color(os_color_t fg, os_color_t bg) |
| 504 | { |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Set the screen background color. This sets the text color for the |
| 509 | * background of the screen. If possible, this should immediately redraw |
| 510 | * the main text area with this background color. The color is given as an |
| 511 | * OS_COLOR_xxx value. |
| 512 | * |
| 513 | * If the platform is capable of redisplaying the existing text, then any |
| 514 | * existing text that was originally displayed with 'transparent' |
| 515 | * background color should be redisplayed with the new screen background |
| 516 | * color. In other words, the 'transparent' background color of previously |
| 517 | * drawn text should be a permanent attribute of the character - the color |
| 518 | * should not be mapped on display to the then-current background color, |
| 519 | * because doing so would lose the transparency and thus retain the old |
| 520 | * screen color on a screen color change. |
| 521 | */ |
| 522 | void os_set_screen_color(os_color_t color) |
| 523 | { |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Set the game title. The output layer calls this routine when a game |
| 528 | * sets its title (via an HTML <title> tag, for example). If it's |
| 529 | * convenient to do so, the OS layer can use this string to set a window |
| 530 | * caption, or whatever else makes sense on each system. Most |
| 531 | * character-mode implementations will provide an empty implementation, |
| 532 | * since there's not usually any standard way to show the current |
| 533 | * application title on a character-mode display. |
| 534 | */ |
| 535 | void os_set_title(const char *title) |
| 536 | { |
| 537 | #ifdef GARGLK |
| 538 | garglk_set_story_title(title); |
| 539 | #endif |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Show the system-specific MORE prompt, and wait for the user to respond. |
| 544 | * Before returning, remove the MORE prompt from the screen. |
| 545 | * |
| 546 | * This routine is only used and only needs to be implemented when the OS |
| 547 | * layer takes responsibility for pagination; this will be the case on |
| 548 | * most systems that use proportionally-spaced (variable-pitch) fonts or |
| 549 | * variable-sized windows, since on such platforms the OS layer must do |
| 550 | * most of the formatting work, leaving the standard output layer unable |
| 551 | * to guess where pagination should occur. |
| 552 | * |
| 553 | * If the portable output formatter handles the MORE prompt, which is the |
| 554 | * usual case for character-mode or terminal-style implementations, this |
| 555 | * routine is not used and you don't need to provide an implementation. |
| 556 | * Note that HTML TADS provides an implementation of this routine, because |
| 557 | * the HTML renderer handles line breaking and thus must handle |
| 558 | * pagination. |
| 559 | */ |
| 560 | void os_more_prompt() |
| 561 | { |
| 562 | os_printz("\n[more]\n"); |
| 563 | os_waitc(); |
| 564 | } |
| 565 | |
| 566 | /* ------------------------------------------------------------------------ */ |
| 567 | /* |
| 568 | * User Input Routines |
| 569 | */ |
| 570 | |
| 571 | /* |
| 572 | * Ask the user for a filename, using a system-dependent dialog or other |
| 573 | * mechanism. Returns one of the OS_AFE_xxx status codes (see below). |
| 574 | * |
| 575 | * prompt_type is the type of prompt to provide -- this is one of the |
| 576 | * OS_AFP_xxx codes (see below). The OS implementation doesn't need to |
| 577 | * pay any attention to this parameter, but it can be used if desired to |
| 578 | * determine the type of dialog to present if the system provides |
| 579 | * different types of dialogs for different types of operations. |
| 580 | * |
| 581 | * file_type is one of the OSFTxxx codes for system file type. The OS |
| 582 | * implementation is free to ignore this information, but can use it to |
| 583 | * filter the list of files displayed if desired; this can also be used |
| 584 | * to apply a default suffix on systems that use suffixes to indicate |
| 585 | * file type. If OSFTUNK is specified, it means that no filtering |
| 586 | * should be performed, and no default suffix should be applied. |
| 587 | */ |
| 588 | int os_askfile(const char *prompt, char *fname_buf, int fname_buf_len, |
| 589 | int prompt_type, os_filetype_t file_type) |
| 590 | { |
| 591 | #ifndef GLKUNIX_FILEREF_GET_FILENAME |
| 592 | return OS_AFE_FAILURE; |
| 593 | #else |
| 594 | frefid_t fileref; |
| 595 | glui32 gprompt, gusage; |
| 596 | |
| 597 | if (prompt_type == OS_AFP_OPEN) |
| 598 | gprompt = filemode_Read; |
| 599 | else |
| 600 | gprompt = filemode_ReadWrite; |
| 601 | |
| 602 | if (file_type == OSFTSAVE || file_type == OSFTT3SAV) |
| 603 | gusage = fileusage_SavedGame; |
| 604 | else if (file_type == OSFTLOG || file_type == OSFTTEXT) |
| 605 | gusage = fileusage_Transcript; |
| 606 | else |
| 607 | gusage = fileusage_Data; |
| 608 | |
| 609 | fileref = glk_fileref_create_by_prompt(gusage, gprompt, 0); |
| 610 | if (fileref == NULL) |
| 611 | return OS_AFE_CANCEL; |
| 612 | |
| 613 | strcpy(fname_buf, glkunix_fileref_get_filename(fileref)); |
| 614 | |
| 615 | glk_fileref_destroy(fileref); |
| 616 | |
| 617 | return OS_AFE_SUCCESS; |
| 618 | #endif |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * Read a string of input. Fills in the buffer with a null-terminated |
| 623 | * string containing a line of text read from the standard input. The |
| 624 | * returned string should NOT contain a trailing newline sequence. On |
| 625 | * success, returns 'buf'; on failure, including end of file, returns a |
| 626 | * null pointer. |
| 627 | */ |
| 628 | unsigned char *os_gets(unsigned char *buf, size_t buflen) |
| 629 | { |
| 630 | event_t event; |
| 631 | |
| 632 | os_get_buffer(buf, buflen, 0); |
| 633 | |
| 634 | do |
| 635 | { |
| 636 | glk_select(&event); |
| 637 | if (event.type == evtype_Arrange) |
| 638 | redraw_windows(); |
| 639 | } |
| 640 | while (event.type != evtype_LineInput); |
| 641 | |
| 642 | return os_fill_buffer(buf, event.val1); |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Read a string of input with an optional timeout. This behaves like |
| 647 | * os_gets(), in that it allows the user to edit a line of text (ideally |
| 648 | * using the same editing keys that os_gets() does), showing the line of |
| 649 | * text under construction during editing. This routine differs from |
| 650 | * os_gets() in that it returns if the given timeout interval expires |
| 651 | * before the user presses Return (or the local equivalent). |
| 652 | * |
| 653 | * If the user presses Return before the timeout expires, we store the |
| 654 | * command line in the given buffer, just as os_gets() would, and we |
| 655 | * return OS_EVT_LINE. We also update the display in the same manner that |
| 656 | * os_gets() would, by moving the cursor to a new line and scrolling the |
| 657 | * displayed text as needed. |
| 658 | * |
| 659 | * If a timeout occurs before the user presses Return, we store the |
| 660 | * command line so far in the given buffer, statically store the cursor |
| 661 | * position, insert mode, buffer text, and anything else relevant to the |
| 662 | * editing state, and we return OS_EVT_TIMEOUT. |
| 663 | * |
| 664 | * If the implementation does not support the timeout operation, this |
| 665 | * routine should simply return OS_EVT_NOTIMEOUT immediately when called; |
| 666 | * the routine should not allow the user to perform any editing if the |
| 667 | * timeout is not supported. Callers must use the ordinary os_gets() |
| 668 | * routine, which has no timeout capabilities, if the timeout is not |
| 669 | * supported. |
| 670 | * |
| 671 | * When we return OS_EVT_TIMEOUT, the caller is responsible for doing one |
| 672 | * of two things. |
| 673 | * |
| 674 | * The first possibility is that the caller performs some work that |
| 675 | * doesn't require any display operations (in other words, the caller |
| 676 | * doesn't invoke os_printf, os_getc, or anything else that would update |
| 677 | * the display), and then calls os_gets_timeout() again. In this case, we |
| 678 | * will use the editing state that we statically stored before we returned |
| 679 | * OS_EVT_TIMEOUT to continue editing where we left off. This allows the |
| 680 | * caller to perform some computation in the middle of user command |
| 681 | * editing without interrupting the user - the extra computation is |
| 682 | * transparent to the user, because we act as though we were still in the |
| 683 | * midst of the original editing. |
| 684 | * |
| 685 | * The second possibility is that the caller wants to update the display. |
| 686 | * In this case, the caller must call os_gets_cancel() BEFORE making any |
| 687 | * display changes. Then, the caller must do any post-input work of its |
| 688 | * own, such as updating the display mode (for example, closing HTML font |
| 689 | * tags that were opened at the start of the input). The caller is now |
| 690 | * free to do any display work it wants. |
| 691 | * |
| 692 | * If we have information stored from a previous call that was interrupted |
| 693 | * by a timeout, and os_gets_cancel(TRUE) was never called, we will resume |
| 694 | * editing where we left off when the cancelled call returned; this means |
| 695 | * that we'll restore the cursor position, insertion state, and anything |
| 696 | * else relevant. Note that if os_gets_cancel(FALSE) was called, we must |
| 697 | * re-display the command line under construction, but if os_gets_cancel() |
| 698 | * was never called, we will not have to make any changes to the display |
| 699 | * at all. |
| 700 | * |
| 701 | * Note that when resuming an interrupted editing session (interrupted via |
| 702 | * os_gets_cancel()), the caller must re-display the prompt prior to |
| 703 | * invoking this routine. |
| 704 | * |
| 705 | * Note that we can return OS_EVT_EOF in addition to the other codes |
| 706 | * mentioned above. OS_EVT_EOF indicates that an error occurred reading, |
| 707 | * which usually indicates that the application is being terminated or |
| 708 | * that some hardware error occurred reading the keyboard. |
| 709 | * |
| 710 | * If 'use_timeout' is false, the timeout should be ignored. Without a |
| 711 | * timeout, the function behaves the same as os_gets(), except that it |
| 712 | * will resume editing of a previously-interrupted command line if |
| 713 | * appropriate. (This difference is why the timeout is optional: a caller |
| 714 | * might not need a timeout, but might still want to resume a previous |
| 715 | * input that did time out, in which case the caller would invoke this |
| 716 | * routine with use_timeout==FALSE. The regular os_gets() would not |
| 717 | * satisfy this need, because it cannot resume an interrupted input.) |
| 718 | */ |
| 719 | static char * timebuf = NULL; |
| 720 | static size_t timelen = 0; |
| 721 | |
| 722 | int os_gets_timeout(unsigned char *buf, size_t bufl, |
| 723 | unsigned long timeout_in_milliseconds, int use_timeout) |
| 724 | { |
| 725 | #if defined GLK_TIMERS && defined GLK_MODULE_LINE_ECHO |
| 726 | int timer = use_timeout ? timeout_in_milliseconds : 0; |
| 727 | int timeout = 0; |
| 728 | int initlen = 0; |
| 729 | event_t event; |
| 730 | |
| 731 | /* restore saved buffer contents */ |
| 732 | if (timebuf) |
| 733 | { |
| 734 | assert(timelen && timelen <= bufl); |
| 735 | memcpy(buf, timebuf, timelen); |
| 736 | initlen = timelen - 1; |
| 737 | buf[initlen] = 0; |
| 738 | free(timebuf); |
| 739 | timebuf = 0; |
| 740 | } |
| 741 | |
| 742 | /* start timer and turn off line echo */ |
| 743 | if (timer) |
| 744 | { |
| 745 | glk_request_timer_events(timer); |
| 746 | glk_set_echo_line_event(mainwin, 0); |
| 747 | } |
| 748 | |
| 749 | os_get_buffer(buf, bufl, initlen); |
| 750 | |
| 751 | do |
| 752 | { |
| 753 | glk_select(&event); |
| 754 | if (event.type == evtype_Arrange) |
| 755 | redraw_windows(); |
| 756 | else if (event.type == evtype_Timer && (timeout = 1)) |
| 757 | glk_cancel_line_event(mainwin, &event); |
| 758 | } |
| 759 | while (event.type != evtype_LineInput); |
| 760 | |
| 761 | unsigned char *res = os_fill_buffer(buf, event.val1); |
| 762 | |
| 763 | /* stop timer and turn on line echo */ |
| 764 | if (timer) |
| 765 | { |
| 766 | glk_request_timer_events(0); |
| 767 | glk_set_echo_line_event(mainwin, 1); |
| 768 | } |
| 769 | |
| 770 | /* save or print buffer contents */ |
| 771 | if (res && timer) |
| 772 | { |
| 773 | if (timeout) |
| 774 | { |
| 775 | timelen = strlen((char *)buf) + 1; |
| 776 | timebuf = malloc(timelen); |
| 777 | memcpy(timebuf, buf, timelen); |
| 778 | } |
| 779 | else |
| 780 | { |
| 781 | glk_set_style(style_Input); |
| 782 | os_print((char *)buf, strlen((char *)buf)); |
| 783 | os_print("\n", 1); |
| 784 | glk_set_style(style_Normal); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | return timeout ? OS_EVT_TIMEOUT : res ? OS_EVT_LINE : OS_EVT_EOF; |
| 789 | #else |
| 790 | return OS_EVT_NOTIMEOUT; |
| 791 | #endif |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * Cancel an interrupted editing session. This MUST be called if any |
| 796 | * output is to be displayed after a call to os_gets_timeout() returns |
| 797 | * OS_EVT_TIMEOUT. |
| 798 | * |
| 799 | * 'reset' indicates whether or not we will forget the input state saved |
| 800 | * by os_gets_timeout() when it last returned. If 'reset' is true, we'll |
| 801 | * clear the input state, so that the next call to os_gets_timeout() will |
| 802 | * start with an empty input buffer. If 'reset' is false, we will retain |
| 803 | * the previous input state, if any; this means that the next call to |
| 804 | * os_gets_timeout() will re-display the same input buffer that was under |
| 805 | * construction when it last returned. |
| 806 | * |
| 807 | * This routine need not be called if os_gets_timeout() is to be called |
| 808 | * again with no other output operations between the previous |
| 809 | * os_gets_timeout() call and the next one. |
| 810 | * |
| 811 | * Note that this routine needs only a trivial implementation when |
| 812 | * os_gets_timeout() is not supported (i.e., the function always returns |
| 813 | * OS_EVT_NOTIMEOUT). |
| 814 | */ |
| 815 | void os_gets_cancel(int reset) |
| 816 | { |
| 817 | #if defined GLK_TIMERS && defined GLK_MODULE_LINE_ECHO |
| 818 | if (timebuf) |
| 819 | { |
| 820 | glk_set_style(style_Input); |
| 821 | os_print(timebuf, strlen(timebuf)); |
| 822 | os_print("\n", 1); |
| 823 | glk_set_style(style_Normal); |
| 824 | |
| 825 | if (reset) |
| 826 | { |
| 827 | free(timebuf); |
| 828 | timebuf = 0; |
| 829 | } |
| 830 | } |
| 831 | #endif |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | * Read a character from the keyboard. For extended keystrokes, this |
| 836 | * function returns zero, and then returns the CMD_xxx code for the |
| 837 | * extended keystroke on the next call. For example, if the user |
| 838 | * presses the up-arrow key, the first call to os_getc() should return |
| 839 | * 0, and the next call should return CMD_UP. Refer to the CMD_xxx |
| 840 | * codes below. |
| 841 | * |
| 842 | * os_getc() should return a high-level, translated command code for |
| 843 | * command editing. This means that, where a functional interpretation |
| 844 | * of a key and the raw key-cap interpretation both exist as CMD_xxx |
| 845 | * codes, the functional interpretation should be returned. For |
| 846 | * example, on Unix, Ctrl-E is conventionally used in command editing to |
| 847 | * move to the end of the line, following Emacs key bindings. Hence, |
| 848 | * os_getc() should return CMD_END for this keystroke, rather than |
| 849 | * (CMD_CTRL + 'E' - 'A'), because CMD_END is the high-level command |
| 850 | * code for the operation. |
| 851 | * |
| 852 | * The translation ability of this function allows for system-dependent |
| 853 | * key mappings to functional meanings. |
| 854 | */ |
| 855 | static int glktotads(unsigned int key) |
| 856 | { |
| 857 | if (key < 256) |
| 858 | return key; |
| 859 | switch (key) |
| 860 | { |
| 861 | case keycode_Up: |
| 862 | return CMD_UP; |
| 863 | case keycode_Down: |
| 864 | return CMD_DOWN; |
| 865 | case keycode_Left: |
| 866 | return CMD_LEFT; |
| 867 | case keycode_Right: |
| 868 | return CMD_RIGHT; |
| 869 | case keycode_PageUp: |
| 870 | return CMD_PGUP; |
| 871 | case keycode_PageDown: |
| 872 | return CMD_PGDN; |
| 873 | case keycode_Home: |
| 874 | return CMD_HOME; |
| 875 | case keycode_End: |
| 876 | return CMD_END; |
| 877 | case keycode_Func1: |
| 878 | return CMD_F1; |
| 879 | case keycode_Func2: |
| 880 | return CMD_F2; |
| 881 | case keycode_Func3: |
| 882 | return CMD_F3; |
| 883 | case keycode_Func4: |
| 884 | return CMD_F4; |
| 885 | case keycode_Func5: |
| 886 | return CMD_F5; |
| 887 | case keycode_Func6: |
| 888 | return CMD_F6; |
| 889 | case keycode_Func7: |
| 890 | return CMD_F7; |
| 891 | case keycode_Func8: |
| 892 | return CMD_F8; |
| 893 | case keycode_Func9: |
| 894 | return CMD_F9; |
| 895 | case keycode_Func10: |
| 896 | return CMD_F10; |
| 897 | default: |
| 898 | return 0; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | static int bufchar = 0; |
| 903 | static int waitchar = 0; |
| 904 | static int timechar = 0; |
| 905 | |
| 906 | static int getglkchar(void) |
| 907 | { |
| 908 | event_t event; |
| 909 | |
| 910 | timechar = 0; |
| 911 | |
| 912 | glk_request_char_event(mainwin); |
| 913 | |
| 914 | do |
| 915 | { |
| 916 | glk_select(&event); |
| 917 | if (event.type == evtype_Arrange) |
| 918 | redraw_windows(); |
| 919 | else if (event.type == evtype_Timer) |
| 920 | timechar = 1; |
| 921 | } |
| 922 | while (event.type != evtype_CharInput && event.type != evtype_Timer); |
| 923 | |
| 924 | glk_cancel_char_event(mainwin); |
| 925 | |
| 926 | return timechar ? 0 : event.val1; |
| 927 | } |
| 928 | |
| 929 | int os_getc(void) |
| 930 | { |
| 931 | unsigned int c; |
| 932 | |
| 933 | if (bufchar) |
| 934 | { |
| 935 | c = bufchar; |
| 936 | bufchar = 0; |
| 937 | return c; |
| 938 | } |
| 939 | |
| 940 | c = waitchar ? waitchar : getglkchar(); |
| 941 | waitchar = 0; |
| 942 | |
| 943 | if (c == keycode_Return) |
| 944 | c = '\n'; |
| 945 | else if (c == keycode_Tab) |
| 946 | c = '\t'; |
| 947 | else if (c == keycode_Escape) |
| 948 | c = '\e'; |
| 949 | |
| 950 | if (c < 256) |
| 951 | return c; |
| 952 | |
| 953 | bufchar = glktotads(c); |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Read a character from the keyboard, following the same protocol as |
| 960 | * os_getc() for CMD_xxx codes (i.e., when an extended keystroke is |
| 961 | * encountered, os_getc_raw() returns zero, then returns the CMD_xxx code |
| 962 | * on the subsequent call). |
| 963 | * |
| 964 | * This function differs from os_getc() in that this function returns the |
| 965 | * low-level, untranslated key code whenever possible. This means that, |
| 966 | * when a functional interpretation of a key and the raw key-cap |
| 967 | * interpretation both exist as CMD_xxx codes, this function returns the |
| 968 | * key-cap interpretation. For the Unix Ctrl-E example in the comments |
| 969 | * describing os_getc() above, this function should return 5 (the ASCII |
| 970 | * code for Ctrl-E), because the CMD_CTRL interpretation is the low-level |
| 971 | * key code. |
| 972 | * |
| 973 | * This function should return all control keys using their ASCII control |
| 974 | * codes, whenever possible. Similarly, this function should return ASCII |
| 975 | * 27 for the Escape key, if possible. |
| 976 | * |
| 977 | * For keys for which there is no portable ASCII representation, this |
| 978 | * should return the CMD_xxx sequence. So, this function acts exactly the |
| 979 | * same as os_getc() for arrow keys, function keys, and other special keys |
| 980 | * that have no ASCII representation. This function returns a |
| 981 | * non-translated version ONLY when an ASCII representation exists - in |
| 982 | * practice, this means that this function and os_getc() vary only for |
| 983 | * CTRL keys and Escape. |
| 984 | */ |
| 985 | int os_getc_raw(void) |
| 986 | { |
| 987 | return os_getc(); |
| 988 | } |
| 989 | |
| 990 | /* wait for a character to become available from the keyboard */ |
| 991 | void os_waitc(void) |
| 992 | { |
| 993 | waitchar = getglkchar(); |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * Get an input event. The event types are shown above. If use_timeout |
| 998 | * is false, this routine should simply wait until one of the events it |
| 999 | * recognizes occurs, then return the appropriate information on the |
| 1000 | * event. If use_timeout is true, this routine should return |
| 1001 | * OS_EVT_TIMEOUT after the given number of milliseconds elapses if no |
| 1002 | * event occurs first. |
| 1003 | * |
| 1004 | * This function is not obligated to obey the timeout. If a timeout is |
| 1005 | * specified and it is not possible to obey the timeout, the function |
| 1006 | * should simply return OS_EVT_NOTIMEOUT. The trivial implementation |
| 1007 | * thus checks for a timeout, returns an error if specified, and |
| 1008 | * otherwise simply waits for the user to press a key. |
| 1009 | */ |
| 1010 | int os_get_event(unsigned long timeout_in_milliseconds, int use_timeout, |
| 1011 | os_event_info_t *info) |
| 1012 | { |
| 1013 | #ifdef GLK_TIMERS |
| 1014 | /* start timer */ |
| 1015 | int timer = use_timeout ? timeout_in_milliseconds : 0; |
| 1016 | if (timer) |
| 1017 | glk_request_timer_events(timer); |
| 1018 | #else |
| 1019 | /* we can't handle timeouts */ |
| 1020 | if (use_timeout) |
| 1021 | return OS_EVT_NOTIMEOUT; |
| 1022 | #endif |
| 1023 | |
| 1024 | /* get a key */ |
| 1025 | info->key[0] = os_getc_raw(); |
| 1026 | if (info->key[0] == 0 && timechar == 0) |
| 1027 | info->key[1] = os_getc_raw(); |
| 1028 | |
| 1029 | #ifdef GLK_TIMERS |
| 1030 | /* stop timer */ |
| 1031 | if (timer) |
| 1032 | glk_request_timer_events(0); |
| 1033 | #endif |
| 1034 | |
| 1035 | /* return the event */ |
| 1036 | return timechar ? OS_EVT_TIMEOUT : OS_EVT_KEY; |
| 1037 | } |
| 1038 |