renamed Window to PtyWindow

Making space for a more general base window class with support for wide
characters.
This commit is contained in:
Christian Burger 2022-04-25 11:10:07 +02:00
parent ae6130f095
commit 746e458cd2
6 changed files with 73 additions and 52 deletions

17
.vscode/c_cpp_properties.json vendored Normal file
View file

@ -0,0 +1,17 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${default}"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}

View file

@ -55,5 +55,8 @@
"typeinfo": "cpp", "typeinfo": "cpp",
"pointers": "cpp" "pointers": "cpp"
}, },
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools" "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"C_Cpp.default.includePath": [
"${workspaceFolder}/include"
]
} }

View file

@ -4,11 +4,12 @@
#include "App.hpp" #include "App.hpp"
#include "Debug.hpp" #include "Debug.hpp"
#include <NCursesPtyWindow/Window.hpp> #include <NCursesPtyWindow/PtyWindow.hpp>
#include <unistd.h> #include <unistd.h>
#include <utmp.h> #include <utmp.h>
#include <cstdlib> #include <cstdlib>
#include <mutex>
namespace krikkel::NCursesPtyWindow namespace krikkel::NCursesPtyWindow
{ {
@ -21,7 +22,7 @@ namespace krikkel::NCursesPtyWindow
{ {
std::mutex writeMutex; std::mutex writeMutex;
Window *ptyWindow = new Window(&writeMutex PtyWindow *ptyWindow = new PtyWindow(&writeMutex
, Root_Window->lines() , Root_Window->lines()
, Root_Window->cols() , Root_Window->cols()
, 0 , 0

View file

@ -13,7 +13,7 @@ set(CMAKE_CXX_STANDARD 17)
include(CTest) include(CTest)
enable_testing() enable_testing()
add_library(NCursesPtyWindow Window.cpp SingleUserInput.cpp Debug.cpp) add_library(NCursesPtyWindow PtyWindow.cpp SingleUserInput.cpp Debug.cpp)
### path to own system includes ### path to own system includes
include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/include") include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/include")

View file

@ -2,7 +2,7 @@
* @author Christian Burger (christian@krikkel.de) * @author Christian Burger (christian@krikkel.de)
*/ */
#include <NCursesPtyWindow/Window.hpp> #include <NCursesPtyWindow/PtyWindow.hpp>
#include "Debug.hpp" #include "Debug.hpp"
#include <cstdio> #include <cstdio>
@ -18,7 +18,7 @@ namespace krikkel::NCursesPtyWindow
using gsl::narrow; using gsl::narrow;
using std::lock_guard; using std::lock_guard;
Window::Window(std::mutex *writeToNCursesMutex, int lines, int columns, int y, int x) PtyWindow::PtyWindow(std::mutex *writeToNCursesMutex, int lines, int columns, int y, int x)
: writeToNCursesMutex(writeToNCursesMutex), NCursesWindow(lines, columns, y, x) : writeToNCursesMutex(writeToNCursesMutex), NCursesWindow(lines, columns, y, x)
{ {
// to get the original terminal we need to shutdown ncurses for a moment // to get the original terminal we need to shutdown ncurses for a moment
@ -50,47 +50,47 @@ namespace krikkel::NCursesPtyWindow
/// @todo block all signals, this thread does not handle any /// @todo block all signals, this thread does not handle any
readPtyClientThread = readPtyClientThread =
std::thread(&Window::readFromPtyClientThreadMethod, this); std::thread(&PtyWindow::readFromPtyClientThreadMethod, this);
} }
Window::~Window() PtyWindow::~PtyWindow()
{ {
close(fdPtyHost); close(fdPtyHost);
close(fdPtyClient); close(fdPtyClient);
vterm_free(pseudoTerminal); vterm_free(pseudoTerminal);
} }
int Window::getFdPtyClient() const int PtyWindow::getFdPtyClient() const
{ {
return fdPtyClient; return fdPtyClient;
} }
void Window::writeToClient(const char *output, size_t length) void PtyWindow::writeToClient(const char *output, size_t length)
{ {
lock_guard writeLock(writeToPseudoTerminalMutex); lock_guard writeLock(writeToPseudoTerminalMutex);
write(fdPtyHost, output, length); write(fdPtyHost, output, length);
__debug_log("written to PTY client: '" + __debug_make_bytes_printable(std::string(output, length)) + '\''); __debug_log("written to PTY client: '" + __debug_make_bytes_printable(std::string(output, length)) + '\'');
} }
void Window::writeUnicodeCharToClient(wint_t character) void PtyWindow::writeUnicodeCharToClient(wint_t character)
{ {
vterm_keyboard_unichar(pseudoTerminal, character, VTERM_MOD_NONE); vterm_keyboard_unichar(pseudoTerminal, character, VTERM_MOD_NONE);
} }
void Window::writeKeyToClient(VTermKey key) void PtyWindow::writeKeyToClient(VTermKey key)
{ {
__debug_log("writing key: " + std::to_string(key)); __debug_log("writing key: " + std::to_string(key));
vterm_keyboard_key(pseudoTerminal, key, VTERM_MOD_NONE); vterm_keyboard_key(pseudoTerminal, key, VTERM_MOD_NONE);
} }
SingleUserInput Window::readSingleUserInput() SingleUserInput PtyWindow::readSingleUserInput()
{ {
wint_t input; wint_t input;
int result = get_wch(&input); int result = get_wch(&input);
return SingleUserInput(result, input); return SingleUserInput(result, input);
} }
void Window::readFromPtyClientThreadMethod() void PtyWindow::readFromPtyClientThreadMethod()
{ {
/// @todo in theory, there is no need for a timeout or select … /// @todo in theory, there is no need for a timeout or select …
/// file descriptor is blocking /// file descriptor is blocking
@ -107,7 +107,7 @@ namespace krikkel::NCursesPtyWindow
} }
} }
void Window::readFromPtyClient() void PtyWindow::readFromPtyClient()
{ {
size_t bytesRead = read(fdPtyHost, ptyClientOutputBuffer, PTY_CLIENT_OUTPUT_BUFFER_SIZE); size_t bytesRead = read(fdPtyHost, ptyClientOutputBuffer, PTY_CLIENT_OUTPUT_BUFFER_SIZE);
if(bytesRead != -1 && bytesRead != 0) if(bytesRead != -1 && bytesRead != 0)
@ -118,7 +118,7 @@ namespace krikkel::NCursesPtyWindow
__debug_log("read from PTY client: '" + __debug_make_bytes_printable(std::string(ptyClientOutputBuffer, bytesRead)) + '\''); __debug_log("read from PTY client: '" + __debug_make_bytes_printable(std::string(ptyClientOutputBuffer, bytesRead)) + '\'');
} }
VTermScreenCallbacks Window::screenCallbacks = VTermScreenCallbacks PtyWindow::screenCallbacks =
{ {
.damage = staticHandlerDamage, .damage = staticHandlerDamage,
.moverect = staticHandlerMoveRect, .moverect = staticHandlerMoveRect,
@ -130,7 +130,7 @@ namespace krikkel::NCursesPtyWindow
.sb_popline = staticHandlerPopLine, .sb_popline = staticHandlerPopLine,
}; };
int Window::handlerDamage(VTermRect rect) int PtyWindow::handlerDamage(VTermRect rect)
{ {
for(int x = rect.start_col; x < rect.end_col; ++x) for(int x = rect.start_col; x < rect.end_col; ++x)
for(int y = rect.start_row; y < rect.end_row; ++y) for(int y = rect.start_row; y < rect.end_row; ++y)
@ -141,7 +141,7 @@ namespace krikkel::NCursesPtyWindow
return 0; return 0;
} }
int Window::handlerMoveRect(VTermRect dest, VTermRect src) int PtyWindow::handlerMoveRect(VTermRect dest, VTermRect src)
{ {
__debug_log("(unimplemented) move content in rectangle from (" __debug_log("(unimplemented) move content in rectangle from ("
+ std::to_string(src.start_col) + ", " + std::to_string(src.start_col) + ", "
@ -158,7 +158,7 @@ namespace krikkel::NCursesPtyWindow
return 0; return 0;
} }
int Window::handlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible) int PtyWindow::handlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible)
{ {
/// @todo maybe use `mvcur()` instead? /// @todo maybe use `mvcur()` instead?
cursorX = pos.col; cursorX = pos.col;
@ -167,7 +167,7 @@ namespace krikkel::NCursesPtyWindow
return 0; return 0;
} }
int Window::handlerSetTermProp(VTermProp prop, VTermValue *val) int PtyWindow::handlerSetTermProp(VTermProp prop, VTermValue *val)
{ {
/// @todo maybe use "vterm_get_prop_type() —> bool, number, string"? /// @todo maybe use "vterm_get_prop_type() —> bool, number, string"?
__debug_stringify_switch_begin(handlerSetTermProp, prop, val); __debug_stringify_switch_begin(handlerSetTermProp, prop, val);
@ -190,44 +190,44 @@ namespace krikkel::NCursesPtyWindow
return 0; return 0;
} }
int Window::handlerBell() int PtyWindow::handlerBell()
{ {
beep(); beep();
return 0; return 0;
} }
int Window::handlerResize(int rows, int cols) int PtyWindow::handlerResize(int rows, int cols)
{ {
__debug_log("unimplemented handler called"); __debug_log("unimplemented handler called");
return 0; return 0;
} }
int Window::handlerPushLine(int cols, const VTermScreenCell *cells) int PtyWindow::handlerPushLine(int cols, const VTermScreenCell *cells)
{ {
__debug_log("(unimplemented) push line with " + std::to_string(cols) + " columns"); __debug_log("(unimplemented) push line with " + std::to_string(cols) + " columns");
return 0; return 0;
} }
int Window::handlerPopLine(int cols, VTermScreenCell *cells) int PtyWindow::handlerPopLine(int cols, VTermScreenCell *cells)
{ {
__debug_log("unimplemented handler called"); __debug_log("unimplemented handler called");
return 0; return 0;
} }
attr_t Window::extractAttributesFromVTermCell(VTermScreenCell vTermCell) attr_t PtyWindow::extractAttributesFromVTermCell(VTermScreenCell vTermCell)
{ {
attr_t result = A_NORMAL; attr_t result = A_NORMAL;
//__debug_log("unimplemented method called"); //__debug_log("unimplemented method called");
return result; return result;
} }
short Window::extractColorFromVTermCell(VTermScreenCell vTermCell) short PtyWindow::extractColorFromVTermCell(VTermScreenCell vTermCell)
{ {
//__debug_log("unimplemented method called"); //__debug_log("unimplemented method called");
return 0; return 0;
} }
void Window::copyPtyCellToNCursesWindow(int x, int y) void PtyWindow::copyPtyCellToNCursesWindow(int x, int y)
{ {
VTermPos cellPosition = {y, x}; VTermPos cellPosition = {y, x};
VTermScreenCell vTermCell; VTermScreenCell vTermCell;
@ -254,17 +254,17 @@ namespace krikkel::NCursesPtyWindow
} }
} }
int Window::add_wch(const cchar_t *character) int PtyWindow::add_wch(const cchar_t *character)
{ {
return ::wadd_wch(w, character); return ::wadd_wch(w, character);
} }
int Window::get_wch(wint_t *character) int PtyWindow::get_wch(wint_t *character)
{ {
return ::wget_wch(w, character); return ::wget_wch(w, character);
} }
int Window::refresh() int PtyWindow::refresh()
{ {
lock_guard nCursesLock(*writeToNCursesMutex); lock_guard nCursesLock(*writeToNCursesMutex);
move(cursorY, cursorX); move(cursorY, cursorX);
@ -273,7 +273,7 @@ namespace krikkel::NCursesPtyWindow
/// @todo potential racing condition where drawing into terminal while /// @todo potential racing condition where drawing into terminal while
/// resizing? /// resizing?
int Window::wresize(int rows, int cols) int PtyWindow::wresize(int rows, int cols)
{ {
lock_guard nCursesLock(*writeToNCursesMutex); lock_guard nCursesLock(*writeToNCursesMutex);
lock_guard writeLock(writeToPseudoTerminalMutex); lock_guard writeLock(writeToPseudoTerminalMutex);
@ -288,57 +288,57 @@ namespace krikkel::NCursesPtyWindow
return NCursesWindow::wresize(rows, cols); return NCursesWindow::wresize(rows, cols);
} }
int Window::staticHandlerDamage(VTermRect rect, void *user) int PtyWindow::staticHandlerDamage(VTermRect rect, void *user)
{ {
Window *instance = static_cast<Window *>(user); PtyWindow *instance = static_cast<PtyWindow *>(user);
return instance->handlerDamage(rect); return instance->handlerDamage(rect);
} }
int Window::staticHandlerMoveRect(VTermRect dest, VTermRect src, void *user) int PtyWindow::staticHandlerMoveRect(VTermRect dest, VTermRect src, void *user)
{ {
Window *instance = static_cast<Window *>(user); PtyWindow *instance = static_cast<PtyWindow *>(user);
return instance->handlerMoveRect(dest, src); return instance->handlerMoveRect(dest, src);
} }
int Window::staticHandlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible, void *user) int PtyWindow::staticHandlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible, void *user)
{ {
Window *instance = static_cast<Window *>(user); PtyWindow *instance = static_cast<PtyWindow *>(user);
return instance->handlerMoveCursor(pos, oldpos, visible); return instance->handlerMoveCursor(pos, oldpos, visible);
} }
int Window::staticHandlerSetTermProp(VTermProp prop, VTermValue *val, void *user) int PtyWindow::staticHandlerSetTermProp(VTermProp prop, VTermValue *val, void *user)
{ {
Window *instance = static_cast<Window *>(user); PtyWindow *instance = static_cast<PtyWindow *>(user);
return instance->handlerSetTermProp(prop, val); return instance->handlerSetTermProp(prop, val);
} }
int Window::staticHandlerBell(void *user) int PtyWindow::staticHandlerBell(void *user)
{ {
Window *instance = static_cast<Window *>(user); PtyWindow *instance = static_cast<PtyWindow *>(user);
return instance->handlerBell(); return instance->handlerBell();
} }
int Window::staticHandlerResize(int rows, int cols, void *user) int PtyWindow::staticHandlerResize(int rows, int cols, void *user)
{ {
Window *instance = static_cast<Window *>(user); PtyWindow *instance = static_cast<PtyWindow *>(user);
return instance->handlerResize(rows, cols); return instance->handlerResize(rows, cols);
} }
int Window::staticHandlerPushLine(int cols, const VTermScreenCell *cells, void *user) int PtyWindow::staticHandlerPushLine(int cols, const VTermScreenCell *cells, void *user)
{ {
Window *instance = static_cast<Window *>(user); PtyWindow *instance = static_cast<PtyWindow *>(user);
return instance->handlerPushLine(cols, cells); return instance->handlerPushLine(cols, cells);
} }
int Window::staticHandlerPopLine(int cols, VTermScreenCell *cells, void *user) int PtyWindow::staticHandlerPopLine(int cols, VTermScreenCell *cells, void *user)
{ {
Window *instance = static_cast<Window *>(user); PtyWindow *instance = static_cast<PtyWindow *>(user);
return instance->handlerPopLine(cols, cells); return instance->handlerPopLine(cols, cells);
} }
void Window::staticHandlerOutput(const char *s, size_t len, void *user) void PtyWindow::staticHandlerOutput(const char *s, size_t len, void *user)
{ {
Window *instance = static_cast<Window *>(user); PtyWindow *instance = static_cast<PtyWindow *>(user);
__debug_log("output handler writing to client: '" + __debug_make_bytes_printable(std::string(s, len)) + "'"); __debug_log("output handler writing to client: '" + __debug_make_bytes_printable(std::string(s, len)) + "'");
instance->writeToClient(s, len); instance->writeToClient(s, len);
} }

View file

@ -29,11 +29,11 @@ inline void UNDEF(get_wch)(wint_t *character) { get_wch(character); }
namespace krikkel::NCursesPtyWindow namespace krikkel::NCursesPtyWindow
{ {
class Window : public NCursesWindow class PtyWindow : public NCursesWindow
{ {
public: public:
Window(std::mutex *writeToNCursesMutex, int lines, int columns, int y, int x); PtyWindow(std::mutex *writeToNCursesMutex, int lines, int columns, int y, int x);
~Window(); ~PtyWindow();
int getFdPtyClient() const; int getFdPtyClient() const;
void writeToClient(const char * string, size_t length); void writeToClient(const char * string, size_t length);