Changeset 5995 for code/branches/console/src
- Timestamp:
- Oct 28, 2009, 10:44:26 AM (15 years ago)
- Location:
- code/branches/console/src/libraries/core
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/console/src/libraries/core/IOConsole.cc
r5994 r5995 50 50 { 51 51 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 54 55 55 56 termios* IOConsole::originalTerminalSettings_; 57 58 namespace EscapeMode 59 { 60 enum Value 61 { 62 None, 63 First, 64 Second 65 }; 66 } 56 67 57 68 IOConsole::IOConsole() 58 69 : shell_(Shell::getInstance()) 59 , escapeMode_(None)60 70 , buffer_(Shell::getInstance().getInputBuffer()) 71 , bStatusPrinted_(false) 61 72 { 62 73 this->originalTerminalSettings_ = new termios; 63 74 this->setTerminalMode(); 64 75 this->shell_.registerListener(this); 76 77 78 // Manually set the widths of the individual status lines 79 this->statusLineWidths_.push_back(20); 65 80 } 66 81 … … 96 111 { 97 112 unsigned char c = 0; 113 std::string escapeSequence; 114 EscapeMode escapeMode = EscapeMode::None; 98 115 while (read(STDIN_FILENO, &c, 1) == 1) 99 116 { 100 if ( this->escapeMode_ ==First && (c == '[' || c=='O') )101 this->escapeMode_ =Second;117 if (escapeMode == EscapeMode::First && (c == '[' || c=='O') ) 118 escapeMode = EscapeMode::Second; 102 119 // Get Alt+Tab combination when switching applications 103 else if ( this->escapeMode_== First && c == '\t')120 else if (escapeMode == First && c == '\t') 104 121 { 105 122 this->buffer_->buttonPressed(KeyEvent(KeyCode::Tab, '\t', KeyboardModifier::Alt)); 106 this->escapeMode_ =None;123 escapeMode = EscapeMode::None; 107 124 } 108 else if ( this->escapeMode_ ==Second)125 else if (escapeMode == EscapeMode::Second) 109 126 { 110 this->escapeSequence_+= c;111 this->escapeMode_ =None;112 if ( this->escapeSequence_== "A")127 escapeSequence += c; 128 escapeMode = EscapeMode::None; 129 if (escapeSequence == "A") 113 130 this->buffer_->buttonPressed(KeyEvent(KeyCode::Up, 0, 0)); 114 else if ( this->escapeSequence_== "B")131 else if (escapeSequence == "B") 115 132 this->buffer_->buttonPressed(KeyEvent(KeyCode::Down, 0, 0)); 116 else if ( this->escapeSequence_== "C")133 else if (escapeSequence == "C") 117 134 this->buffer_->buttonPressed(KeyEvent(KeyCode::Right, 0, 0)); 118 else if ( this->escapeSequence_== "D")135 else if (escapeSequence == "D") 119 136 this->buffer_->buttonPressed(KeyEvent(KeyCode::Left, 0, 0)); 120 else if ( this->escapeSequence_ == "1~" || this->escapeSequence_== "H")137 else if (escapeSequence == "1~" || escapeSequence == "H") 121 138 this->buffer_->buttonPressed(KeyEvent(KeyCode::Home, 0, 0)); 122 else if ( this->escapeSequence_== "2~")139 else if (escapeSequence == "2~") 123 140 this->buffer_->buttonPressed(KeyEvent(KeyCode::Insert, 0, 0)); 124 else if ( this->escapeSequence_== "3~")141 else if (escapeSequence == "3~") 125 142 this->buffer_->buttonPressed(KeyEvent(KeyCode::Delete, 0, 0)); 126 else if ( this->escapeSequence_ == "4~" || this->escapeSequence_== "F")143 else if (escapeSequence == "4~" || escapeSequence == "F") 127 144 this->buffer_->buttonPressed(KeyEvent(KeyCode::End, 0, 0)); 128 else if ( this->escapeSequence_== "5~")145 else if (escapeSequence == "5~") 129 146 this->buffer_->buttonPressed(KeyEvent(KeyCode::AltPageUp, 0, 0)); 130 else if ( this->escapeSequence_== "6~")147 else if (escapeSequence == "6~") 131 148 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;135 149 else 136 150 // 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; 138 154 } 139 155 else // not in an escape sequence OR user might have pressed just ESC 140 156 { 141 if ( this->escapeMode_ ==First)157 if (escapeMode == EscapeMode::First) 142 158 { 143 159 this->buffer_->buttonPressed(KeyEvent(KeyCode::Escape, c, 0)); 144 this->escapeMode_ =None;160 escapeMode = EscapeMode::None; 145 161 } 146 162 if (c == '\033') 147 163 { 148 this->escapeMode_ =First;149 this->escapeSequence_.clear();164 escapeMode = EscapeMode::First; 165 escapeSequence.clear(); 150 166 } 151 167 else … … 154 170 switch (c) 155 171 { 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; 161 175 default: 162 176 // We don't encode the key code (would be a very large switch) … … 171 185 172 186 // If there is still an escape key pending (escape key ONLY), then 173 // it sure isn't an escape sequence here174 if ( this->escapeMode_ ==First)187 // it sure isn't an escape sequence anymore 188 if (escapeMode == EscapeMode::First) 175 189 this->buffer_->buttonPressed(KeyEvent(KeyCode::Escape, '\033', 0)); 176 // Reset in any case because escape sequences always come in one piece177 this->escapeMode_ = None;178 190 179 191 // Print input line … … 181 193 } 182 194 183 void IOConsole::print (const std::string& text)195 void IOConsole::printLogText(const std::string& text) 184 196 { 185 197 std::string output; … … 195 207 196 208 // Colour line 209 /* 197 210 switch (level) 198 211 { … … 206 219 default: break; 207 220 } 221 */ 208 222 209 223 // Print output line … … 211 225 212 226 // Reset colour to white 213 std::cout << "\033[37m";227 // std::cout << "\033[37m"; 214 228 std::cout.flush(); 215 229 } … … 217 231 void IOConsole::printInputLine() 218 232 { 219 // set cursor to the beginning of the line and erase the line233 // Set cursor to the beginning of the line and erase the line 220 234 std::cout << "\033[1G\033[K"; 221 // print status line235 // Print status line 222 236 //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 prompt224 std::cout << "orxonox>";225 // save cursor position237 // Indicate a command prompt 238 std::cout << promptString; 239 // Save cursor position 226 240 std::cout << "\033[s"; 227 // print commandLine buffer241 // Print command line buffer 228 242 std::cout << this->shell_.getInput(); 229 // restore cursor position and move it to the right243 // Restore cursor position and move it to the right 230 244 std::cout << "\033[u"; 231 245 if (this->buffer_->getCursorPosition() > 0) … … 234 248 } 235 249 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 236 304 #elif defined(ORXONOX_PLATFORM_WINDOWS) 237 305 … … 289 357 void IOConsole::onlyLastLineChanged() 290 358 { 291 // Save cursor position and move it t he beginning of the second to last line292 std::cout << "\033[s\033[ 1F";293 // Erase the second to lastline359 // 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 294 362 std::cout << "\033[K"; 295 this->print(*(this->shell_.getNewestLineIterator())); 363 // Reprint the last output line 364 this->printLogText(*(this->shell_.getNewestLineIterator())); 296 365 // Restore cursor 297 366 std::cout << "\033[u"; … … 305 374 void IOConsole::lineAdded() 306 375 { 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(); 312 385 } 313 386 … … 339 412 std::cout << "\033[1G"; 340 413 // 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; 342 415 this->printInputLine(); 343 416 } -
code/branches/console/src/libraries/core/IOConsole.h
r5983 r5995 53 53 54 54 private: 55 enum EscapeMode56 {57 None,58 First,59 Second60 };61 55 62 56 void setTerminalMode(); 63 57 static void resetTerminalMode(); 58 int getTerminalSize(int* x, int* y); 64 59 65 60 void print(const std::string& line); 66 61 void printInputLine(); 62 void printStatusLines(); 67 63 68 64 // Methods from ShellListener … … 74 70 void executed(); 75 71 void exit(); 76 77 72 Shell& shell_; 78 EscapeMode escapeMode_;79 std::string escapeSequence_;80 73 InputBuffer* buffer_; 81 74 static termios* originalTerminalSettings_; 75 bool bPrintStatusLine_; 76 bool bStatusPrinted_; 77 std::vector<unsigned> statusLineWidths_; 82 78 83 79 static IOConsole* singletonPtr_s;
Note: See TracChangeset
for help on using the changeset viewer.