2023-08-28 12:31:18 +02:00
|
|
|
/* Terminal handling.
|
2018-10-19 16:32:44 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-10-19 16:32:44 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <villas/exceptions.hpp>
|
|
|
|
#include <villas/log.hpp>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <villas/terminal.hpp>
|
2018-10-19 16:32:44 +02:00
|
|
|
|
|
|
|
using namespace villas;
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
class Terminal *Terminal::current = nullptr;
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Terminal::Terminal() {
|
|
|
|
int ret;
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
window.ws_row = 0;
|
|
|
|
window.ws_col = 0;
|
2019-02-12 16:17:36 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
isTty = isatty(STDERR_FILENO);
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Logger logger = logging.get("terminal");
|
2019-10-27 20:23:47 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
if (isTty) {
|
|
|
|
struct sigaction sa_resize;
|
|
|
|
sa_resize.sa_flags = SA_SIGINFO;
|
|
|
|
sa_resize.sa_sigaction = resize;
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
sigemptyset(&sa_resize.sa_mask);
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = sigaction(SIGWINCH, &sa_resize, nullptr);
|
|
|
|
if (ret)
|
|
|
|
throw SystemError("Failed to register signal handler");
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Try to get initial terminal dimensions
|
|
|
|
ret = ioctl(STDERR_FILENO, TIOCGWINSZ, &window);
|
|
|
|
if (ret)
|
|
|
|
logger->warn("Failed to get terminal dimensions");
|
|
|
|
} else {
|
|
|
|
logger->info("stderr is not associated with a terminal! Using fallback "
|
|
|
|
"values for window size...");
|
|
|
|
}
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Fallback if for some reason we can not determine a prober window size
|
|
|
|
if (window.ws_col == 0)
|
|
|
|
window.ws_col = 150;
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
if (window.ws_row == 0)
|
|
|
|
window.ws_row = 50;
|
2018-10-19 16:32:44 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void Terminal::resize(int, siginfo_t *, void *) {
|
|
|
|
if (!current)
|
|
|
|
current = new Terminal();
|
2021-09-20 15:27:22 +00:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Logger logger = logging.get("terminal");
|
2021-09-20 15:27:22 +00:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int ret;
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = ioctl(STDERR_FILENO, TIOCGWINSZ, ¤t->window);
|
|
|
|
if (ret)
|
|
|
|
throw SystemError("Failed to get terminal dimensions");
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
logger->debug("New terminal size: {}x{}", current->window.ws_row,
|
|
|
|
current->window.ws_col);
|
2018-10-19 16:32:44 +02:00
|
|
|
};
|