diff --git a/Makefile b/Makefile index 1faa069..f3bdf73 100644 --- a/Makefile +++ b/Makefile @@ -12,8 +12,8 @@ KBD_PATH ?= /dev/input/event1 MOUSE_PATH ?= /dev/input/mice C_DEFINES := -DKBD_PATH=\"$(KBD_PATH)\" -DMOUSE_PATH=\"$(MOUSE_PATH)\" -CC := ../ndk/bin/arm-linux-androideabi-gcc -CFLAGS := -Wall -Wextra -std=c99 +CC ?= cc +CFLAGS := -Wall -Wextra -static REL_FLAGS := -DNDEBUG -O2 DEB_FLAGS := -DDEBUG -O0 -g diff --git a/src/main.c b/src/main.c index a0a56f9..b4d1a26 100644 --- a/src/main.c +++ b/src/main.c @@ -60,7 +60,6 @@ typedef struct Mouse { typedef struct Input { int kbd_fd; int mouse_fd; - int home_fd; char map[KEY_MAX / 8 + 1]; u8 controller; Mouse mouse; @@ -77,22 +76,14 @@ init_input(void) { in.kbd_fd = -1; in.mouse_fd = -1; - // in.kbd_fd = open(KBD_PATH, O_RDONLY | O_NONBLOCK); - in.kbd_fd = open("/dev/input/event0", O_RDONLY | O_NONBLOCK); + in.kbd_fd = open(KBD_PATH, O_RDONLY | O_NONBLOCK); if (in.kbd_fd == -1) { // NOTE: Some applications may not require a keyboard so this is // optional, but we are still displaying an error. fprintf(stderr, "error: couldn't open keyboard %s: %s.\n", KBD_PATH, strerror(errno)); } - // NOTE: nook home and power buttons event handler. - in.home_fd = open("/dev/input/event1", O_RDONLY | O_NONBLOCK); - if (in.home_fd == -1) { - fprintf(stderr, "error: couldn't open home buttons %s: %s.\n", "/dev/input/event1", strerror(errno)); - } - - // in.mouse_fd = open(MOUSE_PATH, O_RDONLY | O_NONBLOCK); - in.mouse_fd = open("/dev/input/event2", O_RDONLY | O_NONBLOCK); + in.mouse_fd = open(MOUSE_PATH, O_RDONLY | O_NONBLOCK); if (in.mouse_fd == -1) { // NOTE: Some applications may not require a mouse so this is // optional, but we are still displaying an error. @@ -127,16 +118,16 @@ poll_mouse(void) { if (mouse_event.type == EV_REL) { if (mouse_event.code == REL_X) { in.mouse.x = CLAMP( - in.mouse.x / zoom + (s32)mouse_event.value, 0, (s32)screen_width); + in.mouse.x / (s32)zoom + (s32)mouse_event.value, 0, (s32)screen_width); } else if (mouse_event.code == REL_Y) { in.mouse.y = CLAMP( - in.mouse.y / zoom + (s32)mouse_event.value, 0, (s32)screen_height); + in.mouse.y / (s32)zoom + (s32)mouse_event.value, 0, (s32)screen_height); } } else if (mouse_event.type == EV_ABS) { if (mouse_event.code == ABS_X) { - in.mouse.x = CLAMP((s32)mouse_event.value / zoom, 0, (s32)screen_width); + in.mouse.x = CLAMP((s32)mouse_event.value / (s32)zoom, 0, (s32)screen_width); } else if (mouse_event.code == ABS_Y) { - in.mouse.y = CLAMP((s32)mouse_event.value / zoom, 0, (s32)screen_height); + in.mouse.y = CLAMP((s32)mouse_event.value / (s32)zoom, 0, (s32)screen_height); } } else if (mouse_event.type == EV_KEY) { switch (mouse_event.code) { @@ -154,39 +145,17 @@ poll_mouse(void) { in.mouse.buttons &= ~0x10; } } break; - default: { - // NOTE: nook press. - if (mouse_event.value == 1) { - in.mouse.buttons |= 0x01; - } else { - in.mouse.buttons &= ~0x01; - } - } break; + default: break; } } in.mouse.update = true; } } -void -poll_home(void) { - if (in.home_fd == -1) { - return; - } - - struct input_event event; - if (read(in.home_fd, &event, sizeof(event)) != -1) { - if (event.code == 102 && event.value == 1) { - exit(EXIT_SUCCESS); - } - } -} - void poll_input(void) { poll_keyboard(); poll_mouse(); - poll_home(); } void @@ -317,15 +286,6 @@ handle_keyboard(void) { default: break; } - // NOTE: Nook overrides. - switch (key_code) { - case 156: { rune = 0x10; } break; // top left - case 139: { rune = 0x20; } break; // bottom left - case 151: { rune = 0x40; } break; // top right - case 158: { rune = 0x80; } break; // bottom right - default: break; - } - if (rune) { controller_now |= rune; continue; @@ -567,8 +527,6 @@ main(int argc, char *argv[]) { // Main loop. Time frame_time = time_now(); - size_t frames_update = 0; - size_t frames_refresh = 0; while (true) { poll_input(); size_t elapsed = time_elapsed(frame_time); @@ -580,10 +538,6 @@ main(int argc, char *argv[]) { // Blit ppu.pixels to the framebuffer. blit_framebuffer(); - if (++frames_update > frames_per_update) { - write(fb_file, "0", 0); - frames_update = 0; - } frame_time = time_now(); } } diff --git a/src/ppu.c b/src/ppu.c index dde934e..4da3dd6 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -4,7 +4,7 @@ #include #include -// #include +#include #include #include "ppu.h" @@ -23,16 +23,12 @@ */ // Parameters. -static int zoom = 2; -static int frames_per_update = 5; -static int blits_per_refresh = 60 * 5; +static size_t zoom = 4; static size_t screen_width = 0; static size_t screen_height = 0; static size_t bpp = 0; static int fb_file = 0; -static int refresh_file = 0; -static frames_refresh = 0; static u8 *framebuffer = 0; @@ -121,51 +117,45 @@ set_tty(bool graphics) { 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); - // } - // } + 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. - fb_file = open("/dev/graphics/fb0", O_RDWR); + // TODO: Pass as macro or input parameter + fb_file = open("/dev/fb0", O_RDWR); if (fb_file <= 0) { fprintf(stderr, "error: couldn't open the framebuffer\n"); exit(EXIT_FAILURE); } - refresh_file = open("/sys/class/graphics/fb0/epd_refresh", O_RDWR); - if (refresh_file == 0) { - fprintf(stderr, "error: couldn't open the refresh file\n"); - exit(EXIT_FAILURE); - } - struct fb_var_screeninfo info; if (ioctl(fb_file, FBIOGET_VSCREENINFO, &info) != 0) { fprintf(stderr, "error: couldn't get the framebuffer size\n"); @@ -195,7 +185,7 @@ ppu_init(void) { } // Prepare TTY for graphics mode. - // set_tty(true); + set_tty(true); // Initialize default palette. palette[0] = 0x444444; @@ -210,11 +200,11 @@ ppu_init(void) { // Clear framebuffer. memset(framebuffer, 0xFF, len); - write(refresh_file, "1", 1); // Make sure we perform an initial screen drawing. reqdraw = 1; redraw_screen(); + close(fb_file); return 1; } @@ -231,7 +221,7 @@ blit_framebuffer(void) { for (size_t i = 0; i < screen_width; i++) { size_t idx = i + j * screen_width; if (bpp == 16) { - u16 *p = framebuffer; + u16 *p = (u16*)framebuffer; for (size_t zz = 0; zz < zoom; zz++) { size_t fb_idx = (i * zoom + j * screen_width * zoom * zoom + zz * screen_width * zoom); for (size_t z = 0; z < zoom; z++) { @@ -239,8 +229,13 @@ blit_framebuffer(void) { } } } else if (bpp == 32) { - u32 *p = framebuffer; - p[idx] = palette[pixels_fg[idx] << 2 | pixels_bg[idx]]; + u32 *p = (u32*)framebuffer; + for (size_t zz = 0; zz < zoom; zz++) { + size_t fb_idx = (i * zoom + j * screen_width * zoom * zoom + zz * screen_width * zoom); + for (size_t z = 0; z < zoom; z++) { + p[fb_idx + z] = palette[pixels_fg[idx] << 2 | pixels_bg[idx]]; + } + } } else { fprintf(stderr, "error: wrong bits per pixel (expected 16 or 32)\n"); exit(EXIT_FAILURE); @@ -249,12 +244,5 @@ blit_framebuffer(void) { } dirty_lines[j] = 0; } - // NOTE: Maybe this should happen on blit_framebuffer depending on - // the number of actual updates (uxn applications that don't modify - // the framebuffer shouldn't have to blink). - if (++frames_refresh > blits_per_refresh) { - write(refresh_file, "1", 1); - frames_refresh = 0; - } reqdraw = 0; }