-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.c
298 lines (275 loc) · 5.97 KB
/
keyboard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include "defs.h"
#include "util.h"
#include "keyboard.h"
typedef struct {
Byte isDown:1, isRepeat:1, timeout;
} KeyState;
// Timeout before pressed key starts to autorepeat (in frames)
#define repeatTimeout 25
// Time between autorepeat key triggers
#define repeatPeriod 1
#define LEN(arr) ((sizeof arr) / (sizeof *(arr)))
static KeyState keyStates[40];
static KeyEvent pushes[16];
static Byte pushesStart,
nPushes,
autoKey = (Byte)-1;
static const unsigned halfRowAddrs[8] = {
0x7FFEu,
0xBFFEu,
0xDFFEu,
0xEFFEu,
0xF7FEu,
0xFBFEu,
0xFDFEu,
0xFEFEu
};
static const Byte halfRowMasks[8] = {
2,
0,
0,
0,
0,
0,
0,
1
};
static void PushPress(unsigned key)
{
KeyEvent *event;
if (nPushes < LEN(pushes)) {
Click();
event = pushes + (pushesStart + nPushes) % LEN(pushes);
++nPushes;
event->key = key;
event->capsShift = Key_IsDown(Key_capsShift);
event->symbolShift = Key_IsDown(Key_symbolShift);
}
}
void ScanHalfRow(Byte i)
{
KeyState *keyState;
unsigned halfRowAddr, input;
Byte halfRowMask, bit, bitN, keyStateI;
halfRowAddr = halfRowAddrs[i];
halfRowMask = halfRowMasks[i];
// Get half-row keyboard state
input = (Input16(halfRowAddr) & 0x1F) | halfRowMasks[i];
// Test key bits
if (input == 0x1F) {
// Optimized case when all keys are up on the half-row, the most
// common situation.
__builtin_memset(keyStates + 5*i, 0, 5*sizeof *keyStates);
} else {
for (bit = 1, bitN = 0; bit <= 16; bit <<= 1, ++bitN) {
keyStateI = 5*i + bitN;
keyState = keyStates + keyStateI;
if (input & bit) {
// Key is up
__builtin_memset(keyState, 0, sizeof *keyState);
} else {
// Key is down
unsigned key = (halfRowAddr&0xFF00) | bit;
if (keyState->isDown) {
if (autoKey == keyStateI) {
if (keyState->timeout) {
--keyState->timeout;
} else {
keyState->timeout = repeatPeriod;
PushPress(key);
}
}
} else {
keyState->isDown = 1;
keyState->timeout = repeatTimeout;
autoKey = keyStateI;
PushPress(key);
}
}
}
}
}
void Key_Scan(void)
{
Byte i;
for (i = 0; i < 8; ++i) {
ScanHalfRow(i);
}
}
Byte Key_IsDown(unsigned key)
{
return !((key & 0xFF) & Input16((key & 0xFF00) | 0xFE));
}
unsigned Key_ToEnum(char character)
{
switch (character) {
default: return 0;
case '\n': return Key_enter;
case ' ': return Key_space;
case '0': return Key_0;
case '1': return Key_1;
case '2': return Key_2;
case '3': return Key_3;
case '4': return Key_4;
case '5': return Key_5;
case '6': return Key_6;
case '7': return Key_7;
case '8': return Key_8;
case '9': return Key_9;
case 'a': return Key_a;
case 'b': return Key_b;
case 'c': return Key_c;
case 'd': return Key_d;
case 'e': return Key_e;
case 'f': return Key_f;
case 'g': return Key_g;
case 'h': return Key_h;
case 'i': return Key_i;
case 'j': return Key_j;
case 'k': return Key_k;
case 'l': return Key_l;
case 'm': return Key_m;
case 'n': return Key_n;
case 'o': return Key_o;
case 'p': return Key_p;
case 'q': return Key_q;
case 'r': return Key_r;
case 's': return Key_s;
case 't': return Key_t;
case 'u': return Key_u;
case 'v': return Key_v;
case 'w': return Key_w;
case 'x': return Key_x;
case 'y': return Key_y;
case 'z': return Key_z;
}
}
char Key_ToChar(unsigned enumKey)
{
switch (enumKey) {
default: return 0;
case Key_enter: return '\n';
case Key_space: return ' ';
case Key_0: return '0';
case Key_1: return '1';
case Key_2: return '2';
case Key_3: return '3';
case Key_4: return '4';
case Key_5: return '5';
case Key_6: return '6';
case Key_7: return '7';
case Key_8: return '8';
case Key_9: return '9';
case Key_a: return 'a';
case Key_b: return 'b';
case Key_c: return 'c';
case Key_d: return 'd';
case Key_e: return 'e';
case Key_f: return 'f';
case Key_g: return 'g';
case Key_h: return 'h';
case Key_i: return 'i';
case Key_j: return 'j';
case Key_k: return 'k';
case Key_l: return 'l';
case Key_m: return 'm';
case Key_n: return 'n';
case Key_o: return 'o';
case Key_p: return 'p';
case Key_q: return 'q';
case Key_r: return 'r';
case Key_s: return 's';
case Key_t: return 't';
case Key_u: return 'u';
case Key_v: return 'v';
case Key_w: return 'w';
case Key_x: return 'x';
case Key_y: return 'y';
case Key_z: return 'z';
}
}
KeyEvent *Key_GetPress(void)
{
KeyEvent *result = NULL;
if (nPushes > 0) {
result = pushes + pushesStart;
pushesStart = (pushesStart + 1) % LEN(pushes);
--nPushes;
}
return result;
}
char KeyEvent_ToChar(const KeyEvent *keyEvent)
{
static const char
digitSymbolMap[10] = {
'_',
'!',
'@',
'#',
'$',
'%',
'&',
'\'',
'(',
')'
},
alphaSymbolMap[26] = {
'~', // a
'*', // b
'?', // c
'\\', // d
0, // e
'{', // f
'}', // g
'^', // h
0, // i
'-', // j
'+', // k
'=', // l
'.', // m
',', // n
';', // o
'"', // p
0, // q
'<', // r
'|', // s
'>', // t
']', // u
'/', // v
0, // w
0x60, // x
'[', // y
':' // z
};
char result;
Byte capsShift, symbolShift;
result = Key_ToChar(keyEvent->key);
capsShift = keyEvent->capsShift;
symbolShift = keyEvent->symbolShift;
if (!capsShift && !symbolShift) {
} else if (capsShift && !symbolShift) {
if (isalpha(result)) {
result = toupper(result);
} else {
result = 0;
}
} else if (!capsShift && symbolShift) {
if (isalpha(result)) {
result = alphaSymbolMap[result - 'a'];
} else if (isdigit(result)) {
result = digitSymbolMap[result - '0'];
} else {
result = 0;
}
} else { // capsShift && symbolShift
if (result == 'p') {
result = 0x7F;
} else {
result = 0;
}
}
return result;
}