KEYMAPPER: Allow ports to define default Keymap Action bindings

This commit is contained in:
Tarek Soliman 2012-02-14 23:02:23 -06:00
parent 974f5eb7b8
commit cce5be67dc
3 changed files with 102 additions and 18 deletions

View file

@ -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;
if (_actions.empty())
return;
Common::KeymapperDefaultBindings *defaults = g_system->getKeymapperDefaultBindings();
HashMap<String, const HardwareKey *> mappedKeys;
List<Action*>::iterator it;
String prefix = KEYMAP_KEY_PREFIX + _name + "_";
for (it = _configDomain->begin(); it != _configDomain->end(); ++it) {
const String& key = it->_key;
for (it = _actions.begin(); it != _actions.end(); ++it) {
Action* ua = *it;
String actionId(ua->id);
String confKey = prefix + actionId;
if (!key.hasPrefix(prefix.c_str()))
continue;
String hwKeyId = _configDomain->getVal(confKey);
// parse Action ID
const char *actionId = key.c_str() + prefix.size();
Action *ua = getAction(actionId);
if (!ua) {
warning("'%s' keymap does not contain Action with ID %s",
_name.c_str(), actionId);
_configDomain->erase(key);
continue;
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);
}
}

View file

@ -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

View file

@ -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
//@}