b15f/driver/cli.cpp

152 lines
2.6 KiB
C++
Raw Normal View History

2019-04-05 06:33:31 +00:00
/** TODO
*
* - throw exception -> raise SIGINT
2019-04-05 06:47:06 +00:00
* - delete view stack
2019-04-05 06:33:31 +00:00
*/
2019-04-03 13:20:49 +00:00
#include <stdio.h>
#include <ncurses.h> // sudo apt-get install libncurses5-dev
2019-04-03 14:15:35 +00:00
#include <vector>
#include <string>
#include <iostream>
2019-04-04 14:16:28 +00:00
#include <signal.h>
2019-04-03 14:15:35 +00:00
#include <sys/ioctl.h>
#include <unistd.h>
2019-04-04 14:16:28 +00:00
#include <signal.h>
2019-04-05 06:33:31 +00:00
#include "ui/view_selection.h"
2019-04-04 11:19:11 +00:00
#include "ui/view_info.h"
2019-04-05 06:47:06 +00:00
#include "ui/view_monitor.h"
2019-04-05 06:33:31 +00:00
//#include "drv/b15f.h"
2019-04-03 13:20:49 +00:00
2019-04-04 14:16:28 +00:00
// global error message
std::string ERR_MSG;
2019-04-03 13:20:49 +00:00
2019-04-04 11:19:11 +00:00
std::vector<View*> win_stack;
2019-04-04 14:16:28 +00:00
void cleanup()
{
clrtoeol();
refresh();
endwin();
}
void signal_handler(int signal)
{
if(signal == SIGWINCH)
{
if(win_stack.size())
{
usleep(1000);
win_stack.back()->repaint();
}
}
else if(signal == SIGINT)
{
cleanup();
std::cout << "SIGINT - Abbruch." << std::endl;
if(ERR_MSG.length())
std::cout << "ERR_MSG: " << ERR_MSG << std::endl;
exit(EXIT_FAILURE);
}
}
2019-04-03 13:20:49 +00:00
2019-04-04 11:19:11 +00:00
void init()
2019-04-03 14:15:35 +00:00
{
2019-04-04 14:16:28 +00:00
// init b15 driver
2019-04-05 06:33:31 +00:00
//B15F::getInstance();
2019-04-04 14:16:28 +00:00
// init all ncurses stuff
2019-04-04 11:19:11 +00:00
initscr();
start_color();
curs_set(0); // 0: invisible, 1: normal, 2: very visible
clear();
noecho();
cbreak(); // Line buffering disabled. pass on everything
mousemask(ALL_MOUSE_EVENTS, NULL);
2019-04-03 14:15:35 +00:00
2019-04-04 14:16:28 +00:00
// connect signals to handler
signal(SIGWINCH, signal_handler);
signal(SIGINT, signal_handler);
// set view context
2019-04-04 11:19:11 +00:00
View::setWinContext(newwin(32, 128, 0, 0));
2019-04-03 14:15:35 +00:00
}
2019-04-04 14:16:28 +00:00
void finish(int)
2019-04-04 11:19:11 +00:00
{
cleanup();
exit(EXIT_SUCCESS);
}
2019-04-03 14:15:35 +00:00
2019-04-04 14:16:28 +00:00
void input(int)
2019-04-04 11:19:11 +00:00
{
std::function<void(int)> nextCall;
int key;
do
{
key = wgetch(View::getWinContext());
nextCall = win_stack.back()->keypress(key);
if(key == -1)
{
win_stack.pop_back();
if(win_stack.size())
win_stack.back()->repaint();
return;
2019-04-03 14:15:35 +00:00
}
2019-04-04 11:19:11 +00:00
if(nextCall)
nextCall(key);
2019-04-03 14:15:35 +00:00
}
2019-04-04 11:19:11 +00:00
while(!false);
}
2019-04-04 14:16:28 +00:00
void show_info(int)
2019-04-04 11:19:11 +00:00
{
2019-04-04 14:16:28 +00:00
ViewInfo* view = new ViewInfo();
2019-04-04 11:19:11 +00:00
view->setTitle("Info");
2019-04-04 14:16:28 +00:00
view->setText("Informationen zu Board 15 Famulus Edition\nEs war einmal");
view->setLabelClose("[ Zurueck ]");
2019-04-04 11:19:11 +00:00
view->repaint();
win_stack.push_back(view);
input(0);
}
2019-04-05 06:47:06 +00:00
void show_monitor(int)
{
ViewMonitor* view = new ViewMonitor();
view->setTitle("Monitor");
view->setText("U0 = ");
view->setLabelClose("[ Zurueck ]");
view->repaint();
win_stack.push_back(view);
input(0);
}
2019-04-04 14:16:28 +00:00
void show_main(int)
2019-04-04 11:19:11 +00:00
{
2019-04-05 06:33:31 +00:00
ViewSelection* view = new ViewSelection();
2019-04-04 11:19:11 +00:00
view->setTitle("B15F - Command Line Interface");
2019-04-05 06:47:06 +00:00
view->addChoice("[ Monitor - Eingaben beobachten ]", &show_monitor);
2019-04-05 06:33:31 +00:00
view->addChoice("[ Informationen ]", &show_info);
view->addChoice("", nullptr);
view->addChoice("[ Beenden ]", &finish);
2019-04-04 11:19:11 +00:00
view->repaint();
win_stack.push_back(view);
input(0);
2019-04-03 14:15:35 +00:00
}
2019-04-03 13:20:49 +00:00
int main()
2019-04-04 11:19:11 +00:00
{
init();
2019-04-03 14:15:35 +00:00
2019-04-04 11:19:11 +00:00
show_main(0);
cleanup();
return EXIT_SUCCESS;
2019-04-03 13:20:49 +00:00
}