diff options
author | Bad Diode <bd@badd10de.dev> | 2022-10-13 18:08:53 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2022-10-13 18:08:53 +0200 |
commit | 3f90241e3b5caf0a273947d1b96e21d25d58f993 (patch) | |
tree | e2dedb7f9391c412766307aaecca7a9497f8d3fd | |
parent | abaeae55f15496b0f75d4f51d06d30d0505a0d7f (diff) | |
download | uxnfb-3f90241e3b5caf0a273947d1b96e21d25d58f993.tar.gz uxnfb-3f90241e3b5caf0a273947d1b96e21d25d58f993.zip |
Remove NST bits from this repo
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/main.c | 60 | ||||
-rw-r--r-- | src/ppu.c | 92 |
3 files changed, 49 insertions, 107 deletions
@@ -12,8 +12,8 @@ KBD_PATH ?= /dev/input/event1 | |||
12 | MOUSE_PATH ?= /dev/input/mice | 12 | MOUSE_PATH ?= /dev/input/mice |
13 | C_DEFINES := -DKBD_PATH=\"$(KBD_PATH)\" -DMOUSE_PATH=\"$(MOUSE_PATH)\" | 13 | C_DEFINES := -DKBD_PATH=\"$(KBD_PATH)\" -DMOUSE_PATH=\"$(MOUSE_PATH)\" |
14 | 14 | ||
15 | CC := ../ndk/bin/arm-linux-androideabi-gcc | 15 | CC ?= cc |
16 | CFLAGS := -Wall -Wextra -std=c99 | 16 | CFLAGS := -Wall -Wextra -static |
17 | 17 | ||
18 | REL_FLAGS := -DNDEBUG -O2 | 18 | REL_FLAGS := -DNDEBUG -O2 |
19 | DEB_FLAGS := -DDEBUG -O0 -g | 19 | DEB_FLAGS := -DDEBUG -O0 -g |
@@ -60,7 +60,6 @@ typedef struct Mouse { | |||
60 | typedef struct Input { | 60 | typedef struct Input { |
61 | int kbd_fd; | 61 | int kbd_fd; |
62 | int mouse_fd; | 62 | int mouse_fd; |
63 | int home_fd; | ||
64 | char map[KEY_MAX / 8 + 1]; | 63 | char map[KEY_MAX / 8 + 1]; |
65 | u8 controller; | 64 | u8 controller; |
66 | Mouse mouse; | 65 | Mouse mouse; |
@@ -77,22 +76,14 @@ init_input(void) { | |||
77 | in.kbd_fd = -1; | 76 | in.kbd_fd = -1; |
78 | in.mouse_fd = -1; | 77 | in.mouse_fd = -1; |
79 | 78 | ||
80 | // in.kbd_fd = open(KBD_PATH, O_RDONLY | O_NONBLOCK); | 79 | in.kbd_fd = open(KBD_PATH, O_RDONLY | O_NONBLOCK); |
81 | in.kbd_fd = open("/dev/input/event0", O_RDONLY | O_NONBLOCK); | ||
82 | if (in.kbd_fd == -1) { | 80 | if (in.kbd_fd == -1) { |
83 | // NOTE: Some applications may not require a keyboard so this is | 81 | // NOTE: Some applications may not require a keyboard so this is |
84 | // optional, but we are still displaying an error. | 82 | // optional, but we are still displaying an error. |
85 | fprintf(stderr, "error: couldn't open keyboard %s: %s.\n", KBD_PATH, strerror(errno)); | 83 | fprintf(stderr, "error: couldn't open keyboard %s: %s.\n", KBD_PATH, strerror(errno)); |
86 | } | 84 | } |
87 | 85 | ||
88 | // NOTE: nook home and power buttons event handler. | 86 | in.mouse_fd = open(MOUSE_PATH, O_RDONLY | O_NONBLOCK); |
89 | in.home_fd = open("/dev/input/event1", O_RDONLY | O_NONBLOCK); | ||
90 | if (in.home_fd == -1) { | ||
91 | fprintf(stderr, "error: couldn't open home buttons %s: %s.\n", "/dev/input/event1", strerror(errno)); | ||
92 | } | ||
93 | |||
94 | // in.mouse_fd = open(MOUSE_PATH, O_RDONLY | O_NONBLOCK); | ||
95 | in.mouse_fd = open("/dev/input/event2", O_RDONLY | O_NONBLOCK); | ||
96 | if (in.mouse_fd == -1) { | 87 | if (in.mouse_fd == -1) { |
97 | // NOTE: Some applications may not require a mouse so this is | 88 | // NOTE: Some applications may not require a mouse so this is |
98 | // optional, but we are still displaying an error. | 89 | // optional, but we are still displaying an error. |
@@ -127,16 +118,16 @@ poll_mouse(void) { | |||
127 | if (mouse_event.type == EV_REL) { | 118 | if (mouse_event.type == EV_REL) { |
128 | if (mouse_event.code == REL_X) { | 119 | if (mouse_event.code == REL_X) { |
129 | in.mouse.x = CLAMP( | 120 | in.mouse.x = CLAMP( |
130 | in.mouse.x / zoom + (s32)mouse_event.value, 0, (s32)screen_width); | 121 | in.mouse.x / (s32)zoom + (s32)mouse_event.value, 0, (s32)screen_width); |
131 | } else if (mouse_event.code == REL_Y) { | 122 | } else if (mouse_event.code == REL_Y) { |
132 | in.mouse.y = CLAMP( | 123 | in.mouse.y = CLAMP( |
133 | in.mouse.y / zoom + (s32)mouse_event.value, 0, (s32)screen_height); | 124 | in.mouse.y / (s32)zoom + (s32)mouse_event.value, 0, (s32)screen_height); |
134 | } | 125 | } |
135 | } else if (mouse_event.type == EV_ABS) { | 126 | } else if (mouse_event.type == EV_ABS) { |
136 | if (mouse_event.code == ABS_X) { | 127 | if (mouse_event.code == ABS_X) { |
137 | in.mouse.x = CLAMP((s32)mouse_event.value / zoom, 0, (s32)screen_width); | 128 | in.mouse.x = CLAMP((s32)mouse_event.value / (s32)zoom, 0, (s32)screen_width); |
138 | } else if (mouse_event.code == ABS_Y) { | 129 | } else if (mouse_event.code == ABS_Y) { |
139 | in.mouse.y = CLAMP((s32)mouse_event.value / zoom, 0, (s32)screen_height); | 130 | in.mouse.y = CLAMP((s32)mouse_event.value / (s32)zoom, 0, (s32)screen_height); |
140 | } | 131 | } |
141 | } else if (mouse_event.type == EV_KEY) { | 132 | } else if (mouse_event.type == EV_KEY) { |
142 | switch (mouse_event.code) { | 133 | switch (mouse_event.code) { |
@@ -154,14 +145,7 @@ poll_mouse(void) { | |||
154 | in.mouse.buttons &= ~0x10; | 145 | in.mouse.buttons &= ~0x10; |
155 | } | 146 | } |
156 | } break; | 147 | } break; |
157 | default: { | 148 | default: break; |
158 | // NOTE: nook press. | ||
159 | if (mouse_event.value == 1) { | ||
160 | in.mouse.buttons |= 0x01; | ||
161 | } else { | ||
162 | in.mouse.buttons &= ~0x01; | ||
163 | } | ||
164 | } break; | ||
165 | } | 149 | } |
166 | } | 150 | } |
167 | in.mouse.update = true; | 151 | in.mouse.update = true; |
@@ -169,24 +153,9 @@ poll_mouse(void) { | |||
169 | } | 153 | } |
170 | 154 | ||
171 | void | 155 | void |
172 | poll_home(void) { | ||
173 | if (in.home_fd == -1) { | ||
174 | return; | ||
175 | } | ||
176 | |||
177 | struct input_event event; | ||
178 | if (read(in.home_fd, &event, sizeof(event)) != -1) { | ||
179 | if (event.code == 102 && event.value == 1) { | ||
180 | exit(EXIT_SUCCESS); | ||
181 | } | ||
182 | } | ||
183 | } | ||
184 | |||
185 | void | ||
186 | poll_input(void) { | 156 | poll_input(void) { |
187 | poll_keyboard(); | 157 | poll_keyboard(); |
188 | poll_mouse(); | 158 | poll_mouse(); |
189 | poll_home(); | ||
190 | } | 159 | } |
191 | 160 | ||
192 | void | 161 | void |
@@ -317,15 +286,6 @@ handle_keyboard(void) { | |||
317 | default: break; | 286 | default: break; |
318 | } | 287 | } |
319 | 288 | ||
320 | // NOTE: Nook overrides. | ||
321 | switch (key_code) { | ||
322 | case 156: { rune = 0x10; } break; // top left | ||
323 | case 139: { rune = 0x20; } break; // bottom left | ||
324 | case 151: { rune = 0x40; } break; // top right | ||
325 | case 158: { rune = 0x80; } break; // bottom right | ||
326 | default: break; | ||
327 | } | ||
328 | |||
329 | if (rune) { | 289 | if (rune) { |
330 | controller_now |= rune; | 290 | controller_now |= rune; |
331 | continue; | 291 | continue; |
@@ -567,8 +527,6 @@ main(int argc, char *argv[]) { | |||
567 | 527 | ||
568 | // Main loop. | 528 | // Main loop. |
569 | Time frame_time = time_now(); | 529 | Time frame_time = time_now(); |
570 | size_t frames_update = 0; | ||
571 | size_t frames_refresh = 0; | ||
572 | while (true) { | 530 | while (true) { |
573 | poll_input(); | 531 | poll_input(); |
574 | size_t elapsed = time_elapsed(frame_time); | 532 | size_t elapsed = time_elapsed(frame_time); |
@@ -580,10 +538,6 @@ main(int argc, char *argv[]) { | |||
580 | 538 | ||
581 | // Blit ppu.pixels to the framebuffer. | 539 | // Blit ppu.pixels to the framebuffer. |
582 | blit_framebuffer(); | 540 | blit_framebuffer(); |
583 | if (++frames_update > frames_per_update) { | ||
584 | write(fb_file, "0", 0); | ||
585 | frames_update = 0; | ||
586 | } | ||
587 | frame_time = time_now(); | 541 | frame_time = time_now(); |
588 | } | 542 | } |
589 | } | 543 | } |
@@ -4,7 +4,7 @@ | |||
4 | 4 | ||
5 | #include <linux/fb.h> | 5 | #include <linux/fb.h> |
6 | #include <sys/ioctl.h> | 6 | #include <sys/ioctl.h> |
7 | // #include <sys/kd.h> | 7 | #include <sys/kd.h> |
8 | #include <sys/mman.h> | 8 | #include <sys/mman.h> |
9 | 9 | ||
10 | #include "ppu.h" | 10 | #include "ppu.h" |
@@ -23,16 +23,12 @@ | |||
23 | */ | 23 | */ |
24 | 24 | ||
25 | // Parameters. | 25 | // Parameters. |
26 | static int zoom = 2; | 26 | static size_t zoom = 4; |
27 | static int frames_per_update = 5; | ||
28 | static int blits_per_refresh = 60 * 5; | ||
29 | 27 | ||
30 | static size_t screen_width = 0; | 28 | static size_t screen_width = 0; |
31 | static size_t screen_height = 0; | 29 | static size_t screen_height = 0; |
32 | static size_t bpp = 0; | 30 | static size_t bpp = 0; |
33 | static int fb_file = 0; | 31 | static int fb_file = 0; |
34 | static int refresh_file = 0; | ||
35 | static frames_refresh = 0; | ||
36 | 32 | ||
37 | static u8 *framebuffer = 0; | 33 | static u8 *framebuffer = 0; |
38 | 34 | ||
@@ -121,51 +117,45 @@ set_tty(bool graphics) { | |||
121 | fprintf(stderr,"error: couldn't open tty\n"); | 117 | fprintf(stderr,"error: couldn't open tty\n"); |
122 | exit(EXIT_FAILURE); | 118 | exit(EXIT_FAILURE); |
123 | } | 119 | } |
124 | 120 | if (graphics) { | |
125 | // if (graphics) { | 121 | if (ioctl(tty, KDSETMODE, KD_GRAPHICS)) { |
126 | // if (ioctl(tty, KDSETMODE, KD_GRAPHICS)) { | 122 | fprintf(stderr,"error: setting graphics mode failed\n"); |
127 | // fprintf(stderr,"error: setting graphics mode failed\n"); | 123 | exit(EXIT_FAILURE); |
128 | // exit(EXIT_FAILURE); | 124 | } |
129 | // } | 125 | struct termios t; |
130 | // struct termios t; | 126 | if (tcgetattr(STDIN_FILENO, &t)) { |
131 | // if (tcgetattr(STDIN_FILENO, &t)) { | 127 | fprintf(stderr, "error: couldn't disable terminal echo\n"); |
132 | // fprintf(stderr, "error: couldn't disable terminal echo\n"); | 128 | exit(EXIT_FAILURE); |
133 | // exit(EXIT_FAILURE); | 129 | } |
134 | // } | 130 | prev_t = t; |
135 | // prev_t = t; | 131 | t.c_lflag &= ~((tcflag_t) ECHO); |
136 | // t.c_lflag &= ~((tcflag_t) ECHO); | 132 | if (tcsetattr(STDIN_FILENO, TCSANOW, &t)) { |
137 | // if (tcsetattr(STDIN_FILENO, TCSANOW, &t)) { | 133 | fprintf(stderr, "error: couldn't disable terminal echo\n"); |
138 | // fprintf(stderr, "error: couldn't disable terminal echo\n"); | 134 | exit(EXIT_FAILURE); |
139 | // exit(EXIT_FAILURE); | 135 | } |
140 | // } | 136 | } else { |
141 | // } else { | 137 | if (ioctl(tty, KDSETMODE, KD_TEXT)) { |
142 | // if (ioctl(tty, KDSETMODE, KD_TEXT)) { | 138 | fprintf(stderr,"error: setting text mode failed\n"); |
143 | // fprintf(stderr,"error: setting text mode failed\n"); | 139 | exit(EXIT_FAILURE); |
144 | // exit(EXIT_FAILURE); | 140 | } |
145 | // } | 141 | if (tcsetattr(STDIN_FILENO, TCSANOW, &prev_t)) { |
146 | // if (tcsetattr(STDIN_FILENO, TCSANOW, &prev_t)) { | 142 | fprintf(stderr, "error: couldn't restore terminal attributes\n"); |
147 | // fprintf(stderr, "error: couldn't restore terminal attributes\n"); | 143 | exit(EXIT_FAILURE); |
148 | // exit(EXIT_FAILURE); | 144 | } |
149 | // } | 145 | } |
150 | // } | ||
151 | close(tty); | 146 | close(tty); |
152 | } | 147 | } |
153 | 148 | ||
154 | int | 149 | int |
155 | ppu_init(void) { | 150 | ppu_init(void) { |
156 | // Open frambuffer and get the size. | 151 | // Open frambuffer and get the size. |
157 | fb_file = open("/dev/graphics/fb0", O_RDWR); | 152 | // TODO: Pass as macro or input parameter |
153 | fb_file = open("/dev/fb0", O_RDWR); | ||
158 | if (fb_file <= 0) { | 154 | if (fb_file <= 0) { |
159 | fprintf(stderr, "error: couldn't open the framebuffer\n"); | 155 | fprintf(stderr, "error: couldn't open the framebuffer\n"); |
160 | exit(EXIT_FAILURE); | 156 | exit(EXIT_FAILURE); |
161 | } | 157 | } |
162 | 158 | ||
163 | refresh_file = open("/sys/class/graphics/fb0/epd_refresh", O_RDWR); | ||
164 | if (refresh_file == 0) { | ||
165 | fprintf(stderr, "error: couldn't open the refresh file\n"); | ||
166 | exit(EXIT_FAILURE); | ||
167 | } | ||
168 | |||
169 | struct fb_var_screeninfo info; | 159 | struct fb_var_screeninfo info; |
170 | if (ioctl(fb_file, FBIOGET_VSCREENINFO, &info) != 0) { | 160 | if (ioctl(fb_file, FBIOGET_VSCREENINFO, &info) != 0) { |
171 | fprintf(stderr, "error: couldn't get the framebuffer size\n"); | 161 | fprintf(stderr, "error: couldn't get the framebuffer size\n"); |
@@ -195,7 +185,7 @@ ppu_init(void) { | |||
195 | } | 185 | } |
196 | 186 | ||
197 | // Prepare TTY for graphics mode. | 187 | // Prepare TTY for graphics mode. |
198 | // set_tty(true); | 188 | set_tty(true); |
199 | 189 | ||
200 | // Initialize default palette. | 190 | // Initialize default palette. |
201 | palette[0] = 0x444444; | 191 | palette[0] = 0x444444; |
@@ -210,11 +200,11 @@ ppu_init(void) { | |||
210 | 200 | ||
211 | // Clear framebuffer. | 201 | // Clear framebuffer. |
212 | memset(framebuffer, 0xFF, len); | 202 | memset(framebuffer, 0xFF, len); |
213 | write(refresh_file, "1", 1); | ||
214 | 203 | ||
215 | // Make sure we perform an initial screen drawing. | 204 | // Make sure we perform an initial screen drawing. |
216 | reqdraw = 1; | 205 | reqdraw = 1; |
217 | redraw_screen(); | 206 | redraw_screen(); |
207 | close(fb_file); | ||
218 | 208 | ||
219 | return 1; | 209 | return 1; |
220 | } | 210 | } |
@@ -231,7 +221,7 @@ blit_framebuffer(void) { | |||
231 | for (size_t i = 0; i < screen_width; i++) { | 221 | for (size_t i = 0; i < screen_width; i++) { |
232 | size_t idx = i + j * screen_width; | 222 | size_t idx = i + j * screen_width; |
233 | if (bpp == 16) { | 223 | if (bpp == 16) { |
234 | u16 *p = framebuffer; | 224 | u16 *p = (u16*)framebuffer; |
235 | for (size_t zz = 0; zz < zoom; zz++) { | 225 | for (size_t zz = 0; zz < zoom; zz++) { |
236 | size_t fb_idx = (i * zoom + j * screen_width * zoom * zoom + zz * screen_width * zoom); | 226 | size_t fb_idx = (i * zoom + j * screen_width * zoom * zoom + zz * screen_width * zoom); |
237 | for (size_t z = 0; z < zoom; z++) { | 227 | for (size_t z = 0; z < zoom; z++) { |
@@ -239,8 +229,13 @@ blit_framebuffer(void) { | |||
239 | } | 229 | } |
240 | } | 230 | } |
241 | } else if (bpp == 32) { | 231 | } else if (bpp == 32) { |
242 | u32 *p = framebuffer; | 232 | u32 *p = (u32*)framebuffer; |
243 | p[idx] = palette[pixels_fg[idx] << 2 | pixels_bg[idx]]; | 233 | for (size_t zz = 0; zz < zoom; zz++) { |
234 | size_t fb_idx = (i * zoom + j * screen_width * zoom * zoom + zz * screen_width * zoom); | ||
235 | for (size_t z = 0; z < zoom; z++) { | ||
236 | p[fb_idx + z] = palette[pixels_fg[idx] << 2 | pixels_bg[idx]]; | ||
237 | } | ||
238 | } | ||
244 | } else { | 239 | } else { |
245 | fprintf(stderr, "error: wrong bits per pixel (expected 16 or 32)\n"); | 240 | fprintf(stderr, "error: wrong bits per pixel (expected 16 or 32)\n"); |
246 | exit(EXIT_FAILURE); | 241 | exit(EXIT_FAILURE); |
@@ -249,12 +244,5 @@ blit_framebuffer(void) { | |||
249 | } | 244 | } |
250 | dirty_lines[j] = 0; | 245 | dirty_lines[j] = 0; |
251 | } | 246 | } |
252 | // NOTE: Maybe this should happen on blit_framebuffer depending on | ||
253 | // the number of actual updates (uxn applications that don't modify | ||
254 | // the framebuffer shouldn't have to blink). | ||
255 | if (++frames_refresh > blits_per_refresh) { | ||
256 | write(refresh_file, "1", 1); | ||
257 | frames_refresh = 0; | ||
258 | } | ||
259 | reqdraw = 0; | 247 | reqdraw = 0; |
260 | } | 248 | } |