2022-04-03 10:16:20 +02:00
|
|
|
/**
|
|
|
|
* @author Christian Burger (christian@krikkel.de)
|
|
|
|
*/
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
#include "Window.hpp"
|
2022-04-03 10:16:20 +02:00
|
|
|
#include "Debug.hpp"
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <gsl/gsl>
|
|
|
|
#include <cctype>
|
|
|
|
#include <termios.h>
|
2022-04-13 10:46:44 +02:00
|
|
|
#include <fcntl.h>
|
2022-04-03 10:16:20 +02:00
|
|
|
|
2022-04-05 10:28:10 +02:00
|
|
|
namespace krikkel::NCursesPtyWindow
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
using gsl::narrow;
|
2022-04-13 10:46:44 +02:00
|
|
|
using std::lock_guard;
|
2022-04-03 10:16:20 +02:00
|
|
|
|
2022-04-16 20:27:24 +02:00
|
|
|
Window::Window(std::mutex *writeToNCursesMutex, int lines, int columns, int y, int x)
|
|
|
|
: writeToNCursesMutex(writeToNCursesMutex), NCursesWindow(lines, columns, y, x)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
// to get the original terminal we need to shutdown ncurses for a moment
|
2022-04-05 12:34:35 +02:00
|
|
|
/// @todo maybe try `reset_prog_mode()` and `reset_shell_mode()` instead
|
2022-04-03 10:16:20 +02:00
|
|
|
::endwin();
|
|
|
|
tcgetattr(STDIN_FILENO, &terminalParameters);
|
|
|
|
::refresh();
|
|
|
|
|
|
|
|
winsize windowSize =
|
|
|
|
{
|
|
|
|
.ws_row = narrow<short unsigned int>(this->height())
|
|
|
|
, .ws_col = narrow<short unsigned int>(this->width())
|
|
|
|
};
|
|
|
|
openpty(&fdPtyHost, &fdPtyClient, NULL, &terminalParameters, &windowSize);
|
|
|
|
|
|
|
|
pseudoTerminal = vterm_new(windowSize.ws_row, windowSize.ws_col);
|
|
|
|
vterm_set_utf8(pseudoTerminal, true);
|
|
|
|
pseudoTerminalScreen = vterm_obtain_screen(pseudoTerminal);
|
|
|
|
vterm_screen_reset(pseudoTerminalScreen, true);
|
|
|
|
vterm_screen_set_callbacks(pseudoTerminalScreen, &screenCallbacks, this);
|
2022-04-08 21:35:49 +02:00
|
|
|
vterm_output_set_callback(pseudoTerminal, &staticHandlerOutput, this);
|
2022-04-13 10:46:44 +02:00
|
|
|
vterm_screen_enable_altscreen(pseudoTerminalScreen, true);
|
2022-04-03 10:16:20 +02:00
|
|
|
|
2022-04-08 21:35:49 +02:00
|
|
|
//raw(); //— cbreak might suffice
|
2022-04-07 00:28:37 +02:00
|
|
|
//noecho(); — already set
|
|
|
|
//nodelay(true); — @todo needs some reprogramming
|
2022-04-13 10:46:44 +02:00
|
|
|
keypad(true);
|
2022-04-07 00:28:37 +02:00
|
|
|
nonl();
|
2022-04-03 10:16:20 +02:00
|
|
|
|
|
|
|
/// @todo block all signals, this thread does not handle any
|
2022-04-05 12:34:35 +02:00
|
|
|
readPtyClientThread =
|
2022-04-13 19:41:41 +02:00
|
|
|
std::thread(&Window::readFromPtyClientThreadMethod, this);
|
2022-04-03 10:16:20 +02:00
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
Window::~Window()
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
close(fdPtyHost);
|
|
|
|
close(fdPtyClient);
|
|
|
|
vterm_free(pseudoTerminal);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::getFdPtyClient() const
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
return fdPtyClient;
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
void Window::writeToClient(const char *output, size_t length)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-13 10:46:44 +02:00
|
|
|
lock_guard writeLock(writeToPseudoTerminalMutex);
|
|
|
|
write(fdPtyHost, output, length);
|
2022-04-08 22:03:12 +02:00
|
|
|
__debug_log("written to PTY client: '" + __debug_make_bytes_printable(std::string(output, length)) + '\'');
|
2022-04-03 10:16:20 +02:00
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
void Window::writeUnicodeCharToClient(wint_t character)
|
2022-04-13 10:46:44 +02:00
|
|
|
{
|
|
|
|
vterm_keyboard_unichar(pseudoTerminal, character, VTERM_MOD_NONE);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
void Window::writeKeyToClient(VTermKey key)
|
2022-04-13 10:46:44 +02:00
|
|
|
{
|
|
|
|
__debug_log("writing key: " + std::to_string(key));
|
|
|
|
vterm_keyboard_key(pseudoTerminal, key, VTERM_MOD_NONE);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
SingleUserInput Window::readSingleUserInput()
|
2022-04-13 10:46:44 +02:00
|
|
|
{
|
|
|
|
wint_t input;
|
|
|
|
int result = get_wch(&input);
|
|
|
|
return SingleUserInput(result, input);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
void Window::readFromPtyClientThreadMethod()
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
/// @todo in theory, there is no need for a timeout or select …
|
|
|
|
/// file descriptor is blocking
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
readFromPtyClient();
|
|
|
|
struct timeval timeout = { .tv_sec = 0, .tv_usec = 200000 };
|
|
|
|
fd_set readFds;
|
|
|
|
|
|
|
|
FD_ZERO(&readFds);
|
|
|
|
FD_SET(fdPtyHost, &readFds);
|
|
|
|
if(select(fdPtyHost + 1, &readFds, NULL, NULL, &timeout) < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
void Window::readFromPtyClient()
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-07 14:28:35 +02:00
|
|
|
size_t bytesRead = read(fdPtyHost, ptyClientOutputBuffer, PTY_CLIENT_OUTPUT_BUFFER_SIZE);
|
|
|
|
if(bytesRead != -1 && bytesRead != 0)
|
2022-04-13 10:46:44 +02:00
|
|
|
{
|
|
|
|
lock_guard writeLock(writeToPseudoTerminalMutex);
|
2022-04-07 14:28:35 +02:00
|
|
|
vterm_input_write(pseudoTerminal, ptyClientOutputBuffer, bytesRead);
|
2022-04-13 10:46:44 +02:00
|
|
|
}
|
2022-04-08 22:03:12 +02:00
|
|
|
__debug_log("read from PTY client: '" + __debug_make_bytes_printable(std::string(ptyClientOutputBuffer, bytesRead)) + '\'');
|
2022-04-03 10:16:20 +02:00
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
VTermScreenCallbacks Window::screenCallbacks =
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
.damage = staticHandlerDamage,
|
2022-04-05 10:23:50 +02:00
|
|
|
.moverect = staticHandlerMoveRect,
|
2022-04-03 10:16:20 +02:00
|
|
|
.movecursor = staticHandlerMoveCursor,
|
|
|
|
.settermprop = staticHandlerSetTermProp,
|
|
|
|
.bell = staticHandlerBell,
|
|
|
|
.resize = staticHandlerResize,
|
|
|
|
.sb_pushline = staticHandlerPushLine,
|
2022-04-05 10:23:50 +02:00
|
|
|
.sb_popline = staticHandlerPopLine,
|
2022-04-03 10:16:20 +02:00
|
|
|
};
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::handlerDamage(VTermRect rect)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
for(int x = rect.start_col; x < rect.end_col; ++x)
|
|
|
|
for(int y = rect.start_row; y < rect.end_row; ++y)
|
|
|
|
copyPtyCellToNCursesWindow(x, y);
|
|
|
|
|
2022-04-05 10:23:50 +02:00
|
|
|
refresh();
|
2022-04-03 10:16:20 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::handlerMoveRect(VTermRect dest, VTermRect src)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
__debug_log("(unimplemented) move content in rectangle from ("
|
|
|
|
+ std::to_string(src.start_col) + ", "
|
|
|
|
+ std::to_string(src.start_row) + ", "
|
|
|
|
+ std::to_string(src.end_col) + ", "
|
|
|
|
+ std::to_string(src.end_row) + ") "
|
|
|
|
+ "size: " + std::to_string((src.start_col - src.end_col) * (src.start_row - src.end_row))
|
|
|
|
+ " to ("
|
|
|
|
+ std::to_string(dest.start_col) + ", "
|
|
|
|
+ std::to_string(dest.start_row) + ", "
|
|
|
|
+ std::to_string(dest.end_col) + ", "
|
|
|
|
+ std::to_string(dest.end_row) + ") "
|
|
|
|
+ "size: " + std::to_string((dest.start_col - dest.end_col) * (dest.start_row - dest.end_row)));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::handlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-05 12:34:35 +02:00
|
|
|
/// @todo maybe use `mvcur()` instead?
|
2022-04-05 10:23:50 +02:00
|
|
|
cursorX = pos.col;
|
|
|
|
cursorY = pos.row;
|
|
|
|
refresh();
|
2022-04-03 10:16:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::handlerSetTermProp(VTermProp prop, VTermValue *val)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-08 22:03:12 +02:00
|
|
|
/// @todo maybe use "vterm_get_prop_type() —> bool, number, string"?
|
|
|
|
__debug_stringify_switch_begin(handlerSetTermProp, prop, val);
|
|
|
|
__debug_stringify_switch_case(VTERM_PROP_CURSORVISIBLE);
|
|
|
|
__debug_stringify_switch_case(VTERM_PROP_CURSORBLINK);
|
|
|
|
__debug_stringify_switch_case(VTERM_PROP_ALTSCREEN);
|
|
|
|
__debug_stringify_switch_case(VTERM_PROP_REVERSE);
|
|
|
|
__debug_stringify_switch_case_end_bool(val->boolean);
|
|
|
|
__debug_stringify_switch_case(VTERM_PROP_TITLE);
|
|
|
|
__debug_stringify_switch_case(VTERM_PROP_ICONNAME);
|
|
|
|
__debug_stringify_switch_case_end_string(val->string);
|
|
|
|
__debug_stringify_switch_case(VTERM_PROP_CURSORSHAPE);
|
|
|
|
__debug_stringify_switch_case(VTERM_PROP_MOUSE);
|
|
|
|
__debug_stringify_switch_case_end_number(val->number);
|
|
|
|
__debug_stringify_switch_end(prop);
|
|
|
|
|
|
|
|
__debug_log(std::string("unimplemented handler called: ")
|
|
|
|
+ __debug_stringify_get_string(handlerSetTermProp)
|
|
|
|
);
|
2022-04-03 10:16:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::handlerBell()
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
beep();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::handlerResize(int rows, int cols)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
__debug_log("unimplemented handler called");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::handlerPushLine(int cols, const VTermScreenCell *cells)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
__debug_log("(unimplemented) push line with " + std::to_string(cols) + " columns");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::handlerPopLine(int cols, VTermScreenCell *cells)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
__debug_log("unimplemented handler called");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
attr_t Window::extractAttributesFromVTermCell(VTermScreenCell vTermCell)
|
2022-04-05 10:23:50 +02:00
|
|
|
{
|
|
|
|
attr_t result = A_NORMAL;
|
2022-04-08 21:35:49 +02:00
|
|
|
//__debug_log("unimplemented method called");
|
2022-04-05 10:23:50 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
short Window::extractColorFromVTermCell(VTermScreenCell vTermCell)
|
2022-04-05 10:23:50 +02:00
|
|
|
{
|
2022-04-08 21:35:49 +02:00
|
|
|
//__debug_log("unimplemented method called");
|
2022-04-05 10:23:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
void Window::copyPtyCellToNCursesWindow(int x, int y)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
|
|
|
VTermPos cellPosition = {y, x};
|
2022-04-05 10:23:50 +02:00
|
|
|
VTermScreenCell vTermCell;
|
|
|
|
cchar_t converted;
|
|
|
|
attr_t formatting;
|
|
|
|
short colorPair;
|
|
|
|
wchar_t character;
|
|
|
|
|
|
|
|
vterm_screen_get_cell(pseudoTerminalScreen, cellPosition, &vTermCell);
|
|
|
|
formatting = extractAttributesFromVTermCell(vTermCell);
|
|
|
|
colorPair = extractColorFromVTermCell(vTermCell);
|
|
|
|
|
|
|
|
if(vTermCell.chars[0] == 0)
|
|
|
|
character = ' ';
|
|
|
|
else
|
|
|
|
character = *vTermCell.chars;
|
|
|
|
|
2022-04-16 20:27:24 +02:00
|
|
|
{
|
|
|
|
lock_guard nCursesLock(*writeToNCursesMutex);
|
|
|
|
setcchar(&converted, &character, formatting, colorPair, NULL);
|
|
|
|
move(cellPosition.row, cellPosition.col);
|
|
|
|
chgat(1, formatting, colorPair, NULL);
|
|
|
|
add_wch(&converted);
|
|
|
|
}
|
2022-04-05 10:23:50 +02:00
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::add_wch(const cchar_t *character)
|
2022-04-05 10:23:50 +02:00
|
|
|
{
|
|
|
|
return ::wadd_wch(w, character);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::get_wch(wint_t *character)
|
2022-04-13 10:46:44 +02:00
|
|
|
{
|
|
|
|
return ::wget_wch(w, character);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::refresh()
|
2022-04-05 10:23:50 +02:00
|
|
|
{
|
2022-04-16 20:27:24 +02:00
|
|
|
lock_guard nCursesLock(*writeToNCursesMutex);
|
2022-04-05 10:23:50 +02:00
|
|
|
move(cursorY, cursorX);
|
|
|
|
return NCursesWindow::refresh();
|
2022-04-03 10:16:20 +02:00
|
|
|
}
|
|
|
|
|
2022-04-05 12:34:35 +02:00
|
|
|
/// @todo potential racing condition where drawing into terminal while
|
|
|
|
/// resizing?
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::wresize(int rows, int cols)
|
2022-04-05 12:34:35 +02:00
|
|
|
{
|
2022-04-16 20:27:24 +02:00
|
|
|
lock_guard nCursesLock(*writeToNCursesMutex);
|
|
|
|
lock_guard writeLock(writeToPseudoTerminalMutex);
|
2022-04-05 12:34:35 +02:00
|
|
|
winsize windowSize =
|
|
|
|
{
|
|
|
|
.ws_row = narrow<unsigned short>(rows)
|
|
|
|
, .ws_col = narrow<unsigned short>(cols)
|
|
|
|
};
|
|
|
|
ioctl(fdPtyHost, TIOCSWINSZ, &windowSize);
|
|
|
|
vterm_set_size(pseudoTerminal, rows, cols);
|
|
|
|
|
|
|
|
return NCursesWindow::wresize(rows, cols);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::staticHandlerDamage(VTermRect rect, void *user)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-13 19:41:41 +02:00
|
|
|
Window *instance = static_cast<Window *>(user);
|
2022-04-03 10:16:20 +02:00
|
|
|
return instance->handlerDamage(rect);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::staticHandlerMoveRect(VTermRect dest, VTermRect src, void *user)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-13 19:41:41 +02:00
|
|
|
Window *instance = static_cast<Window *>(user);
|
2022-04-03 10:16:20 +02:00
|
|
|
return instance->handlerMoveRect(dest, src);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::staticHandlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible, void *user)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-13 19:41:41 +02:00
|
|
|
Window *instance = static_cast<Window *>(user);
|
2022-04-03 10:16:20 +02:00
|
|
|
return instance->handlerMoveCursor(pos, oldpos, visible);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::staticHandlerSetTermProp(VTermProp prop, VTermValue *val, void *user)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-13 19:41:41 +02:00
|
|
|
Window *instance = static_cast<Window *>(user);
|
2022-04-03 10:16:20 +02:00
|
|
|
return instance->handlerSetTermProp(prop, val);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::staticHandlerBell(void *user)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-13 19:41:41 +02:00
|
|
|
Window *instance = static_cast<Window *>(user);
|
2022-04-03 10:16:20 +02:00
|
|
|
return instance->handlerBell();
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::staticHandlerResize(int rows, int cols, void *user)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-13 19:41:41 +02:00
|
|
|
Window *instance = static_cast<Window *>(user);
|
2022-04-03 10:16:20 +02:00
|
|
|
return instance->handlerResize(rows, cols);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::staticHandlerPushLine(int cols, const VTermScreenCell *cells, void *user)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-13 19:41:41 +02:00
|
|
|
Window *instance = static_cast<Window *>(user);
|
2022-04-03 10:16:20 +02:00
|
|
|
return instance->handlerPushLine(cols, cells);
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
int Window::staticHandlerPopLine(int cols, VTermScreenCell *cells, void *user)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-04-13 19:41:41 +02:00
|
|
|
Window *instance = static_cast<Window *>(user);
|
2022-04-03 10:16:20 +02:00
|
|
|
return instance->handlerPopLine(cols, cells);
|
|
|
|
}
|
2022-04-08 21:35:49 +02:00
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
void Window::staticHandlerOutput(const char *s, size_t len, void *user)
|
2022-04-08 21:35:49 +02:00
|
|
|
{
|
2022-04-13 19:41:41 +02:00
|
|
|
Window *instance = static_cast<Window *>(user);
|
2022-04-13 10:46:44 +02:00
|
|
|
__debug_log("output handler writing to client: '" + __debug_make_bytes_printable(std::string(s, len)) + "'");
|
|
|
|
instance->writeToClient(s, len);
|
2022-04-08 21:35:49 +02:00
|
|
|
}
|
2022-04-03 10:16:20 +02:00
|
|
|
}
|