kNCurses/DemoApp.cpp

117 lines
3.7 KiB
C++
Raw Normal View History

/**
* @author Christian Burger (christian@krikkel.de)
*/
2022-05-08 21:26:12 +02:00
#include "DemoApp.hpp"
#include "Debug.hpp"
2022-05-06 22:42:09 +02:00
#include <kNCurses/VerticalTilingWindowManager.hpp>
#include <kNCurses/Window.hpp>
#include <kNCurses/PtyWindow.hpp>
#include <unistd.h>
#include <utmp.h>
#include <cstdlib>
2022-04-29 20:25:47 +02:00
namespace krikkel::NCurses
{
2022-05-06 18:37:48 +02:00
using std::scoped_lock;
2022-04-29 20:25:47 +02:00
DemoApp::DemoApp() : NCursesApplication(false)
{
}
2022-04-29 20:25:47 +02:00
int DemoApp::run()
{
setUpWindows();
mainLoop();
return 0;
}
void DemoApp::setUpWindows()
{
windowManager = new VerticalTilingWindowManager(Root_Window, &ncursesMutex);
dummyWindowTop = new Window(windowManager);
2022-05-06 22:05:57 +02:00
dummyWindowTop->move(0, 0);
ptyWindow = new PtyWindow(&ncursesMutex, 0, 0, 0, 0);
windowManager->addWindow(ptyWindow);
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);
}
}
void DemoApp::mainLoop()
{
while(true)
{
SingleUserInput input = windowManager->readSingleUserInput();
if(input.isNormalKey())
{
ptyWindow->writeUnicodeCharToClient(input.getRawInput());
2022-05-06 18:37:48 +02:00
{
scoped_lock lock(ncursesMutex);
2022-05-06 22:05:57 +02:00
dummyWindowTop->addch(input.getRawInput());
dummyWindowTop->refresh();
2022-05-06 18:37:48 +02:00
}
}
if(input.isFunctionKey())
{
switch(input.getRawInput())
{
case KEY_RESIZE:
windowManager->updateLayout();
break;
case KEY_F(1):
2022-05-06 22:05:57 +02:00
if(dummyWindowTop->isHidden())
windowManager->showWindow(dummyWindowTop);
2022-05-06 22:05:57 +02:00
else
windowManager->hideWindow(dummyWindowTop);
windowManager->updateLayout();
windowManager->refresh();
break;
case KEY_F(2):
2022-05-06 22:05:57 +02:00
if(ptyWindow->isHidden())
windowManager->showWindow(ptyWindow);
2022-05-06 22:05:57 +02:00
else
windowManager->hideWindow(ptyWindow);
windowManager->updateLayout();
windowManager->refresh();
break;
case KEY_F(3):
2022-05-06 22:05:57 +02:00
if(dummyWindowBottom->isHidden())
windowManager->showWindow(dummyWindowBottom);
2022-05-06 22:05:57 +02:00
else
windowManager->hideWindow(dummyWindowBottom);
windowManager->updateLayout();
windowManager->refresh();
break;
case KEY_F(5):
windowManager->refresh();
break;
default:
2022-05-06 22:05:57 +02:00
ptyWindow->writeFunctionKeyToClient(input.mapKeyNcursesToVTerm());
}
}
}
}
}