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