2018-10-19 16:32:44 +02:00
|
|
|
/** Terminal handling.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2021-06-21 16:09:11 -04:00
|
|
|
* @copyright 2014-2021, Institute for Automation of Complex Power Systems, EONERC
|
2018-10-19 16:32:44 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLAScommon
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <villas/terminal.hpp>
|
|
|
|
#include <villas/exceptions.hpp>
|
|
|
|
#include <villas/log.hpp>
|
|
|
|
|
|
|
|
using namespace villas;
|
|
|
|
|
2019-01-22 16:02:27 +01:00
|
|
|
class Terminal * Terminal::current = nullptr;
|
2018-10-19 16:32:44 +02:00
|
|
|
|
|
|
|
Terminal::Terminal()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-02-12 16:17:36 +01:00
|
|
|
window.ws_row = 0;
|
|
|
|
window.ws_col = 0;
|
|
|
|
|
2021-09-20 15:27:22 +00:00
|
|
|
isTty = isatty(STDERR_FILENO);
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2021-09-20 15:27:22 +00:00
|
|
|
Logger logger = logging.get("terminal");
|
2019-10-27 20:23:47 +01:00
|
|
|
|
2021-09-20 15:27:22 +00:00
|
|
|
if (isTty) {
|
2018-10-19 16:32:44 +02:00
|
|
|
struct sigaction sa_resize;
|
|
|
|
sa_resize.sa_flags = SA_SIGINFO;
|
|
|
|
sa_resize.sa_sigaction = resize;
|
|
|
|
|
|
|
|
sigemptyset(&sa_resize.sa_mask);
|
|
|
|
|
2019-04-07 15:12:32 +02:00
|
|
|
ret = sigaction(SIGWINCH, &sa_resize, nullptr);
|
2018-10-19 16:32:44 +02:00
|
|
|
if (ret)
|
2018-12-02 02:47:55 +01:00
|
|
|
throw SystemError("Failed to register signal handler");
|
2018-10-19 16:32:44 +02:00
|
|
|
|
|
|
|
/* Try to get initial terminal dimensions */
|
|
|
|
ret = ioctl(STDERR_FILENO, TIOCGWINSZ, &window);
|
|
|
|
if (ret)
|
2019-10-27 20:23:47 +01:00
|
|
|
logger->warn("Failed to get terminal dimensions");
|
2021-09-20 15:27:22 +00:00
|
|
|
} else {
|
|
|
|
logger->info("stderr is not associated with a terminal! Using fallback values for window size...");
|
2018-10-19 16:32:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fallback if for some reason we can not determine a prober window size */
|
|
|
|
if (window.ws_col == 0)
|
|
|
|
window.ws_col = 150;
|
|
|
|
|
|
|
|
if (window.ws_row == 0)
|
|
|
|
window.ws_row = 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Terminal::resize(int, siginfo_t *, void *)
|
|
|
|
{
|
2021-09-20 15:27:22 +00:00
|
|
|
if (!current)
|
|
|
|
current = new Terminal();
|
|
|
|
|
|
|
|
Logger logger = logging.get("terminal");
|
|
|
|
|
2018-10-19 16:32:44 +02:00
|
|
|
int ret;
|
|
|
|
|
2019-01-22 16:02:27 +01:00
|
|
|
ret = ioctl(STDERR_FILENO, TIOCGWINSZ, ¤t->window);
|
2018-10-19 16:32:44 +02:00
|
|
|
if (ret)
|
2018-12-02 02:47:55 +01:00
|
|
|
throw SystemError("Failed to get terminal dimensions");
|
2018-10-19 16:32:44 +02:00
|
|
|
|
2019-01-22 16:02:27 +01:00
|
|
|
logger->debug("New terminal size: {}x{}", current->window.ws_row, current->window.ws_col);
|
2018-10-19 16:32:44 +02:00
|
|
|
};
|