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