Skip to content

Commit eda239c

Browse files
authored
Fixing gamepad buttons not working in drm backend (#3888)
* Fixing gamepad buttons in drm backend * Remove trailing spaces * Axis enumeration now works properly
1 parent 0712889 commit eda239c

File tree

2 files changed

+120
-50
lines changed

2 files changed

+120
-50
lines changed

src/platforms/rcore_drm.c

+118-48
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282

8383
#define DEFAULT_EVDEV_PATH "/dev/input/" // Path to the linux input events
8484

85+
// So actually the biggest key is KEY_CNT but we only really map the keys up to
86+
// KEY_ALS_TOGGLE
87+
#define KEYMAP_SIZE KEY_ALS_TOGGLE
88+
8589
//----------------------------------------------------------------------------------
8690
// Types and Structures Definition
8791
//----------------------------------------------------------------------------------
@@ -135,38 +139,81 @@ static PlatformData platform = { 0 }; // Platform specific data
135139
//----------------------------------------------------------------------------------
136140
// Local Variables Definition
137141
//----------------------------------------------------------------------------------
138-
// Scancode to keycode mapping for US keyboards
142+
143+
// NOTE: The complete evdev EV_KEY list can be found at /usr/include/linux/input-event-codes.h
144+
// TODO: Complete the LUT with all unicode decimal values
139145
// TODO: Replace this with a keymap from the X11 to get the correct regional map for the keyboard:
140146
// Currently non US keyboards will have the wrong mapping for some keys
141147
// NOTE: Replacing this with the keymap from X11 would probably be useless, as people use the drm
142148
// backend to *avoid* X11
143-
static const int keymapUS[] = {
144-
0, 256, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 45, 61, 259, 258, 81, 87, 69, 82, 84,
145-
89, 85, 73, 79, 80, 91, 93, 257, 341, 65, 83, 68, 70, 71, 72, 74, 75, 76, 59, 39, 96,
146-
340, 92, 90, 88, 67, 86, 66, 78, 77, 44, 46, 47, 344, 332, 342, 32, 280, 290, 291,
147-
292, 293, 294, 295, 296, 297, 298, 299, 282, 281, 327, 328, 329, 333, 324, 325,
148-
326, 334, 321, 322, 323, 320, 330, 0, 85, 86, 300, 301, 89, 90, 91, 92, 93, 94, 95,
149-
335, 345, 331, 283, 346, 101, 268, 265, 266, 263, 262, 269, 264, 267, 260, 261,
150-
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 347, 127,
151-
128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
152-
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
153-
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
154-
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
155-
192, 193, 194, 0, 0, 0, 0, 0, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
156-
211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226,
157-
227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242,
158-
243, 244, 245, 246, 247, 248, 0, 0, 0, 0, 0, 0, 0
159-
};
160-
161-
// NOTE: The complete evdev EV_KEY list can be found at /usr/include/linux/input-event-codes.h
162-
// TODO: Complete the LUT with all unicode decimal values
163149
static const int evkeyToUnicodeLUT[] = {
164150
0, 27, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 45, 61, 8, 0, 113, 119, 101, 114,
165151
116, 121, 117, 105, 111, 112, 0, 0, 13, 0, 97, 115, 100, 102, 103, 104, 106, 107, 108, 59,
166152
39, 96, 0, 92, 122, 120, 99, 118, 98, 110, 109, 44, 46, 47, 0, 0, 0, 32
167153
// LUT currently incomplete, just mapped the most essential keys
168154
};
169155

156+
// This is the map used to map any keycode returned from linux to a raylib code from 'raylib.h'
157+
// NOTE: Use short here to save a little memory
158+
static const short linuxToRaylibMap[KEYMAP_SIZE] = {
159+
// We don't map those with designated initialization, because we would getting
160+
// into loads of naming conflicts
161+
0, 256, 49, 50, 51, 52, 53, 54,
162+
55, 56, 57, 48, 45, 61, 259, 258,
163+
81, 87, 69, 82, 84, 89, 85, 73,
164+
79, 80, 91, 93, 257, 341, 65, 83,
165+
68, 70, 71, 72, 74, 75, 76, 59,
166+
39, 96, 340, 92, 90, 88, 67, 86,
167+
66, 78, 77, 44, 46, 47, 344, 332,
168+
342, 32, 280, 290, 291, 292, 293, 294,
169+
295, 296, 297, 298, 299, 282, 281, 327,
170+
328, 329, 333, 324, 325, 326, 334, 321,
171+
322, 323, 320, 330, 0, 85, 86, 300,
172+
301, 89, 90, 91, 92, 93, 94, 95,
173+
335, 345, 331, 283, 346, 101, 268, 265,
174+
266, 263, 262, 269, 264, 267, 260, 261,
175+
112, 113, 114, 115, 116, 117, 118, 119,
176+
120, 121, 122, 123, 124, 125, 347, 127,
177+
128, 129, 130, 131, 132, 133, 134, 135,
178+
136, 137, 138, 139, 140, 141, 142, 143,
179+
144, 145, 146, 147, 148, 149, 150, 151,
180+
152, 153, 154, 155, 156, 157, 158, 159,
181+
160, 161, 162, 163, 164, 165, 166, 167,
182+
168, 169, 170, 171, 172, 173, 174, 175,
183+
176, 177, 178, 179, 180, 181, 182, 183,
184+
184, 185, 186, 187, 188, 189, 190, 191,
185+
192, 193, 194, 0, 0, 0, 0, 0,
186+
200, 201, 202, 203, 204, 205, 206, 207,
187+
208, 209, 210, 211, 212, 213, 214, 215,
188+
216, 217, 218, 219, 220, 221, 222, 223,
189+
224, 225, 226, 227, 228, 229, 230, 231,
190+
232, 233, 234, 235, 236, 237, 238, 239,
191+
240, 241, 242, 243, 244, 245, 246, 247,
192+
248, 0, 0, 0, 0, 0, 0, 0,
193+
194+
// Gamepads are mapped according to:
195+
// https://www.kernel.org/doc/html/next/input/gamepad.html
196+
// Those mappings are standardized, but that doesn't mean people follow
197+
// the standards, so this is more of an approximation
198+
[BTN_DPAD_UP] = GAMEPAD_BUTTON_LEFT_FACE_UP,
199+
[BTN_DPAD_RIGHT] = GAMEPAD_BUTTON_LEFT_FACE_RIGHT,
200+
[BTN_DPAD_DOWN] = GAMEPAD_BUTTON_LEFT_FACE_DOWN,
201+
[BTN_DPAD_LEFT] = GAMEPAD_BUTTON_LEFT_FACE_LEFT,
202+
[BTN_Y] = GAMEPAD_BUTTON_RIGHT_FACE_UP,
203+
[BTN_B] = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT,
204+
[BTN_A] = GAMEPAD_BUTTON_RIGHT_FACE_DOWN,
205+
[BTN_X] = GAMEPAD_BUTTON_RIGHT_FACE_LEFT,
206+
[BTN_TL] = GAMEPAD_BUTTON_LEFT_TRIGGER_1,
207+
[BTN_TL2] = GAMEPAD_BUTTON_LEFT_TRIGGER_2,
208+
[BTN_TR] = GAMEPAD_BUTTON_RIGHT_TRIGGER_1,
209+
[BTN_TR2] GAMEPAD_BUTTON_RIGHT_TRIGGER_2,
210+
[BTN_SELECT] = GAMEPAD_BUTTON_MIDDLE_LEFT,
211+
[BTN_MODE] = GAMEPAD_BUTTON_MIDDLE,
212+
[BTN_START] = GAMEPAD_BUTTON_MIDDLE_RIGHT,
213+
[BTN_THUMBL] = GAMEPAD_BUTTON_LEFT_THUMB,
214+
[BTN_THUMBR] = GAMEPAD_BUTTON_RIGHT_THUMB,
215+
};
216+
170217
//----------------------------------------------------------------------------------
171218
// Module Internal Functions Declaration
172219
//----------------------------------------------------------------------------------
@@ -574,7 +621,7 @@ void PollInputEvents(void)
574621
CORE.Input.Mouse.previousWheelMove = CORE.Input.Mouse.currentWheelMove;
575622
CORE.Input.Mouse.currentWheelMove = platform.eventWheelMove;
576623
platform.eventWheelMove = (Vector2){ 0.0f, 0.0f };
577-
624+
578625
for (int i = 0; i < MAX_MOUSE_BUTTONS; i++)
579626
{
580627
CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
@@ -1293,7 +1340,10 @@ static void ConfigureEvdevDevice(char *device)
12931340

12941341
// Identify the device
12951342
//-------------------------------------------------------------------------------------------------------
1296-
struct input_absinfo absinfo[ABS_CNT] = { 0 };
1343+
struct {
1344+
bool exist;
1345+
struct input_absinfo info;
1346+
} absinfo[ABS_CNT] = { 0 };
12971347

12981348
// These flags aren't really a one of
12991349
// Some devices could have properties we assosciate with keyboards as well as properties
@@ -1318,8 +1368,12 @@ static void ConfigureEvdevDevice(char *device)
13181368
if (hasAbsXY)
13191369
{
13201370
absAxisCount += 2;
1321-
ioctl(fd, EVIOCGABS(ABS_X), &absinfo[ABS_X]);
1322-
ioctl(fd, EVIOCGABS(ABS_Y), &absinfo[ABS_Y]);
1371+
1372+
absinfo[ABS_X].exist = true;
1373+
absinfo[ABS_Y].exist = true;
1374+
1375+
ioctl(fd, EVIOCGABS(ABS_X), &absinfo[ABS_X].info);
1376+
ioctl(fd, EVIOCGABS(ABS_Y), &absinfo[ABS_Y].info);
13231377
}
13241378

13251379
// If it has any of these buttons it's a touch device
@@ -1340,10 +1394,11 @@ static void ConfigureEvdevDevice(char *device)
13401394
{
13411395
if (TEST_BIT(absBits, axis))
13421396
{
1397+
absinfo[axis].exist = true;
13431398
isGamepad = true;
13441399
absAxisCount++;
13451400

1346-
ioctl(fd, EVIOCGABS(axis), &absinfo[axis]);
1401+
ioctl(fd, EVIOCGABS(axis), &absinfo[axis].info);
13471402
}
13481403
}
13491404
}
@@ -1398,11 +1453,11 @@ static void ConfigureEvdevDevice(char *device)
13981453

13991454
if (absAxisCount > 0)
14001455
{
1401-
platform.absRange.x = absinfo[ABS_X].minimum;
1402-
platform.absRange.width = absinfo[ABS_X].maximum - absinfo[ABS_X].minimum;
1456+
platform.absRange.x = absinfo[ABS_X].info.minimum;
1457+
platform.absRange.width = absinfo[ABS_X].info.maximum - absinfo[ABS_X].info.minimum;
14031458

1404-
platform.absRange.y = absinfo[ABS_Y].minimum;
1405-
platform.absRange.height = absinfo[ABS_Y].maximum - absinfo[ABS_Y].minimum;
1459+
platform.absRange.y = absinfo[ABS_Y].info.minimum;
1460+
platform.absRange.height = absinfo[ABS_Y].info.maximum - absinfo[ABS_Y].info.minimum;
14061461
}
14071462
}
14081463
else if (isGamepad && !isMouse && !isKeyboard && platform.gamepadCount < MAX_GAMEPADS)
@@ -1429,10 +1484,14 @@ static void ConfigureEvdevDevice(char *device)
14291484
int axisIndex = 0;
14301485
for (int axis = ABS_X; axis < ABS_PRESSURE; axis++)
14311486
{
1432-
platform.gamepadAbsAxisRange[index][axisIndex][0] = absinfo[axisIndex].minimum;
1433-
platform.gamepadAbsAxisRange[index][axisIndex][1] = absinfo[axisIndex].maximum - absinfo[axisIndex].minimum;
1487+
if (absinfo[axis].exist)
1488+
{
1489+
platform.gamepadAbsAxisRange[index][axisIndex][0] = absinfo[axisIndex].info.minimum;
1490+
platform.gamepadAbsAxisRange[index][axisIndex][1] = absinfo[axisIndex].info.maximum - absinfo[axisIndex].info.minimum;
14341491

1435-
platform.gamepadAbsAxisMap[index][axis] = axisIndex++;
1492+
platform.gamepadAbsAxisMap[index][axis] = axisIndex;
1493+
axisIndex++;
1494+
}
14361495
}
14371496
}
14381497
}
@@ -1475,7 +1534,7 @@ static void PollKeyboardEvents(void)
14751534
{
14761535

14771536
// Lookup the scancode in the keymap to get a keycode
1478-
keycode = keymapUS[event.code];
1537+
keycode = linuxToRaylibMap[event.code];
14791538

14801539
// Make sure we got a valid keycode
14811540
if ((keycode > 0) && (keycode < MAX_KEYBOARD_KEYS))
@@ -1527,27 +1586,38 @@ static void PollGamepadEvents(void)
15271586
{
15281587
if (event.type == EV_KEY)
15291588
{
1530-
if (event.code >= MAX_GAMEPAD_BUTTONS) continue;
1589+
if (event.code < KEYMAP_SIZE)
1590+
{
1591+
short keycodeRaylib = linuxToRaylibMap[event.code];
15311592

1532-
TRACELOG(LOG_DEBUG, "INPUT: Gamepad %i button: %i, value: %i", i, event.code, event.value);
1593+
TRACELOG(LOG_DEBUG, "INPUT: Gamepad %2i: KEY_%s Keycode(linux): %4i Keycode(raylib): %4i", i, (event.value == 0) ? "UP " : "DOWN", event.code, keycodeRaylib);
15331594

1534-
// 1 - button pressed, 0 - button released
1535-
CORE.Input.Gamepad.currentButtonState[i][event.code] = event.value;
1595+
if ((keycodeRaylib != 0) && (keycodeRaylib < MAX_GAMEPAD_BUTTONS))
1596+
{
1597+
// 1 - button pressed, 0 - button released
1598+
CORE.Input.Gamepad.currentButtonState[i][keycodeRaylib] = event.value;
15361599

1537-
if (event.value == 1) CORE.Input.Gamepad.lastButtonPressed = event.code;
1538-
else CORE.Input.Gamepad.lastButtonPressed = 0; // GAMEPAD_BUTTON_UNKNOWN
1600+
CORE.Input.Gamepad.lastButtonPressed = (event.value == 1)? keycodeRaylib : GAMEPAD_BUTTON_UNKNOWN;
1601+
}
1602+
}
15391603
}
15401604
else if (event.type == EV_ABS)
15411605
{
1542-
if (event.code >= MAX_GAMEPAD_AXIS) continue;
1606+
if (event.code < ABS_CNT)
1607+
{
1608+
int axisRaylib = platform.gamepadAbsAxisMap[i][event.code];
15431609

1544-
TRACELOG(LOG_DEBUG, "INPUT: Gamepad %i axis: %i, value: %i", i, platform.gamepadAbsAxisMap[i][event.code], event.value);
1610+
TRACELOG(LOG_DEBUG, "INPUT: Gamepad %2i: Axis: %2i Value: %i", i, axisRaylib, event.value);
15451611

1546-
int min = platform.gamepadAbsAxisRange[i][event.code][0];
1547-
int range = platform.gamepadAbsAxisRange[i][event.code][1];
1612+
if (axisRaylib < MAX_GAMEPAD_AXIS)
1613+
{
1614+
int min = platform.gamepadAbsAxisRange[i][event.code][0];
1615+
int range = platform.gamepadAbsAxisRange[i][event.code][1];
15481616

1549-
// NOTE: Scaling of event.value to get values between -1..1
1550-
CORE.Input.Gamepad.axisState[i][platform.gamepadAbsAxisMap[i][event.code]] = (2 * (float)(event.value - min) / range) - 1;
1617+
// NOTE: Scaling of event.value to get values between -1..1
1618+
CORE.Input.Gamepad.axisState[i][axisRaylib] = (2 * (float)(event.value - min) / range) - 1;
1619+
}
1620+
}
15511621
}
15521622
}
15531623
}
@@ -1576,7 +1646,7 @@ static void PollMouseEvents(void)
15761646
CORE.Input.Mouse.previousPosition.x = 0.0f;
15771647
}
15781648
else CORE.Input.Mouse.currentPosition.x += event.value;
1579-
1649+
15801650
CORE.Input.Touch.position[0].x = CORE.Input.Mouse.currentPosition.x;
15811651
touchAction = 2; // TOUCH_ACTION_MOVE
15821652
}
@@ -1589,7 +1659,7 @@ static void PollMouseEvents(void)
15891659
CORE.Input.Mouse.previousPosition.y = 0.0f;
15901660
}
15911661
else CORE.Input.Mouse.currentPosition.y += event.value;
1592-
1662+
15931663
CORE.Input.Touch.position[0].y = CORE.Input.Mouse.currentPosition.y;
15941664
touchAction = 2; // TOUCH_ACTION_MOVE
15951665
}

