ManaPlus
Public Member Functions | Static Public Member Functions | Private Attributes
KeyboardConfig Class Reference

#include <keyboardconfig.h>

Public Member Functions

 KeyboardConfig ()
 
void init ()
 
void deinit ()
 
bool isEnabled () const
 
void setEnabled (const bool flag)
 
void refreshActiveKeys ()
 
KeysVectorgetActionVector (const SDL_Event &event)
 
KeysVectorgetActionVectorByKey (const int i)
 
bool isActionActive (const InputActionT index) const
 
void update ()
 
void handleActivateKey (const SDL_Event &event)
 
void handleActivateKey (const int key)
 
void handleDeActicateKey (const SDL_Event &event)
 
void handleDeActicateKey (const int key)
 
InputActionT getActionId (const SDL_Event &event)
 
void handleRepeat (const int time)
 
void resetRepeat (const int key)
 

Static Public Member Functions

static InputActionT getKeyIndex (const SDL_Event &event, const int grp)
 
static std::string getKeyShortString (const std::string &key)
 
static SDLKey getKeyFromEvent (const SDL_Event &event)
 
static int getKeyValueFromEvent (const SDL_Event &event)
 
static std::string getKeyName (const int key)
 

Private Attributes

bool mEnabled
 
const uint8_t * mActiveKeys
 
uint8_t * mActiveKeys2
 
unsigned int mRepeatTime
 
KeyToActionMap mKeyToAction
 
KeyToIdMap mKeyToId
 
KeyTimeMap mKeyTimeMap
 
bool mBlockAltTab
 

Detailed Description

Definition at line 42 of file keyboardconfig.h.

Constructor & Destructor Documentation

◆ KeyboardConfig()

KeyboardConfig::KeyboardConfig ( )

Definition at line 42 of file keyboardconfig.cpp.

42  :
43  mEnabled(true),
44  mActiveKeys(nullptr),
45  mActiveKeys2(nullptr),
46  mRepeatTime(0),
47  mKeyToAction(),
48  mKeyToId(),
49  mKeyTimeMap(),
50  mBlockAltTab(true)
51 {
52 }
KeyToActionMap mKeyToAction
KeyToIdMap mKeyToId
KeyTimeMap mKeyTimeMap
const uint8_t * mActiveKeys
uint8_t * mActiveKeys2
unsigned int mRepeatTime

Member Function Documentation

◆ deinit()

void KeyboardConfig::deinit ( )

Definition at line 63 of file keyboardconfig.cpp.

64 {
65  delete [] mActiveKeys2;
66  mActiveKeys2 = nullptr;
67 }

References mActiveKeys2.

Referenced by Client::gameClear().

◆ getActionId()

InputActionT KeyboardConfig::getActionId ( const SDL_Event &  event)

Definition at line 162 of file keyboardconfig.cpp.

163 {
164  const int i = getKeyValueFromEvent(event);
165 // logger->log("getActionId: %d", i);
166  if (i != 0 && i < SDLK_LAST && mKeyToId.find(i) != mKeyToId.end())
167  return mKeyToId[i];
168  return InputAction::NO_VALUE;
169 }
static int getKeyValueFromEvent(const SDL_Event &event)

References getKeyValueFromEvent(), mKeyToId, and InputAction::NO_VALUE.

Referenced by InputManager::getActionByKey().

◆ getActionVector()

KeysVector * KeyboardConfig::getActionVector ( const SDL_Event &  event)

Definition at line 145 of file keyboardconfig.cpp.

146 {
147  const int i = getKeyValueFromEvent(event);
148 // logger->log("key triggerAction: %d", i);
149  if (i != 0 && i < SDLK_LAST && mKeyToAction.find(i) != mKeyToAction.end())
150  return &mKeyToAction[i];
151  return nullptr;
152 }

References getKeyValueFromEvent(), and mKeyToAction.

Referenced by InputManager::handleEvent().

◆ getActionVectorByKey()

KeysVector * KeyboardConfig::getActionVectorByKey ( const int  i)

Definition at line 154 of file keyboardconfig.cpp.

155 {
156 // logger->log("key triggerAction: %d", i);
157  if (i != 0 && i < SDLK_LAST && mKeyToAction.find(i) != mKeyToAction.end())
158  return &mKeyToAction[i];
159  return nullptr;
160 }

References mKeyToAction.

Referenced by handleRepeat().

◆ getKeyFromEvent()

static SDLKey KeyboardConfig::getKeyFromEvent ( const SDL_Event &  event)
inlinestatic

