/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: ... co-programmer: ... */ //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ #include "shell_input.h" #include "shell_command.h" #include "shell_completion.h" #include "event_handler.h" #include "debug.h" #include "list.h" #include "compiler.h" #include "stdlibincl.h" #include "key_names.h" using namespace std; /** * standard constructor * @todo this constructor is not jet implemented - do it */ ShellInput::ShellInput () { this->pressedKey = SDLK_FIRST; this->inputLine = new char[1]; this->inputLine[0] = '\0'; this->inputHistory = new tList; this->delayed = 0; this->setRepeatDelay(.3, .05); // subscribe all keyboard commands to ES_SEHLL EventHandler* evh = EventHandler::getInstance(); for (int i = 1; i < SDLK_LAST; i++) evh->subscribe(this, ES_SHELL, i); this->completion = new ShellCompletion(this); } /** * standard deconstructor */ ShellInput::~ShellInput () { // delete what has to be deleted here delete[] this->inputLine; delete this->completion; tIterator* itH = this->inputHistory->getIterator(); char* histEl = itH->firstElement(); while (histEl != NULL) { delete[] histEl; histEl = itH->nextElement(); } delete itH; delete this->inputHistory; } /** * sets the Repeate-delay and rate * @param repeatDelay the Delay it takes, to repeate a key * @param repeatRate the rate to repeate a pressed key */ void ShellInput::setRepeatDelay(float repeatDelay, float repeatRate) { this->repeatDelay = repeatDelay; this->repeatRate = repeatRate; } /** * deletes the InputLine */ void ShellInput::flush() { if (likely(this->inputLine != NULL)) { delete[] this->inputLine; } this->inputLine = new char[1]; *this->inputLine = '\0'; this->setText(this->inputLine, true); } /** * adds one character to the inputLine * @param character the character to add to the inputLine */ void ShellInput::addCharacter(char character) { char* addCharLine = new char[strlen(this->inputLine)+2]; sprintf(addCharLine, "%s%c", this->inputLine, character); delete[] this->inputLine; this->inputLine = addCharLine; this->setText(this->inputLine, true); } /** * adds multiple Characters to thr inputLine * @param characters a \\0 terminated char-array to add to the InputLine */ void ShellInput::addCharacters(const char* characters) { char* addCharLine = new char[strlen(this->inputLine)+strlen(characters)+1]; sprintf(addCharLine, "%s%s", this->inputLine, characters); delete[] this->inputLine; this->inputLine = addCharLine; this->setText(this->inputLine, true); } /** * removes characterCount characters from the InputLine * @param characterCount the count of Characters to remove from the input Line */ void ShellInput::removeCharacters(unsigned int characterCount) { if (strlen(this->inputLine) == 0) return; if (characterCount > strlen(this->inputLine)) characterCount = strlen(this->inputLine); char* removeCharLine = new char[strlen(this->inputLine)-characterCount+1]; strncpy(removeCharLine, this->inputLine, strlen(this->inputLine)-characterCount); removeCharLine[strlen(this->inputLine)-characterCount] = '\0'; delete[] this->inputLine; this->inputLine = removeCharLine; this->setText(this->inputLine, true); } /** * executes the command stored in the inputLine * @return true if the command was commited successfully, false otherwise */ bool ShellInput::executeCommand() { ShellBuffer::addBufferLineStatic("Execute Command: %s\n", this->inputLine); char* newCommand = new char[strlen(this->inputLine)+1]; strcpy(newCommand, this->inputLine); this->inputHistory->add(newCommand); ShellCommandBase::execute(this->inputLine); this->flush(); return false; } /** * prints out some nice help about the Shell */ void ShellInput::help() const { PRINT(0)("Help for the most important Shell-commands\n"); PRINT(0)("F1 - HELP; F2 - DEBUG; ` - open/close shell\n"); PRINT(0)("input order:\n"); PRINT(0)("ClassName::objectName function [parameter1, [parameter2 ...]] or\n"); PRINT(0)("Command [parameter]\n"); } void ShellInput::tick(float dt) { if (this->delayed > 0.0) this->delayed -= dt; else if (this->pressedKey != SDLK_FIRST ) { this->delayed = this->repeatRate; if (this->pressedKey == SDLK_BACKSPACE) this->removeCharacters(1); else if (pressedKey < 127) this->addCharacter(this->pressedKey); } } /** * listens for some event * @param event the Event happened */ void ShellInput::process(const Event &event) { if (event.bPressed) { PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type)); if (event.type == SDLK_F1) this->help(); else if (event.type == SDLK_F2) this->debug(); else if (event.type == SDLK_TAB) this->completion->autoComplete(); else if (event.type == SDLK_BACKSPACE) { this->delayed = this->repeatDelay; this->pressedKey = SDLK_BACKSPACE; this->removeCharacters(1); } else if (event.type == SDLK_RETURN) this->executeCommand(); /* else if (event.type == SDLK_UP) { // this->flushInputLine(); tIterator* iterator = this->commandList->getIterator(); char* command = iterator->lastElement(); while (command) { if (!strcmp (command, inputLine)) { inputLine = iterator->prevElement(); return; } command = iterator->prevElement(); } inputLine = iterator->lastElement(); } */ else if (likely(event.type < 127)) { Uint8 *keystate = SDL_GetKeyState(NULL); this->delayed = this->repeatDelay; if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] )) { this->pressedKey = event.type-32; this->addCharacter(event.type-32); } else { this->pressedKey = event.type; this->addCharacter(event.type); } } } else // if(!event.bPressed) { if (this->pressedKey == event.type || (this->pressedKey == event.type - 32)) { this->pressedKey = SDLK_FIRST; this->delayed = 0.0; } } }