Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5995


Ignore:
Timestamp:
Oct 28, 2009, 10:44:26 AM (14 years ago)
Author:
rgrieder
Message:

Committing some unstable changes to the IOConsole for testing purposes.

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

Legend:

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

    r5994 r5995  
    5050{
    5151    IOConsole* IOConsole::singletonPtr_s = NULL;
    52 
    53 #ifdef ORXONOX_PLATFORM_UNIX
     52    const std::string promptString_g = "orxonox>";
     53
     54#if 1//def ORXONOX_PLATFORM_UNIX
    5455
    5556    termios* IOConsole::originalTerminalSettings_;
     57
     58    namespace EscapeMode
     59    {
     60        enum Value
     61        {
     62            None,
     63            First,
     64            Second
     65        };
     66    }
    5667
    5768    IOConsole::IOConsole()
    5869        : shell_(Shell::getInstance())
    59         , escapeMode_(None)
    6070        , buffer_(Shell::getInstance().getInputBuffer())
     71        , bStatusPrinted_(false)
    6172    {
    6273        this->originalTerminalSettings_ = new termios;
    6374        this->setTerminalMode();
    6475        this->shell_.registerListener(this);
     76
     77
     78        // Manually set the widths of the individual status lines
     79        this->statusLineWidths_.push_back(20);
    6580    }
    6681
     
    96111    {
    97112        unsigned char c = 0;
     113        std::string escapeSequence;
     114        EscapeMode escapeMode = EscapeMode::None;
    98115        while (read(STDIN_FILENO, &c, 1) == 1)
    99116        {
    100             if (this->escapeMode_ == First && (c == '[' || c=='O') )
    101                 this->escapeMode_ = Second;
     117            if (escapeMode == EscapeMode::First && (c == '[' || c=='O') )
     118                escapeMode = EscapeMode::Second;
    102119            // Get Alt+Tab combination when switching applications
    103             else if (this->escapeMode_ == First && c == '\t')
     120            else if (escapeMode == First && c == '\t')
    104121            {
    105122                this->buffer_->buttonPressed(KeyEvent(KeyCode::Tab, '\t', KeyboardModifier::Alt));
    106                 this->escapeMode_ = None;
     123                escapeMode = EscapeMode::None;
    107124            }
    108             else if (this->escapeMode_ == Second)
     125            else if (escapeMode == EscapeMode::Second)
    109126            {
    110                 this->escapeSequence_ += c;
    111                 this->escapeMode_ = None;
    112                 if      (this->escapeSequence_ == "A")
     127                escapeSequence += c;
     128                escapeMode = EscapeMode::None;
     129                if      (escapeSequence == "A")
    113130                    this->buffer_->buttonPressed(KeyEvent(KeyCode::Up,       0, 0));
    114                 else if (this->escapeSequence_ == "B")
     131                else if (escapeSequence == "B")
    115132                    this->buffer_->buttonPressed(KeyEvent(KeyCode::Down,     0, 0));
    116                 else if (this->escapeSequence_ == "C")
     133                else if (escapeSequence == "C")
    117134                    this->buffer_->buttonPressed(KeyEvent(KeyCode::Right,    0, 0));
    118                 else if (this->escapeSequence_ == "D")
     135                else if (escapeSequence == "D")
    119136                    this->buffer_->buttonPressed(KeyEvent(KeyCode::Left,     0, 0));
    120                 else if (this->escapeSequence_ == "1~" || this->escapeSequence_ == "H")
     137                else if (escapeSequence == "1~" || escapeSequence == "H")
    121138                    this->buffer_->buttonPressed(KeyEvent(KeyCode::Home,     0, 0));
    122                 else if (this->escapeSequence_ == "2~")
     139                else if (escapeSequence == "2~")
    123140                    this->buffer_->buttonPressed(KeyEvent(KeyCode::Insert,   0, 0));
    124                 else if (this->escapeSequence_ == "3~")
     141                else if (escapeSequence == "3~")
    125142                    this->buffer_->buttonPressed(KeyEvent(KeyCode::Delete,   0, 0));
    126                 else if (this->escapeSequence_ == "4~" || this->escapeSequence_ == "F")
     143                else if (escapeSequence == "4~" || escapeSequence == "F")
    127144                    this->buffer_->buttonPressed(KeyEvent(KeyCode::End,      0, 0));
    128                 else if (this->escapeSequence_ == "5~")
     145                else if (escapeSequence == "5~")
    129146                    this->buffer_->buttonPressed(KeyEvent(KeyCode::AltPageUp,   0, 0));
    130                 else if (this->escapeSequence_ == "6~")
     147                else if (escapeSequence == "6~")
    131148                    this->buffer_->buttonPressed(KeyEvent(KeyCode::AltPageDown, 0, 0));
    132                 else if (this->escapeSequence_.size() > 4)
    133                     // User probably very quickly pressed ESC and [
    134                     this->escapeMode_ = None;
    135149                else
    136150                    // Waiting for sequence to complete
    137                     this->escapeMode_ = Second;
     151                    // If the user presses ESC and then '[' or 'O' while the loop is not
     152                    // running (for instance while loading), the whole sequence gets dropped
     153                    escapeMode = EscapeMode::Second;
    138154            }
    139155            else // not in an escape sequence OR user might have pressed just ESC
    140156            {
    141                 if (this->escapeMode_ == First)
     157                if (escapeMode == EscapeMode::First)
    142158                {
    143159                    this->buffer_->buttonPressed(KeyEvent(KeyCode::Escape, c, 0));
    144                     this->escapeMode_ = None;
     160                    escapeMode = EscapeMode::None;
    145161                }
    146162                if (c == '\033')
    147163                {
    148                     this->escapeMode_ = First;
    149                     this->escapeSequence_.clear();
     164                    escapeMode = EscapeMode::First;
     165                    escapeSequence.clear();
    150166                }
    151167                else
     
    154170                    switch (c)
    155171                    {
    156                     case '\n': code = KeyCode::Return; break;
    157                     case '\r': code = KeyCode::Return; break;
    158                     case  127: code = KeyCode::Back;   break;
    159                     case '\b': code = KeyCode::Back;   break;
    160                     case '\t': code = KeyCode::Tab;    break;
     172                    case '\n'  : case '\r': code = KeyCode::Return; break;
     173                    case '\177': case '\b': code = KeyCode::Back;   break;
     174                    case '\t'             : code = KeyCode::Tab;    break;
    161175                    default:
    162176                        // We don't encode the key code (would be a very large switch)
     
    171185
    172186        // If there is still an escape key pending (escape key ONLY), then
    173         // it sure isn't an escape sequence here
    174         if (this->escapeMode_ == First)
     187        // it sure isn't an escape sequence anymore
     188        if (escapeMode == EscapeMode::First)
    175189            this->buffer_->buttonPressed(KeyEvent(KeyCode::Escape, '\033', 0));
    176         // Reset in any case because escape sequences always come in one piece
    177         this->escapeMode_ = None;
    178190
    179191        // Print input line
     
    181193    }
    182194
    183     void IOConsole::print(const std::string& text)
     195    void IOConsole::printLogText(const std::string& text)
    184196    {
    185197        std::string output;
     
    195207
    196208        // Colour line
     209/*
    197210        switch (level)
    198211        {
     
    206219        default: break;
    207220        }
     221*/
    208222
    209223        // Print output line
     
    211225
    212226        // Reset colour to white
    213         std::cout << "\033[37m";
     227//        std::cout << "\033[37m";
    214228        std::cout.flush();
    215229    }
     
    217231    void IOConsole::printInputLine()
    218232    {
    219         // set cursor to the beginning of the line and erase the line
     233        // Set cursor to the beginning of the line and erase the line
    220234        std::cout << "\033[1G\033[K";
    221         // print status line
     235        // Print status line
    222236        //std::cout << std::fixed << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgFPS() << " fps, " << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgTickTime() << " ms avg ticktime # ";
    223         // Show an arrow to indicate a command prompt
    224         std::cout << "orxonox>";
    225         // save cursor position
     237        // Indicate a command prompt
     238        std::cout << promptString;
     239        // Save cursor position
    226240        std::cout << "\033[s";
    227         // print commandLine buffer
     241        // Print command line buffer
    228242        std::cout << this->shell_.getInput();
    229         // restore cursor position and move it to the right
     243        // Restore cursor position and move it to the right
    230244        std::cout << "\033[u";
    231245        if (this->buffer_->getCursorPosition() > 0)
     
    234248    }
    235249
     250    void IOConsole::printStatusLines()
     251    {
     252        if (!this->statusLineWidths_.empty())
     253        {
     254            if (this->bStatusPrinted_)
     255            {
     256                // Erase the status lines first (completely, including new lines!)
     257
     258            }
     259            // Check terminal size
     260            int x, y;
     261            if (this->getTerminalSize(&x, &y) && (x < statusTextWidth_g || y < (2 + statusTextHeight_g)))
     262            {
     263                this->bStatusPrinted_ = false;
     264                return;
     265            }
     266        }
     267    }
     268
     269    int IOConsole::getTerminalSize(int* x, int* y)
     270    {
     271#ifdef TIOCGSIZE
     272        struct ttysize win;
     273#elif defined(TIOCGWINSZ)
     274        struct winsize win;
     275#endif
     276
     277#ifdef TIOCGSIZE
     278        if (ioctl(STDIN_FILENO, TIOCGSIZE, &win))
     279            return 0;
     280        *y = win.ts_lines;
     281        *x = win.ts_cols;
     282#elif defined TIOCGWINSZ
     283        if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win))
     284            return 0;
     285        *y = win.ws_row;
     286        *x = win.ws_col;
     287#else
     288        {
     289            const char* s = getenv("LINES");
     290            if (s)
     291                *y = strtol(s, NULL, 10);
     292            else
     293                *y = 25;
     294            s = getenv("COLUMNS");
     295            if (s)
     296                *x = strtol(s, NULL, 10);
     297            else
     298                *x = 80;
     299        }
     300#endif
     301        return 1;
     302    }
     303
    236304#elif defined(ORXONOX_PLATFORM_WINDOWS)
    237305
     
    289357    void IOConsole::onlyLastLineChanged()
    290358    {
    291         // Save cursor position and move it the beginning of the second to last line
    292         std::cout << "\033[s\033[1F";
    293         // Erase the second to last line
     359        // Save cursor position and move it to the beginning of the first output line
     360        std::cout << "\033[s\033[" << (1 + statusTextHeight_g) << "F";
     361        // Erase the line
    294362        std::cout << "\033[K";
    295         this->print(*(this->shell_.getNewestLineIterator()));
     363        // Reprint the last output line
     364        this->printLogText(*(this->shell_.getNewestLineIterator()));
    296365        // Restore cursor
    297366        std::cout << "\033[u";
     
    305374    void IOConsole::lineAdded()
    306375    {
    307         // Move curosr the beginning of the line and erase it
    308         std::cout << "\033[1G\033[K";
    309         this->print(*(this->shell_.getNewestLineIterator()));
    310         std::cout << std::endl;
    311         this->printInputLine();
     376        // Save cursor and move it to the beginning of the first status line
     377        std::cout << "\033[s\033[" << statusTextHeight_g << "F";
     378        // Create a new line and move cursor to the beginning of it (one cell up)
     379        std::cout << std::endl << "\033[1F";
     380        // Print the new output line
     381        this->printLogText(*(this->shell_.getNewestLineIterator()));
     382        // Restore cursor (for horizontal position) and move it down again (just in case the lines were shifted)
     383        std::cout << "\033[u\033[" << (1 + statusTextHeight_g) << "B";
     384        std::cout.flush();
    312385    }
    313386
     
    339412        std::cout << "\033[1G";
    340413        // Print command so the user knows what he has typed
    341         std::cout << "orxonox>" << this->shell_.getInput() << std::endl;
     414        std::cout << promptString_g << this->shell_.getInput() << std::endl;
    342415        this->printInputLine();
    343416    }
  • code/branches/console/src/libraries/core/IOConsole.h

    r5983 r5995  
    5353
    5454    private:
    55         enum EscapeMode
    56         {
    57             None,
    58             First,
    59             Second
    60         };
    6155
    6256        void setTerminalMode();
    6357        static void resetTerminalMode();
     58        int getTerminalSize(int* x, int* y);
    6459
    6560        void print(const std::string& line);
    6661        void printInputLine();
     62        void printStatusLines();
    6763
    6864        // Methods from ShellListener
     
    7470        void executed();
    7571        void exit();
    76 
    7772        Shell&                  shell_;
    78         EscapeMode              escapeMode_;
    79         std::string             escapeSequence_;
    8073        InputBuffer*            buffer_;
    8174        static termios*         originalTerminalSettings_;
     75        bool                    bPrintStatusLine_;
     76        bool                    bStatusPrinted_;
     77        std::vector<unsigned>   statusLineWidths_;
    8278
    8379        static IOConsole* singletonPtr_s;
Note: See TracChangeset for help on using the changeset viewer.