Definition at line 82 of file keyboardconfig.h.

84  {
85 #ifdef USE_SDL2
86  return event.key.keysym.scancode;
87 #else // USE_SDL2
88 
89  return event.key.keysym.sym;
90 #endif // USE_SDL2
91  }

◆ getKeyIndex()

InputActionT KeyboardConfig::getKeyIndex ( const SDL_Event &  event,
const int  grp 
)
static

Get the key function index by providing the keys value.

Definition at line 69 of file keyboardconfig.cpp.

70 {
71  const int keyValue = getKeyValueFromEvent(event);
72  return inputManager.getKeyIndex(keyValue, grp, InputType::KEYBOARD);
73 }
InputActionT getKeyIndex(const int value, const int grp, const InputTypeT type) const
InputManager inputManager
@ KEYBOARD
Definition: inputtype.h:38

References InputManager::getKeyIndex(), getKeyValueFromEvent(), inputManager, and InputType::KEYBOARD.

Referenced by Game::updateHistory().

◆ getKeyName()

std::string KeyboardConfig::getKeyName ( const int  key)
static

Definition at line 80 of file keyboardconfig.cpp.

81 {
82  if (key == -1)
83  return "";
84  if (key > -1)
85  {
86 #ifdef USE_SDL2
87  return SDL_GetScancodeName(
88  static_cast<SDL_Scancode>(key));
89 #else // USE_SDL2
90 
91  return SDL_GetKeyName(static_cast<SDLKey>(key));
92 #endif // USE_SDL2
93  }
94 
95  // TRANSLATORS: long key name, should be short
96  return strprintf(_("key_%d"), CAST_S32(key));
97 }
#define CAST_S32
Definition: cast.h:30
#define _(s)
Definition: gettext.h:35
std::string strprintf(const char *const format,...)

References _, CAST_S32, and strprintf().

Referenced by InputManager::getKeyStringLong(), and InputManager::updateKeyString().

◆ getKeyShortString()

std::string KeyboardConfig::getKeyShortString ( const std::string &  key)
static

Definition at line 99 of file keyboardconfig.cpp.

100 {
101  if (key == "backspace")
102  return "bksp";
103  else if (key == "numlock")
104  return "numlock";
105  else if (key == "caps lock")
106  return "caplock";
107  else if (key == "scroll lock")
108  return "scrlock";
109  else if (key == "right shift")
110  return "rshift";
111  else if (key == "left shift")
112  return "lshift";
113  else if (key == "right ctrl")
114  return "rctrl";
115  else if (key == "left ctrl")
116  return "lctrl";
117  else if (key == "right alt")
118  return "ralt";
119  else if (key == "left alt")
120  return "lalt";
121  else if (key == "right meta")
122  return "rmeta";
123  else if (key == "left meta")
124  return "lmeta";
125  else if (key == "right super")
126  return "rsuper";
127  else if (key == "left super")
128  return "lsuper";
129  else if (key == "print screen")
130  return "print screen";
131  else if (key == "page up")
132  return "pg up";
133  else if (key == "page down")
134  return "pg down";
135 
136  if (key == "unknown key")
137  {
138  // TRANSLATORS: Unknown key short string.
139  // TRANSLATORS: This string must be maximum 5 chars
140  return _("u key");
141  }
142  return key;
143 }

References _.

Referenced by EAthena::BeingRecv::processBeingSpawn(), EAthena::BeingRecv::processBeingVisible(), TmwAthena::BeingRecv::processBeingVisible(), LocalPlayer::setHome(), InputManager::updateKeyString(), and SocialNavigationTab::updateList().

◆ getKeyValueFromEvent()

static int KeyboardConfig::getKeyValueFromEvent ( const SDL_Event &  event)
inlinestatic

Definition at line 93 of file keyboardconfig.h.

95  {
96 #ifdef USE_SDL2
97  return event.key.keysym.scancode;
98 #else // USE_SDL2
99 
100  if (event.key.keysym.sym != 0U)
101  return CAST_S32(event.key.keysym.sym);
102  else if (event.key.keysym.scancode > 1)
103  return -event.key.keysym.scancode;
104  return 0;
105 #endif // USE_SDL2
106  }

References CAST_S32.

Referenced by getActionId(), getActionVector(), getKeyIndex(), handleActivateKey(), handleDeActicateKey(), and InputManager::setNewKey().

◆ handleActivateKey() [1/2]

void KeyboardConfig::handleActivateKey ( const int  key)

