From 144d8a10cba6d94847606ef423b3816ad25da14b Mon Sep 17 00:00:00 2001 From: Richard Cornwell Date: Mon, 24 Oct 2022 18:37:03 -0400 Subject: [PATCH] SCP: On Windows fixed writing to Console when Console is file. --- sim_console.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/sim_console.c b/sim_console.c index 3168ccb3..a7100c24 100644 --- a/sim_console.c +++ b/sim_console.c @@ -3670,11 +3670,21 @@ return (WAIT_OBJECT_0 == WaitForSingleObject (std_input, ms_timeout)); static uint8 out_buf[ESC_HOLD_MAX]; /* Buffered characters pending output */ static int32 out_ptr = 0; +static void sim_console_write(uint8 *outbuf, int32 outsz) +{ + DWORD unused; + DWORD mode; + + if (GetConsoleMode(std_output, &mode)) { + WriteConsoleA(std_output, outbuf, outsz, &unused, NULL); + } else { + BOOL result = WriteFile(std_output, outbuf, outsz, &unused, NULL); + } +} + static t_stat sim_out_hold_svc (UNIT *uptr) { -DWORD unused; - -WriteConsoleA(std_output, out_buf, out_ptr, &unused, NULL); +sim_console_write(out_buf, out_ptr); out_ptr = 0; return SCPE_OK; } @@ -3683,16 +3693,16 @@ return SCPE_OK; static t_stat sim_os_putchar (int32 c) { -DWORD unused; uint32 now; static uint32 last_bell_time; +uint8 ch = (c & 0xff); -if (c != 0177) { - switch (c) { +if (ch != 0177) { + switch (ch) { case BELL_CHAR: now = sim_os_msec (); if ((now - last_bell_time) > BELL_INTERVAL_MS) { - WriteConsoleA(std_output, &c, 1, &unused, NULL); + sim_console_write(&ch, 1); last_bell_time = now; } break; @@ -3701,26 +3711,26 @@ if (c != 0177) { case CSI_CHAR: case ESC_CHAR: if (out_ptr) { - WriteConsoleA(std_output, out_buf, out_ptr, &unused, NULL); + sim_console_write(out_buf, out_ptr); out_ptr = 0; sim_cancel (&out_hold_unit); } - out_buf[out_ptr++] = (uint8)c; + out_buf[out_ptr++] = ch; sim_activate_after (&out_hold_unit, ESC_HOLD_USEC_DELAY); out_hold_unit.action = &sim_out_hold_svc; break; default: if (out_ptr) { if (out_ptr >= ESC_HOLD_MAX) { /* Stop buffering if full */ - WriteConsoleA(std_output, out_buf, out_ptr, &unused, NULL); + sim_console_write(out_buf, out_ptr); out_ptr = 0; - WriteConsoleA(std_output, &c, 1, &unused, NULL); + sim_console_write(&ch, 1); } else - out_buf[out_ptr++] = (uint8)c; + out_buf[out_ptr++] = ch; } else - WriteConsoleA(std_output, &c, 1, &unused, NULL); + sim_console_write(&ch, 1); } } return SCPE_OK;