File: hardware-input.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (333 lines) | stat: -rw-r--r-- 9,541 bytes parent folder | download | duplicates (3)
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
/* 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_HARDWARE_KEY_H
#define COMMON_HARDWARE_KEY_H

#include "common/scummsys.h"

#include "common/array.h"
#include "common/events.h"
#include "common/keyboard.h"
#include "common/str.h"

namespace Common {

typedef uint32 HardwareInputCode;

enum HardwareInputType {
	/** Empty / invalid input type */
	kHardwareInputTypeInvalid,
	/** Keyboard input that sends -up and -down events */
	kHardwareInputTypeKeyboard,
	/** Mouse input that sends -up and -down events */
	kHardwareInputTypeMouse,
	/** Joystick input that sends -up and -down events */
	kHardwareInputTypeJoystickButton,
	/** Joystick input that sends "analog" values */
	kHardwareInputTypeJoystickHalfAxis,
	/** Input that sends single events */
	kHardwareInputTypeCustom
};

/**
* Describes an available hardware input
*/
struct HardwareInput {
	/** unique id used for saving/loading to config */
	String id;

	/** Human readable description */
	U32String description;

	/** Type tag */
	HardwareInputType type;

	/**
	 * A platform specific unique identifier for an input event
	 * generated when this input is triggered.
	 * This is only relevant when type == kHardwareInputTypeGeneric
	 */
	HardwareInputCode inputCode;

	/**
	 * The KeyState that is generated by the back-end
	 * when this hardware key is pressed.
	 * This is only relevant when type == kHardwareInputTypeKeyboard
	 */
	KeyState key;

	HardwareInput()
		: inputCode(0), type(kHardwareInputTypeInvalid) { }

	static HardwareInput createCustom(const String &i, HardwareInputCode ic, const U32String &desc) {
		return createSimple(kHardwareInputTypeCustom, i, ic, desc);
	}

	static HardwareInput createKeyboard(const String &i, KeyState ky, const U32String &desc) {
		HardwareInput hardwareInput;
		hardwareInput.id = i;
		hardwareInput.description = desc;
		hardwareInput.type = kHardwareInputTypeKeyboard;
		hardwareInput.inputCode = 0;
		hardwareInput.key = ky;
		return hardwareInput;
	}

	static HardwareInput createJoystickButton(const String &i, uint8 button, const U32String &desc) {
		return createSimple(kHardwareInputTypeJoystickButton, i, button, desc);
	}

	static HardwareInput createJoystickHalfAxis(const String &i, uint8 axis, bool positiveHalf, const U32String &desc) {
		return createSimple(kHardwareInputTypeJoystickHalfAxis, i, axis * 2 + (positiveHalf ? 1 : 0), desc);
	}

	static HardwareInput createMouse(const String &i, uint8 button, const U32String &desc) {
		return createSimple(kHardwareInputTypeMouse, i, button, desc);
	}

private:
	static HardwareInput createSimple(HardwareInputType type, const String &i, HardwareInputCode ic, const U32String &desc) {
		HardwareInput hardwareInput;
		hardwareInput.id = i;
		hardwareInput.description = desc;
		hardwareInput.type = type;
		hardwareInput.inputCode = ic;
		return hardwareInput;
	}
};

/**
 * Entry in a static table of custom backend hardware inputs
 */
struct HardwareInputTableEntry {
	const char *hwId;
	HardwareInputCode code;
	const char *desc;

	static const HardwareInputTableEntry *findWithCode(const HardwareInputTableEntry *_entries, HardwareInputCode code) {
		for (const HardwareInputTableEntry *hw = _entries;  hw->hwId; hw++) {
			if (hw->code == code) {
				return hw;
			}
		}
		return nullptr;
	}

	static const HardwareInputTableEntry *findWithId(const HardwareInputTableEntry *_entries, const String &id) {
		for (const HardwareInputTableEntry *hw = _entries;  hw->hwId; hw++) {
			if (id.equals(hw->hwId)) {
				return hw;
			}
		}
		return nullptr;
	}
};

/**
 * Entry in a static table of available non-modifier keys
 */
struct KeyTableEntry {
	const char *hwId;
	KeyCode keycode;
	const char *desc;
};

/**
 * Entry in a static table of available key modifiers
 */
struct ModifierTableEntry {
	byte flag;
	const char *id;
	const char *desc;
};

enum AxisType {
	/** An axis that sends "analog" values from JOYAXIS_MIN to JOYAXIS_MAX. e.g. a gamepad stick axis */
	kAxisTypeFull,
	/** An axis that sends "analog" values from 0 to JOYAXIS_MAX. e.g. a gamepad trigger */
	kAxisTypeHalf
};

struct AxisTableEntry {
	const char *hwId;
	HardwareInputCode code;
	AxisType type;
	const char *desc;

