kNCurses/App.cpp

110 lines
3.7 KiB
C++

/**
* @author Christian Burger (christian@krikkel.de)
*/
#include "App.hpp"
#include "Debug.hpp"
#include <kNCurses/VerticalTilingWindowManager.hpp>
#include <kNCurses/Window.hpp>
#include <kNCurses/PtyWindow.hpp>
#include <unistd.h>
#include <utmp.h>
#include <cstdlib>
#include <mutex>
namespace krikkel::NCurses
{
using std::scoped_lock;
DemoApp::DemoApp() : NCursesApplication(false)
{
}
int DemoApp::run()
{
std::recursive_mutex ncursesMutex;
VerticalTilingWindowManager windowManager(Root_Window, &ncursesMutex);
Window *dummyWindowTop = new Window(&windowManager);
dummyWindowTop->move(0, 0);
PtyWindow *ptyWindow = new PtyWindow(&ncursesMutex, 0, 0, 0, 0);
windowManager.addWindow(ptyWindow);
Window *dummyWindowBottom = new Window(&windowManager);
windowManager.updateLayout();
windowManager.refresh();
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 = windowManager.readSingleUserInput();
if(input.isNormalKey())
{
ptyWindow->writeUnicodeCharToClient(input.getRawInput());
{
scoped_lock lock(ncursesMutex);
dummyWindowTop->addch(input.getRawInput());
dummyWindowTop->refresh();
}
}
if(input.isFunctionKey())
{
switch(input.getRawInput())
{
case KEY_RESIZE:
windowManager.updateLayout();
break;
case KEY_F(1):
if(dummyWindowTop->isHidden())
windowManager.showWindow(dummyWindowTop);
else
windowManager.hideWindow(dummyWindowTop);
windowManager.updateLayout();
windowManager.refresh();
break;
case KEY_F(2):
if(ptyWindow->isHidden())
windowManager.showWindow(ptyWindow);
else
windowManager.hideWindow(ptyWindow);
windowManager.updateLayout();
windowManager.refresh();
break;
case KEY_F(3):
if(dummyWindowBottom->isHidden())
windowManager.showWindow(dummyWindowBottom);
else
windowManager.hideWindow(dummyWindowBottom);
windowManager.updateLayout();
windowManager.refresh();
break;
case KEY_F(5):
windowManager.refresh();
break;
default:
ptyWindow->writeFunctionKeyToClient(input.mapKeyNcursesToVTerm());
}
}
}
return 0;
}
}