From a46fb7bd7eb1cf4c3237c2f3c944dfc82bcc2ee5 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 4 Mar 2022 19:01:53 +0100 Subject: [PATCH] Disable tty echo --- src/main.c | 5 --- src/ppu.c | 112 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 67 insertions(+), 50 deletions(-) diff --git a/src/main.c b/src/main.c index e213bbb..de566ac 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -6,10 +5,6 @@ #include #include -#include -#include -#include - #include "shorthand.h" #include "ppu.c" #include "uxn-fast.c" diff --git a/src/ppu.c b/src/ppu.c index b0ddc5f..ff93ec1 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -1,19 +1,25 @@ +#include #include +#include + +#include +#include +#include #include "ppu.h" /* -Copyright (c) 2021 Devine Lu Linvega -Copyright (c) 2021 Andrew Alderwick -Copyright (c) 2021 Bad Diode + Copyright (c) 2021 Devine Lu Linvega + Copyright (c) 2021 Andrew Alderwick + Copyright (c) 2021 Bad Diode -Permission to use, copy, modify, and distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. + Permission to use, copy, modify, and distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE. -*/ + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE. + */ static size_t screen_width = 0; static size_t screen_height = 0; @@ -37,12 +43,12 @@ static u8 blending[5][16] = { void ppu_pixel(u8 *layer, u16 x, u16 y, u8 color) { - if(x < screen_width && y < screen_height) { - Uint32 i = x + y *screen_width; - if(color != layer[i]) { - layer[i] = color; - } - } + if(x < screen_width && y < screen_height) { + Uint32 i = x + y *screen_width; + if(color != layer[i]) { + layer[i] = color; + } + } dirty_lines[y] |= 1; } @@ -54,9 +60,9 @@ ppu_1bpp(u8 *layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy) { u8 ch1 = (sprite[v] >> (7 - h)) & 0x1; if(ch1 || blending[4][color]) ppu_pixel(layer, - x + (flipx ? 7 - h : h), - y + (flipy ? 7 - v : v), - blending[ch1][color]); + x + (flipx ? 7 - h : h), + y + (flipy ? 7 - v : v), + blending[ch1][color]); } } @@ -70,9 +76,9 @@ ppu_2bpp(u8 *layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy) { u8 ch = ch1 + ch2 * 2; if(ch || blending[4][color]) ppu_pixel(layer, - x + (flipx ? 7 - h : h), - y + (flipy ? 7 - v : v), - blending[ch][color]); + x + (flipx ? 7 - h : h), + y + (flipy ? 7 - v : v), + blending[ch][color]); } } @@ -85,32 +91,48 @@ redraw_screen(void) { int ppu_init(void) { - // Open frambuffer and get the size. - int fb = open("/dev/fb0", O_RDWR); - if (fb <= 0) { - fprintf(stderr, "error: couldn't open the framebuffer\n"); - exit(EXIT_FAILURE); - } - struct fb_var_screeninfo info; - if (ioctl(fb, FBIOGET_VSCREENINFO, &info) != 0) { - fprintf(stderr, "error: couldn't get the framebuffer size\n"); - exit(EXIT_FAILURE); - } + // Open frambuffer and get the size. + int fb = open("/dev/fb0", O_RDWR); + if (fb <= 0) { + fprintf(stderr, "error: couldn't open the framebuffer\n"); + exit(EXIT_FAILURE); + } + struct fb_var_screeninfo info; + if (ioctl(fb, FBIOGET_VSCREENINFO, &info) != 0) { + fprintf(stderr, "error: couldn't get the framebuffer size\n"); + exit(EXIT_FAILURE); + } - // Mmap the framebuffer to a buffer object. - screen_width = info.xres; - screen_height = info.yres; - size_t len = 4 * screen_width * screen_height; - framebuffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0); - if (framebuffer == MAP_FAILED) { - fprintf(stderr, "error: couldn't mmap the framebuffer\n"); - exit(EXIT_FAILURE); - } + // Mmap the framebuffer to a buffer object. + screen_width = info.xres; + screen_height = info.yres; + size_t len = 4 * screen_width * screen_height; + framebuffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0); + if (framebuffer == MAP_FAILED) { + fprintf(stderr, "error: couldn't mmap the framebuffer\n"); + exit(EXIT_FAILURE); + } - // Allocate intermediate buffers. - pixels_fg = malloc(screen_width * screen_height); - pixels_bg = malloc(screen_width * screen_height); - dirty_lines = malloc(screen_height); + // Allocate intermediate buffers. + pixels_fg = malloc(screen_width * screen_height); + pixels_bg = malloc(screen_width * screen_height); + dirty_lines = malloc(screen_height); + if (pixels_fg == NULL || pixels_bg == NULL || dirty_lines == NULL) { + fprintf(stderr, "error: couldn't allocate memory for the ppu\n"); + 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); + } // Initialize default palette. palette[0] = 0x444444;