Definition at line 214 of file keyboardconfig.cpp.

215 {
216  if (mActiveKeys2 == nullptr)
217  return;
218  if (key < -1 && key > -500)
219  mActiveKeys2[-key] = 1;
220  resetRepeat(key);
221 }
void resetRepeat(const int key)

References mActiveKeys2, and resetRepeat().

◆ handleActivateKey() [2/2]

void KeyboardConfig::handleActivateKey ( const SDL_Event &  event)

Definition at line 204 of file keyboardconfig.cpp.

205 {
206  if (mActiveKeys2 == nullptr)
207  return;
208  const int key = getKeyValueFromEvent(event);
209  if (key < -1 && key > -500)
210  mActiveKeys2[-key] = 1;
211  resetRepeat(key);
212 }

References getKeyValueFromEvent(), mActiveKeys2, and resetRepeat().

Referenced by InputManager::handleEvent().

◆ handleDeActicateKey() [1/2]

void KeyboardConfig::handleDeActicateKey ( const int  key)

Definition at line 233 of file keyboardconfig.cpp.

234 {
235  if (mActiveKeys2 == nullptr)
236  return;
237  if (key < -1 && key > -500)
238  mActiveKeys2[-key] = 0;
239  resetRepeat(key);
240 }

References mActiveKeys2, and resetRepeat().

◆ handleDeActicateKey() [2/2]

void KeyboardConfig::handleDeActicateKey ( const SDL_Event &  event)

Definition at line 223 of file keyboardconfig.cpp.

224 {
225  if (mActiveKeys2 == nullptr)
226  return;
227  const int key = getKeyValueFromEvent(event);
228  if (key < -1 && key > -500)
229  mActiveKeys2[-key] = 0;
230  resetRepeat(key);
231 }

References getKeyValueFromEvent(), mActiveKeys2, and resetRepeat().

Referenced by InputManager::handleEvent().

◆ handleRepeat()

void KeyboardConfig::handleRepeat ( const int  time)

Definition at line 242 of file keyboardconfig.cpp.

