Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/KeyMap.h
5651 views
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <string>
21
#include <string_view>
22
#include <map>
23
#include <vector>
24
#include <set>
25
26
#include "Common/Input/InputState.h" // InputMapping
27
#include "Common/Input/KeyCodes.h" // keyboard keys
28
#include "Common/Data/Collections/TinySet.h"
29
#include "Core/KeyMapDefaults.h"
30
31
#define KEYMAP_ERROR_KEY_ALREADY_USED -1
32
#define KEYMAP_ERROR_UNKNOWN_KEY 0
33
34
// Don't change any of these - it'll break backwards compatibility with configs.
35
enum VirtKey {
36
VIRTKEY_FIRST = 0x40000001,
37
VIRTKEY_AXIS_X_MIN = 0x40000001,
38
VIRTKEY_AXIS_Y_MIN = 0x40000002,
39
VIRTKEY_AXIS_X_MAX = 0x40000003,
40
VIRTKEY_AXIS_Y_MAX = 0x40000004,
41
VIRTKEY_RAPID_FIRE = 0x40000005,
42
VIRTKEY_FASTFORWARD = 0x40000006,
43
VIRTKEY_PAUSE = 0x40000007,
44
VIRTKEY_SPEED_TOGGLE = 0x40000008,
45
VIRTKEY_AXIS_RIGHT_X_MIN = 0x40000009,
46
VIRTKEY_AXIS_RIGHT_Y_MIN = 0x4000000a,
47
VIRTKEY_AXIS_RIGHT_X_MAX = 0x4000000b,
48
VIRTKEY_AXIS_RIGHT_Y_MAX = 0x4000000c,
49
VIRTKEY_REWIND = 0x4000000d,
50
VIRTKEY_SAVE_STATE = 0x4000000e,
51
VIRTKEY_LOAD_STATE = 0x4000000f,
52
VIRTKEY_NEXT_SLOT = 0x40000010,
53
VIRTKEY_TOGGLE_FULLSCREEN = 0x40000011,
54
VIRTKEY_ANALOG_LIGHTLY = 0x40000012,
55
VIRTKEY_AXIS_SWAP = 0x40000013,
56
VIRTKEY_DEVMENU = 0x40000014,
57
VIRTKEY_FRAME_ADVANCE = 0x40000015,
58
VIRTKEY_RECORD = 0x40000016,
59
VIRTKEY_SPEED_CUSTOM1 = 0x40000017,
60
VIRTKEY_SPEED_CUSTOM2 = 0x40000018,
61
VIRTKEY_TEXTURE_DUMP = 0x40000019,
62
VIRTKEY_TEXTURE_REPLACE = 0x4000001A,
63
VIRTKEY_SCREENSHOT = 0x4000001B,
64
VIRTKEY_MUTE_TOGGLE = 0x4000001C,
65
VIRTKEY_OPENCHAT = 0x4000001D,
66
VIRTKEY_ANALOG_ROTATE_CW = 0x4000001E,
67
VIRTKEY_ANALOG_ROTATE_CCW = 0x4000001F,
68
VIRTKEY_SCREEN_ROTATION_VERTICAL = 0x40000020,
69
VIRTKEY_SCREEN_ROTATION_VERTICAL180 = 0x40000021,
70
VIRTKEY_SCREEN_ROTATION_HORIZONTAL = 0x40000022,
71
VIRTKEY_SCREEN_ROTATION_HORIZONTAL180 = 0x40000023,
72
VIRTKEY_SPEED_ANALOG = 0x40000024,
73
VIRTKEY_VR_CAMERA_ADJUST = 0x40000025,
74
VIRTKEY_VR_CAMERA_RESET = 0x40000026,
75
VIRTKEY_PREVIOUS_SLOT = 0x40000027,
76
VIRTKEY_TOGGLE_WLAN = 0x40000028,
77
VIRTKEY_EXIT_APP = 0x40000029,
78
VIRTKEY_TOGGLE_MOUSE = 0x40000030,
79
VIRTKEY_TOGGLE_TOUCH_CONTROLS = 0x40000031,
80
VIRTKEY_RESET_EMULATION = 0x40000032,
81
VIRTKEY_TOGGLE_DEBUGGER = 0x40000033,
82
VIRTKEY_PAUSE_NO_MENU = 0x40000034,
83
VIRTKEY_TOGGLE_TILT = 0x40000035,
84
VIRTKEY_LAST,
85
VIRTKEY_COUNT = VIRTKEY_LAST - VIRTKEY_FIRST
86
};
87
88
struct MappedAnalogAxis {
89
int axisId;
90
int direction;
91
};
92
93
struct MappedAnalogAxes {
94
MappedAnalogAxis leftX;
95
MappedAnalogAxis leftY;
96
MappedAnalogAxis rightX;
97
MappedAnalogAxis rightY;
98
};
99
100
// KeyMap
101
// A translation layer for key assignment. Provides
102
// integration with Core's config state.
103
//
104
// Does not handle input state managment.
105
//
106
// Platform ports should map their platform's keys to KeyMap's keys (NKCODE_*).
107
//
108
// Then have KeyMap transform those into psp buttons.
109
110
class IniFile;
111
112
namespace KeyMap {
113
// Combo of InputMappings.
114
struct MultiInputMapping {
115
MultiInputMapping() {}
116
explicit MultiInputMapping(const InputMapping &mapping) {
117
mappings.push_back(mapping);
118
}
119
120
static MultiInputMapping FromConfigString(std::string_view str);
121
std::string ToConfigString() const;
122
std::string ToVisualString() const;
123
124
bool operator <(const MultiInputMapping &other) {
125
for (size_t i = 0; i < mappings.capacity(); i++) {
126
// If one ran out of entries, the other wins.
127
if (mappings.size() == i && other.mappings.size() > i) return true;
128
if (mappings.size() >= i && other.mappings.size() == i) return false;
129
if (mappings[i] < other.mappings[i]) return true;
130
if (mappings[i] > other.mappings[i]) return false;
131
}
132
return false;
133
}
134
135
bool operator ==(const MultiInputMapping &other) const {
136
return mappings == other.mappings;
137
}
138
139
bool EqualsSingleMapping(const InputMapping &other) const {
140
return mappings.size() == 1 && mappings[0] == other;
141
}
142
143
bool empty() const {
144
return mappings.empty();
145
}
146
147
bool HasMouse() const {
148
for (auto &m : mappings) {
149
return m.deviceId == DEVICE_ID_MOUSE;
150
}
151
return false;
152
}
153
154
FixedVec<InputMapping, 3> mappings;
155
};
156
157
typedef std::map<int, std::vector<MultiInputMapping>> KeyMapping;
158
159
// Once the multimappings are inserted here, they must not be empty.
160
// If one would be, delete the whole entry from the map instead.
161
// This is automatically handled by SetInputMapping.
162
extern std::set<InputDeviceID> g_seenDeviceIds;
163
extern int g_controllerMapGeneration;
164
// Key & Button names
165
struct KeyMap_IntStrPair {
166
int key;
167
const char *name;
168
};
169
170
// Use if you need to display the textual name
171
std::string GetKeyName(InputKeyCode keyCode);
172
std::string GetKeyOrAxisName(const InputMapping &mapping);
173
std::string GetAxisName(int axisId);
174
std::string GetPspButtonName(int btn);
175
const char *GetPspButtonNameCharPointer(int btn);
176
177
const KeyMap_IntStrPair *GetMappableKeys(size_t *count);
178
179
// Use to translate input mappings to and from PSP buttons. You should have already translated
180
// your platform's keys to InputMapping keys.
181
182
// Note that this one does not handle combos, since there's only one input.
183
bool InputMappingToPspButton(const InputMapping &mapping, std::vector<int> *pspButtons);
184
bool InputMappingsFromPspButton(int btn, std::vector<MultiInputMapping> *keys, bool ignoreMouse);
185
186
// Careful with these.
187
bool InputMappingsFromPspButtonNoLock(int btn, std::vector<MultiInputMapping> *keys, bool ignoreMouse);
188
void LockMappings();
189
void UnlockMappings();
190
191
// Simplified check.
192
bool PspButtonHasMappings(int btn);
193
194
// Configure the key or axis mapping.
195
// Any configuration will be saved to the Core config.
196
void SetInputMapping(int psp_key, const MultiInputMapping &key, bool replace);
197
// Return false if bind was a duplicate and got removed
198
bool ReplaceSingleKeyMapping(int btn, int index, const MultiInputMapping &key);
199
200
MappedAnalogAxes MappedAxesForDevice(InputDeviceID deviceId);
201
202
void LoadFromIni(IniFile &iniFile);
203
void SaveToIni(IniFile &iniFile);
204
void ClearAllMappings();
205
void DeleteNthMapping(int key, int number);
206
207
void SetDefaultKeyMap(DefaultMaps dmap, bool replace);
208
209
void RestoreDefault();
210
211
void UpdateNativeMenuKeys();
212
213
void NotifyPadConnected(InputDeviceID deviceId, std::string_view name);
214
void NotifyPadDisconnected(InputDeviceID deviceId);
215
216
bool IsNvidiaShield(std::string_view name);
217
bool IsNvidiaShieldTV(std::string_view name);
218
bool IsXperiaPlay(std::string_view name);
219
bool IsMOQII7S(std::string_view name);
220
bool IsRetroid(std::string_view name);
221
bool HasBuiltinController(std::string_view name);
222
223
const std::set<std::string> &GetSeenPads();
224
std::string PadName(InputDeviceID deviceId);
225
void AutoConfForPad(std::string_view name);
226
227
bool IsKeyMapped(InputDeviceID device, int key);
228
229
bool HasChanged(int &prevGeneration);
230
231
// Used for setting thresholds. Technically we could allow a setting per axis, but this is a reasonable compromise.
232
enum class AxisType {
233
TRIGGER,
234
STICK,
235
OTHER,
236
};
237
238
AxisType GetAxisType(InputAxis axis);
239
} // namespace KeyMap
240
241