SDL: Add option "--quit-after-ms N" to automatically quit application after N ms

From 128baec810e8e24479bbc445db389a5d2d5a3534 Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Mon, 20 Oct 2025 09:00:27 +0200
Subject: [PATCH] Add option "--quit-after-ms N" to automatically quit
 application after N ms

---
 include/SDL3/SDL_test_common.h |  4 ++++
 src/test/SDL_test_common.c     | 31 +++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/include/SDL3/SDL_test_common.h b/include/SDL3/SDL_test_common.h
index 91efe8ac34301..81cea77880ee1 100644
--- a/include/SDL3/SDL_test_common.h
+++ b/include/SDL3/SDL_test_common.h
@@ -153,6 +153,10 @@ typedef struct
     SDL_Rect confine;
     bool hide_cursor;
 
+    /* Misc. */
+    int quit_after_ms_interval;
+    SDL_TimerID quit_after_ms_timer;
+
     /* Options info */
     SDLTest_ArgumentParser common_argparser;
     SDLTest_ArgumentParser video_argparser;
diff --git a/src/test/SDL_test_common.c b/src/test/SDL_test_common.c
index e3d8fcf57a1f1..7bce2eeb7f793 100644
--- a/src/test/SDL_test_common.c
+++ b/src/test/SDL_test_common.c
@@ -63,6 +63,7 @@ static const char *video_usage[] = {
     "[--minimize]",
     "[--mouse-focus]",
     "[--noframe]",
+    "[--quit-after-ms N]",
     "[--refresh R]",
     "[--renderer driver]",
     "[--resizable]",
@@ -86,6 +87,16 @@ static const char *audio_usage[] = {
     NULL
 };
 
+// tests can quit after N ms
+static Uint32 SDLCALL quit_after_ms_cb(void *userdata, SDL_TimerID timerID, Uint32 interval)
+{
+    SDL_Event event;
+    event.type = SDL_EVENT_QUIT;
+    event.common.timestamp = 0;
+    SDL_PushEvent(&event);
+    return 0;
+}
+
 static void SDL_snprintfcat(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
 {
     size_t length = SDL_strlen(text);
@@ -509,6 +520,17 @@ static int SDLCALL SDLTest_CommonStateParseVideoArguments(void *data, char **arg
         state->window_flags |= SDL_WINDOW_BORDERLESS;
         return 1;
     }
+    if (SDL_strcasecmp(argv[index], "--quit-after-ms") == 0) {
+        ++index;
+        if (!argv[index]) {
+            return -1;
+        }
+        state->quit_after_ms_interval = SDL_atoi(argv[index]);
+        if (state->quit_after_ms_interval <= 0) {
+            return -1;
+        }
+        return 2;
+    }
     if (SDL_strcasecmp(argv[index], "--resizable") == 0) {
         state->window_flags |= SDL_WINDOW_RESIZABLE;
         return 1;
@@ -1525,6 +1547,11 @@ bool SDLTest_CommonInit(SDLTest_CommonState *state)
 
     SDL_InitSubSystem(state->flags);
 
+
+    if (state->quit_after_ms_interval) {
+        state->quit_after_ms_timer = SDL_AddTimer(state->quit_after_ms_interval, quit_after_ms_cb, NULL);
+    }
+
     return true;
 }
 
@@ -2725,6 +2752,10 @@ void SDLTest_CommonQuit(SDLTest_CommonState *state)
             }
             SDL_free(state->windows);
         }
+
+        if (state->quit_after_ms_timer) {
+            SDL_RemoveTimer(state->quit_after_ms_timer);
+        }
     }
     SDL_Quit();
     SDLTest_CommonDestroyState(state);