Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 2, 2009, 5:54:13 PM (15 years ago)
Author:
rgrieder
Message:

IOConsole cleanup and added ConsoleWriter to the OutputHandler so that we get std::cout output before the creation of the IOConsole.

Location:
code/branches/console/src/libraries/core
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/console/src/libraries/core/Core.cc

    r6004 r6015  
    218218        this->languageInstance_.reset(new Language());
    219219
     220        // create persistent io console
     221        this->ioConsole_.reset(new IOConsole());
     222
    220223        // creates the class hierarchy for all classes with factories
    221224        Identifier::createClassHierarchy();
     
    231234        this->tclBind_.reset(new TclBind(PathConfig::getDataPathString()));
    232235        this->tclThreadManager_.reset(new TclThreadManager(tclBind_->getTclInterpreter()));
    233 
    234         // create persistent io console
    235         this->ioConsole_.reset(new IOConsole());
    236236
    237237        // Create singletons that always exist (in other libraries)
  • code/branches/console/src/libraries/core/Core.h

    r6004 r6015  
    9191            scoped_ptr<ConfigFileManager> configFileManager_;
    9292            scoped_ptr<Language>          languageInstance_;
     93            scoped_ptr<IOConsole>         ioConsole_;
    9394            scoped_ptr<CoreConfiguration> configuration_;
    9495            scoped_ptr<TclBind>           tclBind_;
    9596            scoped_ptr<TclThreadManager>  tclThreadManager_;
    96             scoped_ptr<IOConsole>         ioConsole_;
    9797            // graphical
    9898            scoped_ptr<GraphicsManager>   graphicsManager_;     //!< Interface to OGRE
  • code/branches/console/src/libraries/core/IOConsole.cc

    r6014 r6015  
    3030#include "IOConsole.h"
    3131
    32 #include <cstring>
    3332#include <iomanip>
    3433#include <iostream>
    35 
    36 #include "util/Clock.h"
    37 #include "util/Debug.h"
    38 #include "util/Sleep.h"
    39 #include "core/CommandExecutor.h"
    4034#include "core/Game.h"
    41 #include "core/GameMode.h"
    42 #include "core/Shell.h"
    4335#include "core/input/InputBuffer.h"
    4436
     37// ##########################
     38// ###   Mutual methods   ###
     39// ##########################
     40namespace orxonox
     41{
     42    IOConsole* IOConsole::singletonPtr_s = NULL;
     43
     44    int IOConsole::extractLogLevel(std::string* text)
     45    {
     46        // Handle line colouring by inspecting the first letter
     47        char level = 0;
     48        if (!text->empty())
     49        {
     50            level = (*text)[0];
     51            if (level == -1 || level >= 1 && level <= 6)
     52            {
     53                *text = text->substr(1);
     54                if (level != -1)
     55                    return level;
     56            }
     57        }
     58        return 0;
     59    }
     60
     61    inline bool IOConsole::willPrintStatusLines()
     62    {
     63        return !this->statusLineWidths_.empty()
     64             && this->terminalWidth_  >= this->statusLineMaxWidth_
     65             && this->terminalHeight_ >= (this->minOutputLines_ + this->statusLineWidths_.size());
     66    }
     67
     68    // ###############################
     69    // ###  ShellListener methods  ###
     70    // ###############################
     71
     72    //! Called if all output-lines have to be reprinted
     73    void IOConsole::linesChanged()
     74    {
     75        // Method only gets called upon start to draw all the lines
     76        // or when scrolling. But scrolling is disable and the output
     77        // is already in std::cout when we start the IOConsole
     78    }
     79
     80    //! Called if the text in the input-line has changed
     81    void IOConsole::inputChanged()
     82    {
     83        this->printInputLine();
     84        std::cout.flush();
     85    }
     86
     87    //! Called if the position of the cursor in the input-line has changed
     88    void IOConsole::cursorChanged()
     89    {
     90        this->printInputLine();
     91        std::cout.flush();
     92    }
     93
     94    //! Called if a command is about to be executed
     95    void IOConsole::executed()
     96    {
     97        this->shell_->addOutputLine(this->promptString_ + this->shell_->getInput());
     98    }
     99
     100    //! Called if the console gets closed
     101    void IOConsole::exit()
     102    {
     103        // Exit is not an option, just do nothing (Shell doesn't really exit too)
     104    }
     105}
     106
    45107#ifdef ORXONOX_PLATFORM_UNIX
     108// ###############################
     109// ###   Unix Implementation   ###
     110// ###############################
     111
    46112#include <termios.h>
    47113#include <sys/ioctl.h>
    48114#include <sys/stat.h>
    49 #endif
    50115
    51116namespace orxonox
    52117{
    53     IOConsole* IOConsole::singletonPtr_s = NULL;
    54 
    55 #ifdef ORXONOX_PLATFORM_UNIX
    56 
    57118    namespace EscapeMode
    58119    {
     
    82143        this->lastTerminalWidth_ = this->terminalWidth_;
    83144        this->lastTerminalHeight_ = this->terminalHeight_;
     145
     146        // Disable standard std::cout logging
     147        OutputHandler::getInstance().disableCout();
    84148    }
    85149
    86150    IOConsole::~IOConsole()
    87151    {
    88         // Goto last line and erase it
     152        // Goto last line and create a new one
    89153        if (this->bStatusPrinted_)
    90154            std::cout << "\033[" << this->statusLineWidths_.size() << 'E';
    91         std::cout << "\033[1G\033[K";
    92         std::cout.flush();
     155        std::cout << std::endl;
     156
    93157        resetTerminalMode();
    94158        delete this->originalTerminalSettings_;
    95159        this->shell_->destroy();
    96     }
    97 
    98     void IOConsole::setTerminalMode()
    99     {
    100         termios new_settings;
    101 
    102         tcgetattr(0, this->originalTerminalSettings_);
    103         new_settings = *this->originalTerminalSettings_;
    104         new_settings.c_lflag &= ~(ICANON | ECHO);
    105         //         new_settings.c_lflag |= ( ISIG | IEXTEN );
    106         new_settings.c_cc[VTIME] = 0;
    107         new_settings.c_cc[VMIN]  = 0;
    108         tcsetattr(0, TCSANOW, &new_settings);
    109         COUT(0) << endl;
    110         //       atexit(&IOConsole::resetTerminalMode);
    111     }
    112 
    113     void IOConsole::resetTerminalMode()
    114     {
    115         tcsetattr(0, TCSANOW, IOConsole::originalTerminalSettings_);
     160
     161        // Enable standard std::cout logging again
     162        OutputHandler::getInstance().enableCout();
    116163    }
    117164
     
    121168        std::string escapeSequence;
    122169        EscapeMode::Value escapeMode = EscapeMode::None;
    123         while (read(STDIN_FILENO, &c, 1) == 1)
    124         {
     170        while (std::cin.good())
     171        {
     172            c = std::cin.get();
     173            if (std::cin.bad())
     174                break;
     175
    125176            if (escapeMode == EscapeMode::First && (c == '[' || c=='O') )
    126177                escapeMode = EscapeMode::Second;
     
    191242            }
    192243        }
     244        // Reset error flags in std::cin
     245        std::cin.clear();
    193246
    194247        // If there is still an escape key pending (escape key ONLY), then
     
    205258        if (this->bStatusPrinted_ && heightDiff < 0)
    206259        {
    207             // Terminal width has shrinked. The cursor will still be on the input line,
     260            // Terminal width has shrunk. The cursor will still be on the input line,
    208261            // but that line might very well be the last
    209262            int newLines = std::min((int)this->statusLineWidths_.size(), -heightDiff);
     
    230283    void IOConsole::printLogText(const std::string& text)
    231284    {
    232         std::string output;
    233 
    234         // Handle line colouring by inspecting the first letter
    235         char level = 0;
    236         if (!text.empty())
    237             level = text[0];
    238         if (level >= -1 && level <= 6)
    239             output = text.substr(1);
    240         else
    241             output = text;
     285        std::string output = text;
     286        int level = this->extractLogLevel(&output);
    242287
    243288        // Colour line
     
    297342    }
    298343
    299     inline bool IOConsole::willPrintStatusLines()
    300     {
    301         return !this->statusLineWidths_.empty()
    302              && this->terminalWidth_  >= this->statusLineMaxWidth_
    303              && this->terminalHeight_ >= (this->minOutputLines_ + this->statusLineWidths_.size());
     344    void IOConsole::setTerminalMode()
     345    {
     346        termios new_settings;
     347
     348        tcgetattr(0, this->originalTerminalSettings_);
     349        new_settings = *this->originalTerminalSettings_;
     350        new_settings.c_lflag &= ~(ICANON | ECHO);
     351        //new_settings.c_lflag |= (ISIG | IEXTEN);
     352        new_settings.c_cc[VTIME] = 0;
     353        new_settings.c_cc[VMIN]  = 0;
     354        tcsetattr(0, TCSANOW, &new_settings);
     355    }
     356
     357    void IOConsole::resetTerminalMode()
     358    {
     359        tcsetattr(0, TCSANOW, IOConsole::originalTerminalSettings_);
    304360    }
    305361
     
    333389    }
    334390
    335 #elif defined(ORXONOX_PLATFORM_WINDOWS)
    336 
    337     IOConsole::IOConsole()
    338         : shell_(new Shell("IOConsole", false))
    339         , buffer_(shell_->getInputBuffer())
    340     {
    341         this->setTerminalMode();
    342     }
    343 
    344     IOConsole::~IOConsole()
    345     {
    346     }
    347 
    348     void IOConsole::setTerminalMode()
    349     {
    350     }
    351 
    352     void IOConsole::resetTerminalMode()
    353     {
    354     }
    355 
    356     void IOConsole::update(const Clock& time)
    357     {
    358     }
    359 
    360     void IOConsole::printLogText(const std::string& text)
    361     {
    362     }
    363 
    364     void IOConsole::printInputLine()
    365     {
    366     }
    367 
    368     void IOConsole::printStatusLines()
    369     {
    370     }
    371 
    372 #endif /* ORXONOX_PLATFORM_UNIX */
    373 
    374391    // ###############################
    375392    // ###  ShellListener methods  ###
    376393    // ###############################
    377394
    378     /**
    379     @brief
    380         Called if all output-lines have to be redrawn.
    381     */
    382     void IOConsole::linesChanged()
    383     {
    384         // Method only gets called upon start to draw all the lines
    385         // But we are using std::cout anyway, so do nothing here
    386     }
    387 
    388     /**
    389     @brief
    390         Called if only the last output-line has changed.
    391     */
     395    //! Called if only the last output-line has changed
    392396    void IOConsole::onlyLastLineChanged()
    393397    {
     
    403407    }
    404408
    405     /**
    406     @brief
    407         Called if a new output-line was added.
    408     */
     409    //! Called if a new output-line was added
    409410    void IOConsole::lineAdded()
    410411    {
     
    429430        std::cout.flush();
    430431    }
    431 
    432     /**
    433     @brief
    434         Called if the text in the input-line has changed.
    435     */
    436     void IOConsole::inputChanged()
    437     {
    438         this->printInputLine();
    439         std::cout.flush();
    440     }
    441 
    442     /**
    443     @brief
    444         Called if the position of the cursor in the input-line has changed.
    445     */
    446     void IOConsole::cursorChanged()
    447     {
    448         this->printInputLine();
    449         std::cout.flush();
    450     }
    451 
    452     /**
    453     @brief
    454         Called if a command is about to be executed
    455     */
    456     void IOConsole::executed()
    457     {
    458         this->shell_->addOutputLine(this->promptString_ + this->shell_->getInput());
    459     }
    460 
    461 
    462     /**
    463     @brief
    464         Called if the console gets closed.
    465     */
    466     void IOConsole::exit()
    467     {
    468         // Exit is not an option, IOConsole always exists
    469     }
    470 
    471432}
     433
     434#elif defined(ORXONOX_PLATFORM_WINDOWS)
     435// ##################################
     436// ###   Windows Implementation   ###
     437// ##################################
     438
     439namespace orxonox
     440{
     441    IOConsole::IOConsole()
     442        : shell_(new Shell("IOConsole", false, true))
     443        , buffer_(shell_->getInputBuffer())
     444        , bStatusPrinted_(false)
     445        , promptString_("orxonox # ")
     446    {
     447/*
     448        this->setTerminalMode();
     449        this->shell_->registerListener(this);
     450
     451        // Manually set the widths of the individual status lines
     452        this->statusLineWidths_.push_back(29);
     453        this->statusLineMaxWidth_ = 29;
     454
     455        this->getTerminalSize();
     456        this->lastTerminalWidth_ = this->terminalWidth_;
     457        this->lastTerminalHeight_ = this->terminalHeight_;
     458
     459        // Disable standard std::cout logging
     460        OutputHandler::getInstance().disableCout();
     461*/
     462    }
     463
     464    IOConsole::~IOConsole()
     465    {
     466/*
     467        resetTerminalMode();
     468        this->shell_->destroy();
     469
     470        // Enable standard std::cout logging again
     471        OutputHandler::getInstance().enableCout();
     472*/
     473    }
     474
     475    void IOConsole::update(const Clock& time)
     476    {
     477/*
     478        unsigned char c = 0;
     479        while (std::cin.good())
     480        {
     481            c = std::cin.get();
     482            if (std::cin.bad())
     483                break;
     484        }
     485        // Reset error flags in std::cin
     486        std::cin.clear();
     487
     488        // Determine terminal width and height
     489        this->lastTerminalWidth_ = this->terminalWidth_;
     490        this->lastTerminalHeight_ = this->terminalHeight_;
     491        this->getTerminalSize();
     492*/
     493    }
     494
     495    void IOConsole::printLogText(const std::string& text)
     496    {
     497    }
     498
     499    void IOConsole::printInputLine()
     500    {
     501    }
     502
     503    void IOConsole::printStatusLines()
     504    {
     505/*
     506        if (this->willPrintStatusLines())
     507        {
     508            this->bStatusPrinted_ = true;
     509        }
     510        else
     511            this->bStatusPrinted_ = false;
     512*/
     513    }
     514
     515    void IOConsole::setTerminalMode()
     516    {
     517    }
     518
     519    void IOConsole::resetTerminalMode()
     520    {
     521    }
     522
     523    void IOConsole::getTerminalSize()
     524    {
     525    }
     526
     527    // ###############################
     528    // ###  ShellListener methods  ###
     529    // ###############################
     530
     531    //! Called if only the last output-line has changed
     532    void IOConsole::onlyLastLineChanged()
     533    {
     534    }
     535
     536    //! Called if a new output-line was added
     537    void IOConsole::lineAdded()
     538    {
     539    }
     540}
     541
     542#endif /* ORXONOX_PLATFORM_UNIX */
  • code/branches/console/src/libraries/core/IOConsole.h

    r6014 r6015  
    3333#include "CorePrereqs.h"
    3434
    35 #include <queue>
    36 #include <boost/scoped_ptr.hpp>
     35#include <string>
     36#include <vector>
    3737#include "util/Singleton.h"
    3838#include "core/Shell.h"
    3939
     40#ifdef ORXONOX_PLATFORM_UNIX
    4041struct termios;
     42#endif
    4143
    4244namespace orxonox
     
    5355
    5456    private:
    55 
    5657        void setTerminalMode();
    5758        void resetTerminalMode();
    5859        void getTerminalSize();
    5960        bool willPrintStatusLines();
     61        int extractLogLevel(std::string* text);
    6062
    6163        void printLogText(const std::string& line);
     
    7173        void executed();
    7274        void exit();
    73 
    7475        Shell*                  shell_;
    7576        InputBuffer*            buffer_;
    76         termios*                originalTerminalSettings_;
    7777        unsigned int            terminalWidth_;
    7878        unsigned int            terminalHeight_;
     
    8686        static const unsigned   minOutputLines_ = 3;
    8787
     88#ifdef ORXONOX_PLATFORM_UNIX
     89        termios*                originalTerminalSettings_;
     90#endif
     91
    8892        static IOConsole* singletonPtr_s;
    8993    };
Note: See TracChangeset for help on using the changeset viewer.