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 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 == 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; |
---|
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 | 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 | |
---|
304 | #elif defined(ORXONOX_PLATFORM_WINDOWS) |
---|
305 | |
---|
306 | IOConsole::IOConsole() |
---|
307 | : shell_(Shell::getInstance()) |
---|
308 | , buffer_(Shell::getInstance().getInputBuffer()) |
---|
309 | { |
---|
310 | this->setTerminalMode(); |
---|
311 | } |
---|
312 | |
---|
313 | IOConsole::~IOConsole() |
---|
314 | { |
---|
315 | } |
---|
316 | |
---|
317 | void IOConsole::setTerminalMode() |
---|
318 | { |
---|
319 | } |
---|
320 | |
---|
321 | void IOConsole::resetTerminalMode() |
---|
322 | { |
---|
323 | } |
---|
324 | |
---|
325 | void IOConsole::update(const Clock& time) |
---|
326 | { |
---|
327 | } |
---|
328 | |
---|
329 | void IOConsole::print(const std::string& text) |
---|
330 | { |
---|
331 | } |
---|
332 | |
---|
333 | void IOConsole::printInputLine() |
---|
334 | { |
---|
335 | } |
---|
336 | |
---|
337 | #endif /* ORXONOX_PLATFORM_UNIX */ |
---|
338 | |
---|
339 | // ############################### |
---|
340 | // ### ShellListener methods ### |
---|
341 | // ############################### |
---|
342 | |
---|
343 | /** |
---|
344 | @brief |
---|
345 | Called if all output-lines have to be redrawn. |
---|
346 | */ |
---|
347 | void IOConsole::linesChanged() |
---|
348 | { |
---|
349 | // Method only gets called upon start to draw all the lines |
---|
350 | // But we are using std::cout anyway, so do nothing here |
---|
351 | } |
---|
352 | |
---|
353 | /** |
---|
354 | @brief |
---|
355 | Called if only the last output-line has changed. |
---|
356 | */ |
---|
357 | void IOConsole::onlyLastLineChanged() |
---|
358 | { |
---|
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 |
---|
362 | std::cout << "\033[K"; |
---|
363 | // Reprint the last output line |
---|
364 | this->printLogText(*(this->shell_.getNewestLineIterator())); |
---|
365 | // Restore cursor |
---|
366 | std::cout << "\033[u"; |
---|
367 | std::cout.flush(); |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | @brief |
---|
372 | Called if a new output-line was added. |
---|
373 | */ |
---|
374 | void IOConsole::lineAdded() |
---|
375 | { |
---|
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(); |
---|
385 | } |
---|
386 | |
---|
387 | /** |
---|
388 | @brief |
---|
389 | Called if the text in the input-line has changed. |
---|
390 | */ |
---|
391 | void IOConsole::inputChanged() |
---|
392 | { |
---|
393 | this->printInputLine(); |
---|
394 | } |
---|
395 | |
---|
396 | /** |
---|
397 | @brief |
---|
398 | Called if the position of the cursor in the input-line has changed. |
---|
399 | */ |
---|
400 | void IOConsole::cursorChanged() |
---|
401 | { |
---|
402 | this->printInputLine(); |
---|
403 | } |
---|
404 | |
---|
405 | /** |
---|
406 | @brief |
---|
407 | Called if a command is about to be executed |
---|
408 | */ |
---|
409 | void IOConsole::executed() |
---|
410 | { |
---|
411 | // Move cursor the beginning of the line |
---|
412 | std::cout << "\033[1G"; |
---|
413 | // Print command so the user knows what he has typed |
---|
414 | std::cout << promptString_g << this->shell_.getInput() << std::endl; |
---|
415 | this->printInputLine(); |
---|
416 | } |
---|
417 | |
---|
418 | |
---|
419 | /** |
---|
420 | @brief |
---|
421 | Called if the console gets closed. |
---|
422 | */ |
---|
423 | void IOConsole::exit() |
---|
424 | { |
---|
425 | // Exit is not an option, IOConsole always exists |
---|
426 | } |
---|
427 | |
---|
428 | } |
---|