File: keymapper.h

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (224 lines) | stat: -rw-r--r-- 6,149 bytes parent folder | download | duplicates (2)
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
/* 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/>.
 *
 */

#ifndef COMMON_KEYMAPPER_H
#define COMMON_KEYMAPPER_H

#include "common/scummsys.h"

#include "backends/keymapper/keymap.h"

#include "common/array.h"
#include "common/config-manager.h"
#include "common/events.h"

namespace Common {

const char *const kGuiKeymapName = "gui";
const char *const kGlobalKeymapName = "global";

struct Action;
class DelayedEventSource;
struct HardwareInput;
class HardwareInputSet;
class KeymapperDefaultBindings;

class Keymapper : public Common::EventMapper {
public:

	Keymapper(EventManager *eventMan);
	~Keymapper();

	// EventMapper interface
	virtual bool mapEvent(const Event &ev, List<Event> &mappedEvents);

	/**
	 * Registers a HardwareInputSet and platform-specific default mappings with the Keymapper
	 *
	 * Transfers ownership to the Keymapper
	 */
	void registerHardwareInputSet(HardwareInputSet *inputs, KeymapperDefaultBindings *backendDefaultBindings);

	/**
	 * Add a keymap to the global domain.
	 * If a saved key setup exists for it in the ini file it will be used.
	 * Else, the key setup will be automatically mapped.
	 *
	 * Transfers ownership of the keymap to the Keymapper
	 */
	void addGlobalKeymap(Keymap *keymap);

	/**
	 * Add a keymap to the game domain.
	 *
	 * Transfers ownership of the keymap to the Keymapper
	 *
	 * @see addGlobalKeyMap
	 * @note initGame() should be called before any game keymaps are added.
	 */
	void addGameKeymap(Keymap *keymap);

	/**
	 * Should be called at end of game to tell Keymapper to deactivate and free
	 * any game keymaps that are loaded.
	 */
	void cleanupGameKeymaps();

	/**
	 * This allows to specify which Game Keymaps are enabled or disabled.
	 * @param id		ID of the game keymap to enable/disable.
	 * @param enable	Whether the keymap is enabled(True means enabled)
	 */
	void setGameKeymapState(const String &id, bool enable);

	/**
	 * Disables all game keymaps that are loaded.
	 */
	void disableAllGameKeymaps();

	/**
	 * Obtain a keymap of the given name from the keymapper.
	 * Game keymaps have priority over global keymaps
	 * @param id		name of the keymap to return
	 */
	Keymap *getKeymap(const String &id) const;

	/**
	 * Obtain a list of all the keymaps registered with the keymapper
	 */
	const KeymapArray &getKeymaps() const { return _keymaps; }

	/**
	 * reload the mappings for all the keymaps from the configuration manager
	 */
	void reloadAllMappings();

	/**
	 * Set which kind of keymap is currently used to map events
	 *
	 * Keymaps with the global type are always enabled
	 */
	void setEnabledKeymapType(Keymap::KeymapType type);
	Keymap::KeymapType enabledKeymapType() const { return _enabledKeymapType; }

	/**
	 * Enable/disable the keymapper
	 */
	void setEnabled(bool enabled) { _enabled = enabled; }

	/**
	 * Return the keymapper's enabled state
	 */
	bool isEnabled() const { return _enabled; }

	/**
	 * Clear all the keymaps and hardware input sets
	 */
	void clear();

	/**
	 * Return a HardwareInput pointer for the given event
	 */
	HardwareInput findHardwareInput(const Event &event);

	void initKeymap(Keymap *keymap, ConfigManager::Domain *domain);
	void reloadKeymapMappings(Keymap *keymap);

private:
	EventManager *_eventMan;
	HardwareInputSet *_hardwareInputs;
	KeymapperDefaultBindings *_backendDefaultBindings;
	DelayedEventSource *_delayedEventSource;

	enum IncomingEventType {
		kIncomingEventIgnored,
		kIncomingEventStart,
		kIncomingEventEnd,
		kIncomingEventInstant
	};

	enum {
		kJoyAxisPressedTreshold   = Common::JOYAXIS_MAX / 2,
		kJoyAxisUnpressedTreshold = Common::JOYAXIS_MAX / 4
	};

	bool _enabled;
	Keymap::KeymapType _enabledKeymapType;

	KeymapArray _keymaps;

	bool _joystickAxisPreviouslyPressed[8]; // size should match the number of valid axis entries of defaultJoystickAxes (in hardware-input.cpp)

	Keymap::KeymapMatch getMappedActions(const Event &event, Keymap::ActionArray &actions, Keymap::KeymapType keymapType) const;
	Event executeAction(const Action *act, const Event &incomingEvent);
	EventType convertStartToEnd(EventType eventType);
	IncomingEventType convertToIncomingEventType(const Event &ev) const;

	void hardcodedEventMapping(Event ev);
	void resetInputState();
};

/**
 * RAII helper to temporarily enable a keymap type
 */
class KeymapTypeEnabler {
public:
	KeymapTypeEnabler(Keymapper *keymapper, Keymap::KeymapType keymapType) :
			_keymapper(keymapper) {
		assert(keymapper);
		_previousKeymapType = keymapper->enabledKeymapType();
		keymapper->setEnabledKeymapType(keymapType);
	}

	~KeymapTypeEnabler() {
		_keymapper->setEnabledKeymapType(_previousKeymapType);
	}

private:
	Keymapper *_keymapper;
	Keymap::KeymapType _previousKeymapType;
};

class DelayedEventSource : public EventSource {
public:
	// EventSource API
	bool pollEvent(Event &event) override;
	bool allowMapping() const override;

	/**
	 * Schedule an event to be produced after the specified delay
	 */
	void scheduleEvent(const Event &ev, uint32 delayMillis);

private:
	struct DelayedEventsEntry {
		const uint32 timerOffset;
		const Event event;
		DelayedEventsEntry(const uint32 offset, const Event ev) : timerOffset(offset), event(ev) { }
	};

	Queue<DelayedEventsEntry> _delayedEvents;
	uint32 _delayedEffectiveTime;
};

} // End of namespace Common

#endif // #ifndef COMMON_KEYMAPPER_H