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