Christian Burger
b9e32941fb
* switching to recursive mutexes for now; easier to implement * need to read user input from the window manager; hidden windows are drawn when reading input from there * note: occasional dead lock between PTY and ncurses mutex * fixed type in class `SingleUserInput`
102 lines
3.3 KiB
C++
102 lines
3.3 KiB
C++
/**
|
|
* @author Christian Burger (christian@krikkel.de)
|
|
*/
|
|
|
|
#include "App.hpp"
|
|
#include "Debug.hpp"
|
|
#include <NCurses/VerticalTilingWindowManager.hpp>
|
|
#include <NCurses/Window.hpp>
|
|
#include <NCurses/PtyWindow.hpp>
|
|
|
|
#include <unistd.h>
|
|
#include <utmp.h>
|
|
#include <cstdlib>
|
|
#include <mutex>
|
|
|
|
namespace krikkel::NCurses
|
|
{
|
|
DemoApp::DemoApp() : NCursesApplication(false)
|
|
{
|
|
|
|
}
|
|
|
|
int DemoApp::run()
|
|
{
|
|
std::recursive_mutex ncursesMutex;
|
|
VerticalTilingWindowManager windowManager(Root_Window, &ncursesMutex);
|
|
|
|
Window *dummyWindow = new Window(&windowManager);
|
|
dummyWindow->move(0, 0);
|
|
PtyWindow *ptyWindow = new PtyWindow(&ncursesMutex, 0, 0, 0, 0);
|
|
windowManager.addWindow(ptyWindow);
|
|
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())
|
|
{
|
|
std::lock_guard lock(ncursesMutex);
|
|
ptyWindow->writeUnicodeCharToClient(input.getRawInput());
|
|
dummyWindow->addch(input.getRawInput());
|
|
dummyWindow->refresh();
|
|
}
|
|
if(input.isFunctionKey())
|
|
{
|
|
switch(input.getRawInput())
|
|
{
|
|
case KEY_RESIZE:
|
|
windowManager.updateLayout();
|
|
break;
|
|
case KEY_F(1):
|
|
windowManager.hideWindow(ptyWindow);
|
|
windowManager.updateLayout();
|
|
windowManager.refresh();
|
|
break;
|
|
case KEY_F(2):
|
|
windowManager.showWindow(ptyWindow);
|
|
windowManager.updateLayout();
|
|
windowManager.refresh();
|
|
break;
|
|
case KEY_F(3):
|
|
windowManager.hideWindow(dummyWindow);
|
|
windowManager.updateLayout();
|
|
windowManager.refresh();
|
|
break;
|
|
case KEY_F(4):
|
|
windowManager.showWindow(dummyWindow);
|
|
windowManager.updateLayout();
|
|
windowManager.refresh();
|
|
break;
|
|
case KEY_F(5):
|
|
windowManager.refresh();
|
|
break;
|
|
default:
|
|
ptyWindow->writeKeyToClient(input.mapKeyNcursesToVTerm());
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|