Skip to content

Commit c498c2b

Browse files
committed
Fixed and cleaned up PollKeyboardEvents() in drm backend
1 parent 729a01b commit c498c2b

File tree

1 file changed

+46
-59
lines changed

1 file changed

+46
-59
lines changed

src/platforms/rcore_drm.c

+46-59
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
#include <sys/ioctl.h> // Required for: ioctl() - UNIX System call for device-specific input/output operations
5858
#include <linux/kd.h> // Linux: KDSKBMODE, K_MEDIUMRAM constants definition
5959
#include <linux/input.h> // Linux: Keycodes constants definition (KEY_A, ...)
60+
61+
// So both `linux/input.h` and `raylib.h` define KEY_F12
62+
// To avoid conflict with the capturing code in rcore.c we undefine the macro KEY_F12,
63+
// so the enum KEY_F12 from raylib is used
64+
#undef KEY_F12
65+
6066
#include <linux/joystick.h> // Linux: Joystick support library
6167

6268
#include <gbm.h> // Generic Buffer Management (native platform for EGL on DRM)
@@ -548,6 +554,16 @@ void PollInputEvents(void)
548554

549555
PollKeyboardEvents();
550556

557+
#if defined(SUPPORT_SSH_KEYBOARD_RPI)
558+
// NOTE: Keyboard reading could be done using input_event(s) or just read from stdin, both methods are used here.
559+
// stdin reading is still used for legacy purposes, it allows keyboard input trough SSH console
560+
561+
if (!platform.eventKeyboardMode) ProcessKeyboard();
562+
#endif
563+
564+
// Check exit key
565+
if (CORE.Input.Keyboard.currentKeyState[CORE.Input.Keyboard.exitKey] == 1) CORE.Window.shouldClose = true;
566+
551567
// Register previous mouse position
552568
if (platform.cursorRelative) CORE.Input.Mouse.currentPosition = (Vector2){ 0.0f, 0.0f };
553569
else CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
@@ -575,13 +591,6 @@ void PollInputEvents(void)
575591
// Map touch position to mouse position for convenience
576592
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
577593

578-
#if defined(SUPPORT_SSH_KEYBOARD_RPI)
579-
// NOTE: Keyboard reading could be done using input_event(s) or just read from stdin, both methods are used here.
580-
// stdin reading is still used for legacy purposes, it allows keyboard input trough SSH console
581-
582-
if (!platform.eventKeyboardMode) ProcessKeyboard();
583-
#endif
584-
585594
// Handle the mouse/touch/gestures events:
586595
// NOTE: Replaces the EventThread handling that is now commented.
587596
PollMouseEvents();
@@ -1206,18 +1215,6 @@ static void ProcessKeyboard(void)
12061215
CORE.Input.Keyboard.keyPressedQueueCount++;
12071216
}
12081217
}
1209-
1210-
// Check exit key (same functionality as GLFW3 KeyCallback())
1211-
if (CORE.Input.Keyboard.currentKeyState[CORE.Input.Keyboard.exitKey] == 1) CORE.Window.shouldClose = true;
1212-
1213-
#if defined(SUPPORT_SCREEN_CAPTURE)
1214-
// Check screen capture key (raylib key: KEY_F12)
1215-
if (CORE.Input.Keyboard.currentKeyState[301] == 1)
1216-
{
1217-
TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter));
1218-
screenshotCounter++;
1219-
}
1220-
#endif
12211218
}
12221219
#endif // SUPPORT_SSH_KEYBOARD_RPI
12231220

