PluginConsole.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2014, Institute for Artificial Intelligence,
00005  *  Universität Bremen.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Institute for Artificial Intelligence,
00019  *     Universität Bremen, nor the names of its contributors may be
00020  *     used to endorse or promote products derived from this software
00021  *     without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
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         // Screen size changed
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 }


beliefstate
Author(s): Jan Winkler
autogenerated on Sun Oct 5 2014 22:30:15