]> git.sesse.net Git - pistorm/commitdiff
[WIP] Keyboard event handling
authorbeeanyew <Bjorn Astrom>
Fri, 4 Dec 2020 21:56:10 +0000 (22:56 +0100)
committerbeeanyew <Bjorn Astrom>
Fri, 4 Dec 2020 21:56:10 +0000 (22:56 +0100)
emulator.c
input/input.c
input/input.h

index dbd4096b45adebfaf660442d7967910c3828ef0d..fe32b78b6c085bd55b8eacc384b0c2434a71b7d7 100644 (file)
@@ -89,7 +89,7 @@ char mouse_buttons = 0;
 #define KICKBASE 0xF80000
 #define KICKSIZE 0x7FFFF
 
-int mem_fd, mouse_fd = -1;
+int mem_fd, mouse_fd = -1, keyboard_fd = -1;
 int mem_fd_gpclk;
 int gayle_emulation_enabled = 1;
 void *gpio_map;
@@ -99,6 +99,7 @@ void *gpclk_map;
 unsigned int cpu_type = M68K_CPU_TYPE_68000;
 unsigned int loop_cycles = 300;
 struct emulator_config *cfg = NULL;
+char keyboard_file[256] = "/dev/input/event1";
 
 // I/O access
 volatile unsigned int *gpio;
@@ -237,6 +238,11 @@ int main(int argc, char *argv[]) {
     }
   }
 
+  keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
+  if (keyboard_fd == -1) {
+    printf("Failed to open keyboard event source.\n");
+  }
+
   sched_setscheduler(0, SCHED_FIFO, &priority);
   mlockall(MCL_CURRENT);  // lock in memory to keep us from paging out
 
