rtop
screen2.h
Go to the documentation of this file.
1 #include <ncurses.h>
2 #include <panel.h>
3 #include <vector>
4 #include <utility>
5 #include <string>
6 #include <fstream>
7 #include <cstdlib>
8 #include <iostream>
9 
10 #ifndef _RTOP_
11 #define _RTOP_
12 #include "rtop_logger.h"
13 #include "rtop_utils.h"
14 #include "view2.h"
15 #include "sm.h"
16 #include "params.h"
17 
18 extern src::severity_logger< severity_level > lg;
19 extern logSpacer log_spacer;
20 
21 namespace rtop
22 {
23  extern MapOfKeyDicts mapKeyDict;
24  extern MemFuncPDict funcPDict;
25 
27 
35  {
36  public:
37  ScreenManager(int);
39  void start();
40  void refresh();
41  void resolveKey(int ch);
42  int CurrentView() const{return smViews.currState();}
43  int uUid() const {return uuid;}
44  private:
45  int uuid;
49  std::vector<View*> views;
54  friend class XMLTree;
55  };
56 
57 
59  ScreenManager::ScreenManager(int id): uuid{id}
60  {
61  // initialize ncurses
62  initscr(); // initialize terminal in curses mode
63  raw(); // disable line buffering, interpret each keypress - don't pass directly to terminal
64  noecho(); // switch off echoing
65  keypad(stdscr, TRUE); // enable function keys and arrow key for the stdscr. get key input from stdscr
66  set_escdelay(100); // esc delay = 100msec, so that ESC is processed without any perceptible delay
67  curs_set(0); // prevent cursor blinking in menu
68  start_color();
69  for(auto pos: colors) // initialize color pairs
70  init_pair(pos.first, (pos.second).first, (pos.second).second);
71  }
72 
74 
81  {
83  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> ScreenManager-"<<uuid<<"_resolvekey::";
84  // set KEY_INPUT global to received key.
85  KEY_INPUT = ch; // KEY_INPUT will be used by GUI objects downstream
86  // resolve key with ScreenManager's key dictionary
87  (mapKeyDict.getKeyDict(smKeys.currState())).resolve(ch); // carried out actions may update ScreenManager's View StateMachine and Key StateMachine
88 
89  // if EXIT global set to true, return immediately
90  if (EXIT)
91  {
92  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- ScreenManager-"<<uuid<<"_resolvekey::EXIT";
94  return;
95  }
96 
97  // if View SM reports view change
98  if (smViews.isChanged())
99  {
100  (funcPDict.getView(smViews.prevState()))->hide(); // hide prev view
101  currView = funcPDict.getView(smViews.currState()); // update the current view for the ScreenManager
102  (funcPDict.getView(smViews.prevState()))->refresh(); // refresh prev view to actually be hidden
103  currView->show(); // show the new current view to actually be visible
104  (funcPDict.getView(smViews.prevState()))->resolveKey(ch); // pass on key to prev view for processing
105  }
106  currView->resolveKey(ch); // pass on the key current view for processing
107  refresh(); // refreshes current view and makes it any changes visible on screen
108  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- ScreenManager-"<<uuid<<"_resolvekey::";
110  }
111 
114  {
115  log_spacer.addSpace();
116  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> ScreenManager-"<<uuid<<"_start::";
117  int ch;
118  while(1)
119  {
120  ch = getch(); // wait until key input
121  BOOST_LOG_SEV(lg, info)<<log_spacer<<"ScreenManager-"<<uuid<<"_start::, KEYINPUT="<<ch;
122  resolveKey(ch); // resolve key
123  if (EXIT) // if EXIT set to true, exit loop
124  break;
125  }
126  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- ScreenManager-"<<uuid<<"_start::";
128  }
129 
131 
136  {
138  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> ScreenManager-"<<uuid<<"_refresh::";
139  currView->refresh();
140  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- ScreenManager-"<<uuid<<"_refresh::";
142  }
143 
146  {
148  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> ScreenManager-"<<uuid<<"_~::";
149  endwin(); // ends the curses mode. frees memory taken up by ncurses and puts the terminal in the normal mode
150  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- ScreenManager-"<<uuid<<"_~::";
152  }
153 
154 } //namespace rtop
155 #endif // _RTOP_
156 
MapOfKeyDicts mapKeyDict
global variable for accessing key dictionaries by their uuid
Definition: key_dict.h:175
logSpacer log_spacer
Definition: rtop_logger.h:186
void show()
sets view to visible
Definition: view2.h:155
bool isChanged() const
returns true if prev_state and curr_state are different
Definition: sm.h:52
ScreenManager(int)
ScreenManager constructor. Initializes ncurses.
Definition: screen2.h:59
std::vector< View * > views
collection of view object pointers
Definition: screen2.h:49
int currState() const
returns current state
Definition: sm.h:48
int prevState() const
returns previous state
Definition: sm.h:50
void resolveKey(int ch)
resolves the key input at ScreenManager level
Definition: screen2.h:80
StateMachine smViews
Views statemachine.
Definition: screen2.h:53
MemFuncPDict funcPDict
Definition: key_dict.h:20
int uUid() const
Definition: screen2.h:43
~ScreenManager()
ScreenManager destructor. exit curses mode, thus releasing all resources.
Definition: screen2.h:145
2nd level UI object. Responsible for switching between panels and resolving key input
Definition: view2.h:30
void refresh()
refreshes view
Definition: view2.h:109
void delSpace()
deletes one white-space from white-space string corresponding to invoking thread
Definition: rtop_logger.h:151
std::map< int, std::pair< int, int > > colors
Definition: params.h:11
void addSpace()
adds one white-space to white-space string corresponding to invoking thread
Definition: rtop_logger.h:135
Top Level UI object. Responsible to switching between views and capturing key input.
Definition: screen2.h:34
int CurrentView() const
Definition: screen2.h:42
View * currView
pointer to current view object
Definition: screen2.h:47
KeyDict getKeyDict(int)
Definition: key_dict.h:154
enables indentation of logs for easy viewing
Definition: rtop_logger.h:118
StateMachine smKeys
Keys statemachine.
Definition: screen2.h:51
int KEY_INPUT
Definition: params.h:10
void refresh()
refreshes screen
Definition: screen2.h:135
void resolveKey(int)
resolves the key input at View level
Definition: view2.h:83
bool EXIT
Definition: params.h:9
statemachine that uses integer corresponding to object uuids to represent states
Definition: sm.h:20
void start()
Captures key input from screen.
Definition: screen2.h:113
parses config file
Definition: fileio.h:28
View * getView(int)
returns pointer to View object of given uuid
Definition: mem_func.h:190
Definition: action.h:7
src::severity_logger< severity_level > lg
Definition: rtop_logger.h:108