[1312] | 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 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[1313] | 29 | #include "Shell.h" |
---|
| 30 | #include "CommandExecutor.h" |
---|
| 31 | #include "CoreIncludes.h" |
---|
| 32 | #include "ConfigValueIncludes.h" |
---|
[1326] | 33 | #include "CoreSettings.h" |
---|
[1312] | 34 | |
---|
[1313] | 35 | #define SHELL_UPDATE_LISTENERS(function) \ |
---|
| 36 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) \ |
---|
| 37 | (*it)->function() |
---|
[1312] | 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
| 41 | Shell::Shell() |
---|
| 42 | { |
---|
[1313] | 43 | RegisterRootObject(Shell); |
---|
| 44 | |
---|
| 45 | this->scrollPosition_ = 0; |
---|
| 46 | this->maxHistoryLength_ = 100; |
---|
| 47 | this->historyPosition_ = 0; |
---|
| 48 | this->historyOffset_ = 0; |
---|
[1322] | 49 | this->finishedLastLine_ = true; |
---|
[1313] | 50 | |
---|
| 51 | this->clearLines(); |
---|
[1322] | 52 | |
---|
[1312] | 53 | this->inputBuffer_.registerListener(this, &Shell::inputChanged, true); |
---|
| 54 | this->inputBuffer_.registerListener(this, &Shell::execute, '\r', false); |
---|
| 55 | this->inputBuffer_.registerListener(this, &Shell::hintandcomplete, '\t', true); |
---|
| 56 | this->inputBuffer_.registerListener(this, &Shell::backspace, '\b', true); |
---|
[1313] | 57 | this->inputBuffer_.registerListener(this, &Shell::deletechar, OIS::KC_DELETE); |
---|
[1312] | 58 | this->inputBuffer_.registerListener(this, &Shell::exit, (char)27, true); |
---|
| 59 | this->inputBuffer_.registerListener(this, &Shell::cursor_right, OIS::KC_RIGHT); |
---|
| 60 | this->inputBuffer_.registerListener(this, &Shell::cursor_left, OIS::KC_LEFT); |
---|
[1313] | 61 | this->inputBuffer_.registerListener(this, &Shell::cursor_end, OIS::KC_END); |
---|
| 62 | this->inputBuffer_.registerListener(this, &Shell::cursor_home, OIS::KC_HOME); |
---|
[1312] | 63 | this->inputBuffer_.registerListener(this, &Shell::history_up, OIS::KC_UP); |
---|
| 64 | this->inputBuffer_.registerListener(this, &Shell::history_down, OIS::KC_DOWN); |
---|
| 65 | this->inputBuffer_.registerListener(this, &Shell::scroll_up, OIS::KC_PGUP); |
---|
| 66 | this->inputBuffer_.registerListener(this, &Shell::scroll_down, OIS::KC_PGDOWN); |
---|
[1322] | 67 | |
---|
| 68 | this->outputBuffer_.registerListener(this); |
---|
| 69 | |
---|
[1313] | 70 | this->setConfigValues(); |
---|
[1312] | 71 | } |
---|
| 72 | |
---|
[1326] | 73 | Shell& Shell::createShell() |
---|
[1312] | 74 | { |
---|
[1326] | 75 | int level = CoreSettings::getSoftDebugLevel(OutputHandler::LD_Shell); |
---|
| 76 | CoreSettings::setSoftDebugLevel(OutputHandler::LD_Shell, -1); |
---|
[1313] | 77 | static Shell instance; |
---|
[1326] | 78 | CoreSettings::setSoftDebugLevel(OutputHandler::LD_Shell, level); |
---|
[1312] | 79 | return instance; |
---|
| 80 | } |
---|
| 81 | |
---|
[1326] | 82 | Shell& Shell::getInstance() |
---|
| 83 | { |
---|
| 84 | static Shell& instance = createShell(); |
---|
| 85 | return instance; |
---|
| 86 | } |
---|
| 87 | |
---|
[1312] | 88 | void Shell::setConfigValues() |
---|
[1313] | 89 | { |
---|
| 90 | SetConfigValue(maxHistoryLength_, 100); |
---|
| 91 | SetConfigValue(historyOffset_, 0); |
---|
[1325] | 92 | SetConfigValueVector(commandHistory_, std::vector<std::string>()); |
---|
[1312] | 93 | |
---|
[1313] | 94 | if (this->historyOffset_ >= this->maxHistoryLength_) |
---|
| 95 | this->historyOffset_ = 0; |
---|
| 96 | |
---|
| 97 | while (this->commandHistory_.size() > this->maxHistoryLength_) |
---|
| 98 | { |
---|
| 99 | unsigned int index = this->commandHistory_.size() - 1; |
---|
| 100 | this->commandHistory_.erase(this->commandHistory_.begin() + index); |
---|
| 101 | ModifyConfigValue(commandHistory_, remove, index); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void Shell::registerListener(ShellListener* listener) |
---|
| 106 | { |
---|
| 107 | this->listeners_.insert(this->listeners_.end(), listener); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void Shell::unregisterListener(ShellListener* listener) |
---|
| 111 | { |
---|
| 112 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) |
---|
| 113 | { |
---|
| 114 | if ((*it) == listener) |
---|
| 115 | this->listeners_.erase(it++); |
---|
| 116 | else |
---|
| 117 | ++it; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | void Shell::setCursorPosition(unsigned int cursor) |
---|
| 122 | { |
---|
| 123 | this->inputBuffer_.setCursorPosition(cursor); |
---|
| 124 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | void Shell::setInput(const std::string& input) |
---|
| 128 | { |
---|
| 129 | this->inputBuffer_.set(input); |
---|
| 130 | this->inputChanged(); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void Shell::addLine(const std::string& line, unsigned int level) |
---|
| 134 | { |
---|
[1322] | 135 | int original_level = OutputHandler::getOutStream().getOutputLevel(); |
---|
| 136 | OutputHandler::getOutStream().setOutputLevel(level); |
---|
| 137 | |
---|
| 138 | if (!this->finishedLastLine_) |
---|
| 139 | this->outputBuffer_ << std::endl; |
---|
| 140 | |
---|
| 141 | this->outputBuffer_ << line << std::endl; |
---|
| 142 | OutputHandler::getOutStream().setOutputLevel(original_level); |
---|
[1313] | 143 | } |
---|
| 144 | |
---|
| 145 | void Shell::clearLines() |
---|
| 146 | { |
---|
| 147 | this->lines_.clear(); |
---|
| 148 | this->scrollIterator_ = this->lines_.begin(); |
---|
| 149 | |
---|
| 150 | this->scrollPosition_ = 0; |
---|
[1322] | 151 | this->finishedLastLine_ = true; |
---|
[1313] | 152 | |
---|
| 153 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | std::list<std::string>::const_iterator Shell::getNewestLineIterator() const |
---|
| 157 | { |
---|
| 158 | if (this->scrollPosition_) |
---|
| 159 | return this->scrollIterator_; |
---|
| 160 | else |
---|
[1322] | 161 | return this->lines_.begin(); |
---|
[1313] | 162 | } |
---|
| 163 | |
---|
| 164 | std::list<std::string>::const_iterator Shell::getEndIterator() const |
---|
| 165 | { |
---|
| 166 | return this->lines_.end(); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | void Shell::addToHistory(const std::string& command) |
---|
| 170 | { |
---|
| 171 | ModifyConfigValue(commandHistory_, set, this->historyOffset_, command); |
---|
| 172 | this->historyPosition_ = 0; |
---|
[1322] | 173 | ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_); |
---|
[1313] | 174 | } |
---|
| 175 | |
---|
| 176 | std::string Shell::getFromHistory() const |
---|
| 177 | { |
---|
[1324] | 178 | unsigned int index = mod(((int)this->historyOffset_) - ((int)this->historyPosition_), this->maxHistoryLength_); |
---|
| 179 | if (index < this->commandHistory_.size() && this->historyPosition_ != 0) |
---|
| 180 | return this->commandHistory_[index]; |
---|
| 181 | else |
---|
| 182 | return ""; |
---|
[1313] | 183 | } |
---|
| 184 | |
---|
[1312] | 185 | void Shell::outputChanged() |
---|
[1313] | 186 | { |
---|
| 187 | std::string output; |
---|
[1322] | 188 | bool newline; |
---|
| 189 | do |
---|
[1313] | 190 | { |
---|
[1322] | 191 | newline = this->outputBuffer_.getLine(&output); |
---|
[1317] | 192 | |
---|
[1322] | 193 | if (!newline && output == "") |
---|
| 194 | break; |
---|
[1317] | 195 | |
---|
[1322] | 196 | if (this->finishedLastLine_) |
---|
| 197 | { |
---|
| 198 | this->lines_.insert(this->lines_.begin(), output); |
---|
[1317] | 199 | |
---|
[1322] | 200 | if (this->scrollPosition_) |
---|
| 201 | this->scrollPosition_++; |
---|
| 202 | else |
---|
| 203 | this->scrollIterator_ = this->lines_.begin(); |
---|
[1313] | 204 | |
---|
[1322] | 205 | this->finishedLastLine_ = newline; |
---|
[1327] | 206 | |
---|
| 207 | if (!this->scrollPosition_) |
---|
| 208 | { |
---|
| 209 | SHELL_UPDATE_LISTENERS(lineAdded); |
---|
| 210 | } |
---|
[1322] | 211 | } |
---|
[1313] | 212 | else |
---|
[1317] | 213 | { |
---|
[1322] | 214 | (*this->lines_.begin()) += output; |
---|
| 215 | this->finishedLastLine_ = newline; |
---|
| 216 | SHELL_UPDATE_LISTENERS(onlyLastLineChanged); |
---|
[1317] | 217 | } |
---|
[1313] | 218 | |
---|
[1322] | 219 | } while (newline); |
---|
[1313] | 220 | } |
---|
| 221 | |
---|
[1312] | 222 | void Shell::inputChanged() |
---|
[1313] | 223 | { |
---|
| 224 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 225 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 226 | } |
---|
| 227 | |
---|
[1312] | 228 | void Shell::execute() |
---|
[1313] | 229 | { |
---|
[1324] | 230 | this->addToHistory(this->inputBuffer_.get()); |
---|
[1322] | 231 | this->addLine(this->inputBuffer_.get(), 0); |
---|
| 232 | |
---|
| 233 | if (!CommandExecutor::execute(this->inputBuffer_.get())) |
---|
[1313] | 234 | this->addLine("Error: Can't execute \"" + this->inputBuffer_.get() + "\".", 1); |
---|
| 235 | |
---|
| 236 | this->clear(); |
---|
| 237 | } |
---|
| 238 | |
---|
[1312] | 239 | void Shell::hintandcomplete() |
---|
[1313] | 240 | { |
---|
| 241 | this->addLine(CommandExecutor::hint(this->inputBuffer_.get()), 0); |
---|
| 242 | this->inputBuffer_.set(CommandExecutor::complete(this->inputBuffer_.get())); |
---|
| 243 | |
---|
| 244 | this->inputChanged(); |
---|
| 245 | } |
---|
| 246 | |
---|
[1312] | 247 | void Shell::backspace() |
---|
[1313] | 248 | { |
---|
| 249 | this->inputBuffer_.removeBehindCursor(); |
---|
| 250 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 251 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | void Shell::deletechar() |
---|
| 255 | { |
---|
| 256 | this->inputBuffer_.removeAtCursor(); |
---|
| 257 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 258 | } |
---|
| 259 | |
---|
[1312] | 260 | void Shell::clear() |
---|
[1313] | 261 | { |
---|
| 262 | this->inputBuffer_.clear(); |
---|
[1324] | 263 | this->historyPosition_ = 0; |
---|
[1313] | 264 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 265 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 266 | } |
---|
| 267 | |
---|
[1312] | 268 | void Shell::cursor_right() |
---|
[1313] | 269 | { |
---|
| 270 | this->inputBuffer_.increaseCursor(); |
---|
| 271 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 272 | } |
---|
| 273 | |
---|
[1312] | 274 | void Shell::cursor_left() |
---|
[1313] | 275 | { |
---|
| 276 | this->inputBuffer_.decreaseCursor(); |
---|
| 277 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | void Shell::cursor_end() |
---|
| 281 | { |
---|
| 282 | this->inputBuffer_.setCursorToEnd(); |
---|
| 283 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | void Shell::cursor_home() |
---|
| 287 | { |
---|
| 288 | this->inputBuffer_.setCursorToBegin(); |
---|
| 289 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 290 | } |
---|
| 291 | |
---|
[1312] | 292 | void Shell::history_up() |
---|
[1313] | 293 | { |
---|
[1324] | 294 | if (this->historyPosition_ < this->commandHistory_.size()) |
---|
[1313] | 295 | { |
---|
| 296 | this->historyPosition_++; |
---|
| 297 | this->inputBuffer_.set(this->getFromHistory()); |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
[1312] | 301 | void Shell::history_down() |
---|
[1313] | 302 | { |
---|
| 303 | if (this->historyPosition_ > 0) |
---|
| 304 | { |
---|
[1322] | 305 | this->historyPosition_--; |
---|
[1313] | 306 | this->inputBuffer_.set(this->getFromHistory()); |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|
[1312] | 310 | void Shell::scroll_up() |
---|
[1313] | 311 | { |
---|
| 312 | if (this->scrollIterator_ != this->lines_.end()) |
---|
| 313 | { |
---|
| 314 | ++this->scrollIterator_; |
---|
| 315 | ++this->scrollPosition_; |
---|
| 316 | |
---|
| 317 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | |
---|
[1312] | 321 | void Shell::scroll_down() |
---|
[1313] | 322 | { |
---|
| 323 | if (this->scrollIterator_ != this->lines_.begin()) |
---|
| 324 | { |
---|
| 325 | --this->scrollIterator_; |
---|
| 326 | --this->scrollPosition_; |
---|
| 327 | |
---|
| 328 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
[1312] | 332 | void Shell::exit() |
---|
[1313] | 333 | { |
---|
| 334 | if (this->inputBuffer_.getSize() > 0) |
---|
| 335 | { |
---|
| 336 | this->clear(); |
---|
| 337 | return; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | this->clear(); |
---|
[1326] | 341 | this->scrollPosition_ = 0; |
---|
| 342 | this->scrollIterator_ = this->lines_.begin(); |
---|
| 343 | |
---|
[1313] | 344 | SHELL_UPDATE_LISTENERS(exit); |
---|
| 345 | } |
---|
[1312] | 346 | } |
---|