2008-07-30 13:47:54 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2014-02-18 02:34:21 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2008-07-30 13:47:54 +00:00
|
|
|
|
2008-08-07 16:38:39 +00:00
|
|
|
#include "backends/keymapper/keymap.h"
|
2008-09-30 13:51:01 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_KEYMAPPER
|
|
|
|
|
2012-02-14 23:02:23 -06:00
|
|
|
#include "common/system.h"
|
|
|
|
|
2017-08-11 13:57:28 +02:00
|
|
|
#include "backends/keymapper/action.h"
|
2012-02-24 13:23:55 -06:00
|
|
|
#include "backends/keymapper/hardware-input.h"
|
2008-07-19 00:57:37 +00:00
|
|
|
|
2008-08-18 10:07:11 +00:00
|
|
|
#define KEYMAP_KEY_PREFIX "keymap_"
|
|
|
|
|
2008-07-19 00:57:37 +00:00
|
|
|
namespace Common {
|
|
|
|
|
2017-08-11 13:57:28 +02:00
|
|
|
Keymap::Keymap(const Keymap& km) : _actions(km._actions), _hwActionMap(), _configDomain(0) {
|
2008-07-19 00:57:37 +00:00
|
|
|
}
|
|
|
|
|
2008-08-14 19:20:25 +00:00
|
|
|
Keymap::~Keymap() {
|
2017-08-11 13:57:28 +02:00
|
|
|
for (ActionList::iterator it = _actions.begin(); it != _actions.end(); ++it)
|
2008-08-14 19:20:25 +00:00
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
2008-08-01 16:44:49 +00:00
|
|
|
void Keymap::addAction(Action *action) {
|
|
|
|
if (findAction(action->id))
|
2009-05-31 10:02:16 +00:00
|
|
|
error("Action with id %s already in KeyMap", action->id);
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2008-07-19 00:57:37 +00:00
|
|
|
_actions.push_back(action);
|
|
|
|
}
|
|
|
|
|
2012-02-24 13:23:55 -06:00
|
|
|
void Keymap::registerMapping(Action *action, const HardwareInput *hwInput) {
|
2017-08-11 13:57:28 +02:00
|
|
|
unregisterMapping(action);
|
|
|
|
|
|
|
|
_hwActionMap[hwInput] = action;
|
2008-07-19 00:57:37 +00:00
|
|
|
}
|
|
|
|
|
2008-08-01 16:44:49 +00:00
|
|
|
void Keymap::unregisterMapping(Action *action) {
|
2017-08-11 13:57:28 +02:00
|
|
|
for (HardwareActionMap::iterator it = _hwActionMap.begin(); it != _hwActionMap.end(); it++) {
|
|
|
|
if (it->_value == action) {
|
|
|
|
_hwActionMap.erase(it);
|
|
|
|
}
|
2008-08-08 14:23:59 +00:00
|
|
|
}
|
2008-07-24 10:00:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 13:57:28 +02:00
|
|
|
const HardwareInput *Keymap::getActionMapping(Action *action) const {
|
|
|
|
for (HardwareActionMap::const_iterator it = _hwActionMap.begin(); it != _hwActionMap.end(); it++) {
|
|
|
|
if (it->_value == action) {
|
|
|
|
return it->_key;
|
|
|
|
}
|
2008-07-19 19:12:49 +00:00
|
|
|
}
|
2017-08-11 13:57:28 +02:00
|
|
|
|
|
|
|
return nullptr;
|
2008-07-19 19:12:49 +00:00
|
|
|
}
|
|
|
|
|
2008-08-18 10:07:11 +00:00
|
|
|
const Action *Keymap::findAction(const char *id) const {
|
2017-08-11 13:57:28 +02:00
|
|
|
for (ActionList::const_iterator it = _actions.begin(); it != _actions.end(); ++it) {
|
2017-08-11 14:19:41 +02:00
|
|
|
if (strcmp((*it)->id, id) == 0)
|
2008-08-01 16:44:49 +00:00
|
|
|
return *it;
|
2008-07-19 19:12:49 +00:00
|
|
|
}
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-11 13:57:28 +02:00
|
|
|
return nullptr;
|
2008-07-19 19:12:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 13:57:28 +02:00
|
|
|
Action *Keymap::getMappedAction(const HardwareInput *hardwareInput) const {
|
|
|
|
return _hwActionMap[hardwareInput];
|
2012-02-22 18:30:47 -06:00
|
|
|
}
|
|
|
|
|
2008-08-14 01:42:02 +00:00
|
|
|
void Keymap::setConfigDomain(ConfigManager::Domain *dom) {
|
|
|
|
_configDomain = dom;
|
|
|
|
}
|
|
|
|
|
2012-02-24 13:23:55 -06:00
|
|
|
void Keymap::loadMappings(const HardwareInputSet *hwKeys) {
|
2009-05-10 17:18:59 +00:00
|
|
|
if (!_configDomain)
|
|
|
|
return;
|
|
|
|
|
2012-02-14 23:02:23 -06:00
|
|
|
if (_actions.empty())
|
|
|
|
return;
|
2008-08-06 19:21:45 +00:00
|
|
|
|
2012-02-14 23:02:23 -06:00
|
|
|
String prefix = KEYMAP_KEY_PREFIX + _name + "_";
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-11 13:57:28 +02:00
|
|
|
for (ActionList::iterator it = _actions.begin(); it != _actions.end(); ++it) {
|
2012-02-14 23:02:23 -06:00
|
|
|
Action* ua = *it;
|
|
|
|
String actionId(ua->id);
|
|
|
|
String confKey = prefix + actionId;
|
|
|
|
|
2012-02-24 13:23:55 -06:00
|
|
|
String hwInputId = _configDomain->getVal(confKey);
|
2012-02-14 23:02:23 -06:00
|
|
|
|
|
|
|
// there's no mapping
|
2012-02-24 13:23:55 -06:00
|
|
|
if (hwInputId.empty())
|
2012-02-14 23:02:23 -06:00
|
|
|
continue;
|
2008-08-06 19:21:45 +00:00
|
|
|
|
2012-02-24 13:23:55 -06:00
|
|
|
const HardwareInput *hwInput = hwKeys->findHardwareInput(hwInputId.c_str());
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2012-02-24 13:23:55 -06:00
|
|
|
if (!hwInput) {
|
|
|
|
warning("HardwareInput with ID '%s' not known", hwInputId.c_str());
|
2008-08-06 19:21:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-14 23:02:23 -06:00
|
|
|
// map the key
|
2017-08-11 13:57:28 +02:00
|
|
|
_hwActionMap[hwInput] = ua;
|
2008-08-06 19:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-14 01:42:02 +00:00
|
|
|
void Keymap::saveMappings() {
|
2009-01-21 02:02:55 +00:00
|
|
|
if (!_configDomain)
|
|
|
|
return;
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2008-08-18 10:07:11 +00:00
|
|
|
String prefix = KEYMAP_KEY_PREFIX + _name + "_";
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-11 13:57:28 +02:00
|
|
|
for (HardwareActionMap::iterator it = _hwActionMap.begin(); it != _hwActionMap.end(); it++) {
|
|
|
|
const Action *action = it->_value;
|
|
|
|
const HardwareInput *input = it->_key;
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-11 14:19:41 +02:00
|
|
|
_configDomain->setVal(prefix + action->id, input->id);
|
2008-08-07 14:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-04 21:26:33 +00:00
|
|
|
} // End of namespace Common
|
2008-09-30 13:51:01 +00:00
|
|
|
|
|
|
|
#endif // #ifdef ENABLE_KEYMAPPER
|