Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/RSDKv5/RSDK/Input/Input.cpp
1163 views
1
#include "RSDK/Core/RetroEngine.hpp"
2
3
using namespace RSDK;
4
5
InputDevice *RSDK::inputDeviceList[INPUTDEVICE_COUNT];
6
int32 RSDK::inputDeviceCount = 0;
7
8
int32 RSDK::inputSlots[PLAYER_COUNT] = { INPUT_NONE, INPUT_NONE, INPUT_NONE, INPUT_NONE };
9
InputDevice *RSDK::inputSlotDevices[PLAYER_COUNT] = { NULL, NULL, NULL, NULL };
10
11
ControllerState RSDK::controller[PLAYER_COUNT + 1];
12
AnalogState RSDK::stickL[PLAYER_COUNT + 1];
13
#if RETRO_REV02
14
AnalogState RSDK::stickR[PLAYER_COUNT + 1];
15
TriggerState RSDK::triggerL[PLAYER_COUNT + 1];
16
TriggerState RSDK::triggerR[PLAYER_COUNT + 1];
17
#endif
18
TouchInfo RSDK::touchInfo;
19
20
GamePadMappings *RSDK::gamePadMappings = NULL;
21
int32 RSDK::gamePadCount = 0;
22
23
#if RETRO_INPUTDEVICE_KEYBOARD
24
#include "Keyboard/KBInputDevice.cpp"
25
#endif
26
27
#if RETRO_INPUTDEVICE_XINPUT
28
#include "XInput/XInputDevice.cpp"
29
#endif
30
31
#if RETRO_INPUTDEVICE_RAWINPUT
32
#include "RawInput/RawInputDevice.cpp"
33
#endif
34
35
#if RETRO_INPUTDEVICE_STEAM
36
#include "Steam/SteamInputDevice.cpp"
37
#endif
38
39
#if RETRO_INPUTDEVICE_NX
40
#include "NX/NXInputDevice.cpp"
41
#endif
42
43
#if RETRO_INPUTDEVICE_SDL2
44
#include "SDL2/SDL2InputDevice.cpp"
45
#endif
46
47
#if RETRO_INPUTDEVICE_GLFW
48
#include "GLFW/GLFWInputDevice.cpp"
49
#endif
50
51
#if RETRO_INPUTDEVICE_PDBOAT
52
#include "Paddleboat/PDBInputDevice.cpp"
53
#endif
54
55
void RSDK::RemoveInputDevice(InputDevice *targetDevice)
56
{
57
if (targetDevice) {
58
for (int32 d = 0; d < inputDeviceCount; ++d) {
59
if (inputDeviceList[d] && inputDeviceList[d] == targetDevice) {
60
uint32 deviceID = targetDevice->id;
61
targetDevice->CloseDevice();
62
inputDeviceCount--;
63
64
delete inputDeviceList[d];
65
inputDeviceList[d] = NULL;
66
67
for (int32 id = d + 1; id <= inputDeviceCount && id < INPUTDEVICE_COUNT; ++id) inputDeviceList[id - 1] = inputDeviceList[id];
68
// clear end device duplicate, prevents issues with new devices deleting stuff they shouldn't be
69
if (inputDeviceCount < INPUTDEVICE_COUNT)
70
inputDeviceList[inputDeviceCount] = NULL;
71
72
for (int32 id = 0; id < PLAYER_COUNT; ++id) {
73
if (inputSlots[id] == deviceID) {
74
#if !RETRO_REV02
75
inputSlots[id] = INPUT_NONE;
76
#endif
77
inputSlotDevices[id] = NULL;
78
}
79
}
80
81
for (int32 id = 0; id < PLAYER_COUNT; ++id) {
82
for (int32 c = 0; c < inputDeviceCount; ++c) {
83
if (inputDeviceList[c] && inputDeviceList[c]->id == inputSlots[id]) {
84
if (inputSlotDevices[id] != inputDeviceList[c])
85
inputSlotDevices[id] = inputDeviceList[c];
86
}
87
}
88
}
89
}
90
}
91
}
92
}
93
94
void RSDK::InitInputDevices()
95
{
96
#if !RETRO_USE_ORIGINAL_CODE
97
// default the input slot state to "auto assign" rather than "none"
98
// this fixes the "controller disconnected" popup since the engine handles the autoassign
99
// without this, the engine has to wait for the game to tell the engine to start autoassignments
100
for (int32 i = 0; i < PLAYER_COUNT; ++i) inputSlots[i] = INPUT_AUTOASSIGN;
101
#endif
102
103
#if RETRO_INPUTDEVICE_KEYBOARD
104
SKU::InitKeyboardInputAPI();
105
#endif
106
107
#if RETRO_INPUTDEVICE_RAWINPUT
108
SKU::InitHIDAPI();
109
#endif
110
111
#if RETRO_INPUTDEVICE_XINPUT
112
SKU::InitXInputAPI();
113
#endif
114
115
#if RETRO_INPUTDEVICE_STEAM
116
SKU::InitSteamInputAPI();
117
#endif
118
119
#if RETRO_INPUTDEVICE_NX
120
SKU::InitNXInputAPI();
121
#endif
122
123
#if RETRO_INPUTDEVICE_SDL2
124
SKU::InitSDL2InputAPI();
125
#endif
126
127
#if RETRO_INPUTDEVICE_GLFW
128
SKU::InitGLFWInputAPI();
129
#endif
130
131
#if RETRO_INPUTDEVICE_PDBOAT
132
SKU::InitPaddleboatInputAPI();
133
#endif
134
}
135
136
void RSDK::ReleaseInputDevices()
137
{
138
#if RETRO_INPUTDEVICE_SDL2
139
SKU::ReleaseSDL2InputAPI();
140
#endif
141
}
142
143
void RSDK::ClearInput()
144
{
145
for (int32 i = 0; i <= PLAYER_COUNT; ++i) {
146
if (i != 0 && inputSlots[i - 1] == INPUT_UNASSIGNED)
147
continue;
148
149
controller[i].keyUp.press = false;
150
controller[i].keyDown.press = false;
151
controller[i].keyLeft.press = false;
152
controller[i].keyRight.press = false;
153
controller[i].keyA.press = false;
154
controller[i].keyB.press = false;
155
controller[i].keyC.press = false;
156
controller[i].keyX.press = false;
157
controller[i].keyY.press = false;
158
controller[i].keyZ.press = false;
159
controller[i].keyStart.press = false;
160
controller[i].keySelect.press = false;
161
162
stickL[i].keyUp.press = false;
163
stickL[i].keyDown.press = false;
164
stickL[i].keyLeft.press = false;
165
stickL[i].keyRight.press = false;
166
167
#if RETRO_REV02
168
stickL[i].keyStick.press = false;
169
170
stickR[i].keyUp.press = false;
171
stickR[i].keyDown.press = false;
172
stickR[i].keyLeft.press = false;
173
stickR[i].keyRight.press = false;
174
stickR[i].keyStick.press = false;
175
176
triggerL[i].keyBumper.press = false;
177
triggerL[i].keyTrigger.press = false;
178
179
triggerR[i].keyBumper.press = false;
180
triggerR[i].keyTrigger.press = false;
181
#else
182
controller[i].keyStickL.press = false;
183
controller[i].keyStickR.press = false;
184
185
controller[i].keyBumperL.press = false;
186
controller[i].keyTriggerL.press = false;
187
188
controller[i].keyBumperR.press = false;
189
controller[i].keyTriggerR.press = false;
190
#endif
191
}
192
}
193
194
void RSDK::ProcessInput()
195
{
196
ClearInput();
197
198
bool32 anyPress = false;
199
for (int32 i = 0; i < inputDeviceCount; ++i) {
200
if (inputDeviceList[i]) {
201
inputDeviceList[i]->UpdateInput();
202
203
anyPress |= inputDeviceList[i]->anyPress;
204
}
205
}
206
207
#if RETRO_REV02
208
if (anyPress || touchInfo.count)
209
videoSettings.dimTimer = 0;
210
else if (videoSettings.dimTimer < videoSettings.dimLimit)
211
++videoSettings.dimTimer;
212
#endif
213
214
for (int32 i = 0; i < PLAYER_COUNT; ++i) {
215
int32 assign = inputSlots[i];
216
if (assign && assign != INPUT_UNASSIGNED) {
217
if (assign == INPUT_AUTOASSIGN) {
218
int32 id = GetAvaliableInputDevice();
219
inputSlots[i] = id;
220
if (id != INPUT_AUTOASSIGN)
221
AssignInputSlotToDevice(CONT_P1 + i, id);
222
}
223
else {
224
InputDevice *device = inputSlotDevices[i];
225
if (device && device->id == assign && device->active)
226
device->ProcessInput(CONT_P1 + i);
227
}
228
}
229
}
230
231
#if !RETRO_REV02 && RETRO_INPUTDEVICE_KEYBOARD
232
RSDK::SKU::HandleSpecialKeys();
233
#endif
234
235
for (int32 c = 0; c <= PLAYER_COUNT; ++c) {
236
if (c <= 0 || inputSlots[c - 1] != INPUT_UNASSIGNED) {
237
InputState *cont[] = {
238
&controller[c].keyUp, &controller[c].keyDown, &controller[c].keyLeft, &controller[c].keyRight,
239
&controller[c].keyA, &controller[c].keyB, &controller[c].keyC, &controller[c].keyX,
240
&controller[c].keyY, &controller[c].keyZ, &controller[c].keyStart, &controller[c].keySelect,
241
};
242
243
#if RETRO_REV02
244
InputState *lStick[] = { &stickL[c].keyUp, &stickL[c].keyDown, &stickL[c].keyLeft, &stickL[c].keyRight, &stickL[c].keyStick };
245
InputState *rStick[] = { &stickR[c].keyUp, &stickR[c].keyDown, &stickR[c].keyLeft, &stickR[c].keyRight, &stickR[c].keyStick };
246
247
InputState *lTrigger[] = { &triggerL[c].keyBumper, &triggerL[c].keyTrigger };
248
InputState *rTrigger[] = { &triggerR[c].keyBumper, &triggerR[c].keyTrigger };
249
#else
250
InputState *lStick[] = { &stickL[c].keyUp, &stickL[c].keyDown, &stickL[c].keyLeft, &stickL[c].keyRight, &controller[c].keyStickL };
251
InputState *rStick[] = { NULL, NULL, NULL, NULL, &controller[c].keyStickR };
252
253
InputState *lTrigger[] = { &controller[c].keyBumperL, &controller[c].keyTriggerL };
254
InputState *rTrigger[] = { &controller[c].keyBumperR, &controller[c].keyTriggerR };
255
#endif
256
257
for (int32 i = 0; i < 12; ++i) {
258
if (cont[i]->press) {
259
if (cont[i]->down)
260
cont[i]->press = false;
261
else
262
cont[i]->down = true;
263
}
264
else
265
cont[i]->down = false;
266
}
267
268
for (int32 i = 0; i < 5; ++i) {
269
if (lStick[i]->press) {
270
if (lStick[i]->down)
271
lStick[i]->press = false;
272
else
273
lStick[i]->down = true;
274
}
275
else
276
lStick[i]->down = false;
277
278
if (rStick[i]) {
279
if (rStick[i]->press) {
280
if (rStick[i]->down)
281
rStick[i]->press = false;
282
else
283
rStick[i]->down = true;
284
}
285
else
286
rStick[i]->down = false;
287
}
288
}
289
290
for (int32 i = 0; i < 2; ++i) {
291
if (lTrigger[i]->press) {
292
if (lTrigger[i]->down)
293
lTrigger[i]->press = false;
294
else
295
lTrigger[i]->down = true;
296
}
297
else
298
lTrigger[i]->down = false;
299
300
if (rTrigger[i]->press) {
301
if (rTrigger[i]->down)
302
rTrigger[i]->press = false;
303
else
304
rTrigger[i]->down = true;
305
}
306
else
307
rTrigger[i]->down = false;
308
}
309
}
310
}
311
}
312
313
void RSDK::ProcessInputDevices()
314
{
315
#if RETRO_INPUTDEVICE_NX
316
SKU::ProcessNXInputDevices();
317
#endif
318
#if RETRO_INPUTDEVICE_PDBOAT
319
SKU::ProcessPaddleboatInputDevices();
320
#endif
321
}
322
323
int32 RSDK::GetInputDeviceType(uint32 deviceID)
324
{
325
for (int32 i = 0; i < inputDeviceCount; ++i) {
326
if (inputDeviceList[i] && inputDeviceList[i]->id == deviceID)
327
return inputDeviceList[i]->gamepadType;
328
}
329
330
#if RETRO_REV02
331
return SKU::userCore->GetDefaultGamepadType();
332
#else
333
int32 platform = gameVerInfo.platform;
334
335
switch (platform) {
336
337
#if RETRO_INPUTDEVICE_NX
338
return currentNXControllerType;
339
#else
340
return (DEVICE_API_NONE << 16) | (DEVICE_TYPE_CONTROLLER << 8) | (DEVICE_SWITCH_HANDHELD << 0);
341
#endif
342
343
default:
344
case PLATFORM_PS4:
345
case PLATFORM_XB1:
346
case PLATFORM_PC:
347
case PLATFORM_DEV: return (DEVICE_API_NONE << 16) | (DEVICE_TYPE_CONTROLLER << 8) | (0 << 0); break;
348
}
349
#endif
350
}
351