src/raylib.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -721,9 +721,9 @@ typedef enum {
721721
GAMEPAD_BUTTON_LEFT_FACE_DOWN, // Gamepad left DPAD down button
722722
GAMEPAD_BUTTON_LEFT_FACE_LEFT, // Gamepad left DPAD left button
723723
GAMEPAD_BUTTON_RIGHT_FACE_UP, // Gamepad right button up (i.e. PS3: Triangle, Xbox: Y)
724-
GAMEPAD_BUTTON_RIGHT_FACE_RIGHT, // Gamepad right button right (i.e. PS3: Square, Xbox: X)
724+
GAMEPAD_BUTTON_RIGHT_FACE_RIGHT, // Gamepad right button right (i.e. PS3: Circle, Xbox: B)
725725
GAMEPAD_BUTTON_RIGHT_FACE_DOWN, // Gamepad right button down (i.e. PS3: Cross, Xbox: A)
726-
GAMEPAD_BUTTON_RIGHT_FACE_LEFT, // Gamepad right button left (i.e. PS3: Circle, Xbox: B)
726+
GAMEPAD_BUTTON_RIGHT_FACE_LEFT, // Gamepad right button left (i.e. PS3: Square, Xbox: X)
727727
GAMEPAD_BUTTON_LEFT_TRIGGER_1, // Gamepad top/back trigger left (first), it could be a trailing button
728728
GAMEPAD_BUTTON_LEFT_TRIGGER_2, // Gamepad top/back trigger left (second), it could be a trailing button
729729
GAMEPAD_BUTTON_RIGHT_TRIGGER_1, // Gamepad top/back trigger right (one), it could be a trailing button

0 commit comments

Comments
 (0)