refactoring: closes #10

This commit is contained in:
Christian Burger 2022-04-13 19:41:41 +02:00
parent 8e71512f12
commit 4ca933a158
6 changed files with 59 additions and 59 deletions

View file

@ -2,8 +2,8 @@
* @author Christian Burger (christian@krikkel.de) * @author Christian Burger (christian@krikkel.de)
*/ */
#include "NCursesPtyApp.hpp" #include "App.hpp"
#include "NCursesPtyWindow.hpp" #include "Window.hpp"
#include "Debug.hpp" #include "Debug.hpp"
#include <unistd.h> #include <unistd.h>
@ -12,14 +12,14 @@
namespace krikkel::NCursesPtyWindow namespace krikkel::NCursesPtyWindow
{ {
NCursesPtyApp::NCursesPtyApp() : NCursesApplication(false) App::App() : NCursesApplication(false)
{ {
} }
int NCursesPtyApp::run() int App::run()
{ {
NCursesPtyWindow *ptyWindow = new NCursesPtyWindow( Window *ptyWindow = new Window(
Root_Window->lines() Root_Window->lines()
, Root_Window->cols() , Root_Window->cols()
, 0 , 0

View file

@ -10,10 +10,10 @@
namespace krikkel::NCursesPtyWindow namespace krikkel::NCursesPtyWindow
{ {
class NCursesPtyApp : public NCursesApplication class App : public NCursesApplication
{ {
public: public:
NCursesPtyApp(); App();
private: private:
int run() override; int run() override;

View file

@ -12,7 +12,7 @@ include(ExternalProject)
include("cmake/ncurses.cmake") include("cmake/ncurses.cmake")
include("cmake/gsl.cmake") include("cmake/gsl.cmake")
add_library(NCursesPtyWindow NCursesPtyWindow.cpp SingleUserInput.cpp Debug.cpp) add_library(NCursesPtyWindow Window.cpp SingleUserInput.cpp Debug.cpp)
### libraries ### libraries
@ -27,7 +27,7 @@ find_package(Threads REQUIRED)
target_link_libraries(NCursesPtyWindow Threads::Threads) target_link_libraries(NCursesPtyWindow Threads::Threads)
### demo application ### demo application
add_executable(NCursesPtyApp main.cpp NCursesPtyApp.cpp) add_executable(NCursesPtyApp main.cpp App.cpp)
target_link_libraries(NCursesPtyApp ${CURSES_LIBRARIES} NCursesPtyWindow) target_link_libraries(NCursesPtyApp ${CURSES_LIBRARIES} NCursesPtyWindow)
set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_NAME ${PROJECT_NAME})

View file

@ -2,7 +2,7 @@
* @author Christian Burger (christian@krikkel.de) * @author Christian Burger (christian@krikkel.de)
*/ */
#include "NCursesPtyWindow.hpp" #include "Window.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;
NCursesPtyWindow::NCursesPtyWindow(int lines, int columns, int y, int x) Window::Window(int lines, int columns, int y, int x)
: NCursesWindow(lines, columns, y, x) : 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(&NCursesPtyWindow::readFromPtyClientThreadMethod, this); std::thread(&Window::readFromPtyClientThreadMethod, this);
} }
NCursesPtyWindow::~NCursesPtyWindow() Window::~Window()
{ {
close(fdPtyHost); close(fdPtyHost);
close(fdPtyClient); close(fdPtyClient);
vterm_free(pseudoTerminal); vterm_free(pseudoTerminal);
} }
int NCursesPtyWindow::getFdPtyClient() const int Window::getFdPtyClient() const
{ {
return fdPtyClient; return fdPtyClient;
} }
void NCursesPtyWindow::writeToClient(const char *output, size_t length) void Window::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 NCursesPtyWindow::writeUnicodeCharToClient(wint_t character) void Window::writeUnicodeCharToClient(wint_t character)
{ {
vterm_keyboard_unichar(pseudoTerminal, character, VTERM_MOD_NONE); vterm_keyboard_unichar(pseudoTerminal, character, VTERM_MOD_NONE);
} }
void NCursesPtyWindow::writeKeyToClient(VTermKey key) void Window::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 NCursesPtyWindow::readSingleUserInput() SingleUserInput Window::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 NCursesPtyWindow::readFromPtyClientThreadMethod() void Window::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 NCursesPtyWindow::readFromPtyClient() void Window::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 NCursesPtyWindow::screenCallbacks = VTermScreenCallbacks Window::screenCallbacks =
{ {
.damage = staticHandlerDamage, .damage = staticHandlerDamage,
.moverect = staticHandlerMoveRect, .moverect = staticHandlerMoveRect,
@ -130,7 +130,7 @@ namespace krikkel::NCursesPtyWindow
.sb_popline = staticHandlerPopLine, .sb_popline = staticHandlerPopLine,
}; };
int NCursesPtyWindow::handlerDamage(VTermRect rect) int Window::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 NCursesPtyWindow::handlerMoveRect(VTermRect dest, VTermRect src) int Window::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 NCursesPtyWindow::handlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible) int Window::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 NCursesPtyWindow::handlerSetTermProp(VTermProp prop, VTermValue *val) int Window::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 NCursesPtyWindow::handlerBell() int Window::handlerBell()
{ {
beep(); beep();
return 0; return 0;
} }
int NCursesPtyWindow::handlerResize(int rows, int cols) int Window::handlerResize(int rows, int cols)
{ {
__debug_log("unimplemented handler called"); __debug_log("unimplemented handler called");
return 0; return 0;
} }
int NCursesPtyWindow::handlerPushLine(int cols, const VTermScreenCell *cells) int Window::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 NCursesPtyWindow::handlerPopLine(int cols, VTermScreenCell *cells) int Window::handlerPopLine(int cols, VTermScreenCell *cells)
{ {
__debug_log("unimplemented handler called"); __debug_log("unimplemented handler called");
return 0; return 0;
} }
attr_t NCursesPtyWindow::extractAttributesFromVTermCell(VTermScreenCell vTermCell) attr_t Window::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 NCursesPtyWindow::extractColorFromVTermCell(VTermScreenCell vTermCell) short Window::extractColorFromVTermCell(VTermScreenCell vTermCell)
{ {
//__debug_log("unimplemented method called"); //__debug_log("unimplemented method called");
return 0; return 0;
} }
void NCursesPtyWindow::copyPtyCellToNCursesWindow(int x, int y) void Window::copyPtyCellToNCursesWindow(int x, int y)
{ {
VTermPos cellPosition = {y, x}; VTermPos cellPosition = {y, x};
VTermScreenCell vTermCell; VTermScreenCell vTermCell;
@ -251,17 +251,17 @@ namespace krikkel::NCursesPtyWindow
add_wch(&converted); add_wch(&converted);
} }
int NCursesPtyWindow::add_wch(const cchar_t *character) int Window::add_wch(const cchar_t *character)
{ {
return ::wadd_wch(w, character); return ::wadd_wch(w, character);
} }
int NCursesPtyWindow::get_wch(wint_t *character) int Window::get_wch(wint_t *character)
{ {
return ::wget_wch(w, character); return ::wget_wch(w, character);
} }
int NCursesPtyWindow::refresh() int Window::refresh()
{ {
move(cursorY, cursorX); move(cursorY, cursorX);
return NCursesWindow::refresh(); return NCursesWindow::refresh();
@ -269,7 +269,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 NCursesPtyWindow::wresize(int rows, int cols) int Window::wresize(int rows, int cols)
{ {
std::lock_guard writeLock(writeToPseudoTerminalMutex); std::lock_guard writeLock(writeToPseudoTerminalMutex);
winsize windowSize = winsize windowSize =
@ -283,57 +283,57 @@ namespace krikkel::NCursesPtyWindow
return NCursesWindow::wresize(rows, cols); return NCursesWindow::wresize(rows, cols);
} }
int NCursesPtyWindow::staticHandlerDamage(VTermRect rect, void *user) int Window::staticHandlerDamage(VTermRect rect, void *user)
{ {
NCursesPtyWindow *instance = static_cast<NCursesPtyWindow *>(user); Window *instance = static_cast<Window *>(user);
return instance->handlerDamage(rect); return instance->handlerDamage(rect);
} }
int NCursesPtyWindow::staticHandlerMoveRect(VTermRect dest, VTermRect src, void *user) int Window::staticHandlerMoveRect(VTermRect dest, VTermRect src, void *user)
{ {
NCursesPtyWindow *instance = static_cast<NCursesPtyWindow *>(user); Window *instance = static_cast<Window *>(user);
return instance->handlerMoveRect(dest, src); return instance->handlerMoveRect(dest, src);
} }
int NCursesPtyWindow::staticHandlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible, void *user) int Window::staticHandlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible, void *user)
{ {
NCursesPtyWindow *instance = static_cast<NCursesPtyWindow *>(user); Window *instance = static_cast<Window *>(user);
return instance->handlerMoveCursor(pos, oldpos, visible); return instance->handlerMoveCursor(pos, oldpos, visible);
} }
int NCursesPtyWindow::staticHandlerSetTermProp(VTermProp prop, VTermValue *val, void *user) int Window::staticHandlerSetTermProp(VTermProp prop, VTermValue *val, void *user)
{ {
NCursesPtyWindow *instance = static_cast<NCursesPtyWindow *>(user); Window *instance = static_cast<Window *>(user);
return instance->handlerSetTermProp(prop, val); return instance->handlerSetTermProp(prop, val);
} }
int NCursesPtyWindow::staticHandlerBell(void *user) int Window::staticHandlerBell(void *user)
{ {
NCursesPtyWindow *instance = static_cast<NCursesPtyWindow *>(user); Window *instance = static_cast<Window *>(user);
return instance->handlerBell(); return instance->handlerBell();
} }
int NCursesPtyWindow::staticHandlerResize(int rows, int cols, void *user) int Window::staticHandlerResize(int rows, int cols, void *user)
{ {
NCursesPtyWindow *instance = static_cast<NCursesPtyWindow *>(user); Window *instance = static_cast<Window *>(user);
return instance->handlerResize(rows, cols); return instance->handlerResize(rows, cols);
} }
int NCursesPtyWindow::staticHandlerPushLine(int cols, const VTermScreenCell *cells, void *user) int Window::staticHandlerPushLine(int cols, const VTermScreenCell *cells, void *user)
{ {
NCursesPtyWindow *instance = static_cast<NCursesPtyWindow *>(user); Window *instance = static_cast<Window *>(user);
return instance->handlerPushLine(cols, cells); return instance->handlerPushLine(cols, cells);
} }
int NCursesPtyWindow::staticHandlerPopLine(int cols, VTermScreenCell *cells, void *user) int Window::staticHandlerPopLine(int cols, VTermScreenCell *cells, void *user)
{ {
NCursesPtyWindow *instance = static_cast<NCursesPtyWindow *>(user); Window *instance = static_cast<Window *>(user);
return instance->handlerPopLine(cols, cells); return instance->handlerPopLine(cols, cells);
} }
void NCursesPtyWindow::staticHandlerOutput(const char *s, size_t len, void *user) void Window::staticHandlerOutput(const char *s, size_t len, void *user)
{ {
NCursesPtyWindow *instance = static_cast<NCursesPtyWindow *>(user); Window *instance = static_cast<Window *>(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

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

View file

@ -4,6 +4,6 @@
* @author Christian Burger (christian@krikkel.de) * @author Christian Burger (christian@krikkel.de)
*/ */
#include "NCursesPtyApp.hpp" #include "App.hpp"
krikkel::NCursesPtyWindow::NCursesPtyApp cursesPtyApp; krikkel::NCursesPtyWindow::App cursesPtyApp;