From 250acd4c8d345301c1b7e831c3c0f485c77d8ac7 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 21 Jan 2009 02:02:55 +0000 Subject: more cleanup svn-id: r35971 --- backends/keymapper/hardware-key.h | 24 ++++++++++++------------ backends/keymapper/keymap.cpp | 15 +++++++-------- backends/keymapper/keymapper.cpp | 36 ++++++++++++++++++------------------ backends/keymapper/keymapper.h | 12 +++++++----- backends/keymapper/remap-dialog.cpp | 4 ++-- 5 files changed, 46 insertions(+), 45 deletions(-) (limited to 'backends') diff --git a/backends/keymapper/hardware-key.h b/backends/keymapper/hardware-key.h index 55cdf3089c..c89c395d33 100644 --- a/backends/keymapper/hardware-key.h +++ b/backends/keymapper/hardware-key.h @@ -36,14 +36,17 @@ namespace Common { #define HWKEY_ID_SIZE (4) + /** * Describes an available hardware key */ struct HardwareKey { /** unique id used for saving/loading to config */ - char id[HWKEY_ID_SIZE]; + char hwKeyId[HWKEY_ID_SIZE]; + /** Human readable description */ String description; + /** * The KeyState that is generated by the back-end * when this hardware key is pressed. @@ -57,20 +60,19 @@ struct HardwareKey { KeyType typ = kGenericKeyType, ActionType prefAct = kGenericActionType) : key(ky), description(desc), type(typ), preferredAction(prefAct) { assert(i); - strncpy(id, i, HWKEY_ID_SIZE); + strncpy(hwKeyId, i, HWKEY_ID_SIZE); } }; /** * Simple class to encapsulate a device's set of HardwareKeys. - * Each device should extend this and call addHardwareKey a number of times + * Each device should instantiate this and call addHardwareKey a number of times * in its constructor to define the device's available keys. */ class HardwareKeySet { public: - HardwareKeySet() : _count(0) {} virtual ~HardwareKeySet() { List::iterator it; for (it = _keys.begin(); it != _keys.end(); it++) @@ -80,13 +82,12 @@ public: void addHardwareKey(HardwareKey *key) { checkForKey(key); _keys.push_back(key); - ++_count; } const HardwareKey *findHardwareKey(const char *id) const { List::iterator it; for (it = _keys.begin(); it != _keys.end(); it++) { - if (strncmp((*it)->id, id, HWKEY_ID_SIZE) == 0) + if (strncmp((*it)->hwKeyId, id, HWKEY_ID_SIZE) == 0) return (*it); } return 0; @@ -101,12 +102,12 @@ public: return 0; } - List getHardwareKeys() const { + const List &getHardwareKeys() const { return _keys; } - uint count() const { - return _count; + uint size() const { + return _keys.size(); } @@ -115,15 +116,14 @@ private: void checkForKey(HardwareKey *key) { List::iterator it; for (it = _keys.begin(); it != _keys.end(); it++) { - if (strncmp((*it)->id, key->id, HWKEY_ID_SIZE) == 0) - error("Error adding HardwareKey '%s' - id of %s already in use!", key->description.c_str(), key->id); + if (strncmp((*it)->hwKeyId, key->hwKeyId, HWKEY_ID_SIZE) == 0) + error("Error adding HardwareKey '%s' - id of %s already in use!", key->description.c_str(), key->hwKeyId); else if ((*it)->key == key->key) error("Error adding HardwareKey '%s' - key already in use!", key->description.c_str()); } } List _keys; - uint _count; }; diff --git a/backends/keymapper/keymap.cpp b/backends/keymapper/keymap.cpp index c2da8334f9..4cb6b7f518 100644 --- a/backends/keymapper/keymap.cpp +++ b/backends/keymapper/keymap.cpp @@ -139,21 +139,20 @@ void Keymap::loadMappings(const HardwareKeySet *hwKeys) { } void Keymap::saveMappings() { - if (!_configDomain) return; + if (!_configDomain) + return; List::const_iterator it; String prefix = KEYMAP_KEY_PREFIX + _name + "_"; for (it = _actions.begin(); it != _actions.end(); it++) { uint actIdLen = strlen((*it)->id); actIdLen = (actIdLen > ACTION_ID_SIZE) ? ACTION_ID_SIZE : actIdLen; String actId((*it)->id, (*it)->id + actIdLen); + char hwId[HWKEY_ID_SIZE+1]; + memset(hwId, 0, HWKEY_ID_SIZE+1); if ((*it)->getMappedKey()) { - uint hwIdLen = strlen((*it)->getMappedKey()->id); - hwIdLen = (hwIdLen > HWKEY_ID_SIZE) ? HWKEY_ID_SIZE : hwIdLen; - String hwId((*it)->getMappedKey()->id, (*it)->getMappedKey()->id + hwIdLen); - _configDomain->setVal(prefix + actId, hwId); - } else { - _configDomain->setVal(prefix + actId, ""); + memcpy(hwId, (*it)->getMappedKey()->hwKeyId, HWKEY_ID_SIZE); } + _configDomain->setVal(prefix + actId, hwId); } } @@ -168,7 +167,7 @@ bool Keymap::isComplete(const HardwareKeySet *hwKeys) { allMapped = false; } } - return allMapped || (numberMapped == hwKeys->count()); + return allMapped || (numberMapped == hwKeys->size()); } // TODO: diff --git a/backends/keymapper/keymapper.cpp b/backends/keymapper/keymapper.cpp index 500c2b031b..298ec721cd 100644 --- a/backends/keymapper/keymapper.cpp +++ b/backends/keymapper/keymapper.cpp @@ -68,34 +68,31 @@ void Keymapper::registerHardwareKeySet(HardwareKeySet *keys) { } void Keymapper::addGlobalKeymap(Keymap *keymap) { - initKeymap(_globalDomain.getConfigDomain(), keymap); - _globalDomain.addKeymap(keymap); + initKeymap(_globalDomain, keymap); } -void Keymapper::refreshGameDomain() { +void Keymapper::addGameKeymap(Keymap *keymap) { + if (ConfMan.getActiveDomain() == 0) + error("Call to Keymapper::addGameKeymap when no game loaded"); + + // Detect whether the active game changed since last call. + // If so, flush the game key configuration. if (_gameDomain.getConfigDomain() != ConfMan.getActiveDomain()) { cleanupGameKeymaps(); _gameDomain.setConfigDomain(ConfMan.getActiveDomain()); } + initKeymap(_gameDomain, keymap); } -void Keymapper::addGameKeymap(Keymap *keymap) { - if (ConfMan.getActiveDomain() == 0) - error("Call to Keymapper::addGameKeymap when no game loaded"); - - refreshGameDomain(); - initKeymap(_gameDomain.getConfigDomain(), keymap); - _gameDomain.addKeymap(keymap); -} - -void Keymapper::initKeymap(ConfigManager::Domain *domain, Keymap *map) { - map->setConfigDomain(domain); +void Keymapper::initKeymap(Domain &domain, Keymap *map) { + map->setConfigDomain(domain.getConfigDomain()); map->loadMappings(_hardwareKeys); if (map->isComplete(_hardwareKeys) == false) { map->automaticMapping(_hardwareKeys); map->saveMappings(); ConfMan.flushToDisk(); } + domain.addKeymap(map); } void Keymapper::cleanupGameKeymaps() { @@ -157,9 +154,11 @@ bool Keymapper::mapKey(const KeyState& key, bool keyDown) { for (int i = _activeMaps.size() - 1; i >= 0; --i) { MapRecord mr = _activeMaps[i]; action = mr.keymap->getMappedAction(key); - if (action || mr.inherit == false) break; + if (action || mr.inherit == false) + break; } - if (action) _keysDown[key] = action; + if (action) + _keysDown[key] = action; } else { HashMap::iterator it = _keysDown.find(key); if (it != _keysDown.end()) { @@ -167,7 +166,8 @@ bool Keymapper::mapKey(const KeyState& key, bool keyDown) { _keysDown.erase(key); } } - if (!action) return false; + if (!action) + return false; executeAction(action, keyDown); return true; } @@ -215,7 +215,7 @@ void Keymapper::executeAction(const Action *action, bool keyDown) { } } -const HardwareKey *Keymapper::getHardwareKey(const KeyState& key) { +const HardwareKey *Keymapper::findHardwareKey(const KeyState& key) { return (_hardwareKeys) ? _hardwareKeys->findHardwareKey(key) : 0; } diff --git a/backends/keymapper/keymapper.h b/backends/keymapper/keymapper.h index 3c6cd5bc36..8a7ff5f3fc 100644 --- a/backends/keymapper/keymapper.h +++ b/backends/keymapper/keymapper.h @@ -84,9 +84,12 @@ public: void registerHardwareKeySet(HardwareKeySet *keys); /** - * Get the HardwareKeySet that is registered with the Keymapper + * Get a list of all registered HardwareKeys */ - HardwareKeySet *getHardwareKeySet() { return _hardwareKeys; } + const List &getHardwareKeys() const { + assert(_hardwareKeys); + return _hardwareKeys->getHardwareKeys(); + } /** * Add a keymap to the global domain. @@ -161,7 +164,7 @@ public: /** * Return a HardwareKey pointer for the given key state */ - const HardwareKey *getHardwareKey(const KeyState& key); + const HardwareKey *findHardwareKey(const KeyState& key); Domain& getGlobalDomain() { return _globalDomain; } Domain& getGameDomain() { return _gameDomain; } @@ -169,8 +172,7 @@ public: private: - void initKeymap(ConfigManager::Domain *domain, Keymap *keymap); - void refreshGameDomain(); + void initKeymap(Domain &domain, Keymap *keymap); Domain _globalDomain; Domain _gameDomain; diff --git a/backends/keymapper/remap-dialog.cpp b/backends/keymapper/remap-dialog.cpp index 2c40904be9..6f8097486e 100644 --- a/backends/keymapper/remap-dialog.cpp +++ b/backends/keymapper/remap-dialog.cpp @@ -205,7 +205,7 @@ void RemapDialog::stopRemapping() { void RemapDialog::handleKeyUp(Common::KeyState state) { if (_activeRemapAction) { - const HardwareKey *hwkey = _keymapper->getHardwareKey(state); + const HardwareKey *hwkey = _keymapper->findHardwareKey(state); if (hwkey) { _activeRemapAction->mapKey(hwkey); // TODO: _activeRemapAction->getParent()->saveMappings(); @@ -235,7 +235,7 @@ void RemapDialog::loadKeymap() { if (_activeKeymaps->size() > 0 && _kmPopUp->getSelected() == 0) { // load active keymaps - List freeKeys (_keymapper->getHardwareKeySet()->getHardwareKeys()); + List freeKeys (_keymapper->getHardwareKeys()); // add most active keymap's keys Keymapper::MapRecord top = _activeKeymaps->top(); -- cgit v1.2.3