-
-
Notifications
You must be signed in to change notification settings - Fork 340
/
Keymap.cpp
646 lines (541 loc) · 14.2 KB
/
Keymap.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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
* Copyright 2004-2012, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Jérôme Duval
* Axel Dörfler, axeld@pinc-software.de.
* John Scipione, jscipione@gmail.com.
*/
#include <Keymap.h>
#include <new>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ByteOrder.h>
#include <File.h>
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
# include "SystemKeymap.h"
// generated by the build system
#endif
// Private only at this point, as we might want to improve the dead key
// implementation in the future
enum dead_key_index {
kDeadKeyAcute = 1,
kDeadKeyGrave,
kDeadKeyCircumflex,
kDeadKeyDiaeresis,
kDeadKeyTilde
};
static const uint32 kModifierKeys = B_SHIFT_KEY | B_CAPS_LOCK | B_CONTROL_KEY
| B_OPTION_KEY | B_COMMAND_KEY | B_MENU_KEY;
BKeymap::BKeymap()
:
fChars(NULL),
fCharsSize(0)
{
Unset();
}
BKeymap::~BKeymap()
{
delete[] fChars;
}
/*! Load a map from a file.
File format in big endian:
struct key_map
uint32 size of following charset
charset (offsets go into this with size of character followed by
character)
*/
status_t
BKeymap::SetTo(const char* path)
{
BFile file;
status_t status = file.SetTo(path, B_READ_ONLY);
if (status != B_OK)
return status;
return SetTo(file);
}
status_t
BKeymap::SetTo(BDataIO& stream)
{
if (stream.Read(&fKeys, sizeof(fKeys)) < 1)
return B_IO_ERROR;
// convert from big-endian
for (uint32 i = 0; i < sizeof(fKeys) / 4; i++) {
((uint32*)&fKeys)[i] = B_BENDIAN_TO_HOST_INT32(((uint32*)&fKeys)[i]);
}
if (fKeys.version != 3)
return B_BAD_DATA;
if (stream.Read(&fCharsSize, sizeof(uint32)) < 1)
return B_IO_ERROR;
fCharsSize = B_BENDIAN_TO_HOST_INT32(fCharsSize);
if (fCharsSize > 16 * 1024) {
Unset();
return B_BAD_DATA;
}
delete[] fChars;
fChars = new char[fCharsSize];
if (stream.Read(fChars, fCharsSize) != (ssize_t)fCharsSize) {
Unset();
return B_IO_ERROR;
}
return B_OK;
}
status_t
BKeymap::SetToCurrent()
{
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
key_map* keys = NULL;
get_key_map(&keys, &fChars);
if (!keys)
return B_ERROR;
memcpy(&fKeys, keys, sizeof(fKeys));
free(keys);
return B_OK;
#else // ! __BEOS__
fprintf(stderr, "Unsupported operation on this platform!\n");
exit(1);
#endif // ! __BEOS__
}
status_t
BKeymap::SetToDefault()
{
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
fKeys = kSystemKeymap;
fCharsSize = kSystemKeyCharsSize;
fChars = new (std::nothrow) char[fCharsSize];
if (fChars == NULL) {
Unset();
return B_NO_MEMORY;
}
memcpy(fChars, kSystemKeyChars, fCharsSize);
return B_OK;
#else // ! __BEOS__
fprintf(stderr, "Unsupported operation on this platform!\n");
exit(1);
#endif // ! __BEOS__
}
void
BKeymap::Unset()
{
delete[] fChars;
fChars = NULL;
fCharsSize = 0;
memset(&fKeys, 0, sizeof(fKeys));
}
/*! We need to know if a key is a modifier key to choose
a valid key when several are pressed together
*/
bool
BKeymap::IsModifierKey(uint32 keyCode) const
{
return keyCode == fKeys.caps_key
|| keyCode == fKeys.num_key
|| keyCode == fKeys.scroll_key
|| keyCode == fKeys.left_shift_key
|| keyCode == fKeys.right_shift_key
|| keyCode == fKeys.left_command_key
|| keyCode == fKeys.right_command_key
|| keyCode == fKeys.left_control_key
|| keyCode == fKeys.right_control_key
|| keyCode == fKeys.left_option_key
|| keyCode == fKeys.right_option_key
|| keyCode == fKeys.menu_key;
}
//! We need to know a modifier for a key
uint32
BKeymap::Modifier(uint32 keyCode) const
{
if (keyCode == fKeys.caps_key)
return B_CAPS_LOCK;
if (keyCode == fKeys.num_key)
return B_NUM_LOCK;
if (keyCode == fKeys.scroll_key)
return B_SCROLL_LOCK;
if (keyCode == fKeys.left_shift_key)
return B_LEFT_SHIFT_KEY | B_SHIFT_KEY;
if (keyCode == fKeys.right_shift_key)
return B_RIGHT_SHIFT_KEY | B_SHIFT_KEY;
if (keyCode == fKeys.left_command_key)
return B_LEFT_COMMAND_KEY | B_COMMAND_KEY;
if (keyCode == fKeys.right_command_key)
return B_RIGHT_COMMAND_KEY | B_COMMAND_KEY;
if (keyCode == fKeys.left_control_key)
return B_LEFT_CONTROL_KEY | B_CONTROL_KEY;
if (keyCode == fKeys.right_control_key)
return B_RIGHT_CONTROL_KEY | B_CONTROL_KEY;
if (keyCode == fKeys.left_option_key)
return B_LEFT_OPTION_KEY | B_OPTION_KEY;
if (keyCode == fKeys.right_option_key)
return B_RIGHT_OPTION_KEY | B_OPTION_KEY;
if (keyCode == fKeys.menu_key)
return B_MENU_KEY;
return 0;
}
uint32
BKeymap::KeyForModifier(uint32 modifier) const
{
if (modifier == B_CAPS_LOCK)
return fKeys.caps_key;
if (modifier == B_NUM_LOCK)
return fKeys.num_key;
if (modifier == B_SCROLL_LOCK)
return fKeys.scroll_key;
if (modifier == B_LEFT_SHIFT_KEY || modifier == B_SHIFT_KEY)
return fKeys.left_shift_key;
if (modifier == B_RIGHT_SHIFT_KEY)
return fKeys.right_shift_key;
if (modifier == B_LEFT_COMMAND_KEY || modifier == B_COMMAND_KEY)
return fKeys.left_command_key;
if (modifier == B_RIGHT_COMMAND_KEY)
return fKeys.right_command_key;
if (modifier == B_LEFT_CONTROL_KEY || modifier == B_CONTROL_KEY)
return fKeys.left_control_key;
if (modifier == B_RIGHT_CONTROL_KEY)
return fKeys.right_control_key;
if (modifier == B_LEFT_OPTION_KEY || modifier == B_OPTION_KEY)
return fKeys.left_option_key;
if (modifier == B_RIGHT_OPTION_KEY)
return fKeys.right_option_key;
if (modifier == B_MENU_KEY)
return fKeys.menu_key;
return 0;
}
/*! Checks whether a key is an active dead key.
*/
uint8
BKeymap::ActiveDeadKey(uint32 keyCode, uint32 modifiers) const
{
bool enabled;
uint8 deadKey = DeadKey(keyCode, modifiers, &enabled);
if (deadKey == 0 || !enabled)
return 0;
return deadKey;
}
/*! Checks whether a key is a dead key.
If it is, the enabled/disabled state of that dead key will be passed
out via isEnabled (isEnabled is not touched for non-dead keys).
*/
uint8
BKeymap::DeadKey(uint32 keyCode, uint32 modifiers, bool* _isEnabled) const
{
uint32 tableMask = 0;
int32 offset = Offset(keyCode, modifiers, &tableMask);
uint8 deadKeyIndex = DeadKeyIndex(offset);
if (deadKeyIndex > 0 && _isEnabled != NULL) {
uint32 deadTables[] = {
fKeys.acute_tables,
fKeys.grave_tables,
fKeys.circumflex_tables,
fKeys.dieresis_tables,
fKeys.tilde_tables
};
*_isEnabled = (deadTables[deadKeyIndex - 1] & tableMask) != 0;
}
return deadKeyIndex;
}
//! Tell if a key is a dead second key.
bool
BKeymap::IsDeadSecondKey(uint32 keyCode, uint32 modifiers,
uint8 activeDeadKey) const
{
if (!activeDeadKey)
return false;
int32 offset = Offset(keyCode, modifiers);
if (offset < 0)
return false;
uint32 numBytes = fChars[offset];
if (!numBytes)
return false;
const int32* deadOffsets[] = {
fKeys.acute_dead_key,
fKeys.grave_dead_key,
fKeys.circumflex_dead_key,
fKeys.dieresis_dead_key,
fKeys.tilde_dead_key
};
const int32* deadOffset = deadOffsets[activeDeadKey - 1];
for (int32 i = 0; i < 32; i++) {
if (offset == deadOffset[i])
return true;
uint32 deadNumBytes = fChars[deadOffset[i]];
if (!deadNumBytes)
continue;
if (strncmp(&fChars[offset + 1], &fChars[deadOffset[i] + 1],
deadNumBytes) == 0)
return true;
i++;
}
return false;
}
//! Get the char for a key given modifiers and active dead key
void
BKeymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey,
char** chars, int32* numBytes) const
{
*numBytes = 0;
*chars = NULL;
if (keyCode > 128 || fChars == NULL)
return;
// here we take NUMLOCK into account
if ((modifiers & B_NUM_LOCK) != 0) {
switch (keyCode) {
case 0x37:
case 0x38:
case 0x39:
case 0x48:
case 0x49:
case 0x4a:
case 0x58:
case 0x59:
case 0x5a:
case 0x64:
case 0x65:
modifiers ^= B_SHIFT_KEY;
}
}
int32 offset = Offset(keyCode, modifiers);
if (offset < 0)
return;
// here we get the char size
*numBytes = fChars[offset];
if (*numBytes <= 0) {
// if key is not mapped in the option table, fall-through.
if ((modifiers & B_OPTION_KEY) != 0) {
offset = Offset(keyCode, modifiers & ~B_OPTION_KEY);
if (offset < 0)
return;
// get the char size again
*numBytes = fChars[offset];
if (*numBytes <= 0)
return;
} else
return;
}
// here we take an potential active dead key
const int32* deadKey;
switch (activeDeadKey) {
case kDeadKeyAcute:
deadKey = fKeys.acute_dead_key;
break;
case kDeadKeyGrave:
deadKey = fKeys.grave_dead_key;
break;
case kDeadKeyCircumflex:
deadKey = fKeys.circumflex_dead_key;
break;
case kDeadKeyDiaeresis:
deadKey = fKeys.dieresis_dead_key;
break;
case kDeadKeyTilde:
deadKey = fKeys.tilde_dead_key;
break;
default:
{
// if not dead, we copy and return the char
char* str = *chars = new char[*numBytes + 1];
strncpy(str, &fChars[offset + 1], *numBytes);
str[*numBytes] = 0;
return;
}
}
// if dead key, we search for our current offset char in the dead key
// offset table string comparison is needed
for (int32 i = 0; i < 32; i++) {
if (strncmp(&fChars[offset + 1], &fChars[deadKey[i] + 1], *numBytes)
== 0) {
*numBytes = fChars[deadKey[i + 1]];
switch (*numBytes) {
case 0:
// Not mapped
*chars = NULL;
break;
default:
{
// 1-, 2-, 3-, or 4-byte UTF-8 character
char *str = *chars = new char[*numBytes + 1];
strncpy(str, &fChars[deadKey[i + 1] + 1], *numBytes);
str[*numBytes] = 0;
break;
}
}
return;
}
i++;
}
// if not found we return the current char mapped
*chars = new char[*numBytes + 1];
strncpy(*chars, &fChars[offset + 1], *numBytes);
(*chars)[*numBytes] = 0;
}
/*! Get a list of characters translated from a given character and
set of modifiers to another set of modifiers.
*/
status_t
BKeymap::GetModifiedCharacters(const char* in, int32 inModifiers,
int32 outModifiers, BObjectList<const char>* _outList)
{
if (in == NULL || *in == '\0' || _outList == NULL)
return B_BAD_VALUE;
int32 inOffset;
int32 outOffset;
for(uint32 i = 0; i < 128; i++) {
if (inModifiers == 0)
inOffset = fKeys.normal_map[i];
else if (inModifiers == B_SHIFT_KEY)
inOffset = fKeys.shift_map[i];
else if (inModifiers == B_CONTROL_KEY)
inOffset = fKeys.control_map[i];
else if (inModifiers == B_OPTION_KEY)
inOffset = fKeys.option_map[i];
else if (inModifiers == (B_OPTION_KEY | B_SHIFT_KEY))
inOffset = fKeys.option_shift_map[i];
else if (inModifiers == B_CAPS_LOCK)
inOffset = fKeys.caps_map[i];
else if (inModifiers == (B_CAPS_LOCK | B_SHIFT_KEY))
inOffset = fKeys.caps_shift_map[i];
else if (inModifiers == (B_OPTION_KEY | B_CAPS_LOCK))
inOffset = fKeys.option_caps_map[i];
else if (inModifiers == (B_OPTION_KEY | B_CAPS_LOCK | B_SHIFT_KEY))
inOffset = fKeys.option_caps_shift_map[i];
else
return B_BAD_VALUE;
size_t sizeIn = fChars[inOffset++];
if (sizeIn == 0 || memcmp(in, fChars + inOffset, sizeIn) != 0) {
// this character isn't mapped or doesn't match
continue;
}
if (outModifiers == 0)
outOffset = fKeys.normal_map[i];
else if (outModifiers == B_SHIFT_KEY)
outOffset = fKeys.shift_map[i];
else if (outModifiers == B_CONTROL_KEY)
outOffset = fKeys.control_map[i];
else if (outModifiers == B_OPTION_KEY)
outOffset = fKeys.option_map[i];
else if (outModifiers == (B_OPTION_KEY | B_SHIFT_KEY))
outOffset = fKeys.option_shift_map[i];
else if (outModifiers == B_CAPS_LOCK)
outOffset = fKeys.caps_map[i];
else if (outModifiers == (B_CAPS_LOCK | B_SHIFT_KEY))
outOffset = fKeys.caps_shift_map[i];
else if (outModifiers == (B_OPTION_KEY | B_CAPS_LOCK))
outOffset = fKeys.option_caps_map[i];
else if (outModifiers == (B_OPTION_KEY | B_CAPS_LOCK | B_SHIFT_KEY))
outOffset = fKeys.option_caps_shift_map[i];
else
return B_BAD_VALUE;
size_t sizeOut = fChars[outOffset++];
char* out = (char*)malloc(sizeOut + 1);
if (out == NULL)
return B_NO_MEMORY;
memcpy(out, fChars + outOffset, sizeOut);
out[sizeOut] = '\0';
_outList->AddItem((const char*)out);
}
return B_OK;
}
bool
BKeymap::operator==(const BKeymap& other) const
{
return fCharsSize == other.fCharsSize
&& !memcmp(&fKeys, &other.fKeys, sizeof(fKeys))
&& !memcmp(fChars, other.fChars, fCharsSize);
}
bool
BKeymap::operator!=(const BKeymap& other) const
{
return !(*this == other);
}
BKeymap&
BKeymap::operator=(const BKeymap& other)
{
Unset();
fChars = new char[fCharsSize];
fCharsSize = other.fCharsSize;
memcpy(fChars, other.fChars, fCharsSize);
memcpy(&fKeys, &other.fKeys, sizeof(fKeys));
return *this;
}
int32
BKeymap::Offset(uint32 keyCode, uint32 modifiers, uint32* _table) const
{
int32 offset;
uint32 table;
if (keyCode >= 128)
return -1;
switch (modifiers & kModifierKeys) {
case B_SHIFT_KEY:
offset = fKeys.shift_map[keyCode];
table = B_SHIFT_TABLE;
break;
case B_CAPS_LOCK:
offset = fKeys.caps_map[keyCode];
table = B_CAPS_TABLE;
break;
case B_CAPS_LOCK | B_SHIFT_KEY:
offset = fKeys.caps_shift_map[keyCode];
table = B_CAPS_SHIFT_TABLE;
break;
case B_CONTROL_KEY:
offset = fKeys.control_map[keyCode];
table = B_CONTROL_TABLE;
break;
case B_OPTION_KEY:
offset = fKeys.option_map[keyCode];
table = B_OPTION_TABLE;
break;
case B_OPTION_KEY | B_SHIFT_KEY:
offset = fKeys.option_shift_map[keyCode];
table = B_OPTION_SHIFT_TABLE;
break;
case B_OPTION_KEY | B_CAPS_LOCK:
offset = fKeys.option_caps_map[keyCode];
table = B_OPTION_CAPS_TABLE;
break;
case B_OPTION_KEY | B_SHIFT_KEY | B_CAPS_LOCK:
offset = fKeys.option_caps_shift_map[keyCode];
table = B_OPTION_CAPS_SHIFT_TABLE;
break;
default:
offset = fKeys.normal_map[keyCode];
table = B_NORMAL_TABLE;
break;
}
if (_table != NULL)
*_table = table;
if (offset >= (int32)fCharsSize)
return -1;
return offset;
}
uint8
BKeymap::DeadKeyIndex(int32 offset) const
{
if (fChars == NULL || offset <= 0)
return 0;
uint32 numBytes = fChars[offset];
if (!numBytes || numBytes > 4)
return 0;
char chars[5];
strncpy(chars, &fChars[offset + 1], numBytes);
chars[numBytes] = 0;
const int32 deadOffsets[] = {
fKeys.acute_dead_key[1],
fKeys.grave_dead_key[1],
fKeys.circumflex_dead_key[1],
fKeys.dieresis_dead_key[1],
fKeys.tilde_dead_key[1]
};
uint8 result = 0;
for (int32 i = 0; i < 5; i++) {
if (offset == deadOffsets[i])
return i + 1;
uint32 deadNumBytes = fChars[deadOffsets[i]];
if (!deadNumBytes)
continue;
if (strncmp(chars, &fChars[deadOffsets[i] + 1], deadNumBytes) == 0)
return i + 1;
}
return result;
}