1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-23 00:00:01 +01:00
VILLASnode/common/lib/terminal.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

76 lines
2.1 KiB
C++
Raw Normal View History

2018-10-19 16:32:44 +02:00
/** Terminal handling.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2019-01-13 00:42:26 +01:00
* @copyright 2014-2019, 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;
struct winsize Terminal::window;
Terminal::Terminal()
{
int ret;
isTty = isatty(fileno(stdin));
if (isTty) {
struct sigaction sa_resize;
sa_resize.sa_flags = SA_SIGINFO;
sa_resize.sa_sigaction = resize;
sigemptyset(&sa_resize.sa_mask);
ret = sigaction(SIGWINCH, &sa_resize, NULL);
if (ret)
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)
throw SystemError("Failed to get terminal dimensions");
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 *)
{
int ret;
ret = ioctl(STDERR_FILENO, TIOCGWINSZ, &window);
if (ret)
throw SystemError("Failed to get terminal dimensions");
2018-10-19 16:32:44 +02:00
2018-11-30 21:43:37 +01:00
Logger logger = logging.get("terminal");
2018-10-19 16:32:44 +02:00
logger->debug("New terminal size: {}x{}", window.ws_row, window.ws_col);
};