243 {
244  BLOCK_START("KeyboardConfig::handleRepeat")
246  {
247  bool repeat(false);
248  const int key = (*it).first;
249  if (key >= 0)
250  {
251  if ((mActiveKeys != nullptr) && (mActiveKeys[key] != 0U))
252  repeat = true;
253  }
254  else if (key < -1 && key > -500)
255  {
256  if ((mActiveKeys2 != nullptr) && (mActiveKeys2[-key] != 0U))
257  repeat = true;
258  }
259  if (repeat)
260  {
261  int &keyTime = (*it).second;
262  if (time > keyTime && abs(time - keyTime)
264  {
265  keyTime = time;
267  }
268  }
269  }
270  BLOCK_END("KeyboardConfig::handleRepeat")
271 }
bool triggerAction(const KeysVector *const ptrs)
KeysVector * getActionVectorByKey(const int i)
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
KeyTimeMap::iterator KeyTimeMapIter
Definition: inputevent.h:47
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79

References BLOCK_END, BLOCK_START, CAST_S32, FOR_EACH, getActionVectorByKey(), inputManager, mActiveKeys, mActiveKeys2, mKeyTimeMap, mRepeatTime, and InputManager::triggerAction().

Referenced by InputManager::handleRepeat().

◆ init()

void KeyboardConfig::init ( )

Initializes the keyboard config explicitly.

Definition at line 54 of file keyboardconfig.cpp.

55 {
56  mEnabled = true;
57  delete [] mActiveKeys2;
58  mActiveKeys2 = new uint8_t[500];
59  mRepeatTime = config.getIntValue("repeateInterval2") / 10;
60  mBlockAltTab = config.getBoolValue("blockAltTab");
61 }
bool getBoolValue(const std::string &key) const
int getIntValue(const std::string &key) const
Configuration config

References config, Configuration::getBoolValue(), Configuration::getIntValue(), mActiveKeys2, mBlockAltTab, mEnabled, and mRepeatTime.

Referenced by Client::gameInit().

◆ isActionActive()

bool KeyboardConfig::isActionActive ( const InputActionT  index) const

Definition at line 171 of file keyboardconfig.cpp.

172 {
173  if ((mActiveKeys == nullptr) || (mActiveKeys2 == nullptr))
174  return false;
175 
176  const InputFunction &key = inputManager.getKey(index);
177  for (size_t i = 0; i < inputFunctionSize; i ++)
178  {
179  const InputItem &val = key.values[i];
180  if (val.type != InputType::KEYBOARD)
181  continue;
182 
183  const int value = val.value;
184  if (value >= 0)
185  {
186  if (mActiveKeys[value] != 0U)
187  return true;
188  }
189  else if (value < -1 && value > -500)
190  {
191  if (mActiveKeys2[-value] != 0U)
192  return true;
193  }
194  }
195  return false;
196 }
InputFunction & getKey(InputActionT index) A_CONST
const size_t inputFunctionSize
Definition: inputfunction.h:29
InputItem values[inputFunctionSize]
Definition: inputfunction.h:35
int value
Definition: inputitem.h:45
InputTypeT type
Definition: inputitem.h:44

References InputManager::getKey(), inputFunctionSize, inputManager, InputType::KEYBOARD, mActiveKeys, mActiveKeys2, InputItem::type, InputItem::value, and InputFunction::values.

Referenced by InputManager::isActionActive0().

◆ isEnabled()

bool KeyboardConfig::isEnabled ( ) const
inline

Get the enable flag, which will stop the user from doing actions.

Definition at line 59 of file keyboardconfig.h.

60  { return mEnabled; }

References mEnabled.

Referenced by Game::handleInput(), and InputManager::updateConditionMask().

◆ refreshActiveKeys()

void KeyboardConfig::refreshActiveKeys ( )

Takes a snapshot of all the active keys.

Definition at line 75 of file keyboardconfig.cpp.

76 {
77  mActiveKeys = SDL_GetKeyState(nullptr);
78 }

References mActiveKeys.

Referenced by InputManager::handleEvent().

◆ resetRepeat()

void KeyboardConfig::resetRepeat ( const int  key)

Definition at line 273 of file keyboardconfig.cpp.

274 {
275  const KeyTimeMapIter it = mKeyTimeMap.find(key);
276  if (it != mKeyTimeMap.end())
277  (*it).second = tick_time;
278 }
volatile int tick_time
Definition: timer.cpp:53

References mKeyTimeMap, and tick_time.

Referenced by handleActivateKey(), and handleDeActicateKey().

◆ setEnabled()

void KeyboardConfig::setEnabled ( const bool  flag)
inline

◆ update()

void KeyboardConfig::update ( )

Definition at line 198 of file keyboardconfig.cpp.

199 {
202 }
void updateKeyActionMap(KeyToActionMap &actionMap, KeyToIdMap &idMap, KeyTimeMap &keyTimeMap, const InputTypeT type) const

References inputManager, InputType::KEYBOARD, mKeyTimeMap, mKeyToAction, mKeyToId, and InputManager::updateKeyActionMap().

Referenced by Client::gameInit(), initEngines(), and InputManager::update().

Field Documentation

◆ mActiveKeys

const uint8_t* KeyboardConfig::mActiveKeys
private

Stores a list of all the keys

Definition at line 140 of file keyboardconfig.h.

Referenced by handleRepeat(), isActionActive(), and refreshActiveKeys().

◆ mActiveKeys2

uint8_t* KeyboardConfig::mActiveKeys2
private

Stores a list of all the keys

Definition at line 142 of file keyboardconfig.h.

Referenced by deinit(), handleActivateKey(), handleDeActicateKey(), handleRepeat(), init(), and isActionActive().

◆ mBlockAltTab

bool KeyboardConfig::mBlockAltTab
private

Definition at line 152 of file keyboardconfig.h.

Referenced by init().

◆ mEnabled

bool KeyboardConfig::mEnabled
private

Flag to respond to key input

Definition at line 138 of file keyboardconfig.h.

Referenced by init(), isEnabled(), and setEnabled().

◆ mKeyTimeMap

KeyTimeMap KeyboardConfig::mKeyTimeMap
private

Definition at line 150 of file keyboardconfig.h.

Referenced by handleRepeat(), resetRepeat(), and update().

◆ mKeyToAction

KeyToActionMap KeyboardConfig::mKeyToAction
private

Definition at line 146 of file keyboardconfig.h.

Referenced by getActionVector(), getActionVectorByKey(), and update().

◆ mKeyToId

KeyToIdMap KeyboardConfig::mKeyToId
private

Definition at line 148 of file keyboardconfig.h.

Referenced by getActionId(), and update().

◆ mRepeatTime

unsigned int KeyboardConfig::mRepeatTime
private

Definition at line 144 of file keyboardconfig.h.

Referenced by handleRepeat(), and init().


The documentation for this class was generated from the following files: