-
Notifications
You must be signed in to change notification settings - Fork 2
/
keyboard-vt100.cpp
107 lines (101 loc) · 2.9 KB
/
keyboard-vt100.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
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <hid/usages.h>
#include "keyboard-vt100.h"
#include "keyboard.h"
uint32_t hid_key_to_vt100_code(uint8_t keycode, int modifiers,
const keychar_t* keymap, char* buf, size_t buf_size) {
// Consistency check: Max size of byte sequences we produce below.
if (buf_size != 4)
return 0;
uint8_t ch = hid_map_key(keycode, modifiers & MOD_SHIFT, keymap);
if (ch) {
if (modifiers & MOD_CTRL) {
// Handle Ctrl-A to Ctrl-Z. Ignore the Ctrl modifier on any
// other keys.
uint8_t range_start = modifiers & MOD_SHIFT ? 'A' : 'a';
uint8_t range_end = static_cast<uint8_t>(range_start + 26);
if (range_start <= ch && ch < range_end) {
buf[0] = static_cast<char>(ch - range_start + 1);
return 1;
}
}
if (modifiers & MOD_CAPSLOCK) {
if ('a' <= ch && ch <= 'z') {
ch = static_cast<char>(ch - 'a' + 'A');
} else if ('A' <= ch && ch <= 'Z') {
ch = static_cast<char>(ch - 'A' + 'a');
}
}
buf[0] = ch;
return 1;
}
switch (keycode) {
// generate special stuff for a few different keys
case HID_USAGE_KEY_ENTER:
case HID_USAGE_KEY_KP_ENTER:
buf[0] = '\n';
return 1;
case HID_USAGE_KEY_BACKSPACE:
buf[0] = '\b';
return 1;
case HID_USAGE_KEY_TAB:
buf[0] = '\t';
return 1;
case HID_USAGE_KEY_ESC:
buf[0] = 0x1b;
return 1;
// generate vt100 key codes for arrows
case HID_USAGE_KEY_UP:
buf[0] = 0x1b;
buf[1] = '[';
buf[2] = 65;
return 3;
case HID_USAGE_KEY_DOWN:
buf[0] = 0x1b;
buf[1] = '[';
buf[2] = 66;
return 3;
case HID_USAGE_KEY_RIGHT:
buf[0] = 0x1b;
buf[1] = '[';
buf[2] = 67;
return 3;
case HID_USAGE_KEY_LEFT:
buf[0] = 0x1b;
buf[1] = '[';
buf[2] = 68;
return 3;
case HID_USAGE_KEY_HOME:
buf[0] = 0x1b;
buf[1] = '[';
buf[2] = 'H';
return 3;
case HID_USAGE_KEY_END:
buf[0] = 0x1b;
buf[1] = '[';
buf[2] = 'F';
return 3;
case HID_USAGE_KEY_DELETE:
buf[0] = 0x1b;
buf[1] = '[';
buf[2] = '3';
buf[3] = '~';
return 4;
case HID_USAGE_KEY_PAGEUP:
buf[0] = 0x1b;
buf[1] = '[';
buf[2] = '5';
buf[3] = '~';
return 4;
case HID_USAGE_KEY_PAGEDOWN:
buf[0] = 0x1b;
buf[1] = '[';
buf[2] = '6';
buf[3] = '~';
return 4;
}
// ignore unknown keys; character keys were handled above
return 0;
}