-
Notifications
You must be signed in to change notification settings - Fork 1
/
winkbd.c
168 lines (140 loc) · 3.69 KB
/
winkbd.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
/* winkbd.c - keyboard input routines
* Copyright (C) 2017 Sean MacLennan
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this project; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "buff.h"
#include "keys.h"
#include "winkeys.h"
HANDLE hstdin;
/* stack and vars for t[un]getkb / tkbrdy */
#define CSTACK 16 /* must be power of 2 */
static int cstack[CSTACK];
static int cptr = -1;
static int cpushed;
static int Pending;
static int dummy_cb(INPUT_RECORD *event) { return 0; }
/* Return non-zero if you don't want tgetkb to handle the event */
int (*winkbd_event_cb)(INPUT_RECORD *event) = dummy_cb;
static short convertKey(KEY_EVENT_RECORD *event)
{
Byte key = virt[event->wVirtualKeyCode];
if (event->dwControlKeyState == 0 || key == 0)
return key;
if (key >= 128)
return key;
if (event->dwControlKeyState & (CAPSLOCK_ON | SHIFT_PRESSED)) {
switch (key) {
case '`': key = '~'; break;
case '1': key = '!'; break;
case '2': key = '@'; break;
case '3': key = '#'; break;
case '4': key = '$'; break;
case '5': key = '%'; break;
case '6': key = '^'; break;
case '7': key = '&'; break;
case '8': key = '*'; break;
case '9': key = '('; break;
case '0': key = ')'; break;
case '-': key = '_'; break;
case '=': key = '+'; break;
case '[': key = '{'; break;
case ']': key = '}'; break;
case '\\': key = '|'; break;
case ';': key = ':'; break;
case '\'': key = '"'; break;
case ',': key = '<'; break;
case '.': key = '>'; break;
case '/': key = '?'; break;
default:
if (isalpha(key))
key = toupper(key);
}
}
if (event->dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
return M(toupper(key));
if (event->dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
switch (key) {
case ' ':
case '\\':
return 28;
case ']':
return 29;
case '^':
return 30;
case '_':
case '-':
return 31;
default:
if (isalpha(key))
return key - 'a' + 1;
else
return 0;
}
return key;
}
#ifdef FCHECK
static _inline void do_mouse(MOUSE_EVENT_RECORD *mouse) {}
#else
#endif
void tkbdinit(void)
{
hstdin = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(hstdin, WINKBD_EVENT_MASK);
}
int tgetkb(void)
{
Pending = 0;
cptr = (cptr + 1) & (CSTACK - 1);
if (cpushed) {
--cpushed;
return cstack[cptr];
}
INPUT_RECORD input[CSTACK];
DWORD i, n;
int p = cptr;
again:
if (!ReadConsoleInput(hstdin, input, CSTACK, &n))
abort(); /* you can install an abort handler */
for (i = 0; i < n; ++i) {
if (winkbd_event_cb(&input[i]))
continue;
if (input[i].EventType == KEY_EVENT)
if (input[i].Event.KeyEvent.bKeyDown == 0) {
cstack[p] = convertKey(&input[i].Event.KeyEvent);
if (cstack[p]) {
p = (p + 1) & (CSTACK - 1);
++cpushed;
}
}
}
if (cpushed == 0)
goto again; /* keep reading till we get a key */
--cpushed;
return cstack[cptr];
}
int tkbrdy(void)
{
if (cpushed || Pending)
return 1;
return Pending = WaitForSingleObject(hstdin, 0) == WAIT_OBJECT_0;
}
int tdelay(int ms)
{
if (cpushed || Pending)
return 0;
return WaitForSingleObject(hstdin, ms) != WAIT_OBJECT_0;
}