Debian Codegraph @

deps/winpty/src/agent/ConsoleInputReencoding.cc

raw
1 // Copyright (c) 2016 Ryan Prichard
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20
21 #include "ConsoleInputReencoding.h"
22
23 #include "ConsoleInput.h"
24
25 namespace {
26
27 static void outch(std::vector<INPUT_RECORD> &out, wchar_t ch) {
28 ConsoleInput::appendInputRecord(out, TRUE, 0, ch, 0);
29 }
30
31 } // anonymous namespace
32
33 void reencodeEscapedKeyPress(
34 std::vector<INPUT_RECORD> &out,
35 uint16_t virtualKey,
36 uint32_t codePoint,
37 uint16_t keyState) {
38
39 struct EscapedKey {
40 enum { None, Numeric, Letter } kind;
41 wchar_t content[2];
42 };
43
44 EscapedKey escapeCode = {};
45 switch (virtualKey) {
46 case VK_UP: escapeCode = { EscapedKey::Letter, {'A'} }; break;
47 case VK_DOWN: escapeCode = { EscapedKey::Letter, {'B'} }; break;
48 case VK_RIGHT: escapeCode = { EscapedKey::Letter, {'C'} }; break;
49 case VK_LEFT: escapeCode = { EscapedKey::Letter, {'D'} }; break;
50 case VK_CLEAR: escapeCode = { EscapedKey::Letter, {'E'} }; break;
51 case VK_F1: escapeCode = { EscapedKey::Numeric, {'1', '1'} }; break;
52 case VK_F2: escapeCode = { EscapedKey::Numeric, {'1', '2'} }; break;
53 case VK_F3: escapeCode = { EscapedKey::Numeric, {'1', '3'} }; break;
54 case VK_F4: escapeCode = { EscapedKey::Numeric, {'1', '4'} }; break;
55 case VK_F5: escapeCode = { EscapedKey::Numeric, {'1', '5'} }; break;
56 case VK_F6: escapeCode = { EscapedKey::Numeric, {'1', '7'} }; break;
57 case VK_F7: escapeCode = { EscapedKey::Numeric, {'1', '8'} }; break;
58 case VK_F8: escapeCode = { EscapedKey::Numeric, {'1', '9'} }; break;
59 case VK_F9: escapeCode = { EscapedKey::Numeric, {'2', '0'} }; break;
60 case VK_F10: escapeCode = { EscapedKey::Numeric, {'2', '1'} }; break;
61 case VK_F11: escapeCode = { EscapedKey::Numeric, {'2', '3'} }; break;
62 case VK_F12: escapeCode = { EscapedKey::Numeric, {'2', '4'} }; break;
63 case VK_HOME: escapeCode = { EscapedKey::Letter, {'H'} }; break;
64 case VK_INSERT: escapeCode = { EscapedKey::Numeric, {'2'} }; break;
65 case VK_DELETE: escapeCode = { EscapedKey::Numeric, {'3'} }; break;
66 case VK_END: escapeCode = { EscapedKey::Letter, {'F'} }; break;
67 case VK_PRIOR: escapeCode = { EscapedKey::Numeric, {'5'} }; break;
68 case VK_NEXT: escapeCode = { EscapedKey::Numeric, {'6'} }; break;
69 }
70 if (escapeCode.kind != EscapedKey::None) {
71 int flags = 0;
72 if (keyState & SHIFT_PRESSED) { flags |= 0x1; }
73 if (keyState & LEFT_ALT_PRESSED) { flags |= 0x2; }
74 if (keyState & LEFT_CTRL_PRESSED) { flags |= 0x4; }
75 outch(out, L'\x1b');
76 outch(out, L'[');
77 if (escapeCode.kind == EscapedKey::Numeric) {
78 for (wchar_t ch : escapeCode.content) {
79 if (ch != L'\0') {
80 outch(out, ch);
81 }
82 }
83 } else if (flags != 0) {
84 outch(out, L'1');
85 }
86 if (flags != 0) {
87 outch(out, L';');
88 outch(out, L'1' + flags);
89 }
90 if (escapeCode.kind == EscapedKey::Numeric) {
91 outch(out, L'~');
92 } else {
93 outch(out, escapeCode.content[0]);
94 }
95 return;
96 }
97
98 switch (virtualKey) {
99 case VK_BACK:
100 if (keyState & LEFT_ALT_PRESSED) {
101 outch(out, L'\x1b');
102 }
103 outch(out, L'\x7f');
104 return;
105 case VK_TAB:
106 if (keyState & SHIFT_PRESSED) {
107 outch(out, L'\x1b');
108 outch(out, L'[');
109 outch(out, L'Z');
110 return;
111 }
112 break;
113 }
114
115 if (codePoint != 0) {
116 if (keyState & LEFT_ALT_PRESSED) {
117 outch(out, L'\x1b');
118 }
119 ConsoleInput::appendCPInputRecords(out, TRUE, 0, codePoint, 0);
120 }
121 }