Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/RSDKv5/RSDK/Input/GLFW/GLFWInputDevice.cpp
1172 views
1
#ifdef STFU_INTELLISENSE
2
#include <glad/glad.h>
3
#include <GLFW/glfw3.h>
4
#include "GLFWInputDevice.hpp"
5
#endif
6
7
using namespace RSDK;
8
9
void RSDK::SKU::InputDeviceGLFW::UpdateInput()
10
{
11
currentState ^= 1;
12
glfwGetGamepadState(jid, &states[currentState]);
13
bool32 changedButtons = (bool32)(memcmp(&states[currentState], &states[currentState ^ 1], sizeof(GLFWgamepadstate)));
14
15
if (changedButtons) {
16
this->inactiveTimer[0] = 0;
17
this->anyPress = true;
18
}
19
else {
20
++this->inactiveTimer[0];
21
this->anyPress = false;
22
}
23
24
if (changedButtons
25
&& ((states[currentState].buttons[GLFW_GAMEPAD_BUTTON_A] != states[currentState ^ 1].buttons[GLFW_GAMEPAD_BUTTON_A])
26
|| states[currentState].buttons[GLFW_GAMEPAD_BUTTON_START] != states[currentState ^ 1].buttons[GLFW_GAMEPAD_BUTTON_START])) {
27
this->inactiveTimer[1] = 0;
28
}
29
else
30
++this->inactiveTimer[1];
31
32
ProcessInput(CONT_ANY);
33
}
34
35
void RSDK::SKU::InputDeviceGLFW::ProcessInput(int32 controllerID)
36
{
37
controller[controllerID].keyUp.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_DPAD_UP];
38
controller[controllerID].keyDown.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_DPAD_DOWN];
39
controller[controllerID].keyLeft.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_DPAD_LEFT];
40
controller[controllerID].keyRight.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_DPAD_RIGHT];
41
controller[controllerID].keyA.press |= this->states[currentState].buttons[swapABXY ? GLFW_GAMEPAD_BUTTON_B : GLFW_GAMEPAD_BUTTON_A];
42
controller[controllerID].keyB.press |= this->states[currentState].buttons[swapABXY ? GLFW_GAMEPAD_BUTTON_A : GLFW_GAMEPAD_BUTTON_B];
43
// controller[controllerID].keyC.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_C];
44
controller[controllerID].keyX.press |= this->states[currentState].buttons[swapABXY ? GLFW_GAMEPAD_BUTTON_Y : GLFW_GAMEPAD_BUTTON_X];
45
controller[controllerID].keyY.press |= this->states[currentState].buttons[swapABXY ? GLFW_GAMEPAD_BUTTON_X : GLFW_GAMEPAD_BUTTON_Y];
46
// controller[controllerID].keyZ.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_Z];
47
controller[controllerID].keyStart.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_START];
48
controller[controllerID].keySelect.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_BACK];
49
50
#if RETRO_REV02
51
stickL[controllerID].keyStick.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_LEFT_THUMB];
52
stickL[controllerID].hDelta = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_X];
53
stickL[controllerID].vDelta = -this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_Y];
54
stickL[controllerID].keyUp.press |= stickL[controllerID].vDelta > INPUT_DEADZONE;
55
stickL[controllerID].keyDown.press |= stickL[controllerID].vDelta < -INPUT_DEADZONE;
56
stickL[controllerID].keyLeft.press |= stickL[controllerID].hDelta < -INPUT_DEADZONE;
57
stickL[controllerID].keyRight.press |= stickL[controllerID].hDelta > INPUT_DEADZONE;
58
59
stickR[controllerID].keyStick.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_RIGHT_THUMB];
60
stickR[controllerID].hDelta = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_X];
61
stickR[controllerID].vDelta = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_Y];
62
stickR[controllerID].keyUp.press |= stickR[controllerID].vDelta > INPUT_DEADZONE;
63
stickR[controllerID].keyDown.press |= stickR[controllerID].vDelta < -INPUT_DEADZONE;
64
stickR[controllerID].keyLeft.press |= stickR[controllerID].hDelta < -INPUT_DEADZONE;
65
stickR[controllerID].keyRight.press |= stickR[controllerID].hDelta > INPUT_DEADZONE;
66
67
triggerL[controllerID].keyBumper.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_LEFT_BUMPER];
68
triggerL[controllerID].keyTrigger.press |= this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER] > INPUT_DEADZONE;
69
triggerL[controllerID].bumperDelta = triggerL[controllerID].keyBumper.press ? 1.0f : 0.0f;
70
triggerL[controllerID].triggerDelta = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER];
71
72
triggerR[controllerID].keyBumper.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER];
73
triggerR[controllerID].keyTrigger.press |= this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER] > INPUT_DEADZONE;
74
triggerR[controllerID].bumperDelta = triggerR[controllerID].keyBumper.press ? 1.0f : 0.0f;
75
triggerR[controllerID].triggerDelta = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER];
76
#else
77
controller[controllerID].keyStickL.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_LEFT_THUMB];
78
stickL[controllerID].hDeltaL = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_X];
79
stickL[controllerID].vDeltaL = -this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_Y];
80
stickL[controllerID].keyUp.press |= stickL[controllerID].vDeltaL > INPUT_DEADZONE;
81
stickL[controllerID].keyDown.press |= stickL[controllerID].vDeltaL < -INPUT_DEADZONE;
82
stickL[controllerID].keyLeft.press |= stickL[controllerID].hDeltaL < -INPUT_DEADZONE;
83
stickL[controllerID].keyRight.press |= stickL[controllerID].hDeltaL > INPUT_DEADZONE;
84
85
controller[controllerID].keyStickR.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_RIGHT_THUMB];
86
stickL[controllerID].hDeltaR = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_X];
87
stickL[controllerID].vDeltaR = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_Y];
88
89
controller[controllerID].keyBumperL.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_LEFT_BUMPER];
90
controller[controllerID].keyTriggerL.press |= this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER] > INPUT_DEADZONE;
91
stickL[controllerID].triggerDeltaL = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER];
92
93
controller[controllerID].keyBumperR.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER];
94
controller[controllerID].keyTriggerR.press |= this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER] > INPUT_DEADZONE;
95
stickL[controllerID].triggerDeltaR = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER];
96
#endif
97
}
98
99
void RSDK::SKU::InputDeviceGLFW::CloseDevice()
100
{
101
this->active = false;
102
this->isAssigned = false;
103
this->jid = GLFW_JOYSTICK_LAST;
104
}
105
106
RSDK::SKU::InputDeviceGLFW *RSDK::SKU::InitGLFWInputDevice(uint32 id, uint8 controllerID)
107
{
108
if (inputDeviceCount >= INPUTDEVICE_COUNT)
109
return NULL;
110
111
if (inputDeviceList[inputDeviceCount] && inputDeviceList[inputDeviceCount]->active)
112
return NULL;
113
114
if (inputDeviceList[inputDeviceCount])
115
delete inputDeviceList[inputDeviceCount];
116
117
inputDeviceList[inputDeviceCount] = new InputDeviceGLFW();
118
119
InputDeviceGLFW *device = (InputDeviceGLFW *)inputDeviceList[inputDeviceCount];
120
121
device->jid = controllerID;
122
123
const char *name = glfwGetGamepadName(controllerID);
124
125
device->swapABXY = false;
126
uint8 controllerType = DEVICE_XBOX;
127
128
if (strstr(name, "Xbox"))
129
controllerType = DEVICE_XBOX;
130
else if (strstr(name, "PS4") || strstr(name, "PS5"))
131
controllerType = DEVICE_PS4;
132
else if (strstr(name, "Nintendo") || strstr(name, "Switch")) {
133
controllerType = DEVICE_SWITCH_PRO;
134
device->swapABXY = true;
135
}
136
else if (strstr(name, "Saturn"))
137
controllerType = DEVICE_SATURN;
138
139
device->active = true;
140
device->disabled = false;
141
device->gamepadType = (DEVICE_API_GLFW << 16) | (DEVICE_TYPE_CONTROLLER << 8) | (controllerType << 0);
142
device->id = id;
143
144
for (int32 i = 0; i < PLAYER_COUNT; ++i) {
145
if (inputSlots[i] == id) {
146
inputSlotDevices[i] = device;
147
device->isAssigned = true;
148
}
149
}
150
151
inputDeviceCount++;
152
return device;
153
}
154
155
void RSDK::SKU::InitGLFWInputAPI()
156
{
157
char buffer[0x100];
158
#if RETRO_PLATFORM == RETRO_SWITCH
159
glfwUpdateGamepadMappings("53776974636820436f6e74726f6c6c65,Switch "
160
"Controller,a:b0,b:b1,back:b11,dpdown:b15,dpleft:b12,dpright:b14,dpup:b13,leftshoulder:b6,leftstick:b4,lefttrigger:"
161
"b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b5,righttrigger:b9,rightx:a2,righty:a3,start:b10,x:b3,y:b2,");
162
// Support for extra controller types SDL doesn't recognise
163
#endif // ! RETRO_PLATFORM == RETRO_SWITCH
164
165
FileInfo f;
166
InitFileInfo(&f);
167
f.externalFile = true;
168
char path[0x100];
169
sprintf_s(path, sizeof(path), "%sgamecontrollerdb.txt", SKU::userFileDir);
170
171
if (LoadFile(&f, path, FMODE_RB)) {
172
char* buf;
173
AllocateStorage((void**)&buf, f.fileSize + 1, DATASET_TMP, false);
174
buf[f.fileSize] = 0;
175
ReadBytes(&f, buf, f.fileSize);
176
RSDK::PrintLog(PRINT_NORMAL, "[GLFW] gamecontrollerdb size %d: %s", f.fileSize, buf);
177
178
if (glfwUpdateGamepadMappings(buf) == GLFW_FALSE)
179
RSDK::PrintLog(PRINT_NORMAL, "[GLFW] failed to load from gamecontrollerdb");
180
181
RemoveStorageEntry((void**)&buf);
182
CloseFile(&f);
183
}
184
185
for (int32 i = GLFW_JOYSTICK_1; i < GLFW_JOYSTICK_LAST; ++i) {
186
RSDK::PrintLog(PRINT_NORMAL, "[GLFW] joystick %d is '%s', isGamepad %d", i, glfwGetJoystickName(i), glfwJoystickIsGamepad(i));
187
if (glfwJoystickIsGamepad(i)) {
188
uint32 hash;
189
char idBuffer[0x20];
190
sprintf_s(idBuffer, sizeof(idBuffer), "%s%d", "GLFWDevice", i);
191
GenerateHashCRC(&hash, idBuffer);
192
193
SKU::InitGLFWInputDevice(hash, i);
194
}
195
}
196
}
197