scummvm/backends/keymapper/keymap.cpp

174 lines
4.8 KiB
C++
Raw Normal View History

/* 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.
*
*/
#include "backends/keymapper/keymap.h"
#ifdef ENABLE_KEYMAPPER
#include "common/system.h"
#include "common/tokenizer.h"
#include "backends/keymapper/action.h"
#include "backends/keymapper/hardware-input.h"
#define KEYMAP_KEY_PREFIX "keymap_"
namespace Common {
2017-08-13 16:35:58 +02:00
Keymap::Keymap(KeymapType type, const String &name) :
_type(type),
_name(name),
_configDomain(nullptr),
_enabled(true) {
2017-08-13 16:35:58 +02:00
}
2008-08-14 19:20:25 +00:00
Keymap::~Keymap() {
for (ActionArray::iterator it = _actions.begin(); it != _actions.end(); ++it)
2008-08-14 19:20:25 +00:00
delete *it;
}
void Keymap::addAction(Action *action) {
if (findAction(action->id))
error("Action with id %s already in KeyMap", action->id);
2009-05-10 17:18:59 +00:00
_actions.push_back(action);
}
void Keymap::registerMapping(Action *action, const HardwareInput *hwInput) {
ActionArray &actionArray = _hwActionMap.getVal(hwInput);
// Don't allow an input to map to the same action multiple times
ActionArray::const_iterator found = find(actionArray.begin(), actionArray.end(), action);
if (found == actionArray.end()) {
actionArray.push_back(action);
}
}
void Keymap::unregisterMapping(Action *action) {
// Remove the action from all the input mappings
for (HardwareActionMap::iterator itInput = _hwActionMap.begin(); itInput != _hwActionMap.end(); itInput++) {
for (ActionArray::iterator itAction = itInput->_value.begin(); itAction != itInput->_value.end(); itAction++) {
if (*itAction == action) {
itInput->_value.erase(itAction);
break;
}
}
if (itInput->_value.empty()) {
_hwActionMap.erase(itInput);
}
}
}
Array<const HardwareInput *> Keymap::getActionMapping(Action *action) const {
Array<const HardwareInput *> inputs;
for (HardwareActionMap::iterator itInput = _hwActionMap.begin(); itInput != _hwActionMap.end(); itInput++) {
for (ActionArray::iterator itAction = itInput->_value.begin(); itAction != itInput->_value.end(); itAction++) {
if (*itAction == action) {
inputs.push_back(itInput->_key);
break;
}
}
}
return inputs;
}
const Action *Keymap::findAction(const char *id) const {
for (ActionArray::const_iterator it = _actions.begin(); it != _actions.end(); ++it) {
if (strcmp((*it)->id, id) == 0)
return *it;
}
2009-05-10 17:18:59 +00:00
return nullptr;
}
const Keymap::ActionArray &Keymap::getMappedActions(const HardwareInput *hardwareInput) const {
return _hwActionMap[hardwareInput];
}
void Keymap::setConfigDomain(ConfigManager::Domain *dom) {
_configDomain = dom;
}
void Keymap::loadMappings(const HardwareInputSet *hwKeys) {
assert(_configDomain);
2009-05-10 17:18:59 +00:00
if (_actions.empty()) {
return;
}
String prefix = KEYMAP_KEY_PREFIX + _name + "_";
2009-05-10 17:18:59 +00:00
_hwActionMap.clear();
for (ActionArray::const_iterator it = _actions.begin(); it != _actions.end(); ++it) {
Action* ua = *it;
String actionId(ua->id);
String confKey = prefix + actionId;
// The configuration value is a list of space separated hardware input ids
StringTokenizer hwInputIds = _configDomain->getVal(confKey);
String hwInputId;
while ((hwInputId = hwInputIds.nextToken()) != "") {
const HardwareInput *hwInput = hwKeys->findHardwareInput(hwInputId.c_str());
if (!hwInput) {
warning("HardwareInput with ID '%s' not known", hwInputId.c_str());
continue;
}
2009-05-10 17:18:59 +00:00
// map the key
registerMapping(ua, hwInput);
}
}
}
void Keymap::saveMappings() {
2009-01-21 02:02:55 +00:00
if (!_configDomain)
return;
2009-05-10 17:18:59 +00:00
String prefix = KEYMAP_KEY_PREFIX + _name + "_";
2009-05-10 17:18:59 +00:00
for (ActionArray::const_iterator it = _actions.begin(); it != _actions.end(); it++) {
Action *action = *it;
Array<const HardwareInput *> mappedInputs = getActionMapping(action);
// The configuration value is a list of space separated hardware input ids
String confValue;
for (uint j = 0; j < mappedInputs.size(); j++) {
if (!confValue.empty()) {
confValue += " ";
}
confValue += mappedInputs[j]->id;
}
2009-05-10 17:18:59 +00:00
_configDomain->setVal(prefix + action->id, confValue);
}
}
} // End of namespace Common
#endif // #ifdef ENABLE_KEYMAPPER