kNCurses/App.cpp

110 lines
3.7 KiB
C++
Raw Normal View History

/**
* @author Christian Burger (christian@krikkel.de)
*/
2022-04-13 19:41:41 +02:00
#include "App.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>
#include <mutex>
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()
{
std::recursive_mutex ncursesMutex;
VerticalTilingWindowManager windowManager(Root_Window, &ncursesMutex);
2022-05-06 22:05:57 +02:00
Window *dummyWindowTop = new Window(&windowManager);
dummyWindowTop->move(0, 0);
PtyWindow *ptyWindow = new PtyWindow(&ncursesMutex, 0, 0, 0, 0);
windowManager.addWindow(ptyWindow);
2022-05-06 22:05:57 +02:00
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());
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);
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);
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);
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());
}
}
}
return 0;
}
}