refactoring: closes #10
This commit is contained in:
parent
8e71512f12
commit
4ca933a158
|
@ -2,8 +2,8 @@
|
|||
* @author Christian Burger (christian@krikkel.de)
|
||||
*/
|
||||
|
||||
#include "NCursesPtyApp.hpp"
|
||||
#include "NCursesPtyWindow.hpp"
|
||||
#include "App.hpp"
|
||||
#include "Window.hpp"
|
||||
#include "Debug.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
|
@ -12,14 +12,14 @@
|
|||
|
||||
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->cols()
|
||||
, 0
|
|
@ -10,10 +10,10 @@
|
|||
|
||||
namespace krikkel::NCursesPtyWindow
|
||||
{
|
||||
class NCursesPtyApp : public NCursesApplication
|
||||
class App : public NCursesApplication
|
||||
{
|
||||
public:
|
||||
NCursesPtyApp();
|
||||
App();
|
||||
|
||||
private:
|
||||
int run() override;
|
|
@ -12,7 +12,7 @@ include(ExternalProject)
|
|||
include("cmake/ncurses.cmake")
|
||||
include("cmake/gsl.cmake")
|
||||
|
||||
add_library(NCursesPtyWindow NCursesPtyWindow.cpp SingleUserInput.cpp Debug.cpp)
|
||||
add_library(NCursesPtyWindow Window.cpp SingleUserInput.cpp Debug.cpp)
|
||||
|
||||
### libraries
|
||||
|
||||
|
@ -27,7 +27,7 @@ find_package(Threads REQUIRED)
|
|||
target_link_libraries(NCursesPtyWindow Threads::Threads)
|
||||
|
||||
### demo application
|
||||
add_executable(NCursesPtyApp main.cpp NCursesPtyApp.cpp)
|
||||
add_executable(NCursesPtyApp main.cpp App.cpp)
|
||||
target_link_libraries(NCursesPtyApp ${CURSES_LIBRARIES} NCursesPtyWindow)
|
||||
|
||||
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @author Christian Burger (christian@krikkel.de)
|
||||
*/
|
||||
|
||||
#include "NCursesPtyWindow.hpp"
|
||||
#include "Window.hpp"
|
||||
#include "Debug.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
|
@ -18,7 +18,7 @@ namespace krikkel::NCursesPtyWindow
|
|||
using gsl::narrow;
|
||||
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)
|
||||
{
|
||||
// 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(&NCursesPtyWindow::readFromPtyClientThreadMethod, this);
|
||||
std::thread(&Window::readFromPtyClientThreadMethod, this);
|
||||
}
|
||||
|
||||
NCursesPtyWindow::~NCursesPtyWindow()
|
||||
Window::~Window()
|
||||
{
|
||||
close(fdPtyHost);
|
||||
close(fdPtyClient);
|
||||
vterm_free(pseudoTerminal);
|
||||
}
|
||||
|
||||
int NCursesPtyWindow::getFdPtyClient() const
|
||||
int Window::getFdPtyClient() const
|
||||
{
|
||||
return fdPtyClient;
|
||||
}
|
||||
|
||||
void NCursesPtyWindow::writeToClient(const char *output, size_t length)
|
||||
void Window::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 NCursesPtyWindow::writeUnicodeCharToClient(wint_t character)
|
||||
void Window::writeUnicodeCharToClient(wint_t character)
|
||||
{
|
||||
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));
|
||||
vterm_keyboard_key(pseudoTerminal, key, VTERM_MOD_NONE);
|
||||
}
|
||||
|
||||
SingleUserInput NCursesPtyWindow::readSingleUserInput()
|
||||
SingleUserInput Window::readSingleUserInput()
|
||||
{
|
||||
wint_t input;
|
||||
int result = get_wch(&input);
|
||||
return SingleUserInput(result, input);
|
||||
}
|
||||
|
||||
void NCursesPtyWindow::readFromPtyClientThreadMethod()
|
||||
void Window::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 NCursesPtyWindow::readFromPtyClient()
|
||||
void Window::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 NCursesPtyWindow::screenCallbacks =
|
||||
VTermScreenCallbacks Window::screenCallbacks =
|
||||
{
|
||||
.damage = staticHandlerDamage,
|
||||
.moverect = staticHandlerMoveRect,
|
||||
|
@ -130,7 +130,7 @@ namespace krikkel::NCursesPtyWindow
|
|||
.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 y = rect.start_row; y < rect.end_row; ++y)
|
||||
|
@ -141,7 +141,7 @@ namespace krikkel::NCursesPtyWindow
|
|||
return 0;
|
||||
}
|
||||
|
||||
int NCursesPtyWindow::handlerMoveRect(VTermRect dest, VTermRect src)
|
||||
int Window::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 NCursesPtyWindow::handlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible)
|
||||
int Window::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 NCursesPtyWindow::handlerSetTermProp(VTermProp prop, VTermValue *val)
|
||||
int Window::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 NCursesPtyWindow::handlerBell()
|
||||
int Window::handlerBell()
|
||||
{
|
||||
beep();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int NCursesPtyWindow::handlerResize(int rows, int cols)
|
||||
int Window::handlerResize(int rows, int cols)
|
||||
{
|
||||
__debug_log("unimplemented handler called");
|
||||
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");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int NCursesPtyWindow::handlerPopLine(int cols, VTermScreenCell *cells)
|
||||
int Window::handlerPopLine(int cols, VTermScreenCell *cells)
|
||||
{
|
||||
__debug_log("unimplemented handler called");
|
||||
return 0;
|
||||
}
|
||||
|
||||
attr_t NCursesPtyWindow::extractAttributesFromVTermCell(VTermScreenCell vTermCell)
|
||||
attr_t Window::extractAttributesFromVTermCell(VTermScreenCell vTermCell)
|
||||
{
|
||||
attr_t result = A_NORMAL;
|
||||
//__debug_log("unimplemented method called");
|
||||
return result;
|
||||
}
|
||||
|
||||
short NCursesPtyWindow::extractColorFromVTermCell(VTermScreenCell vTermCell)
|
||||
short Window::extractColorFromVTermCell(VTermScreenCell vTermCell)
|
||||
{
|
||||
//__debug_log("unimplemented method called");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void NCursesPtyWindow::copyPtyCellToNCursesWindow(int x, int y)
|
||||
void Window::copyPtyCellToNCursesWindow(int x, int y)
|
||||
{
|
||||
VTermPos cellPosition = {y, x};
|
||||
VTermScreenCell vTermCell;
|
||||
|
@ -251,17 +251,17 @@ namespace krikkel::NCursesPtyWindow
|
|||
add_wch(&converted);
|
||||
}
|
||||
|
||||
int NCursesPtyWindow::add_wch(const cchar_t *character)
|
||||
int Window::add_wch(const cchar_t *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);
|
||||
}
|
||||
|
||||
int NCursesPtyWindow::refresh()
|
||||
int Window::refresh()
|
||||
{
|
||||
move(cursorY, cursorX);
|
||||
return NCursesWindow::refresh();
|
||||
|
@ -269,7 +269,7 @@ namespace krikkel::NCursesPtyWindow
|
|||
|
||||
/// @todo potential racing condition where drawing into terminal while
|
||||
/// resizing?
|
||||
int NCursesPtyWindow::wresize(int rows, int cols)
|
||||
int Window::wresize(int rows, int cols)
|
||||
{
|
||||
std::lock_guard writeLock(writeToPseudoTerminalMutex);
|
||||
winsize windowSize =
|
||||
|
@ -283,57 +283,57 @@ namespace krikkel::NCursesPtyWindow
|
|||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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)) + "'");
|
||||
instance->writeToClient(s, len);
|
||||
}
|
|
@ -26,11 +26,11 @@ inline void UNDEF(get_wch)(wint_t *character) { get_wch(character); }
|
|||
|
||||
namespace krikkel::NCursesPtyWindow
|
||||
{
|
||||
class NCursesPtyWindow : public NCursesWindow
|
||||
class Window : public NCursesWindow
|
||||
{
|
||||
public:
|
||||
NCursesPtyWindow(int lines, int columns, int y, int x);
|
||||
~NCursesPtyWindow();
|
||||
Window(int lines, int columns, int y, int x);
|
||||
~Window();
|
||||
|
||||
int getFdPtyClient() const;
|
||||
void writeToClient(const char * string, size_t length);
|
Loading…
Reference in a new issue