ALL: synced with ScummVM commit 09bf38c120

This commit is contained in:
Pawel Kolodziejski 2020-05-09 20:05:54 +02:00
parent b5d73d4c22
commit b586571900
414 changed files with 111155 additions and 38279 deletions

View file

@ -22,10 +22,10 @@
#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"
#include "backends/keymapper/keymapper-defaults.h"
@ -33,25 +33,19 @@
namespace Common {
Keymap::Keymap(const Keymap& km) : _actions(km._actions), _keymap(), _nonkeymap(), _configDomain(0) {
List<Action *>::iterator it;
Keymap::Keymap(KeymapType type, const String &id, const String &description) :
_type(type),
_id(id),
_description(description),
_enabled(true),
_configDomain(nullptr),
_hardwareInputSet(nullptr),
_backendDefaultBindings(nullptr) {
for (it = _actions.begin(); it != _actions.end(); ++it) {
const HardwareInput *hwInput = (*it)->getMappedInput();
if (hwInput) {
if (hwInput->type == kHardwareInputTypeKeyboard)
_keymap[hwInput->key] = *it;
else if (hwInput->type == kHardwareInputTypeGeneric)
_nonkeymap[hwInput->inputCode] = *it;
}
}
}
Keymap::~Keymap() {
List<Action *>::iterator it;
for (it = _actions.begin(); it != _actions.end(); ++it)
for (ActionArray::iterator it = _actions.begin(); it != _actions.end(); ++it)
delete *it;
}
@ -62,176 +56,278 @@ void Keymap::addAction(Action *action) {
_actions.push_back(action);
}
void Keymap::registerMapping(Action *action, const HardwareInput *hwInput) {
if (hwInput->type == kHardwareInputTypeKeyboard) {
HashMap<KeyState, Action *>::iterator it = _keymap.find(hwInput->key);
// if input is already mapped to a different action then unmap it from there
if (it != _keymap.end() && action != it->_value)
it->_value->mapInput(0);
// now map it
_keymap[hwInput->key] = action;
} else if (hwInput->type == kHardwareInputTypeGeneric) {
HashMap<HardwareInputCode, Action *>::iterator it = _nonkeymap.find(hwInput->inputCode);
// if input is already mapped to a different action then unmap it from there
if (it != _nonkeymap.end() && action != it->_value)
it->_value->mapInput(0);
// now map it
_nonkeymap[hwInput->inputCode] = 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) {
const HardwareInput *hwInput = action->getMappedInput();
if (hwInput) {
if (hwInput->type == kHardwareInputTypeKeyboard)
_keymap.erase(hwInput->key);
else if (hwInput->type == kHardwareInputTypeGeneric)
_nonkeymap.erase(hwInput->inputCode);
// 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);
}
}
}
Action *Keymap::getAction(const char *id) {
return findAction(id);
void Keymap::resetMapping(Action *action) {
unregisterMapping(action);
StringArray hwInputIds = getActionDefaultMappings(action);
registerMappings(action, hwInputIds);
}
Action *Keymap::findAction(const char *id) {
List<Action *>::iterator it;
for (it = _actions.begin(); it != _actions.end(); ++it) {
if (strncmp((*it)->id, id, ACTION_ID_SIZE) == 0)
return *it;
struct HardwareInputTypeIdComparator {
bool operator()(const HardwareInput &x, const HardwareInput &y) const {
if (x.type != y.type) {
return x.type < y.type;
}
return x.id.compareTo(y.id);
}
return 0;
};
Array<HardwareInput> Keymap::getActionMapping(Action *action) const {
Array<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;
}
}
}
// Sort the inputs by type and then id for the remap dialog
Common::sort(inputs.begin(), inputs.end(), HardwareInputTypeIdComparator());
return inputs;
}
const Action *Keymap::findAction(const char *id) const {
List<Action *>::const_iterator it;
for (it = _actions.begin(); it != _actions.end(); ++it) {
if (strncmp((*it)->id, id, ACTION_ID_SIZE) == 0)
for (ActionArray::const_iterator it = _actions.begin(); it != _actions.end(); ++it) {
if (strcmp((*it)->id, id) == 0)
return *it;
}
return 0;
return nullptr;
}
Action *Keymap::getMappedAction(const KeyState& ks) const {
HashMap<KeyState, Action *>::iterator it;
it = _keymap.find(ks);
if (it == _keymap.end())
return 0;
else
return it->_value;
}
Action *Keymap::getMappedAction(const HardwareInputCode code) const {
HashMap<HardwareInputCode, Action *>::iterator it;
it = _nonkeymap.find(code);
if (it == _nonkeymap.end())
return 0;
else
return it->_value;
}
void Keymap::setConfigDomain(ConfigManager::Domain *dom) {
_configDomain = dom;
}
void Keymap::loadMappings(const HardwareInputSet *hwKeys) {
if (!_configDomain)
return;
if (_actions.empty())
return;
Common::KeymapperDefaultBindings *defaults = g_system->getKeymapperDefaultBindings();
HashMap<String, const HardwareInput *> mappedInputs;
List<Action*>::iterator it;
String prefix = KEYMAP_KEY_PREFIX + _name + "_";
for (it = _actions.begin(); it != _actions.end(); ++it) {
Action* ua = *it;
String actionId(ua->id);
String confKey = prefix + actionId;
String hwInputId = _configDomain->getVal(confKey);
bool defaulted = false;
// fall back to the platform-specific defaults
if (hwInputId.empty() && defaults) {
hwInputId = defaults->getDefaultBinding(_name, actionId);
if (!hwInputId.empty())
defaulted = true;
Keymap::ActionArray Keymap::getMappedActions(const Event &event) const {
switch (event.type) {
case EVENT_KEYDOWN:
case EVENT_KEYUP: {
KeyState normalizedKeystate = KeyboardHardwareInputSet::normalizeKeyState(event.kbd);
HardwareInput hardwareInput = HardwareInput::createKeyboard("", normalizedKeystate, "");
return _hwActionMap[hardwareInput];
}
case EVENT_LBUTTONDOWN:
case EVENT_LBUTTONUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_BUTTON_LEFT, "");
return _hwActionMap[hardwareInput];
}
case EVENT_RBUTTONDOWN:
case EVENT_RBUTTONUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_BUTTON_RIGHT, "");
return _hwActionMap[hardwareInput];
}
case EVENT_MBUTTONDOWN:
case EVENT_MBUTTONUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_BUTTON_MIDDLE, "");
return _hwActionMap[hardwareInput];
}
case Common::EVENT_WHEELUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_WHEEL_UP, "");
return _hwActionMap[hardwareInput];
}
case Common::EVENT_WHEELDOWN: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_WHEEL_DOWN, "");
return _hwActionMap[hardwareInput];
}
case EVENT_X1BUTTONDOWN:
case EVENT_X1BUTTONUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_BUTTON_X1, "");
return _hwActionMap[hardwareInput];
}
case EVENT_X2BUTTONDOWN:
case EVENT_X2BUTTONUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_BUTTON_X2, "");
return _hwActionMap[hardwareInput];
}
case EVENT_JOYBUTTON_DOWN:
case EVENT_JOYBUTTON_UP: {
HardwareInput hardwareInput = HardwareInput::createJoystickButton("", event.joystick.button, "");
return _hwActionMap[hardwareInput];
}
case EVENT_JOYAXIS_MOTION: {
if (event.joystick.position != 0) {
bool positiveHalf = event.joystick.position >= 0;
HardwareInput hardwareInput = HardwareInput::createJoystickHalfAxis("", event.joystick.axis, positiveHalf, "");
return _hwActionMap[hardwareInput];
} else {
// Axis position zero is part of both half axes, and triggers actions bound to both
Keymap::ActionArray actions;
HardwareInput hardwareInputPos = HardwareInput::createJoystickHalfAxis("", event.joystick.axis, true, "");
HardwareInput hardwareInputNeg = HardwareInput::createJoystickHalfAxis("", event.joystick.axis, false, "");
actions.push_back(_hwActionMap[hardwareInputPos]);
actions.push_back(_hwActionMap[hardwareInputNeg]);
return actions;
}
// there's no mapping
if (hwInputId.empty())
continue;
}
case EVENT_CUSTOM_BACKEND_HARDWARE: {
HardwareInput hardwareInput = HardwareInput::createCustom("", event.customType, "");
return _hwActionMap[hardwareInput];
}
default:
return ActionArray();
}
}
const HardwareInput *hwInput = hwKeys->findHardwareInput(hwInputId.c_str());
void Keymap::setConfigDomain(ConfigManager::Domain *configDomain) {
_configDomain = configDomain;
}
if (!hwInput) {
warning("HardwareInput with ID '%s' not known", hwInputId.c_str());
continue;
void Keymap::setHardwareInputs(HardwareInputSet *hardwareInputSet) {
_hardwareInputSet = hardwareInputSet;
}
void Keymap::setBackendDefaultBindings(const KeymapperDefaultBindings *backendDefaultBindings) {
_backendDefaultBindings = backendDefaultBindings;
}
StringArray Keymap::getActionDefaultMappings(Action *action) {
// Backend default mappings overrides keymap default mappings, so backends can resolve mapping conflicts.
// Empty mappings are valid and mean the action should not be mapped by default.
if (_backendDefaultBindings) {
KeymapperDefaultBindings::const_iterator it = _backendDefaultBindings->findDefaultBinding(_id, action->id);
if (it != _backendDefaultBindings->end()) {
if (it->_value.empty()) {
return StringArray();
}
return StringArray(1, it->_value);
}
if (defaulted) {
if (mappedInputs.contains(hwInputId)) {
debug(1, "Action [%s] not falling back to hardcoded default value [%s] because the hardware input is in use", confKey.c_str(), hwInputId.c_str());
// If no keymap-specific default mapping was found, look for a standard action binding
it = _backendDefaultBindings->findDefaultBinding(kStandardActionsKeymapName, action->id);
if (it != _backendDefaultBindings->end()) {
if (it->_value.empty()) {
return StringArray();
}
return StringArray(1, it->_value);
}
}
return action->getDefaultInputMapping();
}
void Keymap::loadMappings() {
assert(_configDomain);
assert(_hardwareInputSet);
if (_actions.empty()) {
return;
}
String prefix = KEYMAP_KEY_PREFIX + _id + "_";
_hwActionMap.clear();
for (ActionArray::const_iterator it = _actions.begin(); it != _actions.end(); ++it) {
Action *action = *it;
String confKey = prefix + action->id;
StringArray hwInputIds;
if (_configDomain->contains(confKey)) {
// The configuration value is a list of space separated hardware input ids
StringTokenizer hwInputTokenizer = _configDomain->getVal(confKey);
while (!hwInputTokenizer.empty()) {
hwInputIds.push_back(hwInputTokenizer.nextToken());
}
} else {
// If the configuration key was not found, use the default mapping
hwInputIds = getActionDefaultMappings(action);
}
registerMappings(action, hwInputIds);
}
}
void Keymap::registerMappings(Action *action, const StringArray &hwInputIds) {
assert(_hardwareInputSet);
for (uint i = 0; i < hwInputIds.size(); i++) {
HardwareInput hwInput = _hardwareInputSet->findHardwareInput(hwInputIds[i]);
if (hwInput.type == kHardwareInputTypeInvalid) {
// Silently ignore unknown hardware ids because the current device may not have inputs matching the defaults
debug(1, "HardwareInput with ID '%s' not known", hwInputIds[i].c_str());
continue;
}
warning("Action [%s] fell back to hardcoded default value [%s]", confKey.c_str(), hwInputId.c_str());
}
mappedInputs.setVal(hwInputId, hwInput);
// map the key
ua->mapInput(hwInput);
}
// map the key
registerMapping(action, hwInput);
}
}
void Keymap::saveMappings() {
if (!_configDomain)
return;
List<Action *>::const_iterator it;
String prefix = KEYMAP_KEY_PREFIX + _name + "_";
String prefix = KEYMAP_KEY_PREFIX + _id + "_";
for (it = _actions.begin(); it != _actions.end(); ++it) {
uint actIdLen = strlen((*it)->id);
for (ActionArray::const_iterator it = _actions.begin(); it != _actions.end(); it++) {
Action *action = *it;
Array<HardwareInput> mappedInputs = getActionMapping(action);
actIdLen = (actIdLen > ACTION_ID_SIZE) ? ACTION_ID_SIZE : actIdLen;
String actId((*it)->id, (*it)->id + actIdLen);
String hwId = "";
if ((*it)->getMappedInput()) {
hwId = (*it)->getMappedInput()->id;
if (areMappingsIdentical(mappedInputs, getActionDefaultMappings(action))) {
// If the current mapping is the default, don't write anything to the config manager
_configDomain->erase(prefix + action->id);
continue;
}
_configDomain->setVal(prefix + actId, hwId);
// 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;
}
_configDomain->setVal(prefix + action->id, confValue);
}
}
bool Keymap::isComplete(const HardwareInputSet *hwInputs) {
List<Action *>::iterator it;
bool allMapped = true;
uint numberMapped = 0;
bool Keymap::areMappingsIdentical(const Array<HardwareInput> &mappingsA, const StringArray &mappingsB) {
// Assumes array values are not duplicated, but registerMapping and addDefaultInputMapping ensure that
for (it = _actions.begin(); it != _actions.end(); ++it) {
if ((*it)->getMappedInput()) {
++numberMapped;
} else {
allMapped = false;
uint foundCount = 0;
for (uint i = 0; i < mappingsB.size(); i++) {
// We resolve the hardware input to make sure it is not a default for some hardware we don't have currently
HardwareInput mappingB = _hardwareInputSet->findHardwareInput(mappingsB[i]);
if (mappingB.type == kHardwareInputTypeInvalid) continue;
for (uint j = 0; j < mappingsA.size(); j++) {
if (mappingsA[j].id == mappingB.id) {
foundCount++;
break;
}
}
}
return allMapped || (numberMapped == hwInputs->size());
return foundCount == mappingsA.size();
}
} // End of namespace Common
#endif // #ifdef ENABLE_KEYMAPPER