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/Common/Input/InputState.cpp
Views: 1401
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
std::vector<InputMapping> dpadKeys;
37
std::vector<InputMapping> confirmKeys;
38
std::vector<InputMapping> cancelKeys;
39
std::vector<InputMapping> infoKeys;
40
std::vector<InputMapping> tabLeftKeys;
41
std::vector<InputMapping> tabRightKeys;
42
static std::unordered_map<InputDeviceID, int> uiFlipAnalogY;
43
44
static void AppendKeys(std::vector<InputMapping> &keys, const std::vector<InputMapping> &newKeys) {
45
for (const auto &key : newKeys) {
46
keys.push_back(key);
47
}
48
}
49
50
void SetDPadKeys(const std::vector<InputMapping> &leftKey, const std::vector<InputMapping> &rightKey,
51
const std::vector<InputMapping> &upKey, const std::vector<InputMapping> &downKey) {
52
dpadKeys.clear();
53
54
// Store all directions into one vector for now. In the future it might be
55
// useful to keep track of the different directions separately.
56
AppendKeys(dpadKeys, leftKey);
57
AppendKeys(dpadKeys, rightKey);
58
AppendKeys(dpadKeys, upKey);
59
AppendKeys(dpadKeys, downKey);
60
}
61
62
void SetConfirmCancelKeys(const std::vector<InputMapping> &confirm, const std::vector<InputMapping> &cancel) {
63
confirmKeys = confirm;
64
cancelKeys = cancel;
65
}
66
67
void SetTabLeftRightKeys(const std::vector<InputMapping> &tabLeft, const std::vector<InputMapping> &tabRight) {
68
tabLeftKeys = tabLeft;
69
tabRightKeys = tabRight;
70
}
71
72
void SetInfoKeys(const std::vector<InputMapping> &info) {
73
infoKeys = info;
74
}
75
76
void SetAnalogFlipY(const std::unordered_map<InputDeviceID, int> &flipYByDeviceId) {
77
uiFlipAnalogY = flipYByDeviceId;
78
}
79
80
int GetAnalogYDirection(InputDeviceID deviceId) {
81
auto configured = uiFlipAnalogY.find(deviceId);
82
if (configured != uiFlipAnalogY.end())
83
return configured->second;
84
return 0;
85
}
86
87
// NOTE: Changing the format of FromConfigString/ToConfigString breaks controls.ini backwards compatibility.
88
InputMapping InputMapping::FromConfigString(const std::string_view str) {
89
std::vector<std::string_view> parts;
90
SplitString(str, '-', parts);
91
// We only convert to std::string here to add null terminators for atoi.
92
InputDeviceID deviceId = (InputDeviceID)(atoi(std::string(parts[0]).c_str()));
93
InputKeyCode keyCode = (InputKeyCode)atoi(std::string(parts[1]).c_str());
94
95
InputMapping mapping;
96
mapping.deviceId = deviceId;
97
mapping.keyCode = keyCode;
98
return mapping;
99
}
100
101
std::string InputMapping::ToConfigString() const {
102
return StringFromFormat("%d-%d", (int)deviceId, keyCode);
103
}
104
105
void InputMapping::FormatDebug(char *buffer, size_t bufSize) const {
106
if (IsAxis()) {
107
int direction;
108
int axis = Axis(&direction);
109
snprintf(buffer, bufSize, "Device: %d Axis: %d (%d)", (int)deviceId, axis, direction);
110
} else {
111
snprintf(buffer, bufSize, "Device: %d Key: %d", (int)deviceId, keyCode);
112
}
113
}
114
115