1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "backends/keymapper/keymapper.h"
24
25 #ifdef ENABLE_KEYMAPPER
26
27 #include "common/config-manager.h"
28 #include "common/system.h"
29
30 namespace Common {
31
32 // These magic numbers are provided by fuzzie and WebOS
33 static const uint32 kDelayKeyboardEventMillis = 250;
34 static const uint32 kDelayMouseEventMillis = 50;
35
addKeymap(Keymap * map)36 void Keymapper::Domain::addKeymap(Keymap *map) {
37 iterator it = find(map->getName());
38
39 if (it != end())
40 delete it->_value;
41
42 setVal(map->getName(), map);
43 }
44
deleteAllKeyMaps()45 void Keymapper::Domain::deleteAllKeyMaps() {
46 for (iterator it = begin(); it != end(); ++it)
47 delete it->_value;
48
49 clear();
50 }
51
getKeymap(const String & name)52 Keymap *Keymapper::Domain::getKeymap(const String& name) {
53 iterator it = find(name);
54
55 if (it != end())
56 return it->_value;
57 else
58 return 0;
59 }
60
Keymapper(EventManager * evtMgr)61 Keymapper::Keymapper(EventManager *evtMgr)
62 : _eventMan(evtMgr), _enabled(true), _remapping(false), _hardwareInputs(0), _actionToRemap(0) {
63 ConfigManager::Domain *confDom = ConfMan.getDomain(ConfigManager::kKeymapperDomain);
64
65 _globalDomain.setConfigDomain(confDom);
66 }
67
~Keymapper()68 Keymapper::~Keymapper() {
69 delete _hardwareInputs;
70 }
71
registerHardwareInputSet(HardwareInputSet * inputs)72 void Keymapper::registerHardwareInputSet(HardwareInputSet *inputs) {
73 if (_hardwareInputs)
74 error("Hardware input set already registered");
75
76 if (!inputs) {
77 warning("No hardware input were defined, using defaults");
78 inputs = new HardwareInputSet(true);
79 }
80
81 _hardwareInputs = inputs;
82 }
83
addGlobalKeymap(Keymap * keymap)84 void Keymapper::addGlobalKeymap(Keymap *keymap) {
85 initKeymap(_globalDomain, keymap);
86 }
87
addGameKeymap(Keymap * keymap)88 void Keymapper::addGameKeymap(Keymap *keymap) {
89 if (ConfMan.getActiveDomain() == 0)
90 error("Call to Keymapper::addGameKeymap when no game loaded");
91
92 // Detect whether the active game changed since last call.
93 // If so, flush the game key configuration.
94 if (_gameDomain.getConfigDomain() != ConfMan.getActiveDomain()) {
95 cleanupGameKeymaps();
96 _gameDomain.setConfigDomain(ConfMan.getActiveDomain());
97 }
98
99 initKeymap(_gameDomain, keymap);
100 }
101
initKeymap(Domain & domain,Keymap * map)102 void Keymapper::initKeymap(Domain &domain, Keymap *map) {
103 if (!_hardwareInputs) {
104 warning("No hardware inputs were registered yet (%s)", map->getName().c_str());
105 return;
106 }
107
108 map->setConfigDomain(domain.getConfigDomain());
109 map->loadMappings(_hardwareInputs);
110
111 if (map->isComplete(_hardwareInputs) == false) {
112 map->saveMappings();
113 ConfMan.flushToDisk();
114 }
115
116 domain.addKeymap(map);
117 }
118
cleanupGameKeymaps()119 void Keymapper::cleanupGameKeymaps() {
120 // Flush all game specific keymaps
121 _gameDomain.deleteAllKeyMaps();
122
123 // Now restore the stack of active maps. Re-add all global keymaps, drop
124 // the game specific (=deleted) ones.
125 Stack<MapRecord> newStack;
126
127 for (Stack<MapRecord>::size_type i = 0; i < _activeMaps.size(); i++) {
128 if (_activeMaps[i].global)
129 newStack.push(_activeMaps[i]);
130 }
131
132 _activeMaps = newStack;
133 }
134
getKeymap(const String & name,bool * globalReturn)135 Keymap *Keymapper::getKeymap(const String& name, bool *globalReturn) {
136 Keymap *keymap = _gameDomain.getKeymap(name);
137 bool global = false;
138
139 if (!keymap) {
140 keymap = _globalDomain.getKeymap(name);
141 global = true;
142 }
143
144 if (globalReturn)
145 *globalReturn = global;
146
147 return keymap;
148 }
149
pushKeymap(const String & name,bool transparent)150 bool Keymapper::pushKeymap(const String& name, bool transparent) {
151 bool global;
152
153 assert(!name.empty());
154 Keymap *newMap = getKeymap(name, &global);
155
156 if (!newMap) {
157 warning("Keymap '%s' not registered", name.c_str());
158 return false;
159 }
160
161 pushKeymap(newMap, transparent, global);
162
163 return true;
164 }
165
pushKeymap(Keymap * newMap,bool transparent,bool global)166 void Keymapper::pushKeymap(Keymap *newMap, bool transparent, bool global) {
167 MapRecord mr = {newMap, transparent, global};
168
169 _activeMaps.push(mr);
170 }
171
popKeymap(const char * name)172 void Keymapper::popKeymap(const char *name) {
173 if (!_activeMaps.empty()) {
174 if (name) {
175 String topKeymapName = _activeMaps.top().keymap->getName();
176 if (topKeymapName.equals(name))
177 _activeMaps.pop();
178 else
179 warning("An attempt to pop wrong keymap was blocked (expected %s but was %s)", name, topKeymapName.c_str());
180 } else {
181 _activeMaps.pop();
182 }
183 }
184
185 }
186
mapEvent(const Event & ev,EventSource * source)187 List<Event> Keymapper::mapEvent(const Event &ev, EventSource *source) {
188 if (source && !source->allowMapping()) {
189 return DefaultEventMapper::mapEvent(ev, source);
190 }
191 List<Event> mappedEvents;
192
193 if (_remapping)
194 mappedEvents = remap(ev);
195 else if (ev.type == Common::EVENT_KEYDOWN)
196 mappedEvents = mapKeyDown(ev.kbd);
197 else if (ev.type == Common::EVENT_KEYUP)
198 mappedEvents = mapKeyUp(ev.kbd);
199 else if (ev.type == Common::EVENT_CUSTOM_BACKEND_HARDWARE)
200 mappedEvents = mapNonKey(ev.customType);
201
202 if (!mappedEvents.empty())
203 return mappedEvents;
204 else
205 return DefaultEventMapper::mapEvent(ev, source);
206 }
207
startRemappingMode(Action * actionToRemap)208 void Keymapper::startRemappingMode(Action *actionToRemap) {
209 assert(!_remapping);
210
211 _remapping = true;
212 _actionToRemap = actionToRemap;
213 }
214
mapKeyDown(const KeyState & key)215 List<Event> Keymapper::mapKeyDown(const KeyState& key) {
216 return mapKey(key, true);
217 }
218
mapKeyUp(const KeyState & key)219 List<Event> Keymapper::mapKeyUp(const KeyState& key) {
220 return mapKey(key, false);
221 }
222
mapKey(const KeyState & key,bool keyDown)223 List<Event> Keymapper::mapKey(const KeyState& key, bool keyDown) {
224 if (!_enabled || _activeMaps.empty())
225 return List<Event>();
226
227 Action *action = 0;
228
229 if (keyDown) {
230 // Search for key in active keymap stack
231 for (int i = _activeMaps.size() - 1; i >= 0; --i) {
232 MapRecord mr = _activeMaps[i];
233 debug(5, "Keymapper::mapKey keymap: %s", mr.keymap->getName().c_str());
234 action = mr.keymap->getMappedAction(key);
235
236 if (action || !mr.transparent)
237 break;
238 }
239
240 if (action)
241 _keysDown[key] = action;
242 } else {
243 HashMap<KeyState, Action *>::iterator it = _keysDown.find(key);
244
245 if (it != _keysDown.end()) {
246 action = it->_value;
247 _keysDown.erase(key);
248 }
249 }
250
251 if (!action)
252 return List<Event>();
253
254 return executeAction(action, keyDown ? kIncomingKeyDown : kIncomingKeyUp);
255 }
256
257
mapNonKey(const HardwareInputCode code)258 List<Event> Keymapper::mapNonKey(const HardwareInputCode code) {
259 if (!_enabled || _activeMaps.empty())
260 return List<Event>();
261
262 Action *action = 0;
263
264 // Search for nonkey in active keymap stack
265 for (int i = _activeMaps.size() - 1; i >= 0; --i) {
266 MapRecord mr = _activeMaps[i];
267 debug(5, "Keymapper::mapKey keymap: %s", mr.keymap->getName().c_str());
268 action = mr.keymap->getMappedAction(code);
269
270 if (action || !mr.transparent)
271 break;
272 }
273
274 if (!action)
275 return List<Event>();
276
277 return executeAction(action);
278 }
279
getAction(const KeyState & key)280 Action *Keymapper::getAction(const KeyState& key) {
281 Action *action = 0;
282
283 return action;
284 }
285
executeAction(const Action * action,IncomingEventType incomingType)286 List<Event> Keymapper::executeAction(const Action *action, IncomingEventType incomingType) {
287 List<Event> mappedEvents;
288 List<Event>::const_iterator it;
289 Event evt;
290 for (it = action->events.begin(); it != action->events.end(); ++it) {
291 evt = Event(*it);
292 EventType convertedType = convertDownToUp(evt.type);
293
294 // hardware keys need to send up instead when they are up
295 if (incomingType == kIncomingKeyUp) {
296 if (convertedType == EVENT_INVALID)
297 continue; // don't send any non-down-converted events on up they were already sent on down
298 evt.type = convertedType;
299 }
300
301 evt.mouse = _eventMan->getMousePos();
302
303 // Check if the event is coming from a non-key hardware event
304 // that is mapped to a key event
305 if (incomingType == kIncomingNonKey && convertedType != EVENT_INVALID)
306 // WORKAROUND: Delay the down events coming from non-key hardware events
307 // with a zero delay. This is to prevent DOWN1 DOWN2 UP1 UP2.
308 addDelayedEvent(0, evt);
309 else
310 mappedEvents.push_back(evt);
311
312 // non-keys need to send up as well
313 if (incomingType == kIncomingNonKey && convertedType != EVENT_INVALID) {
314 // WORKAROUND: Delay the up events coming from non-key hardware events
315 // This is for engines that run scripts that check on key being down
316 evt.type = convertedType;
317 const uint32 delay = (convertedType == EVENT_KEYUP ? kDelayKeyboardEventMillis : kDelayMouseEventMillis);
318 addDelayedEvent(delay, evt);
319 }
320 }
321 return mappedEvents;
322 }
323
convertDownToUp(EventType type)324 EventType Keymapper::convertDownToUp(EventType type) {
325 EventType result = EVENT_INVALID;
326 switch (type) {
327 case EVENT_KEYDOWN:
328 result = EVENT_KEYUP;
329 break;
330 case EVENT_LBUTTONDOWN:
331 result = EVENT_LBUTTONUP;
332 break;
333 case EVENT_RBUTTONDOWN:
334 result = EVENT_RBUTTONUP;
335 break;
336 case EVENT_MBUTTONDOWN:
337 result = EVENT_MBUTTONUP;
338 break;
339 default:
340 break;
341 }
342 return result;
343 }
344
findHardwareInput(const KeyState & key)345 const HardwareInput *Keymapper::findHardwareInput(const KeyState& key) {
346 return (_hardwareInputs) ? _hardwareInputs->findHardwareInput(key) : 0;
347 }
348
findHardwareInput(const HardwareInputCode code)349 const HardwareInput *Keymapper::findHardwareInput(const HardwareInputCode code) {
350 return (_hardwareInputs) ? _hardwareInputs->findHardwareInput(code) : 0;
351 }
352
remap(const Event & ev)353 List<Event> Keymapper::remap(const Event &ev) {
354 assert(_remapping);
355 assert(_actionToRemap);
356
357 List<Event> list;
358
359 const HardwareInput *hwInput = 0;
360 Event mappedEvent;
361
362 switch (ev.type) {
363 case EVENT_KEYDOWN:
364 // eat the event by returning an event invalid
365 mappedEvent.type = EVENT_INVALID;
366 list.push_back(mappedEvent);
367 break;
368 case EVENT_KEYUP:
369 hwInput = findHardwareInput(ev.kbd);
370 break;
371 case EVENT_CUSTOM_BACKEND_HARDWARE:
372 hwInput = findHardwareInput(ev.customType);
373 break;
374 default:
375 break;
376 }
377 if (hwInput) {
378 _actionToRemap->mapInput(hwInput);
379 _actionToRemap->getParent()->saveMappings();
380 _remapping = false;
381 _actionToRemap = 0;
382 mappedEvent.type = EVENT_GUI_REMAP_COMPLETE_ACTION;
383 list.push_back(mappedEvent);
384 }
385 return list;
386 }
387
388 } // End of namespace Common
389
390 #endif // #ifdef ENABLE_KEYMAPPER
391