-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKBD.C
291 lines (260 loc) · 7.91 KB
/
KBD.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
/*
* Keyboard handler
*
* Doesn't "officially" belong to JUDAS but feel free to use! This is
* blasphemy-ware too!
*
* Supports the Win95 keyboard extra keys!
*/
#include <conio.h>
#include <dos.h>
#include "judasmem.h"
#ifdef __DJGPP__
#include <go32.h>
#include <dpmi.h>
#endif
typedef struct
{
unsigned char rawcode;
unsigned char ascii;
char *name;
} KEY;
int kbd_init(void);
void kbd_uninit(void);
unsigned char waitkey(void);
unsigned char kbd_getkey(void);
int checkkey(unsigned char rawcode);
unsigned char getascii(unsigned char rawcode);
char *getname(unsigned char rawcode);
static int kbd_lock(void);
static void kbd_unlock(void);
unsigned short kbd_get_ds(void);
#ifdef __DJGPP__
/* DJGPP version */
inline unsigned short kbd_get_ds(void) {
return (unsigned short)_my_ds();
}
#else
/* WATCOM C++ version */
#pragma aux kbd_get_ds = \
"mov ax, ds" \
modify [ax] \
value [ax];
#endif
extern int kbd_code_lock_start;
extern int kbd_code_lock_end;
extern unsigned char kt[];
extern unsigned short kbd_ds;
#ifdef __WATCOMC__
extern void (__interrupt __far kbd_handler)();
#else
void kbd_handler(void);
#endif
static unsigned char kbd_initialized = 0;
#ifdef __DJGPP__
/* DJGPP version
__dpmi_paddr structure contains (unsigned long offset32, unsigned short selector) */
__dpmi_paddr kbd_oldvect;
__dpmi_paddr kbd_newvect;
#else
/* WATCOM C++ version */
static void (__interrupt __far *kbd_oldvect)();
static void (__interrupt __far *kbd_newvect)();
#endif
static KEY keyname[] = {{0x0e, 8, "BACKSPACE"},
{0x3a, 0, "CAPSLOCK"},
{0x1c, 13, "ENTER"},
{0x01, 27, "ESC"},
{0x45, 0, "NUMLOCK"},
{0x36, 0, "SHIFT"},
{0x46, 0, "SCR. LOCK"},
{0x39, ' ', "SPACE"},
{0x0f, 0, "TAB"},
{0x3b, 0, "F1"},
{0x3c, 0, "F2"},
{0x3d, 0, "F3"},
{0x3e, 0, "F4"},
{0x3f, 0, "F5"},
{0x40, 0, "F6"},
{0x41, 0, "F7"},
{0x42, 0, "F8"},
{0x43, 0, "F9"},
{0x44, 0, "F10"},
{0x57, 0, "F11"},
{0x58, 0, "F12"},
{0x54, 0, "SYSTEM REQ."},
{0x4c, '5', "GREY 5"},
{0x37, '*', "GREY *"},
{0x4a, '-', "GREY -"},
{0x4e, '+', "GREY +"},
{0x1e, 'A', "A"},
{0x31, 'N', "N"},
{0x30, 'B', "B"},
{0x18, 'O', "O"},
{0x2e, 'C', "C"},
{0x19, 'P', "P"},
{0x20, 'D', "D"},
{0x10, 'Q', "Q"},
{0x12, 'E', "E"},
{0x13, 'R', "R"},
{0x21, 'F', "F"},
{0x1f, 'S', "S"},
{0x22, 'G', "G"},
{0x14, 'T', "T"},
{0x23, 'H', "H"},
{0x16, 'U', "U"},
{0x17, 'I', "I"},
{0x2f, 'V', "V"},
{0x24, 'J', "J"},
{0x11, 'W', "W"},
{0x25, 'K', "K"},
{0x2D, 'X', "X"},
{0x26, 'L', "L"},
{0x15, 'Y', "Y"},
{0x32, 'M', "M"},
{0x2c, 'Z', "Z"},
{0x02, '1', "1"},
{0x03, '2', "2"},
{0x04, '3', "3"},
{0x05, '4', "4"},
{0x06, '5', "5"},
{0x07, '6', "6"},
{0x08, '7', "7"},
{0x09, '8', "8"},
{0x0a, '9', "9"},
{0x0b, '0', "0"},
{0x0c, '-', "-"},
{0x0d, '=', "="},
{0x1a, '[', "["},
{0x1b, ']', "]"},
{0x1c, ';', ";"},
{0x27, ';', ";"},
{0x28, 0x27, "'"},
{0x29, '`', "`"},
{0x33, ',', ","},
{0x34, '.', "."},
{0x35, '/', "/"},
{0x2b, '\\', "\\"},
{0x56, 0, "<>"},
{0x37 + 0x80, 0, "PRT. SCR."},
{0x38 + 0x80, 0, "ALT"},
{0x1d + 0x80, 0, "CTRL"},
{0x35 + 0x80, '/', "GREY /"},
{0x53 + 0x80, 8, "DELETE"},
{0x50 + 0x80, 0, "CRS. DOWN"},
{0x4f + 0x80, 0, "END"},
{0x47 + 0x80, 0, "HOME"},
{0x52 + 0x80, 0, "INSERT"},
{0x4b + 0x80, 0, "CRS. LEFT"},
{0x51 + 0x80, 0, "PAGE DOWN"},
{0x49 + 0x80, 0, "PAGE UP"},
{0x4d + 0x80, 0, "CRS. RIGHT"},
{0x48 + 0x80, 0, "CRS. UP"},
{0x5b + 0x80, 0, "WINDOWS KEY"},
{0x5d + 0x80, 0, "MENU KEY"},
{0xff, 0, "?"}};
int kbd_init(void)
{
if (kbd_initialized) return 1;
if (!kbd_lock()) return 0;
kbd_ds = kbd_get_ds();
#ifdef __DJGPP__
/* DJGPP version */
kbd_newvect.offset32 = (unsigned long) &kbd_handler;
kbd_newvect.selector = (unsigned short) _my_cs();
__dpmi_get_protected_mode_interrupt_vector(9, &kbd_oldvect);
__dpmi_set_protected_mode_interrupt_vector(9, &kbd_newvect);
#else
/* WATCOM C++ version */
kbd_newvect = &kbd_handler;
kbd_oldvect = _dos_getvect(9);
_dos_setvect(9, kbd_newvect);
#endif
kbd_initialized = 1;
return 1;
}
void kbd_uninit(void)
{
if (!kbd_initialized) return;
#ifdef __DJGPP__
/* DJGPP version */
__dpmi_set_protected_mode_interrupt_vector(9, &kbd_oldvect);
#else
/* WATCOM C++ version */
_dos_setvect(9, kbd_oldvect);
#endif
kbd_unlock();
kbd_initialized = 0;
}
static int kbd_lock(void)
{
if (!judas_memlock(&kbd_code_lock_start, (int)&kbd_code_lock_end - (int)&kbd_code_lock_start)) return 0;
return 1;
}
static void kbd_unlock(void)
{
judas_memunlock(&kbd_code_lock_start, (int)&kbd_code_lock_end - (int)&kbd_code_lock_start);
}
unsigned char waitkey(void)
{
int index;
for (;;)
{
for (index = 0; index < 255; index++)
{
if (kt[index])
{
kt[index] = 0;
return index;
}
}
}
}
unsigned char kbd_getkey(void)
{
int index;
for (index = 0; index < 255; index++)
{
if (kt[index])
{
kt[index] = 0;
return index;
}
}
return 0;
}
int checkkey(unsigned char rawcode)
{
if (kt[rawcode])
{
kt[rawcode] = 0;
return 1;
}
return 0;
}
unsigned char getascii(unsigned char rawcode)
{
KEY *ptr = &keyname[0];
while (ptr->rawcode != 255)
{
if (ptr->rawcode == rawcode)
{
return ptr->ascii;
}
ptr++;
}
return 0x00;
}
char *getname(unsigned char rawcode)
{
KEY *ptr = &keyname[0];
while (ptr->rawcode != 0xff)
{
if (ptr->rawcode == rawcode)
{
return ptr->name;
}
ptr++;
}
return ptr->name;
}