1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "backends/keymapper/keymap.h"
#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"
#define KEYMAP_KEY_PREFIX "keymap_"
namespace Common {
Keymap::Keymap(KeymapType type, const String &id, const U32String &description) :
_type(type),
_id(id),
_description(description),
_enabled(true),
_configDomain(nullptr),
_hardwareInputSet(nullptr),
_backendDefaultBindings(nullptr) {
}
Keymap::Keymap(KeymapType type, const String &id, const String &description) :
_type(type),
_id(id),
_description(U32String(description)),
_enabled(true),
_configDomain(nullptr),
_hardwareInputSet(nullptr),
_backendDefaultBindings(nullptr) {
}
Keymap::~Keymap() {
for (ActionArray::iterator it = _actions.begin(); it != _actions.end(); ++it)
delete *it;
}
void Keymap::addAction(Action *action) {
if (findAction(action->id))
error("Action with id %s already in KeyMap", action->id);
_actions.push_back(action);
}
void Keymap::registerMapping(Action *action, const HardwareInput &hwInput) {
ActionArray &actionArray = _hwActionMap.getOrCreateVal(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);
}
}
}
void Keymap::resetMapping(Action *action) {
unregisterMapping(action);
StringArray hwInputIds = getActionDefaultMappings(action);
registerMappings(action, hwInputIds);
}
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);
}
};
Array<HardwareInput> Keymap::getActionMapping(const 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 {
for (ActionArray::const_iterator it = _actions.begin(); it != _actions.end(); ++it) {
if (strcmp((*it)->id, id) == 0)
return *it;
}
return nullptr;
}
Keymap::KeymapMatch Keymap::getMappedActions(const Event &event, ActionArray &actions) const {
switch (event.type) {
case EVENT_KEYDOWN:
case EVENT_KEYUP: {
KeyState normalizedKeystate = KeyboardHardwareInputSet::normalizeKeyState(event.kbd);
HardwareInput hardwareInput = HardwareInput::createKeyboard("", normalizedKeystate, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
if (!actions.empty()) {
return kKeymapMatchExact;
}
if (normalizedKeystate.flags & KBD_NON_STICKY) {
// If no matching actions and non-sticky keyboard modifiers are down,
// check again for matches without the exact keyboard modifiers
for (HardwareActionMap::const_iterator itInput = _hwActionMap.begin(); itInput != _hwActionMap.end(); ++itInput) {
if (itInput->_key.type == kHardwareInputTypeKeyboard && itInput->_key.key.keycode == normalizedKeystate.keycode) {
int flags = itInput->_key.key.flags;
if (flags & KBD_NON_STICKY && (flags & normalizedKeystate.flags) == flags) {
actions.push_back(itInput->_value);
return kKeymapMatchPartial;
}
}
}
// Lastly check again for matches no non-sticky keyboard modifiers
normalizedKeystate.flags &= ~KBD_NON_STICKY;
hardwareInput = HardwareInput::createKeyboard("", normalizedKeystate, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
return actions.empty() ? kKeymapMatchNone : kKeymapMatchPartial;
}
break;
}
case EVENT_LBUTTONDOWN:
case EVENT_LBUTTONUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_BUTTON_LEFT, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
break;
}
case EVENT_RBUTTONDOWN:
case EVENT_RBUTTONUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_BUTTON_RIGHT, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
break;
}
case EVENT_MBUTTONDOWN:
case EVENT_MBUTTONUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_BUTTON_MIDDLE, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
break;
}
case Common::EVENT_WHEELUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_WHEEL_UP, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
break;
}
case Common::EVENT_WHEELDOWN: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_WHEEL_DOWN, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
break;
}
case EVENT_X1BUTTONDOWN:
case EVENT_X1BUTTONUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_BUTTON_X1, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
break;
}
case EVENT_X2BUTTONDOWN:
case EVENT_X2BUTTONUP: {
HardwareInput hardwareInput = HardwareInput::createMouse("", MOUSE_BUTTON_X2, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
break;
}
case EVENT_JOYBUTTON_DOWN:
case EVENT_JOYBUTTON_UP: {
HardwareInput hardwareInput = HardwareInput::createJoystickButton("", event.joystick.button, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
break;
}
case EVENT_JOYAXIS_MOTION: {
if (event.joystick.position != 0) {
bool positiveHalf = event.joystick.position >= 0;
HardwareInput hardwareInput = HardwareInput::createJoystickHalfAxis("", event.joystick.axis, positiveHalf, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
} else {
// Axis position zero is part of both half axes, and triggers actions bound to both
HardwareInput hardwareInputPos = HardwareInput::createJoystickHalfAxis("", event.joystick.axis, true, U32String());
HardwareInput hardwareInputNeg = HardwareInput::createJoystickHalfAxis("", event.joystick.axis, false, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInputPos));
actions.push_back(_hwActionMap.getValOrDefault(hardwareInputNeg));
}
break;
}
case EVENT_CUSTOM_BACKEND_HARDWARE: {
HardwareInput hardwareInput = HardwareInput::createCustom("", event.customType, U32String());
actions.push_back(_hwActionMap.getValOrDefault(hardwareInput));
break;
}
default:
break;
}
return actions.empty() ? kKeymapMatchNone : kKeymapMatchExact;
}
void Keymap::setConfigDomain(ConfigManager::Domain *configDomain) {
_configDomain = configDomain;
}
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()) {
return it->_value;
}
// If no keymap-specific default mapping was found, look for a standard action binding
it = _backendDefaultBindings->findDefaultBinding(kStandardActionsKeymapName, action->id);
if (it != _backendDefaultBindings->end()) {
return 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;
}
// map the key
registerMapping(action, hwInput);
}
}
void Keymap::saveMappings() {
if (!_configDomain)
return;
String prefix = KEYMAP_KEY_PREFIX + _id + "_";
for (ActionArray::const_iterator it = _actions.begin(); it != _actions.end(); it++) {
Action *action = *it;
Array<HardwareInput> mappedInputs = getActionMapping(action);
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;
}
// 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::areMappingsIdentical(const Array<HardwareInput> &mappingsA, const StringArray &mappingsB) {
// Assumes array values are not duplicated, but registerMapping and addDefaultInputMapping ensure that
uint foundCount = 0;
uint validDefaultMappings = 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;
validDefaultMappings++;
for (uint j = 0; j < mappingsA.size(); j++) {
if (mappingsA[j].id == mappingB.id) {
foundCount++;
break;
}
}
}
return foundCount == mappingsA.size() && foundCount == validDefaultMappings;
}
} // End of namespace Common
|