diff options
Diffstat (limited to 'backends/common')
-rw-r--r-- | backends/common/keymap-manager.cpp | 67 | ||||
-rw-r--r-- | backends/common/keymap-manager.h | 3 | ||||
-rw-r--r-- | backends/common/keymap.cpp | 54 | ||||
-rw-r--r-- | backends/common/keymap.h | 21 |
4 files changed, 78 insertions, 67 deletions
diff --git a/backends/common/keymap-manager.cpp b/backends/common/keymap-manager.cpp index 81a42507c5..d2d6fa223e 100644 --- a/backends/common/keymap-manager.cpp +++ b/backends/common/keymap-manager.cpp @@ -42,8 +42,10 @@ void KeymapManager::Domain::addKeymap(const String& name, Keymap *map) { void KeymapManager::Domain::deleteAllKeyMaps() { KeymapMap::iterator it; - for (it = _keymaps.begin(); it != _keymaps.end(); it++) + for (it = _keymaps.begin(); it != _keymaps.end(); it++) { + //it->_value->saveMappings() delete it->_value; + } _keymaps.clear(); delete _defaultKeymap; } @@ -109,52 +111,11 @@ void KeymapManager::registerGameKeymap(const String& name, Keymap *map) { void KeymapManager::initKeymap(ConfigManager::Domain *domain, const String& name, Keymap *map) { - if (!loadKeymap(domain, name, map)) + map->loadMappings(domain, name, _hardwareKeys); + if (isMapComplete(map) == false) automaticMap(map); } -bool KeymapManager::loadKeymap(ConfigManager::Domain *domain, - const String& name, - Keymap *map) { - ConfigManager::Domain::iterator it; - String prefix = "km_" + name + "_"; - for (it = domain->begin(); it != domain->end(); it++) { - const String& key = it->_key; - if (!key.hasPrefix(prefix.c_str())) - continue; - - // parse Action ID - const char *actionIdStart = key.c_str() + prefix.size(); - char *err; - int32 actionId = (int32) strtol(actionIdStart, &err, 0); - if (err == actionIdStart) { - warning("'%s' is not a valid Action ID", err); - continue; - } - Action *ua = map->getAction(actionId); - if (!ua) { - warning("'%s' keymap does not contain Action with ID %d", - name.c_str(), (int)actionId); - continue; - } - - // parse HardwareKey ID - int32 hwKeyId = (int32) strtol(it->_value.c_str(), &err, 0); - if (err == it->_value.c_str()) { - warning("'%s' is not a valid HardwareKey ID", err); - continue; - } - const HardwareKey *hwKey = _hardwareKeys->findHardwareKey(hwKeyId); - if (!hwKey) { - warning("HardwareKey with ID %d not known", (int)hwKeyId); - continue; - } - - ua->mapKey(hwKey); - } - return isMapComplete(map); -} - bool KeymapManager::isMapComplete(const Keymap *map) { const List<Action*>& actions = map->getActions(); List<Action*>::const_iterator it; @@ -170,24 +131,6 @@ bool KeymapManager::isMapComplete(const Keymap *map) { return allMapped || (numberMapped == _hardwareKeys->count()); } -void KeymapManager::saveKeymap(ConfigManager::Domain *domain, - const String& name, - const Keymap *map) { - const List<Action*>& actions = map->getActions(); - List<Action*>::const_iterator it; - char buf[11]; - for (it = actions.begin(); it != actions.end(); it++) { - String key("km_"); - sprintf(buf, "%d", (*it)->id); - key += name + "_" + buf; - if ((*it)->getMappedKey()) - sprintf(buf, "%d", (*it)->getMappedKey()->id); - else - strcpy(buf, ""); - domain->setVal(key, buf); - } -} - void KeymapManager::automaticMap(Keymap *map) { List<Action*> actions(map->getActions()), unmapped; List<Action*>::iterator actIt; diff --git a/backends/common/keymap-manager.h b/backends/common/keymap-manager.h index a911bc8d61..b8ab1c7998 100644 --- a/backends/common/keymap-manager.h +++ b/backends/common/keymap-manager.h @@ -28,7 +28,6 @@ #include "backends/common/hardware-key.h" #include "backends/common/keymap.h" -#include "common/config-manager.h" #include "common/hash-str.h" #include "common/hashmap.h" @@ -76,8 +75,6 @@ public: private: void initKeymap(ConfigManager::Domain *domain, const String& name, Keymap *keymap); - bool loadKeymap(ConfigManager::Domain *domain, const String& name, Keymap *keymap); - void saveKeymap(ConfigManager::Domain *domain, const String& name, const Keymap *keymap); void automaticMap(Keymap *map); bool isMapComplete(const Keymap *map); diff --git a/backends/common/keymap.cpp b/backends/common/keymap.cpp index 09ccc14c1d..72457d35a4 100644 --- a/backends/common/keymap.cpp +++ b/backends/common/keymap.cpp @@ -92,4 +92,58 @@ Action *Keymap::getMappedAction(const KeyState& ks) const { return it->_value; } +void Keymap::loadMappings(ConfigManager::Domain *domain, const String& name, const HardwareKeySet *hwKeys) { + ConfigManager::Domain::iterator it; + String prefix = "km_" + name + "_"; + for (it = domain->begin(); it != domain->end(); it++) { + const String& key = it->_key; + if (!key.hasPrefix(prefix.c_str())) + continue; + + // parse Action ID + const char *actionIdStart = key.c_str() + prefix.size(); + char *err; + int32 actionId = (int32) strtol(actionIdStart, &err, 0); + if (err == actionIdStart) { + warning("'%s' is not a valid Action ID", err); + continue; + } + Action *ua = getAction(actionId); + if (!ua) { + warning("'%s' keymap does not contain Action with ID %d", + name.c_str(), (int)actionId); + continue; + } + + // parse HardwareKey ID + int32 hwKeyId = (int32) strtol(it->_value.c_str(), &err, 0); + if (err == it->_value.c_str()) { + warning("'%s' is not a valid HardwareKey ID", err); + continue; + } + const HardwareKey *hwKey = hwKeys->findHardwareKey(hwKeyId); + if (!hwKey) { + warning("HardwareKey with ID %d not known", (int)hwKeyId); + continue; + } + + ua->mapKey(hwKey); + } +} + +void Keymap::saveMappings(ConfigManager::Domain *domain, const String& name) { + List<Action*>::const_iterator it; + char buf[11]; + for (it = _actions.begin(); it != _actions.end(); it++) { + String key("km_"); + sprintf(buf, "%d", (*it)->id); + key += name + "_" + buf; + if ((*it)->getMappedKey()) + sprintf(buf, "%d", (*it)->getMappedKey()->id); + else + strcpy(buf, ""); + domain->setVal(key, buf); + } +} + } // end of namespace Common diff --git a/backends/common/keymap.h b/backends/common/keymap.h index c8b46cfd78..55e5c598d8 100644 --- a/backends/common/keymap.h +++ b/backends/common/keymap.h @@ -26,6 +26,7 @@ #ifndef COMMON_KEYMAP #define COMMON_KEYMAP +#include "common/config-manager.h" #include "common/func.h" #include "common/hashmap.h" #include "common/keyboard.h" @@ -35,6 +36,7 @@ namespace Common { struct HardwareKey; +class HardwareKeySet; /** * Hash function for KeyState @@ -74,11 +76,26 @@ public: /** * Find the Action that a key is mapped to - * @param key the key that is mapped to the required Action - * @return a pointer to the Action or 0 if no + * @param key the key that is mapped to the required Action + * @return a pointer to the Action or 0 if no */ Action *getMappedAction(const KeyState& ks) const; + /** + * Load this keymap's mappings from the given config domain and hardware key set + * @param domain config domain to load keymap from + * @param name name of the keymap to load + * @param hwKeys the set to retrieve hardware key pointers from + */ + void loadMappings(ConfigManager::Domain *domain, const String& name, const HardwareKeySet *hwKeys); + + /** + * Save this keymap's mappings to the given config domain + * @param domain config domain to save keymap to + * @param name name to save the keymap under + */ + void saveMappings(ConfigManager::Domain *domain, const String& name); + private: friend struct Action; /** |