2022-04-03 10:16:20 +02:00
|
|
|
/**
|
|
|
|
* @author Christian Burger (christian@krikkel.de)
|
|
|
|
*/
|
|
|
|
|
2022-04-13 19:41:41 +02:00
|
|
|
#include "App.hpp"
|
2022-04-03 10:16:20 +02:00
|
|
|
#include "Debug.hpp"
|
2022-05-06 22:42:09 +02:00
|
|
|
#include <kNCurses/VerticalTilingWindowManager.hpp>
|
|
|
|
#include <kNCurses/Window.hpp>
|
|
|
|
#include <kNCurses/PtyWindow.hpp>
|
2022-04-03 10:16:20 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utmp.h>
|
2022-04-05 10:23:50 +02:00
|
|
|
#include <cstdlib>
|
2022-04-25 11:10:07 +02:00
|
|
|
#include <mutex>
|
2022-04-03 10:16:20 +02:00
|
|
|
|
2022-04-29 20:25:47 +02:00
|
|
|
namespace krikkel::NCurses
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
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-05 10:23:50 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-29 20:25:47 +02:00
|
|
|
int DemoApp::run()
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-05-06 14:02:18 +02:00
|
|
|
std::recursive_mutex ncursesMutex;
|
2022-05-03 11:23:32 +02:00
|
|
|
VerticalTilingWindowManager windowManager(Root_Window, &ncursesMutex);
|
|
|
|
|
2022-05-06 22:05:57 +02:00
|
|
|
Window *dummyWindowTop = new Window(&windowManager);
|
|
|
|
dummyWindowTop->move(0, 0);
|
2022-05-03 11:23:32 +02:00
|
|
|
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);
|
2022-05-03 11:23:32 +02:00
|
|
|
windowManager.updateLayout();
|
|
|
|
windowManager.refresh();
|
2022-04-03 10:16:20 +02:00
|
|
|
|
|
|
|
if(fork() == 0)
|
|
|
|
{
|
2022-04-05 10:23:50 +02:00
|
|
|
const char *shellPath = getenv("SHELL");
|
|
|
|
if(!shellPath)
|
|
|
|
shellPath = "/bin/bash";
|
|
|
|
|
2022-04-03 10:16:20 +02:00
|
|
|
login_tty(ptyWindow->getFdPtyClient());
|
|
|
|
const int sizeArg = 2;
|
2022-04-05 10:23:50 +02:00
|
|
|
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);
|
2022-04-03 10:16:20 +02:00
|
|
|
}
|
|
|
|
|
2022-04-05 10:23:50 +02:00
|
|
|
while(true)
|
2022-04-03 10:16:20 +02:00
|
|
|
{
|
2022-05-06 14:02:18 +02:00
|
|
|
SingleUserInput input = windowManager.readSingleUserInput();
|
2022-04-13 10:46:44 +02:00
|
|
|
if(input.isNormalKey())
|
2022-05-06 14:02:18 +02:00
|
|
|
{
|
2022-04-13 10:46:44 +02:00
|
|
|
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
|
|
|
}
|
2022-05-06 14:02:18 +02:00
|
|
|
}
|
2022-04-13 10:46:44 +02:00
|
|
|
if(input.isFunctionKey())
|
2022-04-05 12:34:35 +02:00
|
|
|
{
|
2022-05-06 14:02:18 +02:00
|
|
|
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);
|
2022-05-06 14:02:18 +02:00
|
|
|
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);
|
2022-05-06 14:02:18 +02:00
|
|
|
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);
|
2022-05-06 14:02:18 +02:00
|
|
|
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());
|
2022-05-06 14:02:18 +02:00
|
|
|
}
|
2022-04-05 12:34:35 +02:00
|
|
|
}
|
2022-04-03 10:16:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|