From 51380c765425810fc974dab17a7820fb42de455f Mon Sep 17 00:00:00 2001 From: Johannes Marbach Date: Sat, 9 Jul 2022 17:10:45 +0200 Subject: [PATCH] Disable terminal keyboard while unl0kr is running --- CHANGELOG.md | 1 + terminal.c | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11b70af..b8cad61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - feat: Add config option to customise bullet character (#17) - fix: Use actual screen DPI value to compute sizes and spaces - feat: Allow shutting down the device on inactivity +- fix: Disable terminal keyboard while unl0kr is running ## 0.2.0 (2022-05-27) diff --git a/terminal.c b/terminal.c index d07b3bb..ff94582 100644 --- a/terminal.c +++ b/terminal.c @@ -37,6 +37,9 @@ static int current_fd = -1; +static int original_mode = KD_TEXT; +static int original_kb_mode = K_UNICODE; + /** * Static prototypes @@ -87,14 +90,42 @@ static void close_current_terminal(void) { void ul_terminal_prepare_current_terminal(void) { reopen_current_terminal(); - if (current_fd < 0 || ioctl(current_fd, KDSETMODE, KD_GRAPHICS) != 0) { - ul_log(UL_LOG_LEVEL_WARNING, "Could not set current terminal to graphics mode"); + + if (current_fd < 0) { + ul_log(UL_LOG_LEVEL_WARNING, "Could not prepare current terminal"); + return; + } + + if (ioctl(current_fd, KDGETMODE, &original_mode) != 0) { + ul_log(UL_LOG_LEVEL_WARNING, "Could not get terminal mode"); + } + + if (ioctl(current_fd, KDSETMODE, KD_GRAPHICS) != 0) { + ul_log(UL_LOG_LEVEL_WARNING, "Could not set terminal mode to graphics"); + } + + if (ioctl(current_fd, KDGKBMODE, &original_kb_mode) != 0) { + ul_log(UL_LOG_LEVEL_WARNING, "Could not get terminal keyboard mode"); + } + + if (ioctl(current_fd, KDSKBMODE, K_OFF) != 0) { + ul_log(UL_LOG_LEVEL_WARNING, "Could not set terminal keyboard mode to off"); } } void ul_terminal_reset_current_terminal(void) { - if (current_fd < 0 || ioctl(current_fd, KDSETMODE, KD_TEXT) != 0) { - ul_log(UL_LOG_LEVEL_WARNING, "Could not reset current terminal to text mode"); + if (current_fd < 0) { + ul_log(UL_LOG_LEVEL_WARNING, "Could not reset current terminal"); + return; } + + if (ioctl(current_fd, KDSETMODE, original_mode) != 0) { + ul_log(UL_LOG_LEVEL_WARNING, "Could not reset terminal mode"); + } + + if (ioctl(current_fd, KDSKBMODE, original_kb_mode) != 0) { + ul_log(UL_LOG_LEVEL_WARNING, "Could not reset terminal keyboard mode"); + } + close_current_terminal(); }