Implemented terminal raw mode on POSIX.
h3rald h3rald@h3rald.com
Tue, 21 Jul 2026 16:18:32 +0200
3 files changed,
57 insertions(+),
1 deletions(-)
M
devices/terminal.c
→
devices/terminal.c
@@ -1,11 +1,65 @@
#include <stdio.h> #include "../xyw.h" +#ifndef _WIN32 +#include <termios.h> +#include <unistd.h> + +static struct termios terminal_orig_termios; +static int terminal_raw_mode_active = 0; +#endif + /* terminal device */ #define TERMINAL_INPUT 0x0 #define TERMINAL_OUTPUT 0x1 #define TERMINAL_ON_KEYPRESS 0x2 #define TERMINAL_ON_ARGUMENT 0x4 + +void terminal_init(xyw_byte *data) +{ + (void)data; +#ifndef _WIN32 + struct termios raw; + + if (!isatty(STDIN_FILENO)) + { + // Not a real terminal (e.g. piped input/tests) — nothing to save or set + return; + } + + if (tcgetattr(STDIN_FILENO, &terminal_orig_termios) != 0) + { + XYW_DBG("terminal: tcgetattr failed, raw mode not enabled\n"); + return; + } + + raw = terminal_orig_termios; + raw.c_lflag &= ~(ICANON | ECHO | ISIG); + // Read returns as soon as 1 byte is available, no inter-byte timeout + raw.c_cc[VMIN] = 1; + raw.c_cc[VTIME] = 0; + + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) != 0) + { + XYW_DBG("terminal: tcsetattr failed, raw mode not enabled\n"); + return; + } + + terminal_raw_mode_active = 1; +#endif +} + +void terminal_teardown(xyw_byte *data) +{ + (void)data; +#ifndef _WIN32 + if (terminal_raw_mode_active) + { + tcsetattr(STDIN_FILENO, TCSAFLUSH, &terminal_orig_termios); + terminal_raw_mode_active = 0; + } +#endif +} void terminal_output(xyw_byte *data, xyw_byte addr, xyw_byte *error) {
M
devices/terminal.h
→
devices/terminal.h
@@ -1,4 +1,6 @@
#include "../xyw.h" +void terminal_init(xyw_byte *data); void terminal_output(xyw_byte *data, xyw_byte addr, xyw_byte *error); xyw_byte terminal_input(xyw_byte *data, xyw_byte addr, xyw_byte *error); +void terminal_teardown(xyw_byte *data);
M
xywrun.c
→
xywrun.c
@@ -368,7 +368,7 @@ xyw_device xyw_devices[XYW_TOTAL_DEVICES] = {
// System device (not implemented yet) {&xyw_memory[0xff00], system_setup, system_input, 0, 0}, // Terminal device - {&xyw_memory[0xff10], 0, terminal_input, terminal_output, 0}, + {&xyw_memory[0xff10], 0, terminal_input, terminal_output, terminal_teardown}, // Clock device {&xyw_memory[0xff20], clock_setup, clock_input, clock_output, 0}, // File device