From b6e040dfc2ea8accd4d54a434f4d9fe821a56cb5 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 4 Oct 2024 04:14:47 +0300 Subject: [PATCH] Kernel/Shell: Fix backspace and delete key byte sequences --- kernel/kernel/Terminal/Serial.cpp | 2 - kernel/kernel/Terminal/TTY.cpp | 73 +---------------------- userspace/libraries/LibInput/KeyEvent.cpp | 12 ++-- userspace/programs/Shell/main.cpp | 55 +++++++++++------ 4 files changed, 46 insertions(+), 96 deletions(-) diff --git a/kernel/kernel/Terminal/Serial.cpp b/kernel/kernel/Terminal/Serial.cpp index c6d7febb07..8f92668a18 100644 --- a/kernel/kernel/Terminal/Serial.cpp +++ b/kernel/kernel/Terminal/Serial.cpp @@ -237,8 +237,6 @@ namespace Kernel *ptr = m_input.front(); if (*ptr == '\r') *ptr = '\n'; - if (*ptr == 127) - *ptr++ = '\b', *ptr++ = ' ', *ptr = '\b'; m_input.pop(); ptr++; } diff --git a/kernel/kernel/Terminal/TTY.cpp b/kernel/kernel/Terminal/TTY.cpp index 8cf7e18d4f..aecd9a0fe0 100644 --- a/kernel/kernel/Terminal/TTY.cpp +++ b/kernel/kernel/Terminal/TTY.cpp @@ -185,79 +185,10 @@ namespace Kernel if (event.released()) return; - const char* ansi_c_str = LibInput::key_to_utf8(event.key, event.modifier); - - if (event.ctrl()) - { - ansi_c_str = nullptr; - switch (event.key) - { - case LibInput::Key::A: ansi_c_str = "\x01"; break; - case LibInput::Key::B: ansi_c_str = "\x02"; break; - case LibInput::Key::C: ansi_c_str = "\x03"; break; - case LibInput::Key::D: ansi_c_str = "\x04"; break; - case LibInput::Key::E: ansi_c_str = "\x05"; break; - case LibInput::Key::F: ansi_c_str = "\x06"; break; - case LibInput::Key::G: ansi_c_str = "\x07"; break; - case LibInput::Key::H: ansi_c_str = "\x08"; break; - case LibInput::Key::I: ansi_c_str = "\x09"; break; - case LibInput::Key::J: ansi_c_str = "\x0A"; break; - case LibInput::Key::K: ansi_c_str = "\x0B"; break; - case LibInput::Key::L: ansi_c_str = "\x0C"; break; - case LibInput::Key::M: ansi_c_str = "\x0D"; break; - case LibInput::Key::N: ansi_c_str = "\x0E"; break; - case LibInput::Key::O: ansi_c_str = "\x0F"; break; - case LibInput::Key::P: ansi_c_str = "\x10"; break; - case LibInput::Key::Q: ansi_c_str = "\x11"; break; - case LibInput::Key::R: ansi_c_str = "\x12"; break; - case LibInput::Key::S: ansi_c_str = "\x13"; break; - case LibInput::Key::T: ansi_c_str = "\x14"; break; - case LibInput::Key::U: ansi_c_str = "\x15"; break; - case LibInput::Key::V: ansi_c_str = "\x16"; break; - case LibInput::Key::W: ansi_c_str = "\x17"; break; - case LibInput::Key::X: ansi_c_str = "\x18"; break; - case LibInput::Key::Y: ansi_c_str = "\x19"; break; - case LibInput::Key::Z: ansi_c_str = "\x1A"; break; - default: break; - } - } - else - { - switch (event.key) - { - case LibInput::Key::Enter: - case LibInput::Key::NumpadEnter: - ansi_c_str = "\n"; - break; - case LibInput::Key::Backspace: - ansi_c_str = "\b"; - break; - case LibInput::Key::Escape: - ansi_c_str = "\e"; - break; - case LibInput::Key::Delete: - ansi_c_str = "\x7F"; - break; - case LibInput::Key::ArrowUp: - ansi_c_str = "\e[A"; - break; - case LibInput::Key::ArrowDown: - ansi_c_str = "\e[B"; - break; - case LibInput::Key::ArrowRight: - ansi_c_str = "\e[C"; - break; - case LibInput::Key::ArrowLeft: - ansi_c_str = "\e[D"; - break; - default: - break; - } - } - + const char* ansi_c_str = LibInput::key_to_utf8_ansi(event.key, event.modifier); if (ansi_c_str) { - auto* ptr = (const uint8_t*)ansi_c_str; + auto* ptr = reinterpret_cast(ansi_c_str); while (*ptr) handle_input_byte(*ptr++); } diff --git a/userspace/libraries/LibInput/KeyEvent.cpp b/userspace/libraries/LibInput/KeyEvent.cpp index 9c7888b225..9f83631089 100644 --- a/userspace/libraries/LibInput/KeyEvent.cpp +++ b/userspace/libraries/LibInput/KeyEvent.cpp @@ -58,10 +58,10 @@ namespace LibInput "å", "ä", "ö", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, - /*"Insert", "PrintScreen", "Delete", "Home", "End", "PageUp", "PageDown",*/ nullptr, nullptr, "\x7F", nullptr, nullptr, nullptr, nullptr, "\n", " ", + /*"Insert", "PrintScreen", "Delete", "Home", "End", "PageUp", "PageDown",*/ nullptr, nullptr, "\e[3~", nullptr, nullptr, nullptr, nullptr, "\n", " ", "!", "\"", "#", "¤", "%", "&", "/", "§", "½", "(", ")", "[", "]", "{", "}", - "=", "?", "+", "\\", "´", "`", "¨", "¸", "\b \b", "@", "£", "$", "€", + "=", "?", "+", "\\", "´", "`", "¨", "¸", "\x7F", "@", "£", "$", "€", "\e", "\t", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, "'", "*", "^", "~", "\e[A", "\e[B", "\e[D", "\e[C", ",", ";", ".", ":", "-", "_", nullptr, nullptr, "<", ">", "|", "¬", "¦", @@ -77,10 +77,10 @@ namespace LibInput "Å", "Ä", "Ö", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, - /*"Insert", "PrintScreen", "Delete", "Home", "End", "PageUp", "PageDown",*/ nullptr, nullptr, "\x7F", nullptr, nullptr, nullptr, nullptr, "\n", " ", + /*"Insert", "PrintScreen", "Delete", "Home", "End", "PageUp", "PageDown",*/ nullptr, nullptr, "\e[3~", nullptr, nullptr, nullptr, nullptr, "\n", " ", "!", "\"", "#", "¤", "%", "&", "/", "§", "½", "(", ")", "[", "]", "{", "}", - "=", "?", "+", "\\", "´", "`", "¨", "¸", "\b \b", "@", "£", "$", "€", + "=", "?", "+", "\\", "´", "`", "¨", "¸", "\x7F", "@", "£", "$", "€", "\e", "\t", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, "'", "*", "^", "~", "\e[A", "\e[B", "\e[D", "\e[C", ",", ";", ".", ":", "-", "_", nullptr, nullptr, "<", ">", "|", "¬", "¦", @@ -96,10 +96,10 @@ namespace LibInput "Å", "Ä", "Ö", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, - /*"Insert", "PrintScreen", "Delete", "Home", "End", "PageUp", "PageDown",*/ nullptr, nullptr, "\x7F", nullptr, nullptr, nullptr, nullptr, "\n", " ", + /*"Insert", "PrintScreen", "Delete", "Home", "End", "PageUp", "PageDown",*/ nullptr, nullptr, "\e[3~", nullptr, nullptr, nullptr, nullptr, "\n", " ", "!", "\"", "#", "¤", "%", "&", "/", "§", "½", "(", ")", "[", "]", "{", "}", - "=", "?", "+", "\\", "´", "`", "¨", "¸", "\b \b", "@", "£", "$", "€", + "=", "?", "+", "\\", "´", "`", "¨", "¸", "\x7F", "@", "£", "$", "€", "\e", "\t", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, "'", "*", "^", "~", "\e[A", "\e[B", "\e[D", "\e[C", ",", ";", ".", ":", "-", "_", nullptr, nullptr, "<", ">", "|", "¬", "¦", diff --git a/userspace/programs/Shell/main.cpp b/userspace/programs/Shell/main.cpp index 5d3c8f3431..ba2eaaeecf 100644 --- a/userspace/programs/Shell/main.cpp +++ b/userspace/programs/Shell/main.cpp @@ -894,18 +894,41 @@ int main(int argc, char** argv) switch (ch) { case '\e': + { ch = getchar(); if (ch != '[') break; ch = getchar(); + + int value = 0; + while (isdigit(ch)) + { + value = (value * 10) + (ch - '0'); + ch = getchar(); + } + switch (ch) { case 'A': if (index > 0) { index--; col = buffers[index].size(); printf("\e[%dG%s\e[K", prompt_length() + 1, buffers[index].data()); fflush(stdout); } break; case 'B': if (index < buffers.size() - 1) { index++; col = buffers[index].size(); printf("\e[%dG%s\e[K", prompt_length() + 1, buffers[index].data()); fflush(stdout); } break; case 'C': if (col < buffers[index].size()) { col++; while ((buffers[index][col - 1] & 0xC0) == 0x80) col++; printf("\e[C"); fflush(stdout); } break; case 'D': if (col > 0) { while ((buffers[index][col - 1] & 0xC0) == 0x80) col--; col--; printf("\e[D"); fflush(stdout); } break; + case '~': + switch (value) + { + case 3: // delete + if (col >= buffers[index].size()) + break; + buffers[index].remove(col); + while (col < buffers[index].size() && (buffers[index][col] & 0xC0) == 0x80) + buffers[index].remove(col); + printf("\e[s%s \e[u", buffers[index].data() + col); + fflush(stdout); + break; + } } break; + } case '\x0C': // ^L { int x = prompt_length() + character_length(buffers[index].sv().substring(col)) + 1; @@ -916,14 +939,13 @@ int main(int argc, char** argv) break; } case '\b': - if (col > 0) - { - while ((buffers[index][col - 1] & 0xC0) == 0x80) - buffers[index].remove(--col); - buffers[index].remove(--col); - printf("\b\e[s%s \e[u", buffers[index].data() + col); - fflush(stdout); - } + if (col <= 0) + break; + while ((buffers[index][col - 1] & 0xC0) == 0x80) + col--; + col--; + printf("\e[D"); + fflush(stdout); break; case '\x01': // ^A col = 0; @@ -940,15 +962,14 @@ int main(int argc, char** argv) putchar('\n'); clean_exit(0); break; - case '\x7F': // delete - if (col < buffers[index].size()) - { - buffers[index].remove(col); - while (col < buffers[index].size() && (buffers[index][col] & 0xC0) == 0x80) - buffers[index].remove(col); - printf("\e[s%s \e[u", buffers[index].data() + col); - fflush(stdout); - } + case '\x7F': // backspace + if (col <= 0) + break; + while ((buffers[index][col - 1] & 0xC0) == 0x80) + buffers[index].remove(--col); + buffers[index].remove(--col); + printf("\b\e[s%s \e[u", buffers[index].data() + col); + fflush(stdout); break; case '\n': putchar('\n');