CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/KeyMap.h
Views: 1401
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 {
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_LAST,
81
VIRTKEY_COUNT = VIRTKEY_LAST - VIRTKEY_FIRST
82
};
83
84
struct MappedAnalogAxis {
85
int axisId;
86
int direction;
87
};
88
89
struct MappedAnalogAxes {
90
MappedAnalogAxis leftX;
91
MappedAnalogAxis leftY;
92
MappedAnalogAxis rightX;
93
MappedAnalogAxis rightY;
94
};
95
96
// KeyMap
97
// A translation layer for key assignment. Provides
98
// integration with Core's config state.
99
//
100
// Does not handle input state managment.
101
//
102
// Platform ports should map their platform's keys to KeyMap's keys (NKCODE_*).
103
//
104
// Then have KeyMap transform those into psp buttons.
105
106
class IniFile;
107
108
namespace KeyMap {
109
// Combo of InputMappings.
110
struct MultiInputMapping {
111
MultiInputMapping() {}
112
explicit MultiInputMapping(const InputMapping &mapping) {
113
mappings.push_back(mapping);
114
}
115
116
static MultiInputMapping FromConfigString(std::string_view str);
117
std::string ToConfigString() const;
118
std::string ToVisualString() const;
119
120
bool operator <(const MultiInputMapping &other) {
121
for (size_t i = 0; i < mappings.capacity(); i++) {
122
// If one ran out of entries, the other wins.
123
if (mappings.size() == i && other.mappings.size() > i) return true;
124
if (mappings.size() >= i && other.mappings.size() == i) return false;
125
if (mappings[i] < other.mappings[i]) return true;
126
if (mappings[i] > other.mappings[i]) return false;
127
}
128
return false;
129
}
130
131
bool operator ==(const MultiInputMapping &other) const {
132
return mappings == other.mappings;
133
}
134
135
bool EqualsSingleMapping(const InputMapping &other) const {
136
return mappings.size() == 1 && mappings[0] == other;
137
}
138
139
bool empty() const {
140
return mappings.empty();
141
}
142
143
bool HasMouse() const {
144
for (auto &m : mappings) {
145
return m.deviceId == DEVICE_ID_MOUSE;
146
}
147
return false;
148
}
149
150
FixedVec<InputMapping, 3> mappings;
151
};
152
153
typedef std::map<int, std::vector<MultiInputMapping>> KeyMapping;
154
155
// Once the multimappings are inserted here, they must not be empty.
156
// If one would be, delete the whole entry from the map instead.
157
// This is automatically handled by SetInputMapping.
158
extern std::set<InputDeviceID> g_seenDeviceIds;
159
extern int g_controllerMapGeneration;
160
// Key & Button names
161
struct KeyMap_IntStrPair {
162
int key;
163
const char *name;
164
};
165
166
// Use if you need to display the textual name
167
std::string GetKeyName(InputKeyCode keyCode);
168
std::string GetKeyOrAxisName(const InputMapping &mapping);
169
std::string GetAxisName(int axisId);
170
std::string GetPspButtonName(int btn);
171
const char *GetVirtKeyName(int vkey);
172
const char *GetPspButtonNameCharPointer(int btn);
173
174
const KeyMap_IntStrPair *GetMappableKeys(size_t *count);
175
176
// Use to translate input mappings to and from PSP buttons. You should have already translated
177
// your platform's keys to InputMapping keys.
178
179
// Note that this one does not handle combos, since there's only one input.
180
bool InputMappingToPspButton(const InputMapping &mapping, std::vector<int> *pspButtons);
181
bool InputMappingsFromPspButton(int btn, std::vector<MultiInputMapping> *keys, bool ignoreMouse);
182
183
// Careful with these.
184
bool InputMappingsFromPspButtonNoLock(int btn, std::vector<MultiInputMapping> *keys, bool ignoreMouse);
185
void LockMappings();
186
void UnlockMappings();
187
188
// Simplified check.
189
bool PspButtonHasMappings(int btn);
190
191
// Configure the key or axis mapping.
192
// Any configuration will be saved to the Core config.
193
void SetInputMapping(int psp_key, const MultiInputMapping &key, bool replace);
194
// Return false if bind was a duplicate and got removed
195
bool ReplaceSingleKeyMapping(int btn, int index, const MultiInputMapping &key);
196
197
MappedAnalogAxes MappedAxesForDevice(InputDeviceID deviceId);
198
199
void LoadFromIni(IniFile &iniFile);
200
void SaveToIni(IniFile &iniFile);
201
void ClearAllMappings();
202
void DeleteNthMapping(int key, int number);
203
204
void SetDefaultKeyMap(DefaultMaps dmap, bool replace);
205
206
void RestoreDefault();
207
208
void UpdateNativeMenuKeys();
209
210
void NotifyPadConnected(InputDeviceID deviceId, const std::string &name);
211
bool IsNvidiaShield(const std::string &name);
212
bool IsNvidiaShieldTV(const std::string &name);
213
bool IsXperiaPlay(const std::string &name);
214
bool IsOuya(const std::string &name);
215
bool IsMOQII7S(const std::string &name);
216
bool IsRetroid(const std::string &name);
217
bool HasBuiltinController(const std::string &name);
218
219
const std::set<std::string> &GetSeenPads();
220
std::string PadName(InputDeviceID deviceId);
221
void AutoConfForPad(const std::string &name);
222
223
bool IsKeyMapped(InputDeviceID device, int key);
224
225
bool HasChanged(int &prevGeneration);
226
227
// Used for setting thresholds. Technically we could allow a setting per axis, but this is a reasonable compromise.
228
enum class AxisType {
229
TRIGGER,
230
STICK,
231
OTHER,
232
};
233
234
AxisType GetAxisType(InputAxis axis);
235
} // namespace KeyMap
236
237