2022-01-27 20:03:10 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright 2021 Johannes Marbach
|
2024-01-12 09:51:43 +01:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2022-01-27 20:03:10 +01:00
|
|
|
*/
|
|
|
|
|
|
2024-01-12 10:00:05 +01:00
|
|
|
|
2022-01-27 20:03:10 +01:00
|
|
|
#include "terminal.h"
|
|
|
|
|
|
2024-03-28 13:44:33 +01:00
|
|
|
#include "../shared/log.h"
|
2022-01-27 20:03:10 +01:00
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <linux/kd.h>
|
|
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Static variables
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int current_fd = -1;
|
|
|
|
|
|
2023-03-13 21:27:39 +01:00
|
|
|
static int original_mode = -1;
|
2025-05-12 14:25:43 +02:00
|
|
|
static int original_kb_mode = -1;
|
2022-07-09 17:10:45 +02:00
|
|
|
|
2022-01-27 20:03:10 +01:00
|
|
|
/**
|
|
|
|
|
* Public functions
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-23 01:48:35 -07:00
|
|
|
void bbx_terminal_prepare_current_terminal(bool enable_graphics_mode, bool disable_keyboard_input) {
|
2023-03-13 21:27:39 +01:00
|
|
|
/* Exit early if there is nothing to do */
|
|
|
|
|
if (!enable_graphics_mode && !disable_keyboard_input) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-12 17:47:29 +03:00
|
|
|
/* Open the current terminal */
|
|
|
|
|
current_fd = open("/dev/tty0", O_RDWR);
|
2022-07-09 17:10:45 +02:00
|
|
|
if (current_fd < 0) {
|
2025-05-12 17:47:29 +03:00
|
|
|
bbx_log(BBX_LOG_LEVEL_WARNING, "Could not open /dev/tty0");
|
2022-07-09 17:10:45 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-13 21:27:39 +01:00
|
|
|
/* Disable terminal keyboard input (hides entered text) */
|
|
|
|
|
if (disable_keyboard_input) {
|
|
|
|
|
if (ioctl(current_fd, KDGKBMODE, &original_kb_mode) != 0) {
|
2024-03-30 20:52:01 +01:00
|
|
|
bbx_log(BBX_LOG_LEVEL_WARNING, "Could not get terminal keyboard mode");
|
2023-03-13 21:27:39 +01:00
|
|
|
}
|
2022-07-09 17:10:45 +02:00
|
|
|
|
2023-03-13 21:27:39 +01:00
|
|
|
if (ioctl(current_fd, KDSKBMODE, K_OFF) != 0) {
|
2024-03-30 20:52:01 +01:00
|
|
|
bbx_log(BBX_LOG_LEVEL_WARNING, "Could not set terminal keyboard mode to off");
|
2023-03-13 21:27:39 +01:00
|
|
|
}
|
2022-07-09 17:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-13 21:27:39 +01:00
|
|
|
/* Switch terminal into graphics mode (hides command prompt) */
|
|
|
|
|
if (enable_graphics_mode) {
|
|
|
|
|
if (ioctl(current_fd, KDGETMODE, &original_mode) != 0) {
|
2024-03-30 20:52:01 +01:00
|
|
|
bbx_log(BBX_LOG_LEVEL_WARNING, "Could not get terminal mode");
|
2023-03-13 21:27:39 +01:00
|
|
|
}
|
2022-10-03 13:04:26 +02:00
|
|
|
|
2023-03-13 21:27:39 +01:00
|
|
|
if (ioctl(current_fd, KDSETMODE, KD_GRAPHICS) != 0) {
|
2024-03-30 20:52:01 +01:00
|
|
|
bbx_log(BBX_LOG_LEVEL_WARNING, "Could not set terminal mode to graphics");
|
2023-03-13 21:27:39 +01:00
|
|
|
}
|
2022-10-03 13:04:26 +02:00
|
|
|
}
|
2022-01-27 20:03:10 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 01:48:35 -07:00
|
|
|
void bbx_terminal_reset_current_terminal(void) {
|
2023-03-13 21:27:39 +01:00
|
|
|
/* If we haven't previously opened the current terminal, exit */
|
2022-07-09 17:10:45 +02:00
|
|
|
if (current_fd < 0) {
|
2024-03-30 20:52:01 +01:00
|
|
|
bbx_log(BBX_LOG_LEVEL_WARNING, "Could not reset current terminal");
|
2022-07-09 17:10:45 +02:00
|
|
|
return;
|
2022-01-27 20:03:10 +01:00
|
|
|
}
|
2022-07-09 17:10:45 +02:00
|
|
|
|
2023-03-13 21:27:39 +01:00
|
|
|
/* Reset terminal mode if needed */
|
|
|
|
|
if (original_mode >= 0 && ioctl(current_fd, KDSETMODE, original_mode) != 0) {
|
2024-03-30 20:52:01 +01:00
|
|
|
bbx_log(BBX_LOG_LEVEL_WARNING, "Could not reset terminal mode");
|
2022-07-09 17:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-13 21:27:39 +01:00
|
|
|
/* Reset terminal keyboard mode if needed */
|
|
|
|
|
if (original_kb_mode >= 0 && ioctl(current_fd, KDSKBMODE, original_kb_mode) != 0) {
|
2024-03-30 20:52:01 +01:00
|
|
|
bbx_log(BBX_LOG_LEVEL_WARNING, "Could not reset terminal keyboard mode");
|
2022-07-09 17:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-12 17:47:29 +03:00
|
|
|
close(current_fd);
|
|
|
|
|
current_fd = -1;
|
2022-01-27 20:03:10 +01:00
|
|
|
}
|