SDL: wayland: Cap the max key repeat elapsed time (ab114)

From ab114490fcbb218a2794673851220f16aab6ae1c Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Wed, 21 May 2025 12:18:36 -0400
Subject: [PATCH] wayland: Cap the max key repeat elapsed time

Cap the elapsed time to something sane in case the compositor sends a bad timestamp, which can result it in it looking like the key has been pressed for a *very* long time, bringing everything to a halt while it tries to enqueue all the repeat events.

(cherry picked from commit 05f779f61eeefb97c6dc88a042846cfcf731d2ba)
---
 src/video/wayland/SDL_waylandevents.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
index cc2c5f9e37c10..2736389a6768b 100644
--- a/src/video/wayland/SDL_waylandevents.c
+++ b/src/video/wayland/SDL_waylandevents.c
@@ -272,6 +272,15 @@ void Wayland_CreateCursorShapeDevice(struct SDL_WaylandInput *input)
 static bool keyboard_repeat_handle(SDL_WaylandKeyboardRepeat *repeat_info, Uint64 elapsed)
 {
     bool ret = false;
+
+    /* Cap the elapsed time to something sane in case the compositor sends a bad timestamp,
+     * which can result it in it looking like the key has been pressed for a *very* long time,
+     * bringing everything to a halt while it tries to enqueue all the repeat events.
+     *
+     * 3 seconds seems reasonable.
+     */
+    elapsed = SDL_min(elapsed, SDL_MS_TO_NS(3000));
+
     while (elapsed >= repeat_info->next_repeat_ns) {
         if (repeat_info->scancode != SDL_SCANCODE_UNKNOWN) {
             const Uint64 timestamp = repeat_info->wl_press_time_ns + repeat_info->next_repeat_ns;