-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKeyCharacterMap.cpp
206 lines (183 loc) · 4.54 KB
/
KeyCharacterMap.cpp
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
#include "KeyCharacterMap.h"
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
struct Header
{
char magic[8];
unsigned int endian;
unsigned int version;
unsigned int keycount;
unsigned char kbdtype;
char padding[11];
};
KeyCharacterMap::KeyCharacterMap()
{
}
KeyCharacterMap::~KeyCharacterMap()
{
free(m_keys);
}
unsigned short
KeyCharacterMap::get(int keycode, int meta)
{
Key* k = find_key(keycode);
if (k != NULL) {
return k->data[meta & META_MASK];
}
return 0;
}
unsigned short
KeyCharacterMap::getNumber(int keycode)
{
Key* k = find_key(keycode);
if (k != NULL) {
return k->number;
}
return 0;
}
unsigned short
KeyCharacterMap::getMatch(int keycode, const unsigned short* chars,
int charsize, uint32_t modifiers)
{
Key* k = find_key(keycode);
modifiers &= 3; // ignore the SYM key because we don't have keymap entries for it
if (k != NULL) {
const uint16_t* data = k->data;
for (int j=0; j<charsize; j++) {
uint16_t c = chars[j];
for (int i=0; i<(META_MASK + 1); i++) {
if ((modifiers == 0) || ((modifiers & i) != 0)) {
if (c == data[i]) {
return c;
}
}
}
}
}
return 0;
}
unsigned short
KeyCharacterMap::getDisplayLabel(int keycode)
{
Key* k = find_key(keycode);
if (k != NULL) {
return k->display_label;
}
return 0;
}
bool
KeyCharacterMap::getKeyData(int keycode, unsigned short *displayLabel,
unsigned short *number, unsigned short* results)
{
Key* k = find_key(keycode);
if (k != NULL) {
memcpy(results, k->data, sizeof(short)*(META_MASK + 1));
*number = k->number;
*displayLabel = k->display_label;
return true;
} else {
return false;
}
}
bool
KeyCharacterMap::find_char(uint16_t c, uint32_t* key, uint32_t* mods)
{
uint32_t N = m_keyCount;
for (int j=0; j<(META_MASK + 1); j++) {
Key const* keys = m_keys;
for (uint32_t i=0; i<N; i++) {
if (keys->data[j] == c) {
*key = keys->keycode;
*mods = j;
return true;
}
keys++;
}
}
return false;
}
KeyCharacterMap::Key*
KeyCharacterMap::find_key(int keycode)
{
Key* keys = m_keys;
int low = 0;
int high = m_keyCount - 1;
int mid;
int n;
while (low <= high) {
mid = (low + high) / 2;
n = keys[mid].keycode;
if (keycode < n) {
high = mid - 1;
} else if (keycode > n) {
low = mid + 1;
} else {
return keys + mid;
}
}
return NULL;
}
KeyCharacterMap*
KeyCharacterMap::try_file(const char* filename)
{
KeyCharacterMap* rv = NULL;
Key* keys;
int fd;
off_t filesize;
Header header;
int err;
fd = open(filename, O_RDONLY);
if (fd == -1) {
fprintf(stderr,"Can't open keycharmap file");
return NULL;
}
filesize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
// validate the header
if (filesize <= (off_t)sizeof(header)) {
fprintf(stderr,"Bad keycharmap - filesize=%d\n", (int)filesize);
goto cleanup1;
}
err = read(fd, &header, sizeof(header));
if (err == -1) {
fprintf(stderr,"Error reading keycharmap file");
goto cleanup1;
}
if (0 != memcmp(header.magic, "keychar", 8)) {
fprintf(stderr,"Bad keycharmap magic token");
goto cleanup1;
}
if (header.endian != 0x12345678) {
fprintf(stderr,"Bad keycharmap endians");
goto cleanup1;
}
if ((header.version & 0xff) != 2) {
fprintf(stderr,"Only support keycharmap version 2 (got 0x%08x)", header.version);
goto cleanup1;
}
if (filesize < (off_t)(sizeof(Header)+(sizeof(Key)*header.keycount))) {
fprintf(stderr,"Bad keycharmap file size\n");
goto cleanup1;
}
// read the key data
keys = (Key*)malloc(sizeof(Key)*header.keycount);
err = read(fd, keys, sizeof(Key)*header.keycount);
if (err == -1) {
fprintf(stderr,"Error reading keycharmap file");
free(keys);
goto cleanup1;
}
// return the object
rv = new KeyCharacterMap;
rv->m_keyCount = header.keycount;
rv->m_keys = keys;
rv->m_type = header.kbdtype;
cleanup1:
close(fd);
return rv;
}