kNCurses/App.cpp
Christian Burger 2863c8ae16 first release v0.1
Basic pseudo terminal capabilities in an ncurses window provided. Can
run at least `nano`, `top` and `sudo`. Probably a lot more. No colors,
yet.
2022-04-23 22:56:47 +02:00

64 lines
1.7 KiB
C++

/**
* @author Christian Burger (christian@krikkel.de)
*/
#include "App.hpp"
#include "Debug.hpp"
#include <NCursesPtyWindow/Window.hpp>
#include <unistd.h>
#include <utmp.h>
#include <cstdlib>
namespace krikkel::NCursesPtyWindow
{
App::App() : NCursesApplication(false)
{
}
int App::run()
{
std::mutex writeMutex;
Window *ptyWindow = new Window(&writeMutex
, Root_Window->lines()
, Root_Window->cols()
, 0
, 0);
if(fork() == 0)
{
const char *shellPath = getenv("SHELL");
if(!shellPath)
shellPath = "/bin/bash";
login_tty(ptyWindow->getFdPtyClient());
const int sizeArg = 2;
const char *arg[sizeArg] = {shellPath, NULL};
fprintf(stderr, "<CTRL>+C exits shell.\n\n");
execv(shellPath, const_cast<char * const *>(arg));
fprintf(stderr, "Well, well, well … could not start a shell. We "
"tried `%s`. Maybe set `SHELL` environment variable"
" to a working value?", shellPath);
exit(1);
}
while(true)
{
SingleUserInput input = ptyWindow->readSingleUserInput();
if(input.isNormalKey())
ptyWindow->writeUnicodeCharToClient(input.getRawInput());
if(input.isFunctionKey())
{
if(input.getRawInput() == KEY_RESIZE)
ptyWindow->wresize(Root_Window->lines(), Root_Window->cols());
else
ptyWindow->writeKeyToClient(input.mapKeyNcursesToVTerm());
}
}
return 0;
}
}