aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c60
1 files changed, 7 insertions, 53 deletions
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 {
60typedef struct Input { 60typedef 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
171void 155void
172poll_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
185void
186poll_input(void) { 156poll_input(void) {
187 poll_keyboard(); 157 poll_keyboard();
188 poll_mouse(); 158 poll_mouse();
189 poll_home();
190} 159}
191 160
192void 161void
@@ -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 }