1 #include <linux/input.h>
10 #include "platforms/platforms.h"
15 static int lshift = 0, rshift = 0,/* lctrl = 0, rctrl = 0,*/ lalt = 0, altgr = 0;
17 extern int keyboard_fd;
19 // n.b. $fe and $ff are mapped to newmouse standard wheel up/down keycodes, nonexistant on amiga keyboards
20 char keymap_amiga[256] = {
21 /* 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F */
22 /*00*/ 0x80, 0x45, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x41, 0x42,
23 /*10*/ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x44, 0x63, 0x20, 0x21,
24 /*20*/ 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x00, 0x60, 0x2B, 0x31, 0x32, 0x33, 0x34,
25 /*30*/ 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x61, 0x5D, 0x64, 0x40, NONE, 0x50, 0x51, 0x52, 0x53, 0x54,
26 /*40*/ 0x55, 0x56, 0x57, 0x58, 0x69, 0x5B, NONE, 0x3D, 0x3E, 0x3F, 0x4A, 0x2D, 0x2E, 0x2F, 0x4E, 0x1D,
27 /*50*/ 0x1E, 0x1F, 0x0F, 0x3C, NONE, NONE, 0x30, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
28 /*60*/ 0x43, NONE, 0x5C, NONE, NONE, NONE, 0x5F, 0x4C, 0x5A, 0x4F, 0x4E, NONE, 0x4D, NONE, 0x0D, 0x46,
29 /*70*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, 0x66, 0x67, 0x67,
30 /*80*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
31 /*90*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
32 /*A0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
33 /*B0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
34 /*C0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
35 /*D0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
36 /*E0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
37 /*F0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, 0x7A, 0x7B,
40 int handle_modifier(struct input_event *ev) {
41 int *target_modifier = NULL;
42 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)) {
45 target_modifier = &lshift;
48 target_modifier = &rshift;
51 target_modifier = &lalt;
54 target_modifier = &altgr;
57 target_modifier = &lshift;
60 target_modifier = &rshift;
63 *target_modifier = (ev->value == KEYPRESS_RELEASE) ? 0 : 1;
71 #define KEYCASE(a, b, c)case a: return (lshift || rshift) ? c : b;
74 * translates keycodes back into a simpler enumerable value for handling emulator command events
76 * @param *struct/input_event ev pointer to input layer event structure
79 char char_from_input_event(struct input_event *ev) {
81 KEYCASE(KEY_A, 'a', 'A');
82 KEYCASE(KEY_B, 'b', 'B');
83 KEYCASE(KEY_C, 'c', 'C');
84 KEYCASE(KEY_D, 'd', 'D');
85 KEYCASE(KEY_E, 'e', 'E');
86 KEYCASE(KEY_F, 'f', 'F');
87 KEYCASE(KEY_G, 'g', 'G');
88 KEYCASE(KEY_H, 'h', 'H');
89 KEYCASE(KEY_I, 'i', 'I');
90 KEYCASE(KEY_J, 'j', 'J');
91 KEYCASE(KEY_K, 'k', 'K');
92 KEYCASE(KEY_L, 'l', 'L');
93 KEYCASE(KEY_M, 'm', 'M');
94 KEYCASE(KEY_N, 'n', 'N');
95 KEYCASE(KEY_O, 'o', 'O');
96 KEYCASE(KEY_P, 'p', 'P');
97 KEYCASE(KEY_Q, 'q', 'Q');
98 KEYCASE(KEY_R, 'r', 'R');
99 KEYCASE(KEY_S, 's', 'S');
100 KEYCASE(KEY_T, 't', 'T');
101 KEYCASE(KEY_U, 'u', 'U');
102 KEYCASE(KEY_V, 'c', 'V');
103 KEYCASE(KEY_W, 'w', 'W');
104 KEYCASE(KEY_X, 'x', 'X');
105 KEYCASE(KEY_Y, 'y', 'Y');
106 KEYCASE(KEY_Z, 'z', 'Z');
107 KEYCASE(KEY_1, '1', '!');
108 KEYCASE(KEY_2, '2', '@');
109 KEYCASE(KEY_3, '3', '#');
110 KEYCASE(KEY_4, '4', '$');
111 KEYCASE(KEY_5, '5', '%');
112 KEYCASE(KEY_6, '6', '^');
113 KEYCASE(KEY_7, '7', '&');
114 KEYCASE(KEY_8, '8', '*');
115 KEYCASE(KEY_9, '9', '(');
116 KEYCASE(KEY_0, '0', ')');
117 KEYCASE(KEY_F12, 0x1B, 0x1B);
118 KEYCASE(KEY_PAUSE, 0x01, 0x01);
124 int get_key_char(char *c, char *code, char *event_type)
126 if (keyboard_fd == -1)
129 struct input_event ie;
130 while(read(keyboard_fd, &ie, sizeof(struct input_event)) != -1) {
131 if (ie.type == EV_KEY) {
132 handle_modifier(&ie);
133 char ret = char_from_input_event(&ie);
136 *event_type = ie.value;
144 uint8_t mouse_x = 0, mouse_y = 0;
146 int get_mouse_status(uint8_t *x, uint8_t *y, uint8_t *b, uint8_t *e) {
148 if (read(mouse_fd, &mouse_ev, 4) != -1) {
149 *b = ((uint8_t *)&mouse_ev)[0];
150 *e = ((uint8_t *)&mouse_ev)[3];
152 mouse_x += ((uint8_t *)&mouse_ev)[1];
154 mouse_y += (-((uint8_t *)&mouse_ev)[2]);
162 static uint8_t queued_keypresses = 0, queue_output_pos = 0, queue_input_pos = 0;
163 static uint8_t queued_keys[256];
164 static uint8_t queued_events[256];
166 void clear_keypress_queue() {
167 memset(queued_keys, 0x80, 256);
168 memset(queued_events, 0x80, 256);
169 queued_keypresses = 0;
170 queue_output_pos = 0;
174 int queue_keypress(uint8_t keycode, uint8_t event_type, uint8_t platform) {
178 if (event_type != KEYPRESS_REPEAT)
179 keymap = keymap_amiga;
184 if (keymap != NULL) {
185 if (keymap[keycode] != NONE) {
186 if (queued_keypresses < 255) {
187 // printf("Keypress queued, matched %.2X to host key code %.2X\n", keycode, keymap[keycode]);
188 queued_keys[queue_output_pos] = keymap[keycode];
189 queued_events[queue_output_pos] = event_type;
199 int get_num_kb_queued() {
200 return queued_keypresses;
203 void pop_queued_key(uint8_t *c, uint8_t *t) {
204 if (queued_keypresses == 0) {
209 *c = queued_keys[queue_input_pos];
210 *t = queued_events[queue_input_pos];
216 int grab_device(int fd) {
218 rc = ioctl(fd, EVIOCGRAB, (void *)1);
222 int release_device(int fd) {
224 rc = ioctl(fd, EVIOCGRAB, (void *)0);