@@ -349,6 +355,7 @@ int main(int argc, char *argv[]) {
           else
               printf("\n IPL Thread created successfully\n");
 */
+  char c = 0;
 
   m68k_pulse_reset();
   while (42) {
@@ -362,8 +369,7 @@ int main(int argc, char *argv[]) {
       m68k_execute(loop_cycles);
     
     // FIXME: Rework this to use keyboard events instead.
-    /*while (kbhit()) {
-      char c = getchar();
+    while (get_key_char(&c)) {
       if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
         kb_hook_enabled = 1;
         printf("Keyboard hook enabled.\n");
@@ -372,25 +378,27 @@ int main(int argc, char *argv[]) {
         kb_hook_enabled = 0;
         printf("Keyboard hook disabled.\n");
       }
-      if (c == cfg->mouse_toggle_key) {
-        mouse_hook_enabled ^= 1;
-        printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
-        mouse_dx = mouse_dy = mouse_buttons = 0;
-      }
-      if (c == 'r') {
-        cpu_emulation_running ^= 1;
-        printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
+      if (!kb_hook_enabled) {
+        if (c == cfg->mouse_toggle_key) {
+          mouse_hook_enabled ^= 1;
+          printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
+          mouse_dx = mouse_dy = mouse_buttons = 0;
+        }
+        if (c == 'r') {
+          cpu_emulation_running ^= 1;
+          printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
+        }
+        if (c == 'R') {
+          cpu_pulse_reset();
+          m68k_pulse_reset();
+          printf("CPU emulation reset.\n");
+        }
+        if (c == 'q') {
+          printf("Quitting and exiting emulator.\n");
+          goto stop_cpu_emulation;
+        }
       }
-      if (c == 'R') {
-        cpu_pulse_reset();
-        m68k_pulse_reset();
-        printf("CPU emulation reset.\n");
-      }
-      if (c == 'q') {
-        printf("Quitting and exiting emulator.\n");
-        goto stop_cpu_emulation;
-      }
-    }*/
+    }
 /*
     if (toggle == 1){
       srdata = read_reg();
index b0274e34366f08f4d6cb0aebb2467897e3a76d73..9958a990d3c27233d765d36b6b65751d26354116 100644 (file)
 #include <unistd.h>
 #include <linux/input.h>
 
-int kbhit()
+static int lshift = 0, rshift = 0, lctrl = 0, rctrl = 0, lalt = 0, altgr = 0;
+extern int mouse_fd;
+extern int keyboard_fd;
+
+enum keypress_type {
+  KEYPRESS_RELEASE,
+  KEYPRESS_PRESS,
+  KEYPRESS_REPEAT,
+};
+
+int handle_modifier(struct input_event *ev) {
+  int *target_modifier = NULL;
+  if (ev->value != KEYPRESS_REPEAT && (ev->code == KEY_LEFTSHIFT || ev->code == KEY_RIGHTSHIFT || ev->code == KEY_LEFTALT || ev->code == KEY_RIGHTALT || ev->code == KEY_LEFTCTRL || ev->code == KEY_RIGHTCTRL)) {
+    switch(ev->code) {
+      case KEY_LEFTSHIFT:
+        target_modifier = &lshift;
+        break;
+      case KEY_RIGHTSHIFT:
+        target_modifier = &rshift;
+        break;
+      case KEY_LEFTALT:
+        target_modifier = &lalt;
+        break;
+      case KEY_RIGHTALT:
+        target_modifier = &altgr;
+        break;
+      case KEY_LEFTCTRL:
+        target_modifier = &lshift;
+        break;
+      case KEY_RIGHTCTRL:
+        target_modifier = &rshift;
+        break;
+    }
+    *target_modifier = (ev->value == KEYPRESS_RELEASE) ? 0 : 1;
+    return 1;
+  }
+  else {
+    return 0;
+  }
+}
+
+#define KEYCASE(a, b, c)case a: return (lshift || rshift) ? c : b;
+
+char char_from_input_event(struct input_event *ev) {
+  switch(ev->code) {
+    KEYCASE(KEY_A, 'a', 'A');
+    KEYCASE(KEY_B, 'b', 'B');
+    KEYCASE(KEY_C, 'c', 'C');
+    KEYCASE(KEY_D, 'd', 'D');
+    KEYCASE(KEY_E, 'e', 'E');
+    KEYCASE(KEY_F, 'f', 'F');
+    KEYCASE(KEY_G, 'g', 'G');
+    KEYCASE(KEY_H, 'h', 'H');
+    KEYCASE(KEY_I, 'i', 'I');
+    KEYCASE(KEY_J, 'j', 'J');
+    KEYCASE(KEY_K, 'k', 'K');
+    KEYCASE(KEY_L, 'l', 'L');
+    KEYCASE(KEY_M, 'm', 'M');
+    KEYCASE(KEY_N, 'n', 'N');
+    KEYCASE(KEY_O, 'o', 'O');
+    KEYCASE(KEY_P, 'p', 'P');
+    KEYCASE(KEY_Q, 'q', 'Q');
+    KEYCASE(KEY_R, 'r', 'R');
+    KEYCASE(KEY_S, 's', 'S');
+    KEYCASE(KEY_T, 't', 'T');
+    KEYCASE(KEY_U, 'u', 'U');
+    KEYCASE(KEY_V, 'c', 'V');
+    KEYCASE(KEY_W, 'w', 'W');
+    KEYCASE(KEY_X, 'x', 'X');
+    KEYCASE(KEY_Y, 'y', 'Y');
+    KEYCASE(KEY_Z, 'z', 'Z');
+    KEYCASE(KEY_1, '1', '!');
+    KEYCASE(KEY_2, '2', '@');
+    KEYCASE(KEY_3, '3', '#');
+    KEYCASE(KEY_4, '4', '$');
+    KEYCASE(KEY_5, '5', '%');
+    KEYCASE(KEY_6, '6', '^');
+    KEYCASE(KEY_7, '7', '&');
+    KEYCASE(KEY_8, '8', '*');
+    KEYCASE(KEY_9, '9', '(');
+    KEYCASE(KEY_0, '0', ')');
+    KEYCASE(KEY_F12, 0x1B, 0x1B);
+    default:
+      return 0;
+  }
+}
+
+int get_key_char(char *c)
 {
+  if (keyboard_fd == -1)
+    return 0;
+
+  struct input_event ie;
+  while(read(keyboard_fd, &ie, sizeof(struct input_event)) != -1) {
+    if (ie.type == EV_KEY) {
+      if (handle_modifier(&ie))
+        continue;
+      char ret = char_from_input_event(&ie);
+      if (ret != 0) {
+        *c = ret;
+        return 1;
+      }
+    }
+  }
+
   return 0;
 }
 
-extern int mouse_fd;
-
 int get_mouse_status(char *x, char *y, char *b) {
   struct input_event ie;
   if (read(mouse_fd, &ie, sizeof(struct input_event)) != -1) {
index b6565bf90b9ad4ca04bca2e83698d2023784888a..ea2e482577967ef9cbecbed48086a4bc245a1837 100644 (file)
@@ -1,2 +1,2 @@
 int get_mouse_status(char *x, char *y, char *b);
-int kbhit();
+int get_key_char(char *c);