]> git.sesse.net Git - pistorm/blob - input/input.c
support stealing the keyboard from the input layer
[pistorm] / input / input.c
1 #include <linux/input.h>
2 #include <sys/ioctl.h>
3 #include <pthread.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <termios.h>
8 #include <unistd.h>
9
10 #include "platforms/platforms.h"
11 #include "input.h"
12
13 #define NONE 0x80
14
15 static int lshift = 0, rshift = 0,/* lctrl = 0, rctrl = 0,*/ lalt = 0, altgr = 0;
16 extern int mouse_fd;
17 extern int keyboard_fd;
18
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,
38 };
39
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)) {
43     switch(ev->code) {
44       case KEY_LEFTSHIFT:
45         target_modifier = &lshift;
46         break;
47       case KEY_RIGHTSHIFT:
48         target_modifier = &rshift;
49         break;
50       case KEY_LEFTALT:
51         target_modifier = &lalt;
52         break;
53       case KEY_RIGHTALT:
54         target_modifier = &altgr;
55         break;
56       case KEY_LEFTCTRL:
57         target_modifier = &lshift;
58         break;
59       case KEY_RIGHTCTRL:
60         target_modifier = &rshift;
61         break;
62     }
63     *target_modifier = (ev->value == KEYPRESS_RELEASE) ? 0 : 1;
64     return 1;
65   }
66   else {
67     return 0;
68   }
69 }
70
71 #define KEYCASE(a, b, c)case a: return (lshift || rshift) ? c : b;
72
73 /**
74  * translates keycodes back into a simpler enumerable value for handling emulator command events
75  *
76  * @param *struct/input_event  ev  pointer to input layer event structure
77  * @return char
78  */
79 char char_from_input_event(struct input_event *ev) {
80   switch(ev->code) {
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);
119     default:
120       return 0;
121   }
122 }
123
124 int get_key_char(char *c, char *code, char *event_type)
125 {
126   if (keyboard_fd == -1)
127     return 0;
128
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);
134       *c = ret;
135       *code = ie.code;
136       *event_type = ie.value;
137       return 1;
138     }
139   }
140
141   return 0;
142 }
143
144 uint8_t mouse_x = 0, mouse_y = 0;
145
146 int get_mouse_status(uint8_t *x, uint8_t *y, uint8_t *b, uint8_t *e) {
147   uint8_t mouse_ev[4];
148   if (read(mouse_fd, &mouse_ev, 4) != -1) {
149     *b = ((uint8_t *)&mouse_ev)[0];
150     *e = ((uint8_t *)&mouse_ev)[3];
151
152     mouse_x += ((uint8_t *)&mouse_ev)[1];
153     *x = mouse_x;
154     mouse_y += (-((uint8_t *)&mouse_ev)[2]);
155     *y = mouse_y;
156     return 1;
157   }
158
159   return 0;
160 }
161
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];
165
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;
171   queue_input_pos = 0;
172 }
173
174 int queue_keypress(uint8_t keycode, uint8_t event_type, uint8_t platform) {
175   char *keymap = NULL;
176   switch (platform) {
177     case PLATFORM_AMIGA:
178       if (event_type != KEYPRESS_REPEAT)
179         keymap = keymap_amiga;
180       break;
181     default:
182       break;
183   }
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;
190         queue_output_pos++;
191         queued_keypresses++;
192         return 1;
193       }
194     }
195   }
196   return 0;
197 }
198
199 int get_num_kb_queued() {
200   return queued_keypresses;
201 }
202
203 void pop_queued_key(uint8_t *c, uint8_t *t) {
204   if (queued_keypresses == 0) {
205     *c = NONE;
206     *t = NONE;
207     return;
208   }
209   *c = queued_keys[queue_input_pos];
210   *t = queued_events[queue_input_pos];
211   queue_input_pos++;
212   queued_keypresses--;
213   return;
214 }
215
216 int grab_device(int fd) {
217   int rc = 0;
218   rc = ioctl(fd, EVIOCGRAB, (void *)1);
219   return rc;
220 }
221
222 int release_device(int fd) {
223   int rc = 0;
224   rc = ioctl(fd, EVIOCGRAB, (void *)0);
225   return rc;
226 }