Skip to content

Commit 58d252f

Browse files
committed
Support more keystrokes in DECCKM mode
1 parent 95fee86 commit 58d252f

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

examples/ttyinfo.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ void OnSignalThatWillEintrRead(int sig) {
161161
}
162162

163163
int main(int argc, char *argv[]) {
164+
165+
// // emacs sends this to enable decckm mode
166+
// WRITE(1, "\e[?1049h\e[22;0;0t\e[?12;25h\e[?1h\e=");
167+
164168
int e, c, y, x, n, yn, xn;
165169
infd = 0;
166170
outfd = 1;

libc/calls/read-nt.c

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ struct VirtualKey {
8585
#define S(s) W(s "\0\0")
8686
#define W(s) (s[3] << 24 | s[2] << 16 | s[1] << 8 | s[0])
8787

88-
static const struct VirtualKey kVirtualKey[] = {
89-
{kNtVkUp, S("A"), S("1;2A"), S("1;5A"), S("1;6A")},
90-
{kNtVkDown, S("B"), S("1;2B"), S("1;5B"), S("1;6B")},
91-
{kNtVkRight, S("C"), S("1;2C"), S("1;5C"), S("1;6C")},
92-
{kNtVkLeft, S("D"), S("1;2D"), S("1;5D"), S("1;6D")},
88+
static struct VirtualKey kVirtualKey[] = {
89+
{kNtVkUp, S("A"), S("1;2A"), S("1;5A"), S("1;6A")}, // order matters
90+
{kNtVkDown, S("B"), S("1;2B"), S("1;5B"), S("1;6B")}, // order matters
91+
{kNtVkRight, S("C"), S("1;2C"), S("1;5C"), S("1;6C")}, // order matters
92+
{kNtVkLeft, S("D"), S("1;2D"), S("1;5D"), S("1;6D")}, // order matters
93+
{kNtVkEnd, S("F"), S("1;2F"), S("1;5F"), S("1;6F")}, // order matters
94+
{kNtVkHome, S("H"), S("1;2H"), S("1;5H"), S("1;6H")}, // order matters
9395
{kNtVkInsert, S("2~"), S("2;2~"), S("2;5~"), S("2;6~")},
9496
{kNtVkDelete, S("3~"), S("3;2~"), S("3;5~"), S("3;6~")},
95-
{kNtVkHome, S("H"), S("1;2H"), S("1;5H"), S("1;6H")},
96-
{kNtVkEnd, S("F"), S("1;2F"), S("1;5F"), S("1;6F")},
9797
{kNtVkPrior, S("5~"), S("5;2~"), S("5;5~"), S("5;6~")},
9898
{kNtVkNext, S("6~"), S("6;2~"), S("6;5~"), S("6;6~")},
9999
{kNtVkF1, -S("OP"), S("1;2P"), S("11^"), S("1;6P")},
@@ -111,17 +111,6 @@ static const struct VirtualKey kVirtualKey[] = {
111111
{0},
112112
};
113113

114-
// TODO: How can we configure `less` to not need this bloat?
115-
static const struct VirtualKey kDecckm[] = {
116-
{kNtVkUp, -S("OA"), -S("OA"), S("A"), S("A")},
117-
{kNtVkDown, -S("OB"), -S("OB"), S("B"), S("B")},
118-
{kNtVkRight, -S("OC"), -S("OC"), S("C"), S("C")},
119-
{kNtVkLeft, -S("OD"), -S("OD"), S("D"), S("D")},
120-
{kNtVkPrior, S("5~"), S("5;2~"), S("5;5~"), S("5;6~")},
121-
{kNtVkNext, S("6~"), S("6;2~"), S("6;5~"), S("6;6~")},
122-
{0},
123-
};
124-
125114
#define KEYSTROKE_CONTAINER(e) DLL_CONTAINER(struct Keystroke, elem, e)
126115

127116
struct Keystroke {
@@ -142,7 +131,6 @@ struct Keystrokes {
142131
struct Dll *line;
143132
struct Dll *free;
144133
pthread_mutex_t lock;
145-
const struct VirtualKey *vkt;
146134
struct Keystroke pool[512];
147135
};
148136

@@ -180,7 +168,6 @@ textwindows static void FreeKeystrokes(struct Dll **list) {
180168
}
181169

182170
textwindows static void OpenConsole(void) {
183-
__keystroke.vkt = kVirtualKey;
184171
__keystroke.cin = CreateFile(u"CONIN$", kNtGenericRead | kNtGenericWrite,
185172
kNtFileShareRead, 0, kNtOpenExisting, 0, 0);
186173
__keystroke.cot = CreateFile(u"CONOUT$", kNtGenericRead | kNtGenericWrite,
@@ -227,16 +214,16 @@ textwindows static bool IsMouseModeCommand(int x) {
227214
}
228215

229216
textwindows static int GetVirtualKey(uint16_t vk, bool shift, bool ctrl) {
230-
for (int i = 0; __keystroke.vkt[i].vk; ++i) {
231-
if (__keystroke.vkt[i].vk == vk) {
217+
for (int i = 0; kVirtualKey[i].vk; ++i) {
218+
if (kVirtualKey[i].vk == vk) {
232219
if (shift && ctrl) {
233-
return __keystroke.vkt[i].shift_ctrl_str;
220+
return kVirtualKey[i].shift_ctrl_str;
234221
} else if (shift) {
235-
return __keystroke.vkt[i].shift_str;
222+
return kVirtualKey[i].shift_str;
236223
} else if (ctrl) {
237-
return __keystroke.vkt[i].ctrl_str;
224+
return kVirtualKey[i].ctrl_str;
238225
} else {
239-
return __keystroke.vkt[i].normal_str;
226+
return kVirtualKey[i].normal_str;
240227
}
241228
}
242229
}
@@ -737,8 +724,14 @@ textwindows void InterceptTerminalCommands(const char *data, size_t size) {
737724
x = 0;
738725
} else if (data[i] == 'h') {
739726
if (x == 1) {
740-
__keystroke.vkt = kDecckm; // \e[?1h decckm on
727+
// \e[?1h decckm on
741728
__keystroke.ohno_decckm = true;
729+
kVirtualKey[0].normal_str = -S("OA"); // kNtVkUp
730+
kVirtualKey[1].normal_str = -S("OB"); // kNtVkDown
731+
kVirtualKey[2].normal_str = -S("OC"); // kNtVkRight
732+
kVirtualKey[3].normal_str = -S("OD"); // kNtVkLeft
733+
kVirtualKey[4].normal_str = -S("OF"); // kNtVkEnd
734+
kVirtualKey[5].normal_str = -S("OH"); // kNtVkHome
742735
} else if ((ismouse |= IsMouseModeCommand(x))) {
743736
__ttyconf.magic |= kTtyXtMouse;
744737
cm2 |= kNtEnableMouseInput;
@@ -747,8 +740,14 @@ textwindows void InterceptTerminalCommands(const char *data, size_t size) {
747740
t = ASC;
748741
} else if (data[i] == 'l') {
749742
if (x == 1) {
750-
__keystroke.vkt = kVirtualKey; // \e[?1l decckm off
743+
// \e[?1l decckm off
751744
__keystroke.ohno_decckm = false;
745+
kVirtualKey[0].normal_str = S("A"); // kNtVkUp
746+
kVirtualKey[1].normal_str = S("B"); // kNtVkDown
747+
kVirtualKey[2].normal_str = S("C"); // kNtVkRight
748+
kVirtualKey[3].normal_str = S("D"); // kNtVkLeft
749+
kVirtualKey[4].normal_str = S("F"); // kNtVkEnd
750+
kVirtualKey[5].normal_str = S("H"); // kNtVkHome
752751
} else if ((ismouse |= IsMouseModeCommand(x))) {
753752
__ttyconf.magic &= ~kTtyXtMouse;
754753
cm2 |= kNtEnableQuickEditMode; // release mouse

0 commit comments

Comments
 (0)