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