@@ -1470,57 +1467,47 @@ static void PollKeyboardEvents(void)
14701467
// Try to read data from the keyboard and only continue if successful
14711468
while (read(fd, &event, sizeof(event)) == (int)sizeof(event))
14721469
{
1473-
// Button parsing
1474-
if (event.type == EV_KEY)
1475-
{
1470+
// Check if the event is a key event
1471+
if (event.type != EV_KEY) continue;
1472+
14761473
#if defined(SUPPORT_SSH_KEYBOARD_RPI)
1477-
// Change keyboard mode to events
1478-
platform.eventKeyboardMode = true;
1474+
// If the event was a key, we know a working keyboard is connected, so disable the SSH keyboard
1475+
platform.eventKeyboardMode = true;
14791476
#endif
1480-
// Keyboard button parsing
1481-
if ((event.code >= 1) && (event.code <= 255)) //Keyboard keys appear for codes 1 to 255
1482-
{
1483-
keycode = keymapUS[event.code & 0xFF]; // The code we get is a scancode so we look up the appropriate keycode
14841477

1485-
// Make sure we got a valid keycode
1486-
if ((keycode > 0) && (keycode < sizeof(CORE.Input.Keyboard.currentKeyState)))
1478+
// Keyboard keys appear for codes 1 to 255, ignore everthing else
1479+
if ((event.code >= 1) && (event.code <= 255)) {
1480+
1481+
// Lookup the scancode in the keymap to get a keycode
1482+
keycode = keymapUS[event.code];
1483+
1484+
// Make sure we got a valid keycode
1485+
if ((keycode > 0) && (keycode < MAX_KEYBOARD_KEYS)) {
1486+
1487+
// WARNING: https://www.kernel.org/doc/Documentation/input/input.txt
1488+
// Event interface: 'value' is the value the event carries. Either a relative change for EV_REL,
1489+
// absolute new value for EV_ABS (joysticks ...), or 0 for EV_KEY for release, 1 for keypress and 2 for autorepeat
1490+
CORE.Input.Keyboard.currentKeyState[keycode] = (event.value >= 1);
1491+
CORE.Input.Keyboard.keyRepeatInFrame[keycode] = (event.value == 2);
1492+
1493+
// If the key is pressed add it to the queues
1494+
if (event.value == 1)
14871495
{
1488-
// WARNING: https://www.kernel.org/doc/Documentation/input/input.txt
1489-
// Event interface: 'value' is the value the event carries. Either a relative change for EV_REL,
1490-
// absolute new value for EV_ABS (joysticks ...), or 0 for EV_KEY for release, 1 for keypress and 2 for autorepeat
1491-
CORE.Input.Keyboard.currentKeyState[keycode] = (event.value >= 1)? 1 : 0;
1492-
CORE.Input.Keyboard.keyRepeatInFrame[keycode] = (event.value == 2)? 1 : 0;
1493-
if (event.value >= 1)
1496+
if (CORE.Input.Keyboard.keyPressedQueueCount < MAX_CHAR_PRESSED_QUEUE)
14941497
{
1495-
CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = keycode; // Register last key pressed
1498+
CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = keycode;
14961499
CORE.Input.Keyboard.keyPressedQueueCount++;
14971500
}
14981501

1499-
#if defined(SUPPORT_SCREEN_CAPTURE)
1500-
// Check screen capture key (raylib key: KEY_F12)
1501-
if (CORE.Input.Keyboard.currentKeyState[301] == 1)
1502+
if (CORE.Input.Keyboard.charPressedQueueCount < MAX_CHAR_PRESSED_QUEUE)
15021503
{
1503-
TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter));
1504-
screenshotCounter++;
1504+
// TODO/FIXME: This is not actually converting to unicode properly because it's not taking things like shift into account
1505+
CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = EvkeyToUnicodeLUT[event.code];
1506+
CORE.Input.Keyboard.charPressedQueueCount++;
15051507
}
1506-
#endif
1507-
1508-
// Detect char presses (unicode)
1509-
if (event.value == 1)
1510-
{
1511-
// Check if there is space available in the queue
1512-
if (CORE.Input.Keyboard.charPressedQueueCount < MAX_CHAR_PRESSED_QUEUE)
1513-
{
1514-
// Add character to the queue
1515-
CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = EvkeyToUnicodeLUT[event.code];
1516-
CORE.Input.Keyboard.charPressedQueueCount++;
1517-
}
1518-
}
1519-
1520-
if (CORE.Input.Keyboard.currentKeyState[CORE.Input.Keyboard.exitKey] == 1) CORE.Window.shouldClose = true;
1521-
1522-
TRACELOGD("DRM: KEY_%s ScanCode: %4i KeyCode: %4i", (event.value == 0)? "UP" : "DOWN", event.code, keycode);
15231508
}
1509+
1510+
TRACELOG(LOG_DEBUG, "DRM: KEY_%s Keycode(linux): %4i KeyCode(raylib): %4i", (event.value == 0) ? "UP " : "DOWN", event.code, keycode);
15241511
}
15251512
}
15261513
}

0 commit comments

Comments
 (0)