From 746e458cd2065dd7f6ce951320a7003e8788c6cb Mon Sep 17 00:00:00 2001 From: Christian Burger Date: Mon, 25 Apr 2022 11:10:07 +0200 Subject: [PATCH] renamed `Window` to `PtyWindow` Making space for a more general base window class with support for wide characters. --- .vscode/c_cpp_properties.json | 17 ++++ .vscode/settings.json | 5 +- App.cpp | 5 +- CMakeLists.txt | 2 +- Window.cpp => PtyWindow.cpp | 90 +++++++++---------- .../{Window.hpp => PtyWindow.hpp} | 6 +- 6 files changed, 73 insertions(+), 52 deletions(-) create mode 100644 .vscode/c_cpp_properties.json rename Window.cpp => PtyWindow.cpp (75%) rename include/NCursesPtyWindow/{Window.hpp => PtyWindow.hpp} (95%) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..b3c60b3 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -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 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index d5ed929..a6e43ad 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -55,5 +55,8 @@ "typeinfo": "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" + ] } \ No newline at end of file diff --git a/App.cpp b/App.cpp index d471c5d..137e614 100644 --- a/App.cpp +++ b/App.cpp @@ -4,11 +4,12 @@ #include "App.hpp" #include "Debug.hpp" -#include +#include #include #include #include +#include namespace krikkel::NCursesPtyWindow { @@ -21,7 +22,7 @@ namespace krikkel::NCursesPtyWindow { std::mutex writeMutex; - Window *ptyWindow = new Window(&writeMutex + PtyWindow *ptyWindow = new PtyWindow(&writeMutex , Root_Window->lines() , Root_Window->cols() , 0 diff --git a/CMakeLists.txt b/CMakeLists.txt index c094603..f8d7ad5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ set(CMAKE_CXX_STANDARD 17) include(CTest) 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 include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/include") diff --git a/Window.cpp b/PtyWindow.cpp similarity index 75% rename from Window.cpp rename to PtyWindow.cpp index 13ea035..6e2e913 100644 --- a/Window.cpp +++ b/PtyWindow.cpp @@ -2,7 +2,7 @@ * @author Christian Burger (christian@krikkel.de) */ -#include +#include #include "Debug.hpp" #include @@ -18,7 +18,7 @@ namespace krikkel::NCursesPtyWindow using gsl::narrow; 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) { // 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 readPtyClientThread = - std::thread(&Window::readFromPtyClientThreadMethod, this); + std::thread(&PtyWindow::readFromPtyClientThreadMethod, this); } - Window::~Window() + PtyWindow::~PtyWindow() { close(fdPtyHost); close(fdPtyClient); vterm_free(pseudoTerminal); } - int Window::getFdPtyClient() const + int PtyWindow::getFdPtyClient() const { return fdPtyClient; } - void Window::writeToClient(const char *output, size_t length) + void PtyWindow::writeToClient(const char *output, size_t length) { lock_guard writeLock(writeToPseudoTerminalMutex); write(fdPtyHost, 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); } - void Window::writeKeyToClient(VTermKey key) + void PtyWindow::writeKeyToClient(VTermKey key) { __debug_log("writing key: " + std::to_string(key)); vterm_keyboard_key(pseudoTerminal, key, VTERM_MOD_NONE); } - SingleUserInput Window::readSingleUserInput() + SingleUserInput PtyWindow::readSingleUserInput() { wint_t input; int result = get_wch(&input); return SingleUserInput(result, input); } - void Window::readFromPtyClientThreadMethod() + void PtyWindow::readFromPtyClientThreadMethod() { /// @todo in theory, there is no need for a timeout or select … /// 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); 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)) + '\''); } - VTermScreenCallbacks Window::screenCallbacks = + VTermScreenCallbacks PtyWindow::screenCallbacks = { .damage = staticHandlerDamage, .moverect = staticHandlerMoveRect, @@ -130,7 +130,7 @@ namespace krikkel::NCursesPtyWindow .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 y = rect.start_row; y < rect.end_row; ++y) @@ -141,7 +141,7 @@ namespace krikkel::NCursesPtyWindow return 0; } - int Window::handlerMoveRect(VTermRect dest, VTermRect src) + int PtyWindow::handlerMoveRect(VTermRect dest, VTermRect src) { __debug_log("(unimplemented) move content in rectangle from (" + std::to_string(src.start_col) + ", " @@ -158,7 +158,7 @@ namespace krikkel::NCursesPtyWindow 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? cursorX = pos.col; @@ -167,7 +167,7 @@ namespace krikkel::NCursesPtyWindow 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"? __debug_stringify_switch_begin(handlerSetTermProp, prop, val); @@ -190,44 +190,44 @@ namespace krikkel::NCursesPtyWindow return 0; } - int Window::handlerBell() + int PtyWindow::handlerBell() { beep(); return 0; } - int Window::handlerResize(int rows, int cols) + int PtyWindow::handlerResize(int rows, int cols) { __debug_log("unimplemented handler called"); 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"); return 0; } - int Window::handlerPopLine(int cols, VTermScreenCell *cells) + int PtyWindow::handlerPopLine(int cols, VTermScreenCell *cells) { __debug_log("unimplemented handler called"); return 0; } - attr_t Window::extractAttributesFromVTermCell(VTermScreenCell vTermCell) + attr_t PtyWindow::extractAttributesFromVTermCell(VTermScreenCell vTermCell) { attr_t result = A_NORMAL; //__debug_log("unimplemented method called"); return result; } - short Window::extractColorFromVTermCell(VTermScreenCell vTermCell) + short PtyWindow::extractColorFromVTermCell(VTermScreenCell vTermCell) { //__debug_log("unimplemented method called"); return 0; } - void Window::copyPtyCellToNCursesWindow(int x, int y) + void PtyWindow::copyPtyCellToNCursesWindow(int x, int y) { VTermPos cellPosition = {y, x}; 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); } - int Window::get_wch(wint_t *character) + int PtyWindow::get_wch(wint_t *character) { return ::wget_wch(w, character); } - int Window::refresh() + int PtyWindow::refresh() { lock_guard nCursesLock(*writeToNCursesMutex); move(cursorY, cursorX); @@ -273,7 +273,7 @@ namespace krikkel::NCursesPtyWindow /// @todo potential racing condition where drawing into terminal while /// resizing? - int Window::wresize(int rows, int cols) + int PtyWindow::wresize(int rows, int cols) { lock_guard nCursesLock(*writeToNCursesMutex); lock_guard writeLock(writeToPseudoTerminalMutex); @@ -288,57 +288,57 @@ namespace krikkel::NCursesPtyWindow return NCursesWindow::wresize(rows, cols); } - int Window::staticHandlerDamage(VTermRect rect, void *user) + int PtyWindow::staticHandlerDamage(VTermRect rect, void *user) { - Window *instance = static_cast(user); + PtyWindow *instance = static_cast(user); 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(user); + PtyWindow *instance = static_cast(user); 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(user); + PtyWindow *instance = static_cast(user); 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(user); + PtyWindow *instance = static_cast(user); return instance->handlerSetTermProp(prop, val); } - int Window::staticHandlerBell(void *user) + int PtyWindow::staticHandlerBell(void *user) { - Window *instance = static_cast(user); + PtyWindow *instance = static_cast(user); 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(user); + PtyWindow *instance = static_cast(user); 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(user); + PtyWindow *instance = static_cast(user); 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(user); + PtyWindow *instance = static_cast(user); 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(user); + PtyWindow *instance = static_cast(user); __debug_log("output handler writing to client: '" + __debug_make_bytes_printable(std::string(s, len)) + "'"); instance->writeToClient(s, len); } diff --git a/include/NCursesPtyWindow/Window.hpp b/include/NCursesPtyWindow/PtyWindow.hpp similarity index 95% rename from include/NCursesPtyWindow/Window.hpp rename to include/NCursesPtyWindow/PtyWindow.hpp index 6aaa542..ee8fb84 100644 --- a/include/NCursesPtyWindow/Window.hpp +++ b/include/NCursesPtyWindow/PtyWindow.hpp @@ -29,11 +29,11 @@ inline void UNDEF(get_wch)(wint_t *character) { get_wch(character); } namespace krikkel::NCursesPtyWindow { - class Window : public NCursesWindow + class PtyWindow : public NCursesWindow { public: - Window(std::mutex *writeToNCursesMutex, int lines, int columns, int y, int x); - ~Window(); + PtyWindow(std::mutex *writeToNCursesMutex, int lines, int columns, int y, int x); + ~PtyWindow(); int getFdPtyClient() const; void writeToClient(const char * string, size_t length);