	static const AxisTableEntry *findWithCode(const AxisTableEntry *_entries, HardwareInputCode code) {
		for (const AxisTableEntry *hw = _entries;  hw->hwId; hw++) {
			if (hw->code == code) {
				return hw;
			}
		}
		return nullptr;
	}

	static const AxisTableEntry *findWithId(const AxisTableEntry *_entries, const String &id) {
		for (const AxisTableEntry *hw = _entries;  hw->hwId; hw++) {
			if (id.equals(hw->hwId)) {
				return hw;
			}
		}
		return nullptr;
	}
};


/**
 * Interface for querying information about a hardware input device
 */
class HardwareInputSet {
public:
	virtual ~HardwareInputSet();

	/**
	 * Retrieve a hardware input description from an unique identifier
	 *
	 * In case no input was found with the specified id, an empty
	 * HardwareInput structure is return with the type set to
	 * kHardwareInputTypeInvalid.
	 */
	virtual HardwareInput findHardwareInput(const String &id) const = 0;

	/**
	 * Retrieve a hardware input description from one of the events
	 * produced when the input is triggered.
	 *
	 * In case the specified event is not produced by this device,
	 * an empty HardwareInput structure is return with the type set to
	 * kHardwareInputTypeInvalid.
	 */
	virtual HardwareInput findHardwareInput(const Event &event) const = 0;
};

/**
 * A keyboard input device
 *
 * Describes the keys and key + modifiers combinations as HardwareInputs
 */
class KeyboardHardwareInputSet : public HardwareInputSet {
public:
	KeyboardHardwareInputSet(const KeyTableEntry *keys, const ModifierTableEntry *modifiers);

	// HardwareInputSet API
	HardwareInput findHardwareInput(const String &id) const override;
	HardwareInput findHardwareInput(const Event &event) const override;

	/** Transform a keystate into a canonical form that can be used to unambiguously identify the keypress */
	static KeyState normalizeKeyState(const KeyState &keystate);

private:
	const KeyTableEntry *_keys;
	const ModifierTableEntry *_modifiers;
};

/**
 * A mouse input device
 *
 * Describes the mouse buttons
 */
class MouseHardwareInputSet : public HardwareInputSet {
public:
	MouseHardwareInputSet(const HardwareInputTableEntry *buttonEntries);

	// HardwareInputSet API
	HardwareInput findHardwareInput(const String &id) const override;
	HardwareInput findHardwareInput(const Event &event) const override;

private:
	const HardwareInputTableEntry *_buttonEntries;
};

/**
 * A joystick input device
 */
class JoystickHardwareInputSet : public HardwareInputSet {
public:
	JoystickHardwareInputSet(const HardwareInputTableEntry *buttonEntries, const AxisTableEntry *axisEntries);

	// HardwareInputSet API
	HardwareInput findHardwareInput(const String &id) const override;
	HardwareInput findHardwareInput(const Event &event) const override;

private:
	const HardwareInputTableEntry *_buttonEntries;
	const AxisTableEntry *_axisEntries;
};

/**
 * A custom backend input device
 *
 * @todo This is currently unused. Perhaps it should be removed.
 */
class CustomHardwareInputSet : public HardwareInputSet {
public:
	CustomHardwareInputSet(const HardwareInputTableEntry *hardwareEntries);

	// HardwareInputSet API
	HardwareInput findHardwareInput(const String &id) const override;
	HardwareInput findHardwareInput(const Event &event) const override;

private:
	const HardwareInputTableEntry *_hardwareEntries;
};

/**
 * A composite input device that delegates to a set of actual input devices.
 */
class CompositeHardwareInputSet : public HardwareInputSet {
public:
	~CompositeHardwareInputSet() override;

	// HardwareInputSet API
	HardwareInput findHardwareInput(const String &id) const override;
	HardwareInput findHardwareInput(const Event &event) const override;

	/**
	 * Add an input device to this composite device
	 *
	 * Takes ownership of the hardware input set
	 */
	void addHardwareInputSet(HardwareInputSet *hardwareInputSet);

private:
	Array<HardwareInputSet *> _inputSets;
};

/** A standard set of keyboard keys */
extern const KeyTableEntry defaultKeys[];

/** A standard set of keyboard modifiers */
extern const ModifierTableEntry defaultModifiers[];

/** A standard set of mouse buttons */
extern const HardwareInputTableEntry defaultMouseButtons[];

/** A standard set of joystick buttons based on the ScummVM event model */
extern const HardwareInputTableEntry defaultJoystickButtons[];

/** A standard set of joystick axes based on the ScummVM event model */
extern const AxisTableEntry defaultJoystickAxes[];

} // End of namespace Common

#endif // #ifndef COMMON_HARDWARE_KEY_H