| 1 | /****************************************************************************** |
| 2 | * * |
| 3 | * Copyright (C) 2006-2009 by Tor Andersson, Andrew Plotkin. * |
| 4 | * * |
| 5 | * This file is part of Gargoyle. * |
| 6 | * * |
| 7 | * Gargoyle is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU General Public License as published by * |
| 9 | * the Free Software Foundation; either version 2 of the License, or * |
| 10 | * (at your option) any later version. * |
| 11 | * * |
| 12 | * Gargoyle is distributed in the hope that it will be useful, * |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 | * GNU General Public License for more details. * |
| 16 | * * |
| 17 | * You should have received a copy of the GNU General Public License * |
| 18 | * along with Gargoyle; if not, write to the Free Software * |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 | * * |
| 21 | *****************************************************************************/ |
| 22 | |
| 23 | /* cgmisc.c: Miscellaneous functions for Glk API. |
| 24 | Designed by Andrew Plotkin <[email protected]> |
| 25 | http://www.eblong.com/zarf/glk/index.html |
| 26 | |
| 27 | Portions of this file are copyright 1998-2016 by Andrew Plotkin. |
| 28 | It is distributed under the MIT license; see the "LICENSE" file. |
| 29 | */ |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include "glk.h" |
| 35 | #include "garglk.h" |
| 36 | |
| 37 | #ifdef GARGLK |
| 38 | bool gli_terminated = false; |
| 39 | #endif |
| 40 | |
| 41 | static unsigned char char_tolower_table[256]; |
| 42 | static unsigned char char_toupper_table[256]; |
| 43 | |
| 44 | gidispatch_rock_t (*gli_register_obj)(void *obj, glui32 objclass) = NULL; |
| 45 | void (*gli_unregister_obj)(void *obj, glui32 objclass, |
| 46 | gidispatch_rock_t objrock) = NULL; |
| 47 | gidispatch_rock_t (*gli_register_arr)(void *array, glui32 len, |
| 48 | char *typecode) = NULL; |
| 49 | void (*gli_unregister_arr)(void *array, glui32 len, char *typecode, |
| 50 | gidispatch_rock_t objrock) = NULL; |
| 51 | |
| 52 | #ifndef GARGLK |
| 53 | /* This is needed to redisplay prompts properly after debug output. |
| 54 | Not interesting. */ |
| 55 | static int debug_output_counter = 0; |
| 56 | |
| 57 | static int perform_debug_command(char *cmd); |
| 58 | #endif |
| 59 | |
| 60 | void gli_initialize_misc() |
| 61 | { |
| 62 | int ix; |
| 63 | int res; |
| 64 | |
| 65 | /* Initialize the to-uppercase and to-lowercase tables. These should |
| 66 | *not* be localized to a platform-native character set! They are |
| 67 | intended to work on Latin-1 data, and the code below correctly |
| 68 | sets up the tables for that character set. */ |
| 69 | |
| 70 | for (ix=0; ix<256; ix++) { |
| 71 | char_toupper_table[ix] = ix; |
| 72 | char_tolower_table[ix] = ix; |
| 73 | } |
| 74 | for (ix=0; ix<256; ix++) { |
| 75 | if (ix >= 'A' && ix <= 'Z') { |
| 76 | res = ix + ('a' - 'A'); |
| 77 | } |
| 78 | else if (ix >= 0xC0 && ix <= 0xDE && ix != 0xD7) { |
| 79 | res = ix + 0x20; |
| 80 | } |
| 81 | else { |
| 82 | res = 0; |
| 83 | } |
| 84 | if (res) { |
| 85 | char_tolower_table[ix] = res; |
| 86 | char_toupper_table[res] = ix; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | void glk_exit() |
| 93 | { |
| 94 | #ifdef GARGLK |
| 95 | event_t event; |
| 96 | |
| 97 | if (!gli_wait_on_quit) { |
| 98 | winexit(); |
| 99 | } |
| 100 | |
| 101 | garglk_set_story_title("[ press any key to exit ]"); |
| 102 | |
| 103 | /* After a game exits, Gargoyle waits for a keypress, since |
| 104 | otherwise text might get lost. It signals this by changing the |
| 105 | titlebar (as above). However, in full screen mode, the titlebar |
| 106 | isn't visible, so after quitting a game, Gargoyle looks like it's |
| 107 | sitting there doing nothing. On exit, in full screen mode, add a |
| 108 | "press any key to exit" message to all text buffer windows in the |
| 109 | hopes that one is visible. In weird cases this won't work (e.g. |
| 110 | if there is no text buffer window), but the vast majority of the |
| 111 | time this should work fine. */ |
| 112 | if (garglk::winisfullscreen()) { |
| 113 | for (auto *win = glk_window_iterate(nullptr, 0); win != nullptr; win = glk_window_iterate(win, 0)) { |
| 114 | if (win->type == wintype_TextBuffer) { |
| 115 | glk_set_style_stream(glk_window_get_stream(win), style_Subheader); |
| 116 | glk_put_string_stream(glk_window_get_stream(win), const_cast<char *>("\n[Press any key to exit]")); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | gli_terminated = true; |
| 122 | |
| 123 | /* wait for gli_handle_input_key to exit() */ |
| 124 | while (true) { |
| 125 | glk_select(&event); |
| 126 | } |
| 127 | #else |
| 128 | if (gli_debugger) |
| 129 | gidebug_announce_cycle(gidebug_cycle_End); |
| 130 | exit(0); |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | void glk_set_interrupt_handler(void (*func)(void)) |
| 135 | { |
| 136 | /* This cheap library doesn't understand interrupts. */ |
| 137 | } |
| 138 | |
| 139 | unsigned char glk_char_to_lower(unsigned char ch) |
| 140 | { |
| 141 | return char_tolower_table[ch]; |
| 142 | } |
| 143 | |
| 144 | unsigned char glk_char_to_upper(unsigned char ch) |
| 145 | { |
| 146 | return char_toupper_table[ch]; |
| 147 | } |
| 148 | |
| 149 | #ifndef GARGLK |
| 150 | void glk_select(event_t *event) |
| 151 | { |
| 152 | window_t *win = gli_window_get(); |
| 153 | |
| 154 | gli_event_clearevent(event); |
| 155 | |
| 156 | if (gli_debugger) { |
| 157 | gidebug_announce_cycle(gidebug_cycle_InputWait); |
| 158 | if (debug_output_counter) { |
| 159 | debug_output_counter = 0; |
| 160 | printf(">"); |
| 161 | } |
| 162 | } |
| 163 | fflush(stdout); |
| 164 | |
| 165 | if (!win || !(win->char_request || win->line_request)) { |
| 166 | /* No input requests. This is legal, but a pity, because the |
| 167 | correct behavior is to wait forever. Bye bye. */ |
| 168 | while (1) { |
| 169 | getchar(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (win->char_request) { |
| 174 | char *res; |
| 175 | char buf[256]; |
| 176 | glui32 kval; |
| 177 | int len; |
| 178 | |
| 179 | /* How cheap are we? We don't want to fiddle with line |
| 180 | buffering, so we just accept an entire line (terminated by |
| 181 | return) and use the first key. Remember that return has to |
| 182 | be turned into a special keycode (and so would other keys, |
| 183 | if we could recognize them.) */ |
| 184 | |
| 185 | while (1) { |
| 186 | /* If debug mode is on, it may capture input, in which case |
| 187 | we need to loop until real input arrives. */ |
| 188 | |
| 189 | res = fgets(buf, 255, stdin); |
| 190 | if (!res) { |
| 191 | printf("\n<end of input>\n"); |
| 192 | glk_exit(); |
| 193 | } |
| 194 | |
| 195 | if (gli_debugger) { |
| 196 | if (buf[0] == '/') { |
| 197 | perform_debug_command(buf+1); |
| 198 | debug_output_counter = 0; |
| 199 | printf(">"); |
| 200 | continue; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* not debug input */ |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | if (!gli_utf8input) { |
| 209 | kval = buf[0]; |
| 210 | } |
| 211 | else { |
| 212 | int val; |
| 213 | val = strlen(buf); |
| 214 | if (val && (buf[val-1] == '\n' || buf[val-1] == '\r')) |
| 215 | val--; |
| 216 | len = gli_parse_utf8((unsigned char *)buf, val, &kval, 1); |
| 217 | if (!len) |
| 218 | kval = '\n'; |
| 219 | } |
| 220 | |
| 221 | if (kval == '\r' || kval == '\n') { |
| 222 | kval = keycode_Return; |
| 223 | } |
| 224 | else { |
| 225 | if (!win->char_request_uni && kval >= 0x100) |
| 226 | kval = '?'; |
| 227 | } |
| 228 | |
| 229 | win->char_request = FALSE; |
| 230 | event->type = evtype_CharInput; |
| 231 | event->win = win; |
| 232 | event->val1 = kval; |
| 233 | |
| 234 | } |
| 235 | else { |
| 236 | /* line_request */ |
| 237 | char *res; |
| 238 | char buf[256]; |
| 239 | int val; |
| 240 | glui32 ix; |
| 241 | |
| 242 | while (1) { |
| 243 | /* If debug mode is on, it may capture input, in which case |
| 244 | we need to loop until real input arrives. */ |
| 245 | |
| 246 | res = fgets(buf, 255, stdin); |
| 247 | if (!res) { |
| 248 | printf("\n<end of input>\n"); |
| 249 | glk_exit(); |
| 250 | } |
| 251 | |
| 252 | if (gli_debugger) { |
| 253 | if (buf[0] == '/') { |
| 254 | perform_debug_command(buf+1); |
| 255 | debug_output_counter = 0; |
| 256 | printf(">"); |
| 257 | continue; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* not debug input */ |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | val = strlen(buf); |
| 266 | if (val && (buf[val-1] == '\n' || buf[val-1] == '\r')) |
| 267 | val--; |
| 268 | |
| 269 | if (!gli_utf8input) { |
| 270 | if (val > win->linebuflen) |
| 271 | val = win->linebuflen; |
| 272 | if (!win->line_request_uni) { |
| 273 | memcpy(win->linebuf, buf, val); |
| 274 | } |
| 275 | else { |
| 276 | glui32 *destbuf = (glui32 *)win->linebuf; |
| 277 | for (ix=0; ix<val; ix++) |
| 278 | destbuf[ix] = (glui32)(((unsigned char *)buf)[ix]); |
| 279 | } |
| 280 | } |
| 281 | else { |
| 282 | glui32 ubuf[256]; |
| 283 | val = gli_parse_utf8((unsigned char *)buf, val, ubuf, 256); |
| 284 | if (val > win->linebuflen) |
| 285 | val = win->linebuflen; |
| 286 | if (!win->line_request_uni) { |
| 287 | unsigned char *destbuf = (unsigned char *)win->linebuf; |
| 288 | for (ix=0; ix<val; ix++) { |
| 289 | glui32 kval = ubuf[ix]; |
| 290 | if (kval >= 0x100) |
| 291 | kval = '?'; |
| 292 | destbuf[ix] = kval; |
| 293 | } |
| 294 | } |
| 295 | else { |
| 296 | /* We ought to perform Unicode Normalization Form C here. */ |
| 297 | glui32 *destbuf = (glui32 *)win->linebuf; |
| 298 | for (ix=0; ix<val; ix++) |
| 299 | destbuf[ix] = ubuf[ix]; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (!win->line_request_uni) { |
| 304 | if (win->echostr) { |
| 305 | gli_stream_echo_line(win->echostr, win->linebuf, val); |
| 306 | } |
| 307 | } |
| 308 | else { |
| 309 | if (win->echostr) { |
| 310 | gli_stream_echo_line_uni(win->echostr, win->linebuf, val); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (gli_unregister_arr) { |
| 315 | if (!win->line_request_uni) |
| 316 | (*gli_unregister_arr)(win->linebuf, win->linebuflen, |
| 317 | "&+#!Cn", win->inarrayrock); |
| 318 | else |
| 319 | (*gli_unregister_arr)(win->linebuf, win->linebuflen, |
| 320 | "&+#!Iu", win->inarrayrock); |
| 321 | } |
| 322 | |
| 323 | win->line_request = FALSE; |
| 324 | win->line_request_uni = FALSE; |
| 325 | win->linebuf = NULL; |
| 326 | event->type = evtype_LineInput; |
| 327 | event->win = win; |
| 328 | event->val1 = val; |
| 329 | } |
| 330 | |
| 331 | if (gli_debugger) |
| 332 | gidebug_announce_cycle(gidebug_cycle_InputAccept); |
| 333 | } |
| 334 | |
| 335 | void glk_select_poll(event_t *event) |
| 336 | { |
| 337 | gli_event_clearevent(event); |
| 338 | |
| 339 | /* This only checks for timer events at the moment, and we don't |
| 340 | support any, so I guess this is a pretty simple function. */ |
| 341 | } |
| 342 | |
| 343 | void glk_tick() |
| 344 | { |
| 345 | /* Do nothing. */ |
| 346 | } |
| 347 | |
| 348 | void glk_request_timer_events(glui32 millisecs) |
| 349 | { |
| 350 | /* Don't make me laugh. */ |
| 351 | } |
| 352 | #endif |
| 353 | |
| 354 | void gidispatch_set_object_registry( |
| 355 | gidispatch_rock_t (*regi)(void *obj, glui32 objclass), |
| 356 | void (*unregi)(void *obj, glui32 objclass, gidispatch_rock_t objrock)) |
| 357 | { |
| 358 | window_t *win; |
| 359 | stream_t *str; |
| 360 | fileref_t *fref; |
| 361 | |
| 362 | gli_register_obj = regi; |
| 363 | gli_unregister_obj = unregi; |
| 364 | |
| 365 | if (gli_register_obj) { |
| 366 | /* It's now necessary to go through all existing objects, and register |
| 367 | them. */ |
| 368 | for (win = glk_window_iterate(NULL, NULL); |
| 369 | win; |
| 370 | win = glk_window_iterate(win, NULL)) { |
| 371 | win->disprock = (*gli_register_obj)(win, gidisp_Class_Window); |
| 372 | } |
| 373 | for (str = glk_stream_iterate(NULL, NULL); |
| 374 | str; |
| 375 | str = glk_stream_iterate(str, NULL)) { |
| 376 | str->disprock = (*gli_register_obj)(str, gidisp_Class_Stream); |
| 377 | } |
| 378 | for (fref = glk_fileref_iterate(NULL, NULL); |
| 379 | fref; |
| 380 | fref = glk_fileref_iterate(fref, NULL)) { |
| 381 | fref->disprock = (*gli_register_obj)(fref, gidisp_Class_Fileref); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void gidispatch_set_retained_registry( |
| 387 | gidispatch_rock_t (*regi)(void *array, glui32 len, char *typecode), |
| 388 | void (*unregi)(void *array, glui32 len, char *typecode, |
| 389 | gidispatch_rock_t objrock)) |
| 390 | { |
| 391 | gli_register_arr = regi; |
| 392 | gli_unregister_arr = unregi; |
| 393 | } |
| 394 | |
| 395 | gidispatch_rock_t gidispatch_get_objrock(void *obj, glui32 objclass) |
| 396 | { |
| 397 | switch (objclass) { |
| 398 | case gidisp_Class_Window: |
| 399 | return ((window_t *)obj)->disprock; |
| 400 | case gidisp_Class_Stream: |
| 401 | return ((stream_t *)obj)->disprock; |
| 402 | case gidisp_Class_Fileref: |
| 403 | return ((fileref_t *)obj)->disprock; |
| 404 | #ifdef GARGLK |
| 405 | case gidisp_Class_Schannel: |
| 406 | return gli_sound_get_channel_disprock(static_cast<const channel_t *>(obj)); |
| 407 | #endif |
| 408 | default: { |
| 409 | gidispatch_rock_t dummy; |
| 410 | dummy.num = 0; |
| 411 | return dummy; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | #ifndef GARGLK |
| 417 | void gidispatch_set_autorestore_registry( |
| 418 | long (*locatearr)(void *array, glui32 len, char *typecode, |
| 419 | gidispatch_rock_t objrock, int *elemsizeref), |
| 420 | gidispatch_rock_t (*restorearr)(long bufkey, glui32 len, |
| 421 | char *typecode, void **arrayref)) |
| 422 | { |
| 423 | /* CheapGlk is not able to serialize its UI state. Therefore, it |
| 424 | does not have the capability of autosaving and autorestoring. |
| 425 | Therefore, it will never call these hooks. Therefore, we ignore |
| 426 | them and do nothing here. */ |
| 427 | } |
| 428 | |
| 429 | static int perform_debug_command(char *cmd) |
| 430 | { |
| 431 | char *allocbuf = NULL; |
| 432 | int res = 1; |
| 433 | |
| 434 | if (!gli_utf8input) { |
| 435 | /* The string is Latin-1. We should convert to UTF-8. We do |
| 436 | this in a very lazy way: alloc the largest possible buffer. */ |
| 437 | int len = 4*strlen(cmd)+4; |
| 438 | allocbuf = malloc(len); |
| 439 | char *ptr = allocbuf; |
| 440 | int count = 0; |
| 441 | while (*cmd) { |
| 442 | count += gli_encode_utf8(*cmd, ptr+count, len-count); |
| 443 | cmd++; |
| 444 | } |
| 445 | *(ptr+count) = '\0'; |
| 446 | cmd = allocbuf; |
| 447 | } |
| 448 | |
| 449 | int val = strlen(cmd); |
| 450 | if (val && (cmd[val-1] == '\n' || cmd[val-1] == '\r')) { |
| 451 | cmd[val-1] = '\0'; |
| 452 | } |
| 453 | |
| 454 | res = gidebug_perform_command(cmd); |
| 455 | |
| 456 | if (allocbuf) |
| 457 | free(allocbuf); |
| 458 | |
| 459 | return res; |
| 460 | } |
| 461 | |
| 462 | #if GIDEBUG_LIBRARY_SUPPORT |
| 463 | |
| 464 | void gidebug_output(char *text) |
| 465 | { |
| 466 | /* Send a line of text to the "debug console", if the user has |
| 467 | requested debugging mode. */ |
| 468 | /* (The text is UTF-8 whether or not the library output has requested |
| 469 | that encoding. The user will just have to cope.) */ |
| 470 | if (gli_debugger) { |
| 471 | printf("Debug: %s\n", text); |
| 472 | debug_output_counter++; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /* Block and wait for debug commands. The library will accept debug commands |
| 477 | until gidebug_perform_command() returns nonzero. |
| 478 | |
| 479 | This behaves a lot like glk_select(), except that it only handles debug |
| 480 | input, not any of the standard event types. |
| 481 | */ |
| 482 | void gidebug_pause() |
| 483 | { |
| 484 | if (!gli_debugger) { |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | gidebug_announce_cycle(gidebug_cycle_DebugPause); |
| 489 | debug_output_counter = 0; |
| 490 | printf(">>"); |
| 491 | fflush(stdout); |
| 492 | |
| 493 | while (1) { |
| 494 | char buf[256]; |
| 495 | char *res; |
| 496 | int unpause; |
| 497 | |
| 498 | res = fgets(buf, 255, stdin); |
| 499 | if (!res) { |
| 500 | printf("\n<end of input>\n"); |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | /* The slash is optional at the beginning of a line grabbed this |
| 505 | way. */ |
| 506 | if (res[0] == '/') |
| 507 | res++; |
| 508 | |
| 509 | unpause = perform_debug_command(res); |
| 510 | if (unpause) { |
| 511 | debug_output_counter = 0; |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | debug_output_counter = 0; |
| 516 | printf(">>"); |
| 517 | } |
| 518 | |
| 519 | gidebug_announce_cycle(gidebug_cycle_DebugUnpause); |
| 520 | } |
| 521 | |
| 522 | #endif /* GIDEBUG_LIBRARY_SUPPORT */ |
| 523 | #endif |