kNCurses/DemoApp.cpp

129 lines
4.1 KiB
C++

/**
* @author Christian Burger (christian@krikkel.de)
*/
#include "DemoApp.hpp"
#include "Debug.hpp"
#include <kNCurses/VerticalTilingWindowManager.hpp>
#include <kNCurses/HorizontalTilingWindowManager.hpp>
#include <kNCurses/Window.hpp>
#include <kNCurses/PtyWindow.hpp>
#include <unistd.h>
#include <utmp.h>
#include <cstdlib>
namespace krikkel::NCurses
{
using std::scoped_lock;
DemoApp::DemoApp() : NCursesApplication(false)
{
}
int DemoApp::run()
{
setUpWindows();
forkShell();
mainLoop();
return 0;
}
void DemoApp::setUpWindows()
{
windowManager
= new VerticalTilingWindowManager(Root_Window, &ncursesMutex);
dummyWindowTop = new Window(windowManager);
ptyWindow = new PtyWindow(&ncursesMutex, 0, 0, 0, 0);
windowManager->addWindow(ptyWindow);
dummyWindowBottom = new Window(windowManager);
windowManager->updateLayout();
windowManager->refresh();
topWindowManager
= new HorizontalTilingWindowManager(dummyWindowTop, &ncursesMutex);
dummyWindowTopLeft = new Window(topWindowManager);
dummyWindowTopRight = new Window(topWindowManager);
topWindowManager->updateLayout();
topWindowManager->refresh();
}
void DemoApp::forkShell()
{
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);
}
}
void DemoApp::mainLoop()
{
while(true)
{
SingleUserInput input = windowManager->readSingleUserInput();
if(input.isNormalKey())
{
ptyWindow->writeUnicodeCharToClient(input.getRawInput());
{
scoped_lock lock(ncursesMutex);
dummyWindowTopRight->addch(input.getRawInput());
dummyWindowTopRight->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());
}
}
}
}
}