Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_input.cc @ 5239

Last change on this file since 5239 was 5237, checked in by bensch, 19 years ago

orxonox/trunk: implemented an EventFilter, that should filter alt-TAB

File size: 6.9 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: ...
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "shell_input.h"
19
20
21
22#include "shell_command.h"
23#include "shell_completion.h"
24#include "event_handler.h"
25
26#include "debug.h"
27#include "list.h"
28#include "compiler.h"
29#include "stdlibincl.h"
30#include "key_names.h"
31
32
33using namespace std;
34
35SHELL_COMMAND(help, ShellInput, help)
36    ->describe("retrieve some help about the input mode")
37    ->setAlias("help");
38
39/**
40 * standard constructor
41 * @todo this constructor is not jet implemented - do it
42*/
43ShellInput::ShellInput ()
44{
45  this->pressedKey = SDLK_FIRST;
46  this->setClassID(CL_SHELL_INPUT, "ShellInput");
47
48  this->inputLine = new char[1];
49  this->inputLine[0] = '\0';
50  this->inputHistory = new tList<char>;
51  this->delayed = 0;
52  this->setRepeatDelay(.3, .05);
53
54  // subscribe all keyboard commands to ES_SEHLL
55  EventHandler* evh = EventHandler::getInstance();
56  for (int i = 1; i < SDLK_LAST; i++)
57    evh->subscribe(this, ES_SHELL, i);
58
59  this->completion = new ShellCompletion(this);
60}
61
62/**
63 * standard deconstructor
64*/
65ShellInput::~ShellInput ()
66{
67  // delete what has to be deleted here
68  delete[] this->inputLine;
69  delete this->completion;
70
71  tIterator<char>* itH = this->inputHistory->getIterator();
72  char* histEl = itH->firstElement();
73  while (histEl != NULL)
74  {
75    delete[] histEl;
76    histEl = itH->nextElement();
77  }
78  delete itH;
79  delete this->inputHistory;
80}
81
82/**
83 * sets the Repeate-delay and rate
84 * @param repeatDelay the Delay it takes, to repeate a key
85 * @param repeatRate the rate to repeate a pressed key
86 */
87void ShellInput::setRepeatDelay(float repeatDelay, float repeatRate)
88{
89  this->repeatDelay = repeatDelay;
90  this->repeatRate = repeatRate;
91}
92
93/**
94 * deletes the InputLine
95 */
96void ShellInput::flush()
97{
98  if (likely(this->inputLine != NULL))
99  {
100    delete[] this->inputLine;
101  }
102  this->inputLine = new char[1];
103  *this->inputLine = '\0';
104  this->setText(this->inputLine, true);
105}
106
107/**
108 * adds one character to the inputLine
109 * @param character the character to add to the inputLine
110 */
111void ShellInput::addCharacter(char character)
112{
113  char* addCharLine = new char[strlen(this->inputLine)+2];
114
115  sprintf(addCharLine, "%s%c", this->inputLine, character);
116  delete[] this->inputLine;
117  this->inputLine = addCharLine;
118  this->setText(this->inputLine, true);
119}
120
121/**
122 * adds multiple Characters to thr inputLine
123 * @param characters a \\0 terminated char-array to add to the InputLine
124 */
125void ShellInput::addCharacters(const char* characters)
126{
127  char* addCharLine = new char[strlen(this->inputLine)+strlen(characters)+1];
128
129  sprintf(addCharLine, "%s%s", this->inputLine, characters);
130  delete[] this->inputLine;
131  this->inputLine = addCharLine;
132  this->setText(this->inputLine, true);
133}
134
135/**
136 * removes characterCount characters from the InputLine
137 * @param characterCount the count of Characters to remove from the input Line
138 */
139void ShellInput::removeCharacters(unsigned int characterCount)
140{
141  if (strlen(this->inputLine) == 0)
142    return;
143
144  if (characterCount > strlen(this->inputLine))
145    characterCount = strlen(this->inputLine);
146
147  char* removeCharLine = new char[strlen(this->inputLine)-characterCount+1];
148
149  strncpy(removeCharLine, this->inputLine, strlen(this->inputLine)-characterCount);
150  removeCharLine[strlen(this->inputLine)-characterCount] = '\0';
151  delete[] this->inputLine;
152  this->inputLine = removeCharLine;
153  this->setText(this->inputLine, true);
154}
155
156/**
157 * executes the command stored in the inputLine
158 * @return true if the command was commited successfully, false otherwise
159 */
160bool ShellInput::executeCommand()
161{
162  ShellBuffer::addBufferLineStatic("Execute Command: %s\n", this->inputLine);
163
164  char* newCommand = new char[strlen(this->inputLine)+1];
165  strcpy(newCommand, this->inputLine);
166  this->inputHistory->add(newCommand);
167
168  ShellCommandBase::execute(this->inputLine);
169
170  this->flush();
171
172  return false;
173}
174
175
176/**
177 * prints out some nice help about the Shell
178 */
179void ShellInput::help(const char* className, const char* functionName)
180{
181  printf("%s::%s\n", className, functionName);
182
183  if (strlen(className) == 0)
184  {
185    PRINT(0)("Help for the most important Shell-commands\n");
186    PRINT(0)("F1 - HELP; F2 - DEBUG; ` - open/close shell\n");
187    PRINT(0)("input order:\n");
188    PRINT(0)("ClassName [objectName] function [parameter1, [parameter2 ...]]  or\n");
189    PRINT(0)("Alias [parameter]\n");
190    PRINT(0)("- Also try 'help className'");
191  }
192  else if (strlen (className) > 0 && strlen (functionName) == 0)
193  {
194    ShellCommandClass::help(className);
195    //PRINTF(1)("%s::%s\n", className, functionName);
196  }
197}
198
199/**
200 * ticks the ShellInput
201 * @param dt the time passed since the last update
202 */
203void ShellInput::tick(float dt)
204{
205  if (this->delayed > 0.0)
206    this->delayed -= dt;
207  else if (this->pressedKey != SDLK_FIRST )
208  {
209    this->delayed = this->repeatRate;
210    if (this->pressedKey == SDLK_BACKSPACE)
211      this->removeCharacters(1);
212    else if (pressedKey < 127)
213      this->addCharacter(this->pressedKey);
214  }
215}
216
217/**
218 * listens for some event
219 * @param event the Event happened
220 */
221void ShellInput::process(const Event &event)
222{
223  if (event.bPressed)
224  {
225    PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type));
226    if (event.type == SDLK_F1)
227      this->help();
228    else if (event.type == SDLK_F2)
229      this->debug();
230    else if (event.type == SDLK_TAB)
231      this->completion->autoComplete();
232    else if (event.type == SDLK_BACKSPACE)
233    {
234      this->delayed = this->repeatDelay;
235      this->pressedKey = SDLK_BACKSPACE;
236      this->removeCharacters(1);
237    }
238    else if (event.type == SDLK_RETURN)
239      this->executeCommand();
240    /*
241    else if (event.type == SDLK_UP)
242    {
243//      this->flushInputLine();
244    tIterator<char>* iterator = this->commandList->getIterator();
245    char* command = iterator->lastElement();
246    while (command)
247    {
248    if (!strcmp (command, inputLine))
249    {
250    inputLine = iterator->prevElement();
251    return;
252  }
253    command = iterator->prevElement();
254  }
255    inputLine = iterator->lastElement();
256  }
257    */
258    else if (likely(event.type < 127))
259    {
260      Uint8 *keystate = SDL_GetKeyState(NULL);
261      this->delayed = this->repeatDelay;
262      if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] ))
263      {
264        this->pressedKey = event.type-32;
265        this->addCharacter(event.type-32);
266      }
267      else
268      {
269        this->pressedKey = event.type;
270        this->addCharacter(event.type);
271      }
272    }
273  }
274  else // if(!event.bPressed)
275  {
276    if (this->pressedKey == event.type || (this->pressedKey == event.type - 32))
277    {
278      this->pressedKey = SDLK_FIRST;
279      this->delayed = 0.0;
280    }
281  }
282}
Note: See TracBrowser for help on using the repository browser.