]> git.sesse.net Git - pistorm/blobdiff - input/input.c
Add Meson build files.
[pistorm] / input / input.c
index d4d17f2fd63d09cc20fa7043931e44bc77b4473b..735744be9a29b644ed59df0bc2281ea78a2c3be9 100644 (file)
@@ -1,36 +1,43 @@
-#include <termios.h>
-#include <unistd.h>
+// SPDX-License-Identifier: MIT
+
+#include <linux/input.h>
+#include <sys/ioctl.h>
+#include <pthread.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
-#include <linux/input.h>
-#include "../platforms/platforms.h"
+#include <termios.h>
+#include <unistd.h>
+
+#include "platforms/platforms.h"
 #include "input.h"
 
 #define NONE 0x80
 
-static int lshift = 0, rshift = 0, lctrl = 0, rctrl = 0, lalt = 0, altgr = 0;
+static int lshift = 0, rshift = 0,/* lctrl = 0, rctrl = 0,*/ lalt = 0, altgr = 0;
 extern int mouse_fd;
 extern int keyboard_fd;
 
-char keymap_amiga[256] = { \
-/*      00    01    02    03    04    05    06    07    08    09    0A    0B    0C    0D    0E    0F */ \
-/*00*/ 0x80, 0x45, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x41, 0x42, \
-/*10*/ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x44, 0x63, 0x20, 0x21, \
-/*20*/ 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x00, 0x60, 0x2B, 0x31, 0x32, 0x33, 0x34, \
-/*30*/ 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x61, 0x5D, 0x64, 0x40, NONE, 0x50, 0x51, 0x52, 0x53, 0x54, \
-/*40*/ 0x55, 0x56, 0x57, 0x58, 0x69, 0x5B, NONE, 0x3D, 0x3E, 0x3F, 0x4A, 0x2D, 0x2E, 0x2F, 0x4E, 0x1D, \
-/*50*/ 0x1E, 0x1F, 0x0F, 0x3C, NONE, NONE, 0x30, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, \
-/*60*/ 0x43, NONE, 0x5C, NONE, NONE, NONE, 0x5F, 0x4C, 0x5A, 0x4F, 0x4E, NONE, 0x4D, NONE, 0x0D, 0x46, \
-/*70*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, 0x66, 0x67, NONE, \
-/*80*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, 0x67, NONE, NONE, NONE, NONE, \
-/*90*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, \
-/*A0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, \
-/*B0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, \
-/*C0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, \
-/*D0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, \
-/*E0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, \
-/*F0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE }; \
+// n.b. $fe and $ff are mapped to newmouse standard wheel up/down keycodes, nonexistant on amiga keyboards
+char keymap_amiga[256] = {
+/*      00    01    02    03    04    05    06    07    08    09    0A    0B    0C    0D    0E    0F */
+/*00*/ 0x80, 0x45, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x41, 0x42,
+/*10*/ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x44, 0x63, 0x20, 0x21,
+/*20*/ 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x00, 0x60, 0x2B, 0x31, 0x32, 0x33, 0x34,
+/*30*/ 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x61, 0x5D, 0x64, 0x40, NONE, 0x50, 0x51, 0x52, 0x53, 0x54,
+/*40*/ 0x55, 0x56, 0x57, 0x58, 0x59, 0x5B, NONE, 0x3D, 0x3E, 0x3F, 0x4A, 0x2D, 0x2E, 0x2F, 0x5E, 0x1D,
+/*50*/ 0x1E, 0x1F, 0x0F, 0x3C, NONE, NONE, 0x30, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
+/*60*/ 0x43, NONE, 0x5C, NONE, 0x65, NONE, 0x5F, 0x4C, 0x5A, 0x4F, 0x4E, NONE, 0x4D, NONE, 0x0D, 0x46,
+/*70*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, 0x66, 0x67, 0x67,
+/*80*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
+/*90*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
+/*A0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
+/*B0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
+/*C0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
+/*D0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
+/*E0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
+/*F0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, 0x7A, 0x7B,
+};
 
 int handle_modifier(struct input_event *ev) {
   int *target_modifier = NULL;
@@ -65,6 +72,12 @@ int handle_modifier(struct input_event *ev) {
 
 #define KEYCASE(a, b, c)case a: return (lshift || rshift) ? c : b;
 
+/**
+ * translates keycodes back into a simpler enumerable value for handling emulator command events
+ *
+ * @param *struct/input_event  ev  pointer to input layer event structure
+ * @return char
+ */
 char char_from_input_event(struct input_event *ev) {
   switch(ev->code) {
     KEYCASE(KEY_A, 'a', 'A');
@@ -104,6 +117,7 @@ char char_from_input_event(struct input_event *ev) {
     KEYCASE(KEY_9, '9', '(');
     KEYCASE(KEY_0, '0', ')');
     KEYCASE(KEY_F12, 0x1B, 0x1B);
+    KEYCASE(KEY_PAUSE, 0x01, 0x01);
     default:
       return 0;
   }
@@ -129,16 +143,18 @@ int get_key_char(char *c, char *code, char *event_type)
   return 0;
 }
 
-uint16_t mouse_x = 0, mouse_y = 0, mouse_b = 0;
+uint8_t mouse_x = 0, mouse_y = 0;
 
-int get_mouse_status(char *x, char *y, char *b) {
-  struct input_event ie;
-  if (read(mouse_fd, &ie, sizeof(struct input_event)) != -1) {
-    *b = ((char *)&ie)[0];
-    mouse_x += ((char *)&ie)[1];
+int get_mouse_status(uint8_t *x, uint8_t *y, uint8_t *b, uint8_t *e) {
+  uint8_t mouse_ev[4];
+  if (read(mouse_fd, &mouse_ev, 4) != -1) {
+    *b = ((uint8_t *)&mouse_ev)[0];
+    *e = ((uint8_t *)&mouse_ev)[3];
+
+    mouse_x += ((uint8_t *)&mouse_ev)[1];
     *x = mouse_x;
-    mouse_y += (-((char *)&ie)[2]);
-    *y = mouse_y; //-((char *)&ie)[2];
+    mouse_y += (-((uint8_t *)&mouse_ev)[2]);
+    *y = mouse_y;
     return 1;
   }
 
@@ -170,7 +186,7 @@ int queue_keypress(uint8_t keycode, uint8_t event_type, uint8_t platform) {
   if (keymap != NULL) {
     if (keymap[keycode] != NONE) {
       if (queued_keypresses < 255) {
-        //printf("Keypress queued, matched %.2X to host key code %.2X\n", keycode, keymap[keycode]);
+        // printf("Keypress queued, matched %.2X to host key code %.2X\n", keycode, keymap[keycode]);
         queued_keys[queue_output_pos] = keymap[keycode];
         queued_events[queue_output_pos] = event_type;
         queue_output_pos++;
@@ -198,3 +214,15 @@ void pop_queued_key(uint8_t *c, uint8_t *t) {
   queued_keypresses--;
   return;
 }
+
+int grab_device(int fd) {
+  int rc = 0;
+  rc = ioctl(fd, EVIOCGRAB, (void *)1);
+  return rc;
+}
+
+int release_device(int fd) {
+  int rc = 0;
+  rc = ioctl(fd, EVIOCGRAB, (void *)0);
+  return rc;
+}