diff options
author | Tarek Soliman | 2012-02-14 23:02:23 -0600 |
---|---|---|
committer | Tarek Soliman | 2012-02-15 17:07:52 -0600 |
commit | cce5be67dc3d171a5b30a9863f250471adecd5b0 (patch) | |
tree | 6e911ceff6d6b310668f6aee7d153a0a25708e7f /backends | |
parent | 974f5eb7b8291535ea34be5260607d6e383543a7 (diff) | |
download | scummvm-rg350-cce5be67dc3d171a5b30a9863f250471adecd5b0.tar.gz scummvm-rg350-cce5be67dc3d171a5b30a9863f250471adecd5b0.tar.bz2 scummvm-rg350-cce5be67dc3d171a5b30a9863f250471adecd5b0.zip |
KEYMAPPER: Allow ports to define default Keymap Action bindings
Diffstat (limited to 'backends')
-rw-r--r-- | backends/keymapper/keymap.cpp | 56 | ||||
-rw-r--r-- | backends/keymapper/keymapper-defaults.h | 56 |
2 files changed, 93 insertions, 19 deletions
diff --git a/backends/keymapper/keymap.cpp b/backends/keymapper/keymap.cpp index bd020937eb..daf92e1d15 100644 --- a/backends/keymapper/keymap.cpp +++ b/backends/keymapper/keymap.cpp @@ -24,7 +24,10 @@ #ifdef ENABLE_KEYMAPPER +#include "common/system.h" + #include "backends/keymapper/hardware-key.h" +#include "backends/keymapper/keymapper-defaults.h" #define KEYMAP_KEY_PREFIX "keymap_" @@ -121,35 +124,50 @@ void Keymap::loadMappings(const HardwareKeySet *hwKeys) { if (!_configDomain) return; - ConfigManager::Domain::iterator it; - String prefix = KEYMAP_KEY_PREFIX + _name + "_"; - - for (it = _configDomain->begin(); it != _configDomain->end(); ++it) { - const String& key = it->_key; - - if (!key.hasPrefix(prefix.c_str())) - continue; + if (_actions.empty()) + return; - // parse Action ID - const char *actionId = key.c_str() + prefix.size(); - Action *ua = getAction(actionId); + Common::KeymapperDefaultBindings *defaults = g_system->getKeymapperDefaultBindings(); - if (!ua) { - warning("'%s' keymap does not contain Action with ID %s", - _name.c_str(), actionId); - _configDomain->erase(key); + HashMap<String, const HardwareKey *> mappedKeys; + List<Action*>::iterator it; + String prefix = KEYMAP_KEY_PREFIX + _name + "_"; - continue; + for (it = _actions.begin(); it != _actions.end(); ++it) { + Action* ua = *it; + String actionId(ua->id); + String confKey = prefix + actionId; + + String hwKeyId = _configDomain->getVal(confKey); + + bool defaulted = false; + // fall back to the platform-specific defaults + if (hwKeyId.empty() && defaults) { + hwKeyId = defaults->getDefaultBinding(_name, actionId); + if (!hwKeyId.empty()) + defaulted = true; } + // there's no mapping + if (hwKeyId.empty()) + continue; - const HardwareKey *hwKey = hwKeys->findHardwareKey(it->_value.c_str()); + const HardwareKey *hwKey = hwKeys->findHardwareKey(hwKeyId.c_str()); if (!hwKey) { - warning("HardwareKey with ID '%s' not known", it->_value.c_str()); - _configDomain->erase(key); + warning("HardwareKey with ID '%s' not known", hwKeyId.c_str()); continue; } + if (defaulted) { + if (mappedKeys.contains(hwKeyId)) { + debug(1, "Action [%s] not falling back to hardcoded default value [%s] because the key is in use", confKey.c_str(), hwKeyId.c_str()); + continue; + } + warning("Action [%s] fell back to hardcoded default value [%s]", confKey.c_str(), hwKeyId.c_str()); + } + + mappedKeys.setVal(hwKeyId, hwKey); + // map the key ua->mapKey(hwKey); } } diff --git a/backends/keymapper/keymapper-defaults.h b/backends/keymapper/keymapper-defaults.h new file mode 100644 index 0000000000..5b84ebeaa9 --- /dev/null +++ b/backends/keymapper/keymapper-defaults.h @@ -0,0 +1,56 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* 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 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#ifdef ENABLE_KEYMAPPER + +#ifndef KEYMAPPER_DEFAULTS_H +#define KEYMAPPER_DEFAULTS_H + +#include "common/scummsys.h" +#include "common/hashmap.h" +#include "common/str.h" +#include "common/hash-str.h" + +namespace Common { + +class KeymapperDefaultBindings : HashMap<String, String> { +public: + /** + * This sets a default hwKey for a given Keymap Action + * @param keymapId String representing Keymap id (Keymap.name) + * @param actionId String representing Action id (Action.id) + * @param hwKeyId String representing the HardwareKey id (HardwareKey.hwKeyId) + */ + void setDefaultBinding(String keymapId, String actionId, String hwKeyId) { setVal(keymapId + "_" + actionId, hwKeyId); } + /** + * This retrieves the assigned default hwKey for a given Keymap Action + * @param keymapId String representing Keymap id (Keymap.name) + * @param actionId String representing Action id (Action.id) + * @return hwKeyId String representing the HardwareKey id (HardwareKey.hwKeyId) + */ + String getDefaultBinding(String keymapId, String actionId) { return getVal(keymapId + "_" + actionId); } +}; + +} //namespace Common + +#endif // #ifndef KEYMAPPER_DEFAULTS_H +#endif // #ifdef ENABLE_KEYMAPPER |