| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Oliver Scheuss |
|---|
| 24 | * Reto Grieder |
|---|
| 25 | * Co-authors: |
|---|
| 26 | * ... |
|---|
| 27 | * |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include "IOConsole.h" |
|---|
| 31 | |
|---|
| 32 | #include <cstring> |
|---|
| 33 | #include <iomanip> |
|---|
| 34 | #include <iostream> |
|---|
| 35 | |
|---|
| 36 | #include "util/Clock.h" |
|---|
| 37 | #include "util/Debug.h" |
|---|
| 38 | #include "util/Sleep.h" |
|---|
| 39 | #include "core/CommandExecutor.h" |
|---|
| 40 | #include "core/Game.h" |
|---|
| 41 | #include "core/GameMode.h" |
|---|
| 42 | #include "core/Shell.h" |
|---|
| 43 | #include "core/input/InputBuffer.h" |
|---|
| 44 | |
|---|
| 45 | #ifdef ORXONOX_PLATFORM_UNIX |
|---|
| 46 | #include <termios.h> |
|---|
| 47 | #endif |
|---|
| 48 | |
|---|
| 49 | namespace orxonox |
|---|
| 50 | { |
|---|
| 51 | IOConsole* IOConsole::singletonPtr_s = NULL; |
|---|
| 52 | const std::string promptString_g = "orxonox>"; |
|---|
| 53 | |
|---|
| 54 | #if 1//def ORXONOX_PLATFORM_UNIX |
|---|
| 55 | |
|---|
| 56 | termios* IOConsole::originalTerminalSettings_; |
|---|
| 57 | |
|---|
| 58 | namespace EscapeMode |
|---|
| 59 | { |
|---|
| 60 | enum Value |
|---|
| 61 | { |
|---|
| 62 | None, |
|---|
| 63 | First, |
|---|
| 64 | Second |
|---|
| 65 | }; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | IOConsole::IOConsole() |
|---|
| 69 | : shell_(Shell::getInstance()) |
|---|
| 70 | , buffer_(Shell::getInstance().getInputBuffer()) |
|---|
| 71 | , bStatusPrinted_(false) |
|---|
| 72 | { |
|---|
| 73 | this->originalTerminalSettings_ = new termios; |
|---|
| 74 | this->setTerminalMode(); |
|---|
| 75 | this->shell_.registerListener(this); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | // Manually set the widths of the individual status lines |
|---|
| 79 | this->statusLineWidths_.push_back(20); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | IOConsole::~IOConsole() |
|---|
| 83 | { |
|---|
| 84 | std::cout << "\033[0G\033[K"; |
|---|
| 85 | std::cout.flush(); |
|---|
| 86 | resetTerminalMode(); |
|---|
| 87 | delete this->originalTerminalSettings_; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | void IOConsole::setTerminalMode() |
|---|
| 91 | { |
|---|
| 92 | termios new_settings; |
|---|
| 93 | |
|---|
| 94 | tcgetattr(0, this->originalTerminalSettings_); |
|---|
| 95 | new_settings = *this->originalTerminalSettings_; |
|---|
| 96 | new_settings.c_lflag &= ~(ICANON | ECHO); |
|---|
| 97 | // new_settings.c_lflag |= ( ISIG | IEXTEN ); |
|---|
| 98 | new_settings.c_cc[VTIME] = 0; |
|---|
| 99 | new_settings.c_cc[VMIN] = 0; |
|---|
| 100 | tcsetattr(0, TCSANOW, &new_settings); |
|---|
| 101 | COUT(0) << endl; |
|---|
| 102 | // atexit(&IOConsole::resetTerminalMode); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | void IOConsole::resetTerminalMode() |
|---|
| 106 | { |
|---|
| 107 | tcsetattr(0, TCSANOW, IOConsole::originalTerminalSettings_); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | void IOConsole::update(const Clock& time) |
|---|
| 111 | { |
|---|
| 112 | unsigned char c = 0; |
|---|
| 113 | std::string escapeSequence; |
|---|
| 114 | EscapeMode::Value escapeMode = EscapeMode::None; |
|---|
| 115 | while (read(STDIN_FILENO, &c, 1) == 1) |
|---|
| 116 | { |
|---|
| 117 | if (escapeMode == EscapeMode::First && (c == '[' || c=='O') ) |
|---|
| 118 | escapeMode = EscapeMode::Second; |
|---|
| 119 | // Get Alt+Tab combination when switching applications |
|---|
| 120 | else if (escapeMode == EscapeMode::First && c == '\t') |
|---|
| 121 | { |
|---|
| 122 | this->buffer_->buttonPressed(KeyEvent(KeyCode::Tab, '\t', KeyboardModifier::Alt)); |
|---|
| 123 | escapeMode = EscapeMode::None; |
|---|
| 124 | } |
|---|
| 125 | else if (escapeMode == EscapeMode::Second) |
|---|
| 126 | { |
|---|
| 127 | escapeSequence += c; |
|---|
| 128 | escapeMode = EscapeMode::None; |
|---|
| 129 | if (escapeSequence == "A") |
|---|
| 130 | this->buffer_->buttonPressed(KeyEvent(KeyCode::Up, 0, 0)); |
|---|
| 131 | else if (escapeSequence == "B") |
|---|
| 132 | this->buffer_->buttonPressed(KeyEvent(KeyCode::Down, 0, 0)); |
|---|
| 133 | else if (escapeSequence == "C") |
|---|
| 134 | this->buffer_->buttonPressed(KeyEvent(KeyCode::Right, 0, 0)); |
|---|
| 135 | else if (escapeSequence == "D") |
|---|
| 136 | this->buffer_->buttonPressed(KeyEvent(KeyCode::Left, 0, 0)); |
|---|
| 137 | else if (escapeSequence == "1~" || escapeSequence == "H") |
|---|
| 138 | this->buffer_->buttonPressed(KeyEvent(KeyCode::Home, 0, 0)); |
|---|
| 139 | else if (escapeSequence == "2~") |
|---|
| 140 | this->buffer_->buttonPressed(KeyEvent(KeyCode::Insert, 0, 0)); |
|---|
| 141 | else if (escapeSequence == "3~") |
|---|
| 142 | this->buffer_->buttonPressed(KeyEvent(KeyCode::Delete, 0, 0)); |
|---|
| 143 | else if (escapeSequence == "4~" || escapeSequence == "F") |
|---|
| 144 | this->buffer_->buttonPressed(KeyEvent(KeyCode::End, 0, 0)); |
|---|
| 145 | else if (escapeSequence == "5~") |
|---|
| 146 | this->buffer_->buttonPressed(KeyEvent(KeyCode::AltPageUp, 0, 0)); |
|---|
| 147 | else if (escapeSequence == "6~") |
|---|
| 148 | this->buffer_->buttonPressed(KeyEvent(KeyCode::AltPageDown, 0, 0)); |
|---|
| 149 | else |
|---|
| 150 | // Waiting for sequence to complete |
|---|
| 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; |
|---|
| 154 | } |
|---|
| 155 | else // not in an escape sequence OR user might have pressed just ESC |
|---|
| 156 | { |
|---|
| 157 | if (escapeMode == EscapeMode::First) |
|---|
| 158 | { |
|---|
| 159 | this->buffer_->buttonPressed(KeyEvent(KeyCode::Escape, c, 0)); |
|---|
| 160 | escapeMode = EscapeMode::None; |
|---|
| 161 | } |
|---|
| 162 | if (c == '\033') |
|---|
| 163 | { |
|---|
| 164 | escapeMode = EscapeMode::First; |
|---|
| 165 | escapeSequence.clear(); |
|---|
| 166 | } |
|---|
| 167 | else |
|---|
| 168 | { |
|---|
| 169 | KeyCode::ByEnum code; |
|---|
| 170 | switch (c) |
|---|
| 171 | { |
|---|
| 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; |
|---|
| 175 | default: |
|---|
| 176 | // We don't encode the key code (would be a very large switch) |
|---|
| 177 | // because the InputBuffer will only insert the text anyway |
|---|
| 178 | // Replacement character is simply KeyCode::A |
|---|
| 179 | code = KeyCode::A; |
|---|
| 180 | } |
|---|
| 181 | this->buffer_->buttonPressed(KeyEvent(code, c, 0)); |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | // If there is still an escape key pending (escape key ONLY), then |
|---|
| 187 | // it sure isn't an escape sequence anymore |
|---|
| 188 | if (escapeMode == EscapeMode::First) |
|---|
| 189 | this->buffer_->buttonPressed(KeyEvent(KeyCode::Escape, '\033', 0)); |
|---|
| 190 | |
|---|
| 191 | // Print input line |
|---|
| 192 | this->printInputLine(); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | void IOConsole::printLogText(const std::string& text) |
|---|
| 196 | { |
|---|
| 197 | std::string output; |
|---|
| 198 | |
|---|
| 199 | // Handle line colouring by inspecting the first letter |
|---|
| 200 | char level = 0; |
|---|
| 201 | if (!text.empty()) |
|---|
| 202 | level = text[0]; |
|---|
| 203 | if (level >= -1 && level <= 6) |
|---|
| 204 | output = text.substr(1); |
|---|
| 205 | else |
|---|
| 206 | output = text; |
|---|
| 207 | |
|---|
| 208 | // Colour line |
|---|
| 209 | /* |
|---|
| 210 | switch (level) |
|---|
| 211 | { |
|---|
| 212 | case -1: std::cout << "\033[37m"; break; |
|---|
| 213 | case 1: std::cout << "\033[91m"; break; |
|---|
| 214 | case 2: std::cout << "\033[31m"; break; |
|---|
| 215 | case 3: std::cout << "\033[34m"; break; |
|---|
| 216 | case 4: std::cout << "\033[36m"; break; |
|---|
| 217 | case 5: std::cout << "\033[35m"; break; |
|---|
| 218 | case 6: std::cout << "\033[37m"; break; |
|---|
| 219 | default: break; |
|---|
| 220 | } |
|---|
| 221 | */ |
|---|
| 222 | |
|---|
| 223 | // Print output line |
|---|
| 224 | std::cout << output; |
|---|
| 225 | |
|---|
| 226 | // Reset colour to white |
|---|
| 227 | // std::cout << "\033[37m"; |
|---|
| 228 | std::cout.flush(); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | void IOConsole::printInputLine() |
|---|
| 232 | { |
|---|
| 233 | // Set cursor to the beginning of the line and erase the line |
|---|
| 234 | std::cout << "\033[1G\033[K"; |
|---|
| 235 | // Print status line |
|---|
| 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 # "; |
|---|
| 237 | // Indicate a command prompt |
|---|
| 238 | std::cout << promptString_g; |
|---|
| 239 | // Save cursor position |
|---|
| 240 | std::cout << "\033[s"; |
|---|
| 241 | // Print command line buffer |
|---|
| 242 | std::cout << this->shell_.getInput(); |
|---|
| 243 | // Restore cursor position and move it to the right |
|---|
| 244 | std::cout << "\033[u"; |
|---|
| 245 | if (this->buffer_->getCursorPosition() > 0) |
|---|
| 246 | std::cout << "\033[" << this->buffer_->getCursorPosition() << "C"; |
|---|
| 247 | std::cout.flush(); |
|---|
| 248 | } |
|---|
| 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 | /* |
|---|
| 261 | int x, y; |
|---|
| 262 | if (this->getTerminalSize(&x, &y) && (x < statusTextWidth_g || y < (2 + statusTextHeight_g))) |
|---|
| 263 | { |
|---|
| 264 | this->bStatusPrinted_ = false; |
|---|
| 265 | return; |
|---|
| 266 | } |
|---|
| 267 | */ |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | int IOConsole::getTerminalSize(int* x, int* y) |
|---|
| 272 | { |
|---|
| 273 | #ifdef TIOCGSIZE |
|---|
| 274 | struct ttysize win; |
|---|
| 275 | #elif defined(TIOCGWINSZ) |
|---|
| 276 | struct winsize win; |
|---|
| 277 | #endif |
|---|
| 278 | |
|---|
| 279 | #ifdef TIOCGSIZE |
|---|
| 280 | if (ioctl(STDIN_FILENO, TIOCGSIZE, &win)) |
|---|
| 281 | return 0; |
|---|
| 282 | *y = win.ts_lines; |
|---|
| 283 | *x = win.ts_cols; |
|---|
| 284 | #elif defined TIOCGWINSZ |
|---|
| 285 | if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win)) |
|---|
| 286 | return 0; |
|---|
| 287 | *y = win.ws_row; |
|---|
| 288 | *x = win.ws_col; |
|---|
| 289 | #else |
|---|
| 290 | { |
|---|
| 291 | const char* s = getenv("LINES"); |
|---|
| 292 | if (s) |
|---|
| 293 | *y = strtol(s, NULL, 10); |
|---|
| 294 | else |
|---|
| 295 | *y = 25; |
|---|
| 296 | s = getenv("COLUMNS"); |
|---|
| 297 | if (s) |
|---|
| 298 | *x = strtol(s, NULL, 10); |
|---|
| 299 | else |
|---|
| 300 | *x = 80; |
|---|
| 301 | } |
|---|
| 302 | #endif |
|---|
| 303 | return 1; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | #elif defined(ORXONOX_PLATFORM_WINDOWS) |
|---|
| 307 | |
|---|
| 308 | IOConsole::IOConsole() |
|---|
| 309 | : shell_(Shell::getInstance()) |
|---|
| 310 | , buffer_(Shell::getInstance().getInputBuffer()) |
|---|
| 311 | { |
|---|
| 312 | this->setTerminalMode(); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | IOConsole::~IOConsole() |
|---|
| 316 | { |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | void IOConsole::setTerminalMode() |
|---|
| 320 | { |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | void IOConsole::resetTerminalMode() |
|---|
| 324 | { |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | void IOConsole::update(const Clock& time) |
|---|
| 328 | { |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | void IOConsole::print(const std::string& text) |
|---|
| 332 | { |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | void IOConsole::printInputLine() |
|---|
| 336 | { |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | #endif /* ORXONOX_PLATFORM_UNIX */ |
|---|
| 340 | |
|---|
| 341 | // ############################### |
|---|
| 342 | // ### ShellListener methods ### |
|---|
| 343 | // ############################### |
|---|
| 344 | |
|---|
| 345 | /** |
|---|
| 346 | @brief |
|---|
| 347 | Called if all output-lines have to be redrawn. |
|---|
| 348 | */ |
|---|
| 349 | void IOConsole::linesChanged() |
|---|
| 350 | { |
|---|
| 351 | // Method only gets called upon start to draw all the lines |
|---|
| 352 | // But we are using std::cout anyway, so do nothing here |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | /** |
|---|
| 356 | @brief |
|---|
| 357 | Called if only the last output-line has changed. |
|---|
| 358 | */ |
|---|
| 359 | void IOConsole::onlyLastLineChanged() |
|---|
| 360 | { |
|---|
| 361 | // Save cursor position and move it to the beginning of the first output line |
|---|
| 362 | std::cout << "\033[s\033[" << (1 + 0/*statusTextHeight_g*/) << "F"; |
|---|
| 363 | // Erase the line |
|---|
| 364 | std::cout << "\033[K"; |
|---|
| 365 | // Reprint the last output line |
|---|
| 366 | this->printLogText(*(this->shell_.getNewestLineIterator())); |
|---|
| 367 | // Restore cursor |
|---|
| 368 | std::cout << "\033[u"; |
|---|
| 369 | std::cout.flush(); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | /** |
|---|
| 373 | @brief |
|---|
| 374 | Called if a new output-line was added. |
|---|
| 375 | */ |
|---|
| 376 | void IOConsole::lineAdded() |
|---|
| 377 | { |
|---|
| 378 | // Save cursor and move it to the beginning of the first status line |
|---|
| 379 | std::cout << "\033[s\033[" << 0/*statusTextHeight_g*/ << "F"; |
|---|
| 380 | // Create a new line and move cursor to the beginning of it (one cell up) |
|---|
| 381 | std::cout << std::endl << "\033[1F"; |
|---|
| 382 | // Print the new output line |
|---|
| 383 | this->printLogText(*(this->shell_.getNewestLineIterator())); |
|---|
| 384 | // Restore cursor (for horizontal position) and move it down again (just in case the lines were shifted) |
|---|
| 385 | std::cout << "\033[u\033[" << (1 + 0/*statusTextHeight_g*/) << "B"; |
|---|
| 386 | std::cout.flush(); |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | /** |
|---|
| 390 | @brief |
|---|
| 391 | Called if the text in the input-line has changed. |
|---|
| 392 | */ |
|---|
| 393 | void IOConsole::inputChanged() |
|---|
| 394 | { |
|---|
| 395 | this->printInputLine(); |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | /** |
|---|
| 399 | @brief |
|---|
| 400 | Called if the position of the cursor in the input-line has changed. |
|---|
| 401 | */ |
|---|
| 402 | void IOConsole::cursorChanged() |
|---|
| 403 | { |
|---|
| 404 | this->printInputLine(); |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | /** |
|---|
| 408 | @brief |
|---|
| 409 | Called if a command is about to be executed |
|---|
| 410 | */ |
|---|
| 411 | void IOConsole::executed() |
|---|
| 412 | { |
|---|
| 413 | // Move cursor the beginning of the line |
|---|
| 414 | std::cout << "\033[1G"; |
|---|
| 415 | // Print command so the user knows what he has typed |
|---|
| 416 | std::cout << promptString_g << this->shell_.getInput() << std::endl; |
|---|
| 417 | this->printInputLine(); |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | /** |
|---|
| 422 | @brief |
|---|
| 423 | Called if the console gets closed. |
|---|
| 424 | */ |
|---|
| 425 | void IOConsole::exit() |
|---|
| 426 | { |
|---|
| 427 | // Exit is not an option, IOConsole always exists |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | } |
|---|