Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Input/InputState.cpp
3186 views
1
#include <vector>
2
#include <cstdio>
3
4
#include "Common/Input/InputState.h"
5
#include "Common/Input/KeyCodes.h"
6
#include "Common/StringUtils.h"
7
8
const char *GetDeviceName(int deviceId) {
9
switch (deviceId) {
10
case DEVICE_ID_ANY: return "any";
11
case DEVICE_ID_DEFAULT: return "built-in";
12
case DEVICE_ID_KEYBOARD: return "kbd";
13
case DEVICE_ID_PAD_0: return "pad1";
14
case DEVICE_ID_PAD_1: return "pad2";
15
case DEVICE_ID_PAD_2: return "pad3";
16
case DEVICE_ID_PAD_3: return "pad4";
17
case DEVICE_ID_PAD_4: return "pad5";
18
case DEVICE_ID_PAD_5: return "pad6";
19
case DEVICE_ID_PAD_6: return "pad7";
20
case DEVICE_ID_PAD_7: return "pad8";
21
case DEVICE_ID_PAD_8: return "pad9";
22
case DEVICE_ID_PAD_9: return "pad10";
23
case DEVICE_ID_XINPUT_0: return "x360"; // keeping these strings for backward compat. Hm, what would break if we changed them to xbox?
24
case DEVICE_ID_XINPUT_1: return "x360_2";
25
case DEVICE_ID_XINPUT_2: return "x360_3";
26
case DEVICE_ID_XINPUT_3: return "x360_4";
27
case DEVICE_ID_ACCELEROMETER: return "accelerometer";
28
case DEVICE_ID_MOUSE: return "mouse";
29
case DEVICE_ID_XR_CONTROLLER_LEFT: return "xr_l";
30
case DEVICE_ID_XR_CONTROLLER_RIGHT: return "xr_r";
31
default:
32
return "unknown";
33
}
34
}
35
36
bool g_IsMappingMouseInput;
37
38
std::vector<InputMapping> dpadKeys;
39
std::vector<InputMapping> confirmKeys;
40
std::vector<InputMapping> cancelKeys;
41
std::vector<InputMapping> infoKeys;
42
std::vector<InputMapping> tabLeftKeys;
43
std::vector<InputMapping> tabRightKeys;
44
static std::unordered_map<InputDeviceID, int> uiFlipAnalogY;
45
46
static void AppendKeys(std::vector<InputMapping> &keys, const std::vector<InputMapping> &newKeys) {
47
for (const auto &key : newKeys) {
48
keys.push_back(key);
49
}
50
}
51
52
void SetDPadKeys(const std::vector<InputMapping> &leftKey, const std::vector<InputMapping> &rightKey,
53
const std::vector<InputMapping> &upKey, const std::vector<InputMapping> &downKey) {
54
dpadKeys.clear();
55
56
// Store all directions into one vector for now. In the future it might be
57
// useful to keep track of the different directions separately.
58
AppendKeys(dpadKeys, leftKey);
59
AppendKeys(dpadKeys, rightKey);
60
AppendKeys(dpadKeys, upKey);
61
AppendKeys(dpadKeys, downKey);
62
}
63
64
void SetConfirmCancelKeys(const std::vector<InputMapping> &confirm, const std::vector<InputMapping> &cancel) {
65
confirmKeys = confirm;
66
cancelKeys = cancel;
67
}
68
69
void SetTabLeftRightKeys(const std::vector<InputMapping> &tabLeft, const std::vector<InputMapping> &tabRight) {
70
tabLeftKeys = tabLeft;
71
tabRightKeys = tabRight;
72
}
73
74
void SetInfoKeys(const std::vector<InputMapping> &info) {
75
infoKeys = info;
76
}
77
78
void SetAnalogFlipY(const std::unordered_map<InputDeviceID, int> &flipYByDeviceId) {
79
uiFlipAnalogY = flipYByDeviceId;
80
}
81
82
int GetAnalogYDirection(InputDeviceID deviceId) {
83
auto configured = uiFlipAnalogY.find(deviceId);
84
if (configured != uiFlipAnalogY.end())
85
return configured->second;
86
return 0;
87
}
88
89
// NOTE: Changing the format of FromConfigString/ToConfigString breaks controls.ini backwards compatibility.
90
InputMapping InputMapping::FromConfigString(const std::string_view str) {
91
std::vector<std::string_view> parts;
92
SplitString(str, '-', parts);
93
// We only convert to std::string here to add null terminators for atoi.
94
InputDeviceID deviceId = (InputDeviceID)(atoi(std::string(parts[0]).c_str()));
95
InputKeyCode keyCode = (InputKeyCode)atoi(std::string(parts[1]).c_str());
96
97
InputMapping mapping;
98
mapping.deviceId = deviceId;
99
mapping.keyCode = keyCode;
100
return mapping;
101
}
102
103
std::string InputMapping::ToConfigString() const {
104
return StringFromFormat("%d-%d", (int)deviceId, keyCode);
105
}
106
107
void InputMapping::FormatDebug(char *buffer, size_t bufSize) const {
108
if (IsAxis()) {
109
int direction;
110
int axis = Axis(&direction);
111
snprintf(buffer, bufSize, "Device: %d Axis: %d (%d)", (int)deviceId, axis, direction);
112
} else {
113
snprintf(buffer, bufSize, "Device: %d Key: %d", (int)deviceId, keyCode);
114
}
115
}
116
117