Add config options to enable terminal quirks

Relates to: #34
This commit is contained in:
Johannes Marbach 2023-03-13 21:27:39 +01:00
parent 36d44b64ae
commit 1bf46979eb
7 changed files with 68 additions and 25 deletions

View file

@ -12,6 +12,7 @@ Nothing at the moment
- feat: Update lvgl to v8.3 (2023-03-08)
- feat!: Deprecate -c CLI option and add support for reading from /etc/unl0kr.conf.d/
- fix: Print correct version in logs
- feat: Add config options to enable terminal quirks (#34)
## 0.3.0 (2022-11-13)

View file

@ -229,6 +229,16 @@ static int parsing_handler(void* user_data, const char* section, const char* key
return 1;
}
}
} else if (strcmp(section, "quirks") == 0) {
if (strcmp(key, "terminal_prevent_graphics_mode") == 0) {
if (parse_bool(value, &(opts->quirks.terminal_prevent_graphics_mode))) {
return 1;
}
} else if (strcmp(key, "terminal_allow_keyboard_input") == 0) {
if (parse_bool(value, &(opts->quirks.terminal_allow_keyboard_input))) {
return 1;
}
}
}
ul_log(UL_LOG_LEVEL_ERROR, "Ignoring invalid config value \"%s\" for key \"%s\" in section \"%s\"", value, key, section);
@ -268,6 +278,8 @@ void ul_config_init_opts(ul_config_opts *opts) {
opts->input.keyboard = true;
opts->input.pointer = true;
opts->input.touchscreen = true;
opts->quirks.terminal_prevent_graphics_mode = false;
opts->quirks.terminal_allow_keyboard_input = false;
}
void ul_config_parse_directory(const char *path, ul_config_opts *opts) {

View file

@ -34,10 +34,10 @@
* General options
*/
typedef struct {
/* Backend to use */
ul_backends_backend_id_t backend;
/* If true, use animations */
bool animations;
/* Backend to use */
ul_backends_backend_id_t backend;
/* Timeout (in seconds) - once elapsed, the device will shutdown. 0 (default) to disable */
uint16_t timeout;
} ul_config_opts_general;
@ -86,6 +86,16 @@ typedef struct {
bool touchscreen;
} ul_config_opts_input;
/**
* (Normally unneeded) quirky options
*/
typedef struct {
/* If true, do *not* switch terminal into graphics mode (will show terminal command prompt) */
bool terminal_prevent_graphics_mode;
/* If true, do *not* turn off terminal keyboard input (will show entered characters) */
bool terminal_allow_keyboard_input;
} ul_config_opts_quirks;
/**
* Options parsed from config file(s)
*/
@ -100,6 +110,8 @@ typedef struct {
ul_config_opts_theme theme;
/* Options related to input devices */
ul_config_opts_input input;
/* Options related to (normally unneeded) quirks */
ul_config_opts_quirks quirks;
} ul_config_opts;
/**

2
main.c
View file

@ -357,7 +357,7 @@ int main(int argc, char *argv[]) {
ul_config_parse_files(cli_opts.config_files, cli_opts.num_config_files, &conf_opts);
/* Prepare current TTY and clean up on termination */
ul_terminal_prepare_current_terminal();
ul_terminal_prepare_current_terminal(!conf_opts.quirks.terminal_prevent_graphics_mode, !conf_opts.quirks.terminal_allow_keyboard_input);
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = sigaction_handler;

View file

@ -37,8 +37,8 @@
static int current_fd = -1;
static int original_mode = KD_TEXT;
static int original_kb_mode = K_UNICODE;
static int original_mode = -1;
static int original_kb_mode = -1;
/**
@ -88,7 +88,13 @@ static void close_current_terminal(void) {
* Public functions
*/
void ul_terminal_prepare_current_terminal(void) {
void ul_terminal_prepare_current_terminal(bool enable_graphics_mode, bool disable_keyboard_input) {
/* Exit early if there is nothing to do */
if (!enable_graphics_mode && !disable_keyboard_input) {
return;
}
/* Reopen the current terminal */
reopen_current_terminal();
if (current_fd < 0) {
@ -96,40 +102,43 @@ void ul_terminal_prepare_current_terminal(void) {
return;
}
// NB: The order of calls appears to matter for some devices. See
// https://gitlab.com/cherrypicker/unl0kr/-/issues/34 for further info.
/* Disable terminal keyboard input (hides entered text) */
if (disable_keyboard_input) {
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, 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");
}
}
if (ioctl(current_fd, KDSKBMODE, K_OFF) != 0) {
ul_log(UL_LOG_LEVEL_WARNING, "Could not set terminal keyboard mode to off");
}
/* Switch terminal into graphics mode (hides command prompt) */
if (enable_graphics_mode) {
if (ioctl(current_fd, KDGETMODE, &original_mode) != 0) {
ul_log(UL_LOG_LEVEL_WARNING, "Could not get terminal mode");
}
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, KDSETMODE, KD_GRAPHICS) != 0) {
ul_log(UL_LOG_LEVEL_WARNING, "Could not set terminal mode to graphics");
}
}
}
void ul_terminal_reset_current_terminal(void) {
/* If we haven't previously opened the current terminal, exit */
if (current_fd < 0) {
ul_log(UL_LOG_LEVEL_WARNING, "Could not reset current terminal");
return;
}
// NB: The order of calls appears to matter for some devices. See
// https://gitlab.com/cherrypicker/unl0kr/-/issues/34 for further info.
if (ioctl(current_fd, KDSETMODE, original_mode) != 0) {
/* Reset terminal mode if needed */
if (original_mode >= 0 && 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) {
/* Reset terminal keyboard mode if needed */
if (original_kb_mode >= 0 && ioctl(current_fd, KDSKBMODE, original_kb_mode) != 0) {
ul_log(UL_LOG_LEVEL_WARNING, "Could not reset terminal keyboard mode");
}

View file

@ -21,10 +21,15 @@
#ifndef UL_TERMINAL_H
#define UL_TERMINAL_H
#include <stdbool.h>
/**
* Prepare the current TTY for graphics output.
*
* @param enable_graphics_mode if true, switch terminal into graphics mode (hides command prompt)
* @param disable_keyboard_input if true, disable terminal keyboard input (hides entered text)
*/
void ul_terminal_prepare_current_terminal(void);
void ul_terminal_prepare_current_terminal(bool enable_graphics_mode, bool disable_keyboard_input);
/**
* Reset the current TTY to text output.

View file

@ -20,3 +20,7 @@ alternate=breezy-dark
#keyboard=false
#pointer=false
#touchscreen=false
#[quirks]
#terminal_prevent_graphics_mode=true
#terminal_allow_keyboard_input=true