Fix TTY cursor and graphics mode

This commit is contained in:
Bad Diode 2022-03-14 19:12:18 +01:00
parent d87d43ce22
commit 5813c416cf
3 changed files with 53 additions and 13 deletions

View File

@ -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:

View File

@ -1,4 +1,5 @@
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@ -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)));

View File

@ -4,6 +4,7 @@
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/kd.h>
#include <sys/mman.h>
#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;