/** * @author Christian Burger (christian@krikkel.de) */ #include "DemoApp.hpp" #include "Debug.hpp" #include #include #include #include #include #include namespace krikkel::NCurses { using std::scoped_lock; DemoApp::DemoApp() : NCursesApplication(false) { } int DemoApp::run() { setUpWindows(); mainLoop(); return 0; } void DemoApp::setUpWindows() { windowManager = new VerticalTilingWindowManager(Root_Window, &ncursesMutex); dummyWindowTop = new Window(windowManager); 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, "+C exits shell.\n\n"); execv(shellPath, const_cast(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); dummyWindowTop->addch(input.getRawInput()); dummyWindowTop->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()); } } } } }