Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00040 #include <plugins/console/PluginConsole.h>
00041
00042
00043 namespace beliefstate {
00044 namespace plugins {
00045 PLUGIN_CLASS::PLUGIN_CLASS() {
00046 this->setPluginVersion("0.1");
00047
00048 m_nScreenWidth = 1;
00049 m_nScreenHeight = 1;
00050
00051 m_bFirstDisplay = true;
00052 m_bNeedsRedisplay = false;
00053
00054 m_nBufferLineSize = 500;
00055 }
00056
00057 PLUGIN_CLASS::~PLUGIN_CLASS() {
00058 }
00059
00060 Result PLUGIN_CLASS::init(int argc, char** argv) {
00061 Result resInit = defaultResult();
00062
00063 this->initCurses();
00064
00065 this->setSubscribedToEvent("status-message", true);
00066 this->setSubscribedToEvent("resize-terminal-window", true);
00067
00068 this->checkScreenSize();
00069 this->setNeedsRedisplay();
00070
00071 return resInit;
00072 }
00073
00074 void PLUGIN_CLASS::initCurses() {
00075 setlocale(LC_ALL, "");
00076
00077 m_winMain = initscr();
00078 start_color();
00079
00080 noecho();
00081 keypad(m_winMain, true);
00082 scrollok(m_winMain, false);
00083 timeout(0.01);
00084 curs_set(0);
00085
00086 m_winLog = newwin(1, 1, 1, 1);
00087
00088 this->registerColor("30", COLOR_BLACK);
00089 this->registerColor("31", COLOR_RED);
00090 this->registerColor("32", COLOR_GREEN);
00091 this->registerColor("33", COLOR_YELLOW);
00092 this->registerColor("34", COLOR_BLUE);
00093 this->registerColor("35", COLOR_MAGENTA);
00094 this->registerColor("36", COLOR_CYAN);
00095 this->registerColor("37", COLOR_WHITE);
00096 }
00097
00098 void PLUGIN_CLASS::deinitCurses() {
00099 endwin();
00100 }
00101
00102 void PLUGIN_CLASS::printInterface() {
00103 m_mtxStatusMessages.lock();
00104
00105 if(m_bFirstDisplay) {
00106 clear();
00107 m_bFirstDisplay = false;
00108 }
00109
00110 wclrtobot(stdscr);
00111 wclrtobot(m_winMain);
00112 wclrtobot(m_winLog);
00113
00114 box(m_winMain, 0, 0);
00115
00116 int nLogWidth = max(4, m_nScreenWidth - 2);
00117 int nLogHeight = max(1, m_nScreenHeight - 2);
00118
00119 int nLinesUsed = 0;
00120 int nReverseIndex = 0;
00121 for(list<StatusMessage>::reverse_iterator itSMr = m_lstStatusMessages.rbegin();
00122 itSMr != m_lstStatusMessages.rend(); itSMr++) {
00123 StatusMessage msgStatus = *itSMr;
00124 std::string strStatus = "[ " + msgStatus.strPrefix + " ] " + msgStatus.strMessage;
00125 int nLinesPerLine = 1;
00126
00127 while(strStatus.length() > nLogWidth) {
00128 strStatus = strStatus.substr(nLogWidth);
00129 strStatus += " ";
00130
00131 nLinesPerLine++;
00132 }
00133
00134 if(nLinesUsed + nLinesPerLine > nLogHeight) {
00135 break;
00136 }
00137
00138 nLinesUsed += nLinesPerLine;
00139 nReverseIndex++;
00140 }
00141
00142 int nLine = 0;
00143 std::list<StatusMessage>::iterator itSM = m_lstStatusMessages.begin();
00144 int nDiff = m_lstStatusMessages.size() - nReverseIndex;
00145
00146 if(nDiff > 0) {
00147 advance(itSM, nDiff);
00148 }
00149
00150 for(; itSM != m_lstStatusMessages.end();
00151 ++itSM) {
00152 StatusMessage msgStatus = *itSM;
00153 short sColor = this->colorNumber(msgStatus.strColorCode);
00154
00155 if(sColor != -1) {
00156 wattron(m_winLog, COLOR_PAIR(sColor));
00157 }
00158
00159 std::string strPrint = "[ " + msgStatus.strPrefix + " ] " + msgStatus.strMessage;
00160
00161 while(strPrint.length() > 0) {
00162 move(nLine, 0);
00163 wclrtoeol(m_winLog);
00164
00165 int nLen = (strPrint.length() > nLogWidth ? nLogWidth : strPrint.length());
00166 mvwaddstr(m_winLog, nLine, 0, strPrint.substr(0, nLen).c_str());
00167 wclrtoeol(m_winLog);
00168 strPrint = " " + strPrint.substr(nLen);
00169
00170 if(strPrint == " ") {
00171 strPrint = "";
00172 }
00173
00174 nLine++;
00175 }
00176
00177 if(sColor != -1) {
00178 wattroff(m_winLog, COLOR_PAIR(sColor));
00179 }
00180 }
00181
00182 wrefresh(m_winMain);
00183 wrefresh(m_winLog);
00184
00185 m_mtxStatusMessages.unlock();
00186 }
00187
00188 bool PLUGIN_CLASS::checkScreenSize() {
00189 int nScreenWidth = 0, nScreenHeight = 0;
00190
00191 m_mtxStatusMessages.lock();
00192 wrefresh(stdscr);
00193 getmaxyx(stdscr, nScreenHeight, nScreenWidth);
00194
00195 if(nScreenWidth != m_nScreenWidth || nScreenHeight != m_nScreenHeight) {
00196
00197 m_nScreenWidth = nScreenWidth;
00198 m_nScreenHeight = nScreenHeight;
00199
00200 wresize(m_winLog, m_nScreenHeight - 2, m_nScreenWidth - 2);
00201 wresize(m_winMain, m_nScreenHeight, m_nScreenWidth);
00202
00203 m_mtxStatusMessages.unlock();
00204
00205 return true;
00206 }
00207
00208 m_mtxStatusMessages.unlock();
00209
00210 return false;
00211 }
00212
00213 Result PLUGIN_CLASS::deinit() {
00214 deinitCurses();
00215
00216 return defaultResult();
00217 }
00218
00219 Result PLUGIN_CLASS::cycle() {
00220 Result resCycle = defaultResult();
00221
00222 if(this->needsRedisplay()) {
00223 this->printInterface();
00224 }
00225
00226 this->deployCycleData(resCycle);
00227
00228 return resCycle;
00229 }
00230
00231 void PLUGIN_CLASS::consumeEvent(Event evEvent) {
00232 if(evEvent.strEventName == "status-message") {
00233 m_mtxStatusMessages.lock();
00234 m_lstStatusMessages.push_back(evEvent.msgStatusMessage);
00235
00236 while(m_lstStatusMessages.size() > m_nBufferLineSize) {
00237 m_lstStatusMessages.pop_front();
00238 }
00239
00240 m_mtxStatusMessages.unlock();
00241
00242 this->setNeedsRedisplay();
00243 } else if(evEvent.strEventName == "resize-terminal-window") {
00244 if(this->checkScreenSize()) {
00245 this->setNeedsRedisplay();
00246 }
00247 }
00248 }
00249
00250 void PLUGIN_CLASS::registerColor(std::string strColorCode, short sColor) {
00251 short sPair = m_mapColors.size() + 1;
00252 init_pair(sPair, sColor, COLOR_BLACK);
00253 m_mapColors[strColorCode] = sPair;
00254 }
00255
00256 short PLUGIN_CLASS::colorNumber(std::string strColorCode) {
00257 std::map<std::string, short>::iterator itEntry = m_mapColors.find(strColorCode);
00258
00259 if(itEntry != m_mapColors.end()) {
00260 return (*itEntry).second;
00261 }
00262
00263 return -1;
00264 }
00265
00266 void PLUGIN_CLASS::setNeedsRedisplay() {
00267 m_mtxRedisplay.lock();
00268 m_bNeedsRedisplay = true;
00269 m_mtxRedisplay.unlock();
00270 }
00271
00272 bool PLUGIN_CLASS::needsRedisplay() {
00273 m_mtxRedisplay.lock();
00274 bool bRedisplay = m_bNeedsRedisplay;
00275 m_bNeedsRedisplay = false;
00276 m_mtxRedisplay.unlock();
00277
00278 return bRedisplay;
00279 }
00280 }
00281
00282 extern "C" plugins::PLUGIN_CLASS* createInstance() {
00283 return new plugins::PLUGIN_CLASS();
00284 }
00285
00286 extern "C" void destroyInstance(plugins::PLUGIN_CLASS* icDestroy) {
00287 delete icDestroy;
00288 }
00289 }