From cce5be67dc3d171a5b30a9863f250471adecd5b0 Mon Sep 17 00:00:00 2001 From: Tarek Soliman Date: Tue, 14 Feb 2012 23:02:23 -0600 Subject: KEYMAPPER: Allow ports to define default Keymap Action bindings --- backends/keymapper/keymap.cpp | 56 ++++++++++++++++++++++----------- backends/keymapper/keymapper-defaults.h | 56 +++++++++++++++++++++++++++++++++ common/system.h | 10 ++++++ 3 files changed, 103 insertions(+), 19 deletions(-) create mode 100644 backends/keymapper/keymapper-defaults.h 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 mappedKeys; + List::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 { +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 diff --git a/common/system.h b/common/system.h index c5e8214d22..34c987bf26 100644 --- a/common/system.h +++ b/common/system.h @@ -54,6 +54,7 @@ class WriteStream; #ifdef ENABLE_KEYMAPPER class HardwareKeySet; class Keymap; +class KeymapperDefaultBindings; #endif } @@ -955,6 +956,15 @@ public: * See keymapper documentation for further reference. */ virtual Common::Keymap *getGlobalKeymap() { return 0; } + + /** + * Return platform-specific default keybindings + * + * @return KeymapperDefaultBindings populated with keybindings + * + * See keymapper documentation for further reference. + */ + virtual Common::KeymapperDefaultBindings *getKeymapperDefaultBindings() { return 0; } #endif //@} -- cgit v1.2.3