B15F
Board 15 Famulus Edition
cli.cpp
1 #define B15F_CLI_DEBUG
2 
3 #include <stdio.h>
4 #include <ncurses.h> // sudo apt-get install libncurses5-dev
5 #include <vector>
6 #include <string>
7 #include <iostream>
8 #include <signal.h>
9 #include <sys/ioctl.h>
10 #include <unistd.h>
11 #include <signal.h>
12 #include <future>
13 #include <thread>
14 #include <chrono>
15 #include "drv/b15f.h"
16 #include "ui/ui.h"
17 #include "ui/view_selection.h"
18 #include "ui/view_info.h"
19 #include "ui/view_monitor.h"
20 #include "ui/view_promt.h"
21 
22 volatile int win_changed_cooldown = 0;
23 volatile bool t_refresh_active = false;
24 
25 void signal_handler(int signal)
26 {
27  if(signal == SIGWINCH)
28  {
29  win_changed_cooldown = 10; // 100ms
30 
31  if (!t_refresh_active)
32  {
33  if(t_refresh.joinable())
34  t_refresh.join();
35  t_refresh_active = true;
36  t_refresh = std::thread([](){
37 
38  while(win_changed_cooldown--)
39  std::this_thread::sleep_for(std::chrono::milliseconds(10));
40 
41  t_refresh_active = false;
42 
43  if(win_stack.size())
44  win_stack.back()->repaint();
45 
46  });
47  }
48 
49  }
50  else if(signal == SIGINT)
51  {
52  cleanup();
53  std::cout << "SIGINT - Abbruch." << std::endl;
54  exit(EXIT_FAILURE);
55  }
56 }
57 
58 void abort_handler(std::exception& ex)
59 {
60  ViewInfo* view = new ViewInfo();
61  view->setTitle("Fehler");
62  std::string msg(ex.what());
63  msg += "\n\nBeende in 5 Sekunden.";
64  view->setText(msg.c_str());
65  view->setLabelClose("");
66  view->repaint();
67 
68  std::this_thread::sleep_for(std::chrono::milliseconds(5000));
69 
70  cleanup();
71  std::cerr << std::endl << "*** EXCEPTION ***" << std::endl << ex.what() << std::endl;
72  exit(EXIT_FAILURE);
73 }
74 
75 void init()
76 {
77  // init b15 driver
79 #ifndef B15F_CLI_DEBUG
80  std::cout << std::endl << "Starte in 3s ..." << std::endl;
81  sleep(3);
82 #endif
83  B15F::setAbortHandler(&abort_handler);
84 
85  // init all ncurses stuff
86  initscr();
87  start_color();
88  curs_set(0); // 0: invisible, 1: normal, 2: very visible
89  clear();
90  noecho();
91  cbreak(); // Line buffering disabled. pass on everything
92  mousemask(ALL_MOUSE_EVENTS, NULL);
93 
94  // connect signals to handler
95  signal(SIGWINCH, signal_handler);
96  signal(SIGINT, signal_handler);
97 
98  // set view context
99  View::setWinContext(newwin(25, 85, 0, 0));
100 }
101 
102 
103 int main()
104 {
105  init();
106 
107  int exit_code = EXIT_SUCCESS;
108 
109  show_main(0);
110 
111  cleanup();
112 
113  return exit_code;
114 }
ViewInfo
Definition: view_info.h:6
B15F::getInstance
static B15F & getInstance(void)
Definition: b15f.cpp:300
B15F::setAbortHandler
static void setAbortHandler(errorhandler_t func)
Definition: b15f.cpp:339