-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDGLKBRD.C
executable file
·430 lines (377 loc) · 16.5 KB
/
DGLKBRD.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "dglkbrd.h"
#include "dgl.h"
#include "dglcmn.h"
#include "dglevent.h"
#include "dglutil.h"
#include <conio.h>
#include <dos.h>
#include <string.h>
#define PIC_CTRL_PORT 0x20
#define KEYBRD_DATA_PORT 0x60
#define KEYBRD_CTRL_PORT 0x61
#define KEYBRD_STATUS_PORT 0x64
#define KEYBRD_CMD_SET_LED 0xED
#define KEYBRD_FLAGS_ADDR 0x417
#define KEYBRD_LED_SCROLLOCK 0x1
#define KEYBRD_LED_NUMLOCK 0x2
#define KEYBRD_LED_CAPSLOCK 0x4
#define KEY_EXTENDED ((KEY)0xe0)
static bool _installed = false;
static INPUTEVENT *keyboard_event;
volatile uint8 keys[128];
volatile KEY _key_last_scan;
volatile KEY _key_scan;
volatile uint16 key_flags;
volatile uint16 key_mod;
static char lookup_key_to_char[128] = {
0, 0, '1', '2', '3', '4', '5', '6', // 00 - 07
'7', '8', '9', '0', '-', '=', 0, '\t', // 08 - 0F
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', // 10 - 17
'o', 'p', '[', ']', 0, 0, 'a', 's', // 18 - 1F
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', // 20 - 27
'\'', '`', 0, '\\', 'z', 'x', 'c', 'v', // 28 - 2F
'b', 'n', 'm', ',', '.', '/', 0, '*', // 30 - 37
0, ' ', 0, 0, 0, 0, 0, 0, // 38 - 3F
0, 0, 0, 0, 0, 0, 0, 0, // 40 - 47
0, 0, 0, 0, 0, 0, 0, 0, // 48 - 4F
0, 0, 0, 0, 0, 0, 0, 0, // 50 - 57
0, 0, 0, 0, 0, 0, 0, 0, // 58 - 5F
0, 0, 0, 0, 0, 0, 0, 0, // 60 - 67
0, 0, 0, 0, 0, 0, 0, 0, // 68 - 6F
0, 0, 0, 0, 0, 0, 0, 0, // 70 - 77
0, 0, 0, 0, 0, 0, 0, 0 // 78 - 7F
};
static char lookup_key_to_char_shift[128] = {
0, 0, '!', '@', '#', '$', '%', '^', // 00 - 07
'&', '*', '(', ')', '_', '+', 0, '\t', // 08 - 0F
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', // 10 - 17
'O', 'P', '{', '}', 0, 0, 'A', 'S', // 18 - 1F
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', // 20 - 27
'"', '~', 0, '|', 'Z', 'X', 'C', 'V', // 28 - 2F
'B', 'N', 'M', '<', '>', '?', 0, '*', // 30 - 37
0, ' ', 0, 0, 0, 0, 0, 0, // 38 - 3F
0, 0, 0, 0, 0, 0, 0, 0, // 40 - 47
0, 0, 0, 0, 0, 0, 0, 0, // 48 - 4F
0, 0, 0, 0, 0, 0, 0, 0, // 50 - 57
0, 0, 0, 0, 0, 0, 0, 0, // 58 - 5F
0, 0, 0, 0, 0, 0, 0, 0, // 60 - 67
0, 0, 0, 0, 0, 0, 0, 0, // 68 - 6F
0, 0, 0, 0, 0, 0, 0, 0, // 70 - 77
0, 0, 0, 0, 0, 0, 0, 0 // 78 - 7F
};
static char lookup_key_to_char_caps[128] = {
0, 0, '1', '2', '3', '4', '5', '6', // 00 - 07
'7', '8', '9', '0', '-', '=', 0, '\t', // 08 - 0F
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', // 10 - 17
'O', 'P', '[', ']', 0, 0, 'A', 'S', // 18 - 1F
'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', // 20 - 27
'\'', '`', 0, '\\', 'Z', 'X', 'C', 'V', // 28 - 2F
'B', 'N', 'M', ',', '.', '/', 0, '*', // 30 - 37
0, ' ', 0, 0, 0, 0, 0, 0, // 38 - 3F
0, 0, 0, 0, 0, 0, 0, 0, // 40 - 47
0, 0, 0, 0, 0, 0, 0, 0, // 48 - 4F
0, 0, 0, 0, 0, 0, 0, 0, // 50 - 57
0, 0, 0, 0, 0, 0, 0, 0, // 58 - 5F
0, 0, 0, 0, 0, 0, 0, 0, // 60 - 67
0, 0, 0, 0, 0, 0, 0, 0, // 68 - 6F
0, 0, 0, 0, 0, 0, 0, 0, // 70 - 77
0, 0, 0, 0, 0, 0, 0, 0 // 78 - 7F
};
static char lookup_key_to_char_caps_shift[128] = {
0, 0, '!', '@', '#', '$', '%', '^', // 00 - 07
'&', '*', '(', ')', '_', '+', 0, '\t', // 08 - 0F
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', // 10 - 17
'o', 'p', '{', '}', 0, 0, 'a', 's', // 18 - 1F
'd', 'f', 'g', 'h', 'j', 'k', 'l', ':', // 20 - 27
'"', '~', 0, '|', 'z', 'x', 'c', 'v', // 28 - 2F
'b', 'n', 'm', '<', '>', '?', 0, '*', // 30 - 37
0, ' ', 0, 0, 0, 0, 0, 0, // 38 - 3F
0, 0, 0, 0, 0, 0, 0, 0, // 40 - 47
0, 0, 0, 0, 0, 0, 0, 0, // 48 - 4F
0, 0, 0, 0, 0, 0, 0, 0, // 50 - 57
0, 0, 0, 0, 0, 0, 0, 0, // 58 - 5F
0, 0, 0, 0, 0, 0, 0, 0, // 60 - 67
0, 0, 0, 0, 0, 0, 0, 0, // 68 - 6F
0, 0, 0, 0, 0, 0, 0, 0, // 70 - 77
0, 0, 0, 0, 0, 0, 0, 0 // 78 - 7F
};
static char lookup_key_to_char_numpad[128] = {
0, 0, 0, 0, 0, 0, 0, 0, // 00 - 07
0, 0, 0, 0, 0, 0, 0, 0, // 08 - 0F
0, 0, 0, 0, 0, 0, 0, 0, // 10 - 17
0, 0, 0, 0, 0, 0, 0, 0, // 18 - 1F
0, 0, 0, 0, 0, 0, 0, 0, // 20 - 27
0, 0, 0, 0, 0, 0, 0, 0, // 28 - 2F
0, 0, 0, 0, 0, 0, 0, 0, // 30 - 37
0, 0, 0, 0, 0, 0, 0, 0, // 38 - 3F
0, 0, 0, 0, 0, 0, 0, 0, // 40 - 47
0, 0, '-', 0, 0, 0, '+', 0, // 48 - 4F
0, 0, 0, 0, 0, 0, 0, 0, // 50 - 57
0, 0, 0, 0, 0, 0, 0, 0, // 58 - 5F
0, 0, 0, 0, 0, 0, 0, 0, // 60 - 67
0, 0, 0, 0, 0, 0, 0, 0, // 68 - 6F
0, 0, 0, 0, 0, 0, 0, 0, // 70 - 77
0, 0, 0, 0, 0, 0, 0, 0 // 78 - 7F
};
static char lookup_key_to_char_numpad_numlock[128] = {
0, 0, 0, 0, 0, 0, 0, 0, // 00 - 07
0, 0, 0, 0, 0, 0, 0, 0, // 08 - 0F
0, 0, 0, 0, 0, 0, 0, 0, // 10 - 17
0, 0, 0, 0, 0, 0, 0, 0, // 18 - 1F
0, 0, 0, 0, 0, 0, 0, 0, // 20 - 27
0, 0, 0, 0, 0, 0, 0, 0, // 28 - 2F
0, 0, 0, 0, 0, 0, 0, 0, // 30 - 37
0, 0, 0, 0, 0, 0, 0, 0, // 38 - 3F
0, 0, 0, 0, 0, 0, 0, '7', // 40 - 47
'8', '9', '-', '4', '5', '6', '+', '1', // 48 - 4F
'2', '3', '0', '.', 0, 0, 0, 0, // 50 - 57
0, 0, 0, 0, 0, 0, 0, 0, // 58 - 5F
0, 0, 0, 0, 0, 0, 0, 0, // 60 - 67
0, 0, 0, 0, 0, 0, 0, 0, // 68 - 6F
0, 0, 0, 0, 0, 0, 0, 0, // 70 - 77
0, 0, 0, 0, 0, 0, 0, 0 // 78 - 7F
};
static char lookup_key_to_char_extended[128] = {
0, 0, 0, 0, 0, 0, 0, 0, // 00 - 07
0, 0, 0, 0, 0, 0, 0, 0, // 08 - 0F
0, 0, 0, 0, 0, 0, 0, 0, // 10 - 17
0, 0, 0, 0, 0, 0, 0, 0, // 18 - 1F
0, 0, 0, 0, 0, 0, 0, 0, // 20 - 27
0, 0, 0, 0, 0, 0, 0, 0, // 28 - 2F
0, 0, 0, 0, 0, '/', 0, 0, // 30 - 37
0, 0, 0, 0, 0, 0, 0, 0, // 38 - 3F
0, 0, 0, 0, 0, 0, 0, 0, // 40 - 47
0, 0, 0, 0, 0, 0, 0, 0, // 48 - 4F
0, 0, 0, 0, 0, 0, 0, 0, // 50 - 57
0, 0, 0, 0, 0, 0, 0, 0, // 58 - 5F
0, 0, 0, 0, 0, 0, 0, 0, // 60 - 67
0, 0, 0, 0, 0, 0, 0, 0, // 68 - 6F
0, 0, 0, 0, 0, 0, 0, 0, // 70 - 77
0, 0, 0, 0, 0, 0, 0, 0 // 78 - 7F
};
void (interrupt far *_old_handler)();
static void reset_key_states() {
_key_last_scan = 0;
_key_scan = 0;
key_flags = 0;
key_mod = 0;
memset((void*)keys, 0, 128);
}
// waits until the keyboard status port indicates the data port
// can be read from once again
static void wait_kb_data_read() {
while ((inp(KEYBRD_STATUS_PORT) & BIT_0) == 0) {
}
}
// waits until the keyboard status port indicates the data port
// can be written to once again
static void wait_kb_data_write() {
while ((inp(KEYBRD_STATUS_PORT) & BIT_1) != 0) {
}
}
// sends data to the keyboard data port. checks for success
// and returns true if the data write succeeded
static bool send_kb_data(uint8 data) {
uint8 result;
wait_kb_data_write();
outp(KEYBRD_DATA_PORT, data);
wait_kb_data_read();
result = inp(KEYBRD_DATA_PORT);
return (result == 0xFA);
}
static uint16 get_kb_flags(void) {
return *((uint16*)KEYBRD_FLAGS_ADDR);
}
static void set_kb_flags(uint16 flags) {
*((uint16*)KEYBRD_FLAGS_ADDR) = flags;
}
// updates the keyboard indicator LEDs from the num/caps/scroll lock flags
// set in the passed keyboard flags. returns FALSE if the LEDs could not
// be updated (if keyboard data write did not succeed)
static bool update_kb_led(uint8 flags) {
if (!send_kb_data(KEYBRD_CMD_SET_LED)) {
dgl_set_error(DGL_KEYBOARD_UPDATE_LED_FAILURE);
return false;
}
if (!send_kb_data((flags >> 4) & 3)) {
dgl_set_error(DGL_KEYBOARD_UPDATE_LED_FAILURE);
return false;
}
return true;
}
static void push_keyboard_event(KEY key, EVENT_ACTION action) {
if (_events_enabled) {
_events_push(&keyboard_event);
keyboard_event->type = EVENT_TYPE_KEYBOARD;
keyboard_event->keyboard.key = key;
keyboard_event->keyboard.action = action;
keyboard_event->keyboard.mod = key_mod;
}
}
// returns true if the key interrupt event should not be handled (at least
// as far as updating key state is concerned)
static bool handler_filter_keys(void) {
if (BIT_ISSET(KEYBRD_MOD_EXTENDED, key_mod)) {
// extended key + leftshift comes with cursor key presses when
// numlock is enabled
if ((_key_scan & 0x7f) == KEY_LEFTSHIFT)
return true;
}
return false;
}
// maintains BIOS keyboard flags/led toggle states (caps/num/scroll lock)
static void handler_update_flags_and_leds(void) {
switch (_key_scan) {
case KEY_CAPSLOCK:
BIT_TOGGLE(KEYBRD_FLAGS_CAPSLOCK, key_flags);
update_kb_led(key_flags);
set_kb_flags(key_flags);
break;
case KEY_NUMLOCK:
BIT_TOGGLE(KEYBRD_FLAGS_NUMLOCK, key_flags);
update_kb_led(key_flags);
set_kb_flags(key_flags);
break;
case KEY_SCROLLLOCK:
BIT_TOGGLE(KEYBRD_FLAGS_SCROLLOCK, key_flags);
update_kb_led(key_flags);
set_kb_flags(key_flags);
break;
default:
break;
}
}
// updates modifier state based on current BIOS flags and/or key states
static void handler_update_modifiers(void) {
if (BIT_ISSET(KEYBRD_FLAGS_NUMLOCK, key_flags))
BIT_SET(KEYBRD_MOD_NUMLOCK, key_mod);
else
BIT_CLEAR(KEYBRD_MOD_NUMLOCK, key_mod);
if (BIT_ISSET(KEYBRD_FLAGS_CAPSLOCK, key_flags))
BIT_SET(KEYBRD_MOD_CAPSLOCK, key_mod);
else
BIT_CLEAR(KEYBRD_MOD_CAPSLOCK, key_mod);
if (keys[KEY_LEFTSHIFT] || keys[KEY_RIGHTSHIFT])
BIT_SET(KEYBRD_MOD_SHIFT, key_mod);
else
BIT_CLEAR(KEYBRD_MOD_SHIFT, key_mod);
}
// keyboard interrupt handler
// TODO: handle 'Pause' key... it's kind of a weird one and this all doesn't
// take care of it at all.
// also, i suspect this may all have issues with international keyboards
// but i don't own any so cannot test...
void interrupt far kb_int_handler(void) {
// read scan code of key that was just pressed
_key_scan = inp(KEYBRD_DATA_PORT);
// handle updating key state and flags/modifiers ...
if (_key_scan == KEY_EXTENDED) {
// "extended key" events come in as two events to this interrupt
// handler. the first has _key_scan = 0xe0 (0x60 in a released state).
// so we catch this "weird" _key_scan value and set the extended
// modifier here and skip updating any other keyboard state.
// for the key event that will follow immediately after this, _key_scan
// will have the _actual_ key that was pressed, but the modifier will
// be set correctly and we can handle it specially if needed.
BIT_SET(KEYBRD_MOD_EXTENDED, key_mod);
} else {
if (!handler_filter_keys()) {
if (_key_scan & 0x80) {
// high bit set indicates key was released, clear high bit
// to get the actual key scan code
_key_scan &= 0x7f;
keys[(int)_key_scan] = 0;
handler_update_modifiers();
push_keyboard_event(_key_scan, EVENT_ACTION_RELEASED);
} else {
if (keys[(int)_key_scan]) {
// nothing interesting to do for held (repeat) events...
push_keyboard_event(_key_scan, EVENT_ACTION_HELD);
} else {
keys[(int)_key_scan] = 1;
// toggle states only need to be handled for a down event
handler_update_flags_and_leds();
handler_update_modifiers();
push_keyboard_event(_key_scan, EVENT_ACTION_PRESSED);
}
}
_key_last_scan = _key_scan;
}
// clear extended modifier for the following event(s) in any case
BIT_CLEAR(KEYBRD_MOD_EXTENDED, key_mod);
}
// indicate key event was processed to keyboard controller
_key_scan = inp(KEYBRD_CTRL_PORT) | 0x80;
outp(KEYBRD_CTRL_PORT, _key_scan);
outp(KEYBRD_CTRL_PORT, _key_scan & 0x7f);
outp(PIC_CTRL_PORT, 0x20);
}
bool keyboard_init(void) {
if (_installed) {
dgl_set_error(DGL_KEYBOARD_ALREADY_INITIALIZED);
return false;
}
reset_key_states();
key_flags = get_kb_flags();
handler_update_modifiers();
_old_handler = _dos_getvect(9);
_dos_setvect(9, kb_int_handler);
_installed = true;
return true;
}
bool keyboard_shutdown(void) {
if (!_installed)
return true; // don't care
_dos_setvect(9, _old_handler);
reset_key_states();
_installed = false;
return true;
}
bool keyboard_is_initialized(void) {
return _installed;
}
KEY keyboard_read_key(void) {
_key_last_scan = 0;
while (_key_last_scan == 0) {
}
return _key_last_scan;
}
void keyboard_wait_for_key(KEY key) {
_key_last_scan = 0;
while (_key_last_scan != key) {
}
}
char key_to_char(KEY key, uint16 modifiers) {
// this is really just here because of the stupid slash key on the numpad
// (but maybe it will be useful for other types of keyboards later...)
if (BIT_ISSET(KEYBRD_MOD_EXTENDED, modifiers)) {
return lookup_key_to_char_extended[(int)key];
}
// handle numpad keys specially
else if ((key >= 0x47) && (key <= 0x53)) {
if (BIT_ISSET(KEYBRD_MOD_NUMLOCK, modifiers)) {
if (BIT_ISSET(KEYBRD_MOD_SHIFT, modifiers))
return lookup_key_to_char_numpad[(int)key];
else
return lookup_key_to_char_numpad_numlock[(int)key];
} else {
if (BIT_ISSET(KEYBRD_MOD_SHIFT, modifiers))
return lookup_key_to_char_numpad_numlock[(int)key];
else
return lookup_key_to_char_numpad[(int)key];
}
} else {
if (BIT_ISSET(KEYBRD_MOD_CAPSLOCK, modifiers)) {
if (BIT_ISSET(KEYBRD_MOD_SHIFT, modifiers))
return lookup_key_to_char_caps_shift[(int)key];
else
return lookup_key_to_char_caps[(int)key];
} else {
if (BIT_ISSET(KEYBRD_MOD_SHIFT, modifiers))
return lookup_key_to_char_shift[(int)key];
else
return lookup_key_to_char[(int)key];
}
}
return 0;
}