From 5813c416cff70e0b773e5151f07bf037fe692679 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Mon, 14 Mar 2022 19:12:18 +0100 Subject: [PATCH] Fix TTY cursor and graphics mode --- Makefile | 2 -- src/main.c | 10 ++++++++++ src/ppu.c | 54 +++++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 8c56874..37b3802 100644 --- a/Makefile +++ b/Makefile @@ -48,8 +48,6 @@ $(UXN_ROM): $(UXN_ASM) ./$(UXN_ASM) $(TAL_SRC) $(UXN_ROM) run: $(BIN) $(UXN_ROM) - # NOTE: This should probably be done on the C code. - # echo 0 > /sys/class/graphics/fbcon/cursor_blink ./$(BIN) $(UXN_ROM) clean: diff --git a/src/main.c b/src/main.c index fcd42a4..29c4d5e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -24,6 +25,13 @@ static Device *devmouse; typedef struct timespec Time; +void +halt(int stub) { + (void)stub; + set_tty(false); + exit(EXIT_SUCCESS); +} + Time time_now(){ struct timespec t; @@ -446,6 +454,8 @@ load_rom(char *file_name) { void init_uxn(Uxn *u, char *file_name) { + signal(SIGINT, halt); + // Setup UXN memory. uxn_boot(u, calloc(0x10000, sizeof(u8))); diff --git a/src/ppu.c b/src/ppu.c index ff93ec1..aa99ed8 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -4,6 +4,7 @@ #include #include +#include #include #include "ppu.h" @@ -89,6 +90,46 @@ redraw_screen(void) { } } +static struct termios prev_t; + +void +set_tty(bool graphics) { + // Disable TTY echo and set graphics mode. + int tty = open("/dev/tty0", O_RDWR); + if (!tty) { + fprintf(stderr,"error: couldn't open tty\n"); + exit(EXIT_FAILURE); + } + + if (graphics) { + if (ioctl(tty, KDSETMODE, KD_GRAPHICS)) { + fprintf(stderr,"error: setting graphics mode failed\n"); + exit(EXIT_FAILURE); + } + struct termios t; + if (tcgetattr(STDIN_FILENO, &t)) { + fprintf(stderr, "error: couldn't disable terminal echo\n"); + exit(EXIT_FAILURE); + } + prev_t = t; + t.c_lflag &= ~((tcflag_t) ECHO); + if (tcsetattr(STDIN_FILENO, TCSANOW, &t)) { + fprintf(stderr, "error: couldn't disable terminal echo\n"); + exit(EXIT_FAILURE); + } + } else { + if (ioctl(tty, KDSETMODE, KD_TEXT)) { + fprintf(stderr,"error: setting text mode failed\n"); + exit(EXIT_FAILURE); + } + if (tcsetattr(STDIN_FILENO, TCSANOW, &prev_t)) { + fprintf(stderr, "error: couldn't restore terminal attributes\n"); + exit(EXIT_FAILURE); + } + } + close(tty); +} + int ppu_init(void) { // Open frambuffer and get the size. @@ -122,17 +163,8 @@ ppu_init(void) { exit(EXIT_FAILURE); } - // Disable echo. - struct termios t; - if (tcgetattr(STDIN_FILENO, &t)) { - fprintf(stderr, "error: couldn't disable terminal echo\n"); - exit(EXIT_FAILURE); - } - t.c_lflag &= ~((tcflag_t) ECHO); - if (tcsetattr(STDIN_FILENO, TCSANOW, &t)) { - fprintf(stderr, "error: couldn't disable terminal echo\n"); - exit(EXIT_FAILURE); - } + // Prepare TTY for graphics mode. + set_tty(true); // Initialize default palette. palette[0] = 0x444444;