]> git.sesse.net Git - pistorm/blob - input/input.c
Add Meson build files.
[pistorm] / input / input.c
1 // SPDX-License-Identifier: MIT
2
3 #include <linux/input.h>
4 #include <sys/ioctl.h>
5 #include <pthread.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <termios.h>
10 #include <unistd.h>
11
12 #include "platforms/platforms.h"
13 #include "input.h"
14
15 #define NONE 0x80
16
17 static int lshift = 0, rshift = 0,/* lctrl = 0, rctrl = 0,*/ lalt = 0, altgr = 0;
18 extern int mouse_fd;
19 extern int keyboard_fd;
20
21 // n.b. $fe and $ff are mapped to newmouse standard wheel up/down keycodes, nonexistant on amiga keyboards
22 char keymap_amiga[256] = {
23 /*      00    01    02    03    04    05    06    07    08    09    0A    0B    0C    0D    0E    0F */
24 /*00*/ 0x80, 0x45, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x41, 0x42,
25 /*10*/ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x44, 0x63, 0x20, 0x21,
26 /*20*/ 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x00, 0x60, 0x2B, 0x31, 0x32, 0x33, 0x34,
27 /*30*/ 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x61, 0x5D, 0x64, 0x40, NONE, 0x50, 0x51, 0x52, 0x53, 0x54,
28 /*40*/ 0x55, 0x56, 0x57, 0x58, 0x59, 0x5B, NONE, 0x3D, 0x3E, 0x3F, 0x4A, 0x2D, 0x2E, 0x2F, 0x5E, 0x1D,
29 /*50*/ 0x1E, 0x1F, 0x0F, 0x3C, NONE, NONE, 0x30, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
30 /*60*/ 0x43, NONE, 0x5C, NONE, 0x65, NONE, 0x5F, 0x4C, 0x5A, 0x4F, 0x4E, NONE, 0x4D, NONE, 0x0D, 0x46,
31 /*70*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, 0x66, 0x67, 0x67,
32 /*80*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
33 /*90*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
34 /*A0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
35 /*B0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
36 /*C0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
37 /*D0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
38 /*E0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
39 /*F0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, 0x7A, 0x7B,
40 };
41
42 int handle_modifier(struct input_event *ev) {
43   int *target_modifier = NULL;
44   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     switch(ev->code) {
46       case KEY_LEFTSHIFT:
47         target_modifier = &lshift;
48         break;
49       case KEY_RIGHTSHIFT:
50         target_modifier = &rshift;
51         break;
52       case KEY_LEFTALT:
53         target_modifier = &lalt;
54         break;
55       case KEY_RIGHTALT:
56         target_modifier = &altgr;
57         break;
58       case KEY_LEFTCTRL:
59         target_modifier = &lshift;
60         break;
61       case KEY_RIGHTCTRL:
62         target_modifier = &rshift;
63         break;
64     }
65     *target_modifier = (ev->value == KEYPRESS_RELEASE) ? 0 : 1;
66     return 1;
67   }
68   else {
69     return 0;
70   }
71 }
72
73 #define KEYCASE(a, b, c)case a: return (lshift || rshift) ? c : b;
74
75 /**
76  * translates keycodes back into a simpler enumerable value for handling emulator command events
77  *
78  * @param *struct/input_event  ev  pointer to input layer event structure
79  * @return char
80  */
81 char char_from_input_event(struct input_event *ev) {
82   switch(ev->code) {
83     KEYCASE(KEY_A, 'a', 'A');
84     KEYCASE(KEY_B, 'b', 'B');
85     KEYCASE(KEY_C, 'c', 'C');
86     KEYCASE(KEY_D, 'd', 'D');
87     KEYCASE(KEY_E, 'e', 'E');
88     KEYCASE(KEY_F, 'f', 'F');
89     KEYCASE(KEY_G, 'g', 'G');
90     KEYCASE(KEY_H, 'h', 'H');
91     KEYCASE(KEY_I, 'i', 'I');
92     KEYCASE(KEY_J, 'j', 'J');
93     KEYCASE(KEY_K, 'k', 'K');
94     KEYCASE(KEY_L, 'l', 'L');
95     KEYCASE(KEY_M, 'm', 'M');
96     KEYCASE(KEY_N, 'n', 'N');
97     KEYCASE(KEY_O, 'o', 'O');
98     KEYCASE(KEY_P, 'p', 'P');
99     KEYCASE(KEY_Q, 'q', 'Q');
100     KEYCASE(KEY_R, 'r', 'R');
101     KEYCASE(KEY_S, 's', 'S');
102     KEYCASE(KEY_T, 't', 'T');
103     KEYCASE(KEY_U, 'u', 'U');
104     KEYCASE(KEY_V, 'c', 'V');
105     KEYCASE(KEY_W, 'w', 'W');
106     KEYCASE(KEY_X, 'x', 'X');
107     KEYCASE(KEY_Y, 'y', 'Y');
108     KEYCASE(KEY_Z, 'z', 'Z');
109     KEYCASE(KEY_1, '1', '!');
110     KEYCASE(KEY_2, '2', '@');
111     KEYCASE(KEY_3, '3', '#');
112     KEYCASE(KEY_4, '4', '$');
113     KEYCASE(KEY_5, '5', '%');
114     KEYCASE(KEY_6, '6', '^');
115     KEYCASE(KEY_7, '7', '&');
116     KEYCASE(KEY_8, '8', '*');
117     KEYCASE(KEY_9, '9', '(');
118     KEYCASE(KEY_0, '0', ')');
119     KEYCASE(KEY_F12, 0x1B, 0x1B);
120     KEYCASE(KEY_PAUSE, 0x01, 0x01);
121     default:
122       return 0;
123   }
124 }
125
126 int get_key_char(char *c, char *code, char *event_type)
127 {
128   if (keyboard_fd == -1)
129     return 0;
130
131   struct input_event ie;
132   while(read(keyboard_fd, &ie, sizeof(struct input_event)) != -1) {
133     if (ie.type == EV_KEY) {
134       handle_modifier(&ie);
135       char ret = char_from_input_event(&ie);
136       *c = ret;
137       *code = ie.code;
138       *event_type = ie.value;
139       return 1;
140     }
141   }
142
143   return 0;
144 }
145
146 uint8_t mouse_x = 0, mouse_y = 0;
147
148 int get_mouse_status(uint8_t *x, uint8_t *y, uint8_t *b, uint8_t *e) {
149   uint8_t mouse_ev[4];
150   if (read(mouse_fd, &mouse_ev, 4) != -1) {
151     *b = ((uint8_t *)&mouse_ev)[0];
152     *e = ((uint8_t *)&mouse_ev)[3];
153
154     mouse_x += ((uint8_t *)&mouse_ev)[1];
155     *x = mouse_x;
156     mouse_y += (-((uint8_t *)&mouse_ev)[2]);
157     *y = mouse_y;
158     return 1;
159   }
160
161   return 0;
162 }
163
164 static uint8_t queued_keypresses = 0, queue_output_pos = 0, queue_input_pos = 0;
165 static uint8_t queued_keys[256];
166 static uint8_t queued_events[256];
167
168 void clear_keypress_queue() {
169   memset(queued_keys, 0x80, 256);
170   memset(queued_events, 0x80, 256);
171   queued_keypresses = 0;
172   queue_output_pos = 0;
173   queue_input_pos = 0;
174 }
175
176 int queue_keypress(uint8_t keycode, uint8_t event_type, uint8_t platform) {
177   char *keymap = NULL;
178   switch (platform) {
179     case PLATFORM_AMIGA:
180       if (event_type != KEYPRESS_REPEAT)
181         keymap = keymap_amiga;
182       break;
183     default:
184       break;
185   }
186   if (keymap != NULL) {
187     if (keymap[keycode] != NONE) {
188       if (queued_keypresses < 255) {
189         // printf("Keypress queued, matched %.2X to host key code %.2X\n", keycode, keymap[keycode]);
190         queued_keys[queue_output_pos] = keymap[keycode];
191         queued_events[queue_output_pos] = event_type;
192         queue_output_pos++;
193         queued_keypresses++;
194         return 1;
195       }
196     }
197   }
198   return 0;
199 }
200
201 int get_num_kb_queued() {
202   return queued_keypresses;
203 }
204
205 void pop_queued_key(uint8_t *c, uint8_t *t) {
206   if (queued_keypresses == 0) {
207     *c = NONE;
208     *t = NONE;
209     return;
210   }
211   *c = queued_keys[queue_input_pos];
212   *t = queued_events[queue_input_pos];
213   queue_input_pos++;
214   queued_keypresses--;
215   return;
216 }
217
218 int grab_device(int fd) {
219   int rc = 0;
220   rc = ioctl(fd, EVIOCGRAB, (void *)1);
221   return rc;
222 }
223
224 int release_device(int fd) {
225   int rc = 0;
226   rc = ioctl(fd, EVIOCGRAB, (void *)0);
227   return rc;
228 }