kNCurses/SingleUserInput.cpp

106 lines
2.9 KiB
C++
Raw Normal View History

/**
* @author Christian Burger (christian@krikkel.de)
*/
#include "SingleUserInput.hpp"
2022-04-14 09:37:37 +02:00
#include "Debug.hpp"
#include <ncurses.h>
#include <vterm.h>
#define KEY_TAB '\t'
#define KEY_ESCAPE 27
#define KEY_RETURN 10
#define KEY_ALT_BACKSPACE 127
#define KEY_ALT_ALT_BACKSPACE '\b'
namespace krikkel::NCursesPtyWindow
{
SingleUserInput::SingleUserInput(int _resultGetWchCall, wint_t _input)
: input(_input), resultGetWchCall(_resultGetWchCall)
{
}
bool SingleUserInput::isNormalKey()
{
return resultGetWchCall == OK;
}
bool SingleUserInput::isFunctionKey()
{
return resultGetWchCall == KEY_CODE_YES;
}
VTermKey SingleUserInput::mapKeyNcursesToVTerm()
{
if(input >= KEY_MAX)
return VTERM_KEY_NONE;
debug();
// thought about array mapping instead of `switch()` statements, but as
// it is only processing user input, it is not time critical enough for
// the wasted space
// @tode unmapped keys: keys erase
if(resultGetWchCall == OK)
switch(input)
{
case KEY_TAB:
return VTERM_KEY_TAB;
case KEY_ESCAPE:
return VTERM_KEY_ESCAPE;
default:
; // we cannot translate
}
if(resultGetWchCall == KEY_CODE_YES)
switch(input)
{
case KEY_ENTER:
return VTERM_KEY_ENTER;
case KEY_BACKSPACE:
return VTERM_KEY_BACKSPACE;
case KEY_UP:
return VTERM_KEY_UP;
case KEY_DOWN:
return VTERM_KEY_DOWN;
case KEY_LEFT:
return VTERM_KEY_LEFT;
case KEY_RIGHT:
return VTERM_KEY_RIGHT;
case KEY_IC:
return VTERM_KEY_INS;
case KEY_DC:
return VTERM_KEY_DEL;
case KEY_HOME:
return VTERM_KEY_HOME;
case KEY_END:
return VTERM_KEY_END;
case KEY_PPAGE:
return VTERM_KEY_PAGEUP;
case KEY_NPAGE:
return VTERM_KEY_PAGEDOWN;
default:
if(input >= KEY_F0 && input < KEY_F0 + 12)
return static_cast<VTermKey>(VTERM_KEY_FUNCTION(input - KEY_F0));
}
__debug_log("previous ncurses input could not be decoded to vterm input");
return VTERM_KEY_NONE;
}
wint_t SingleUserInput::getRawInput()
{
return input;
}
void SingleUserInput::debug()
{
#ifndef NDEGUG
char octalRepresentation[16];
snprintf(octalRepresentation, 16, "%o", input);
__debug_log("mapping ncurses key: " + std::to_string(input) + " octal: " + octalRepresentation);
#endif // NDEBUG
}
}