Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/Shell.cc @ 6482

Last change on this file since 6482 was 6417, checked in by rgrieder, 15 years ago

Merged presentation2 branch back to trunk.
Major new features:

  • Actual GUI with settings, etc.
  • Improved space ship steering (human interaction)
  • Rocket fire and more particle effects
  • Advanced sound framework
  • Property svn:eol-style set to native
File size: 15.0 KB
RevLine 
[1505]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:
[6105]25 *      Reto Grieder
[1505]26 *
27 */
28
29#include "Shell.h"
[3196]30
31#include "util/OutputHandler.h"
[6105]32#include "util/StringUtils.h"
33#include "util/SubString.h"
[1505]34#include "CommandExecutor.h"
35#include "CoreIncludes.h"
36#include "ConfigValueIncludes.h"
37#include "ConsoleCommand.h"
38
39namespace orxonox
40{
[1747]41    SetConsoleCommandShortcut(OutputHandler, log);
42    SetConsoleCommandShortcut(OutputHandler, error);
43    SetConsoleCommandShortcut(OutputHandler, warning);
44    SetConsoleCommandShortcut(OutputHandler, info);
45    SetConsoleCommandShortcut(OutputHandler, debug);
46
[6417]47    Shell::Shell(const std::string& consoleName, bool bScrollable)
[6105]48        : OutputListener(consoleName)
49        , inputBuffer_(new InputBuffer())
50        , consoleName_(consoleName)
51        , bScrollable_(bScrollable)
[1505]52    {
53        RegisterRootObject(Shell);
54
55        this->scrollPosition_ = 0;
56        this->maxHistoryLength_ = 100;
57        this->historyPosition_ = 0;
58        this->historyOffset_ = 0;
[6105]59        this->bFinishedLastLine_ = true;
[1505]60
[6105]61        this->clearOutput();
[1755]62        this->configureInputBuffer();
[1505]63
[6417]64        // Specify file for the command history
65        ConfigFileManager::getInstance().setFilename(ConfigFileType::CommandHistory, "commandHistory.ini");
[3280]66
[6417]67        // Use a stringstream object to buffer the output
[6105]68        this->outputStream_ = &this->outputBuffer_;
69
[1505]70        this->setConfigValues();
[1792]71
[6105]72        // Get the previous output and add it to the Shell
73        for (OutputHandler::OutputVectorIterator it = OutputHandler::getInstance().getOutputVectorBegin();
74            it != OutputHandler::getInstance().getOutputVectorEnd(); ++it)
75        {
76            if (it->first <= this->getSoftDebugLevel())
77            {
78                this->outputBuffer_ << it->second;
79                this->outputChanged(it->first);
80            }
81        }
82
83        // Register the shell as output listener
84        OutputHandler::getInstance().registerOutputListener(this);
[1505]85    }
86
[1755]87    Shell::~Shell()
88    {
[6105]89        OutputHandler::getInstance().unregisterOutputListener(this);
90        this->inputBuffer_->destroy();
[1755]91    }
92
[1505]93    void Shell::setConfigValues()
94    {
[6105]95        SetConfigValue(maxHistoryLength_, 100)
[3280]96            .callback(this, &Shell::commandHistoryLengthChanged);
[6105]97        SetConfigValue(historyOffset_, 0)
[3280]98            .callback(this, &Shell::commandHistoryOffsetChanged);
[6417]99        setConfigValueGeneric(this, &commandHistory_, ConfigFileType::CommandHistory, "Shell", "commandHistory_", std::vector<std::string>());
[6105]100
101#ifdef ORXONOX_RELEASE
102        const unsigned int defaultLevel = 1;
103#else
104        const unsigned int defaultLevel = 3;
105#endif
[6417]106        setConfigValueGeneric(this, &softDebugLevel_, ConfigFileType::Settings, "OutputHandler", "softDebugLevel" + this->consoleName_, defaultLevel)
[6105]107            .description("The maximal level of debug output shown in the Shell");
108        this->setSoftDebugLevel(this->softDebugLevel_);
[1747]109    }
[1505]110
[1747]111    void Shell::commandHistoryOffsetChanged()
112    {
[1505]113        if (this->historyOffset_ >= this->maxHistoryLength_)
114            this->historyOffset_ = 0;
[1747]115    }
[1505]116
[1747]117    void Shell::commandHistoryLengthChanged()
118    {
119        this->commandHistoryOffsetChanged();
120
[1505]121        while (this->commandHistory_.size() > this->maxHistoryLength_)
122        {
123            unsigned int index = this->commandHistory_.size() - 1;
124            this->commandHistory_.erase(this->commandHistory_.begin() + index);
125            ModifyConfigValue(commandHistory_, remove, index);
126        }
127    }
128
[1755]129    void Shell::configureInputBuffer()
[1505]130    {
131        this->inputBuffer_->registerListener(this, &Shell::inputChanged, true);
[6105]132        this->inputBuffer_->registerListener(this, &Shell::execute,         '\r',   false);
133        this->inputBuffer_->registerListener(this, &Shell::execute,         '\n',   false);
134        this->inputBuffer_->registerListener(this, &Shell::hintAndComplete, '\t',   true);
135        this->inputBuffer_->registerListener(this, &Shell::backspace,       '\b',   true);
136        this->inputBuffer_->registerListener(this, &Shell::backspace,       '\177', true);
137        this->inputBuffer_->registerListener(this, &Shell::exit,            '\033', true); // escape
138        this->inputBuffer_->registerListener(this, &Shell::deleteChar,      KeyCode::Delete);
139        this->inputBuffer_->registerListener(this, &Shell::cursorRight,     KeyCode::Right);
140        this->inputBuffer_->registerListener(this, &Shell::cursorLeft,      KeyCode::Left);
141        this->inputBuffer_->registerListener(this, &Shell::cursorEnd,       KeyCode::End);
142        this->inputBuffer_->registerListener(this, &Shell::cursorHome,      KeyCode::Home);
143        this->inputBuffer_->registerListener(this, &Shell::historyUp,       KeyCode::Up);
144        this->inputBuffer_->registerListener(this, &Shell::historyDown,     KeyCode::Down);
145        if (this->bScrollable_)
146        {
147            this->inputBuffer_->registerListener(this, &Shell::scrollUp,    KeyCode::PageUp);
148            this->inputBuffer_->registerListener(this, &Shell::scrollDown,  KeyCode::PageDown);
149        }
150        else
151        {
152            this->inputBuffer_->registerListener(this, &Shell::historySearchUp,   KeyCode::PageUp);
153            this->inputBuffer_->registerListener(this, &Shell::historySearchDown, KeyCode::PageDown);
154        }
[1505]155    }
156
[6105]157    /*
[1505]158    void Shell::history()
159    {
160        Shell& instance = Shell::getInstance();
161
[3300]162        for (unsigned int i = instance.historyOffset_; i < instance.commandHistory_.size(); ++i)
[6417]163            instance.addOutput(instance.commandHistory_[i] + '\n', -1);
[3300]164        for (unsigned int i =  0; i < instance.historyOffset_; ++i)
[6417]165            instance.addOutput(instance.commandHistory_[i] + '\n', -1);
[1505]166    }
[6105]167    */
[1505]168
169    void Shell::registerListener(ShellListener* listener)
170    {
[6105]171        this->listeners_.push_back(listener);
[1505]172    }
173
174    void Shell::unregisterListener(ShellListener* listener)
175    {
176        for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
177        {
178            if ((*it) == listener)
[6105]179                it = this->listeners_.erase(it);
[1505]180            else
181                ++it;
182        }
183    }
184
185    void Shell::setCursorPosition(unsigned int cursor)
186    {
187        this->inputBuffer_->setCursorPosition(cursor);
[6105]188        this->updateListeners<&ShellListener::cursorChanged>();
[1505]189    }
190
[6417]191    void Shell::addOutput(const std::string& text, LineType type)
[1505]192    {
[6417]193        this->outputBuffer_ << text;
194        this->outputChanged(type);
[1505]195    }
196
[6105]197    void Shell::clearOutput()
[1505]198    {
[6105]199        this->outputLines_.clear();
200        this->scrollIterator_ = this->outputLines_.begin();
[1505]201
202        this->scrollPosition_ = 0;
[6105]203        this->bFinishedLastLine_ = true;
[1505]204
[6105]205        this->updateListeners<&ShellListener::linesChanged>();
[1505]206    }
207
[6417]208    Shell::LineList::const_iterator Shell::getNewestLineIterator() const
[1505]209    {
210        if (this->scrollPosition_)
211            return this->scrollIterator_;
212        else
[6105]213            return this->outputLines_.begin();
[1505]214    }
215
[6417]216    Shell::LineList::const_iterator Shell::getEndIterator() const
[1505]217    {
[6105]218        return this->outputLines_.end();
[1505]219    }
220
221    void Shell::addToHistory(const std::string& command)
222    {
223        ModifyConfigValue(commandHistory_, set, this->historyOffset_, command);
224        this->historyPosition_ = 0;
225        ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_);
226    }
227
[6417]228    const std::string& Shell::getFromHistory() const
[1505]229    {
[3301]230        unsigned int index = mod(static_cast<int>(this->historyOffset_) - static_cast<int>(this->historyPosition_), this->maxHistoryLength_);
[1505]231        if (index < this->commandHistory_.size() && this->historyPosition_ != 0)
232            return this->commandHistory_[index];
233        else
[6417]234            return BLANKSTRING;
[1505]235    }
236
[6417]237    void Shell::outputChanged(int lineType)
[1505]238    {
[6105]239        bool newline = false;
[1505]240        do
241        {
[6105]242            std::string output;
243            std::getline(this->outputBuffer_, output);
[1505]244
[6105]245            bool eof = this->outputBuffer_.eof();
246            bool fail = this->outputBuffer_.fail();
247            if (eof)
248                this->outputBuffer_.flush();
249            if (eof || fail)
250                this->outputBuffer_.clear();
251            newline = (!eof && !fail);
252
[6417]253            if (!newline && output.empty())
[1505]254                break;
255
[6105]256            if (this->bFinishedLastLine_)
[1505]257            {
[6417]258                this->outputLines_.push_front(std::make_pair(output, static_cast<LineType>(lineType)));
[1505]259
260                if (this->scrollPosition_)
261                    this->scrollPosition_++;
262                else
[6105]263                    this->scrollIterator_ = this->outputLines_.begin();
[1505]264
[6105]265                this->bFinishedLastLine_ = newline;
[1505]266
267                if (!this->scrollPosition_)
[6105]268                    this->updateListeners<&ShellListener::lineAdded>();
[1505]269            }
270            else
271            {
[6417]272                this->outputLines_.front().first += output;
[6105]273                this->bFinishedLastLine_ = newline;
274                this->updateListeners<&ShellListener::onlyLastLineChanged>();
[1505]275            }
[6417]276            this->bFinishedLastLine_ = newline;
[1505]277
278        } while (newline);
279    }
280
[6105]281    void Shell::clearInput()
282    {
283        this->inputBuffer_->clear();
284        this->historyPosition_ = 0;
285        this->updateListeners<&ShellListener::inputChanged>();
286        this->updateListeners<&ShellListener::cursorChanged>();
287    }
288
289    void Shell::setPromptPrefix(const std::string& str)
290    {
291    }
292
293
294    // ##########################################
295    // ###   InputBuffer callback functions   ###
296    // ##########################################
297
[1505]298    void Shell::inputChanged()
299    {
[6105]300        this->updateListeners<&ShellListener::inputChanged>();
301        this->updateListeners<&ShellListener::cursorChanged>();
[1505]302    }
303
304    void Shell::execute()
305    {
306        this->addToHistory(this->inputBuffer_->get());
[6105]307        this->updateListeners<&ShellListener::executed>();
[1505]308
309        if (!CommandExecutor::execute(this->inputBuffer_->get()))
[6417]310        {
311            this->outputBuffer_ << "Error: Can't execute \"" << this->inputBuffer_->get() << "\"." << std::endl;
312            this->outputChanged(Error);
313        }
[1505]314
[6105]315        this->clearInput();
[1505]316    }
317
[6105]318    void Shell::hintAndComplete()
[1505]319    {
320        this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get()));
[6417]321        this->outputBuffer_ << CommandExecutor::hint(this->inputBuffer_->get()) << std::endl;
322        this->outputChanged(Hint);
[1505]323
324        this->inputChanged();
325    }
326
327    void Shell::backspace()
328    {
329        this->inputBuffer_->removeBehindCursor();
[6105]330        this->updateListeners<&ShellListener::inputChanged>();
331        this->updateListeners<&ShellListener::cursorChanged>();
[1505]332    }
333
[6105]334    void Shell::exit()
[1505]335    {
[6105]336        if (this->inputBuffer_->getSize() > 0)
337        {
338            this->clearInput();
339            return;
340        }
341
342        this->clearInput();
343        this->scrollPosition_ = 0;
344        this->scrollIterator_ = this->outputLines_.begin();
345
346        this->updateListeners<&ShellListener::exit>();
[1505]347    }
348
[6105]349    void Shell::deleteChar()
[1505]350    {
[6105]351        this->inputBuffer_->removeAtCursor();
352        this->updateListeners<&ShellListener::inputChanged>();
[1505]353    }
354
[6105]355    void Shell::cursorRight()
[1505]356    {
357        this->inputBuffer_->increaseCursor();
[6105]358        this->updateListeners<&ShellListener::cursorChanged>();
[1505]359    }
360
[6105]361    void Shell::cursorLeft()
[1505]362    {
363        this->inputBuffer_->decreaseCursor();
[6105]364        this->updateListeners<&ShellListener::cursorChanged>();
[1505]365    }
366
[6105]367    void Shell::cursorEnd()
[1505]368    {
369        this->inputBuffer_->setCursorToEnd();
[6105]370        this->updateListeners<&ShellListener::cursorChanged>();
[1505]371    }
372
[6105]373    void Shell::cursorHome()
[1505]374    {
375        this->inputBuffer_->setCursorToBegin();
[6105]376        this->updateListeners<&ShellListener::cursorChanged>();
[1505]377    }
378
[6105]379    void Shell::historyUp()
[1505]380    {
381        if (this->historyPosition_ < this->commandHistory_.size())
382        {
383            this->historyPosition_++;
384            this->inputBuffer_->set(this->getFromHistory());
385        }
386    }
387
[6105]388    void Shell::historyDown()
[1505]389    {
390        if (this->historyPosition_ > 0)
391        {
392            this->historyPosition_--;
393            this->inputBuffer_->set(this->getFromHistory());
394        }
395    }
396
[6105]397    void Shell::historySearchUp()
[1505]398    {
[6105]399        if (this->historyPosition_ == this->historyOffset_)
400            return;
401        unsigned int cursorPosition = this->getCursorPosition();
[6417]402        const std::string& input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning of the inputline until the cursor position
[6105]403        for (unsigned int newPos = this->historyPosition_ + 1; newPos <= this->historyOffset_; newPos++)
[1505]404        {
[6105]405            if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // search case insensitive
406            {
407                this->historyPosition_ = newPos;
408                this->inputBuffer_->set(this->getFromHistory());
409                this->setCursorPosition(cursorPosition);
410                return;
411            }
412        }
413    }
[1505]414
[6105]415    void Shell::historySearchDown()
416    {
417        if (this->historyPosition_ == 0)
418            return;
419        unsigned int cursorPosition = this->getCursorPosition();
[6417]420        const std::string& input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning
[6105]421        for (unsigned int newPos = this->historyPosition_ - 1; newPos > 0; newPos--)
422        {
423            if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // sear$
424            {
425                this->historyPosition_ = newPos;
426                this->inputBuffer_->set(this->getFromHistory());
427                this->setCursorPosition(cursorPosition);
428                return;
429            }
[1505]430        }
431    }
432
[6105]433    void Shell::scrollUp()
[1505]434    {
[6105]435        if (this->scrollIterator_ != this->outputLines_.end())
[1505]436        {
[6105]437            ++this->scrollIterator_;
438            ++this->scrollPosition_;
[1505]439
[6105]440            this->updateListeners<&ShellListener::linesChanged>();
[1505]441        }
442    }
443
[6105]444    void Shell::scrollDown()
[1505]445    {
[6105]446        if (this->scrollIterator_ != this->outputLines_.begin())
[1505]447        {
[6105]448            --this->scrollIterator_;
449            --this->scrollPosition_;
450
451            this->updateListeners<&ShellListener::linesChanged>();
[1505]452        }
453    }
454}
Note: See TracBrowser for help on using the repository browser.