SDL: Removed SDL_INIT_TIMER

From f3e419596b1be52798e10d026f7168bef5ff25c1 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 16 Sep 2024 22:57:42 -0700
Subject: [PATCH] Removed SDL_INIT_TIMER

This is no longer necessary before calling SDL_AddTimer()
---
 docs/README-migration.md       |  1 +
 examples/game/01-snake/snake.c |  2 +-
 include/SDL3/SDL_init.h        |  2 --
 include/SDL3/SDL_timer.h       |  4 ----
 src/SDL.c                      | 23 ++---------------------
 src/test/SDL_test_harness.c    | 10 ----------
 test/testautomation_timer.c    |  7 -------
 test/testhaptic.c              |  2 +-
 test/testrumble.c              |  2 +-
 test/testtimer.c               |  5 -----
 10 files changed, 6 insertions(+), 52 deletions(-)

diff --git a/docs/README-migration.md b/docs/README-migration.md
index daa521af42a13..b479011c35b45 100644
--- a/docs/README-migration.md
+++ b/docs/README-migration.md
@@ -895,6 +895,7 @@ The following symbols have been renamed:
 The following symbols have been removed:
 * SDL_INIT_NOPARACHUTE
 * SDL_INIT_EVERYTHING - you should only initialize the subsystems you are using
+* SDL_INIT_TIMER - no longer needed before calling SDL_AddTimer()
 
 ## SDL_joystick.h
 
diff --git a/examples/game/01-snake/snake.c b/examples/game/01-snake/snake.c
index 65ef58c6de232..181dd2af508b3 100644
--- a/examples/game/01-snake/snake.c
+++ b/examples/game/01-snake/snake.c
@@ -283,7 +283,7 @@ SDL_AppResult SDL_AppIterate(void *appstate)
 
 SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
 {
-    if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)) {
+    if (!SDL_Init(SDL_INIT_VIDEO)) {
         return SDL_APP_FAILURE;
     }
 
diff --git a/include/SDL3/SDL_init.h b/include/SDL3/SDL_init.h
index d4d4c2a4bec3e..0b3e44c327c60 100644
--- a/include/SDL3/SDL_init.h
+++ b/include/SDL3/SDL_init.h
@@ -57,7 +57,6 @@ extern "C" {
  */
 typedef Uint32 SDL_InitFlags;
 
-#define SDL_INIT_TIMER      0x00000001u
 #define SDL_INIT_AUDIO      0x00000010u /**< `SDL_INIT_AUDIO` implies `SDL_INIT_EVENTS` */
 #define SDL_INIT_VIDEO      0x00000020u /**< `SDL_INIT_VIDEO` implies `SDL_INIT_EVENTS` */
 #define SDL_INIT_JOYSTICK   0x00000200u /**< `SDL_INIT_JOYSTICK` implies `SDL_INIT_EVENTS`, should be initialized on the same thread as SDL_INIT_VIDEO on Windows if you don't set SDL_HINT_JOYSTICK_THREAD */
@@ -117,7 +116,6 @@ typedef void (SDLCALL *SDL_AppQuit_func)(void *appstate);
  *
  * `flags` may be any of the following OR'd together:
  *
- * - `SDL_INIT_TIMER`: timer subsystem
  * - `SDL_INIT_AUDIO`: audio subsystem; automatically initializes the events
  *   subsystem
  * - `SDL_INIT_VIDEO`: video subsystem; automatically initializes the events
diff --git a/include/SDL3/SDL_timer.h b/include/SDL3/SDL_timer.h
index 9d05161b4009a..54780bf679517 100644
--- a/include/SDL3/SDL_timer.h
+++ b/include/SDL3/SDL_timer.h
@@ -160,8 +160,6 @@ typedef Uint32 (SDLCALL *SDL_TimerCallback)(void *userdata, SDL_TimerID timerID,
 /**
  * Call a callback function at a future time.
  *
- * If you use this function, you must pass `SDL_INIT_TIMER` to SDL_Init().
- *
  * The callback function is passed the current timer interval and the user
  * supplied parameter from the SDL_AddTimer() call and should return the next
  * timer interval. If the value returned from the callback is 0, the timer is
@@ -224,8 +222,6 @@ typedef Uint64 (SDLCALL *SDL_NSTimerCallback)(void *userdata, SDL_TimerID timerI
 /**
  * Call a callback function at a future time.
  *
- * If you use this function, you must pass `SDL_INIT_TIMER` to SDL_Init().
- *
  * The callback function is passed the current timer interval and the user
  * supplied parameter from the SDL_AddTimerNS() call and should return the
  * next timer interval. If the value returned from the callback is 0, the
diff --git a/src/SDL.c b/src/SDL.c
index 58cd19d47d3ce..d5a12885098a9 100644
--- a/src/SDL.c
+++ b/src/SDL.c
@@ -307,20 +307,6 @@ SDL_bool SDL_InitSubSystem(SDL_InitFlags flags)
         flags_initialized |= SDL_INIT_EVENTS;
     }
 
-    // Initialize the timer subsystem
-    if (flags & SDL_INIT_TIMER) {
-        if (SDL_ShouldInitSubsystem(SDL_INIT_TIMER)) {
-            SDL_IncrementSubsystemRefCount(SDL_INIT_TIMER);
-            if (!SDL_InitTimers()) {
-                SDL_DecrementSubsystemRefCount(SDL_INIT_TIMER);
-                goto quit_and_error;
-            }
-        } else {
-            SDL_IncrementSubsystemRefCount(SDL_INIT_TIMER);
-        }
-        flags_initialized |= SDL_INIT_TIMER;
-    }
-
     // Initialize the video subsystem
     if (flags & SDL_INIT_VIDEO) {
 #ifndef SDL_VIDEO_DISABLED
@@ -573,13 +559,6 @@ void SDL_QuitSubSystem(SDL_InitFlags flags)
     }
 #endif
 
-    if (flags & SDL_INIT_TIMER) {
-        if (SDL_ShouldQuitSubsystem(SDL_INIT_TIMER)) {
-            SDL_QuitTimers();
-        }
-        SDL_DecrementSubsystemRefCount(SDL_INIT_TIMER);
-    }
-
     if (flags & SDL_INIT_EVENTS) {
         if (SDL_ShouldQuitSubsystem(SDL_INIT_EVENTS)) {
             SDL_QuitEvents();
@@ -632,6 +611,8 @@ void SDL_Quit(void)
     SDL_DBus_Quit();
 #endif
 
+    SDL_QuitTimers();
+
     SDL_SetObjectsInvalid();
     SDL_AssertionsQuit();
 
diff --git a/src/test/SDL_test_harness.c b/src/test/SDL_test_harness.c
index 982e2bc93d8a2..9704f36ba7955 100644
--- a/src/test/SDL_test_harness.c
+++ b/src/test/SDL_test_harness.c
@@ -178,8 +178,6 @@ static Uint64 SDLTest_GenerateExecKey(const char *runSeed, const char *suiteName
 /**
  * Set timeout handler for test.
  *
- * Note: SDL_Init(SDL_INIT_TIMER) will be called if it wasn't done so before.
- *
  * \param timeout Timeout interval in seconds.
  * \param callback Function that will be called after timeout has elapsed.
  *
@@ -200,14 +198,6 @@ static SDL_TimerID SDLTest_SetTestTimeout(int timeout, SDL_TimerCallback callbac
         return 0;
     }
 
-    /* Init SDL timer if not initialized before */
-    if (!SDL_WasInit(SDL_INIT_TIMER)) {
-        if (!SDL_InitSubSystem(SDL_INIT_TIMER)) {
-            SDLTest_LogError("Failed to init timer subsystem: %s", SDL_GetError());
-            return 0;
-        }
-    }
-
     /* Set timer */
     timeoutInMilliseconds = timeout * 1000;
     timerID = SDL_AddTimer(timeoutInMilliseconds, callback, 0x0);
diff --git a/test/testautomation_timer.c b/test/testautomation_timer.c
index 1115badbae8fb..2bfb2da373366 100644
--- a/test/testautomation_timer.c
+++ b/test/testautomation_timer.c
@@ -22,13 +22,6 @@ static int g_timerCallbackCalled = 0;
 
 static void SDLCALL timerSetUp(void **arg)
 {
-    /* Start SDL timer subsystem */
-    SDL_bool ret = SDL_InitSubSystem(SDL_INIT_TIMER);
-    SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_TIMER)");
-    SDLTest_AssertCheck(ret == SDL_TRUE, "Check result from SDL_InitSubSystem(SDL_INIT_TIMER)");
-    if (!ret) {
-        SDLTest_LogError("%s", SDL_GetError());
-    }
 }
 
 /* Test case functions */
diff --git a/test/testhaptic.c b/test/testhaptic.c
index e55bf29573054..f458b469dfb35 100644
--- a/test/testhaptic.c
+++ b/test/testhaptic.c
@@ -79,7 +79,7 @@ int main(int argc, char **argv)
     }
 
     /* Initialize the force feedbackness */
-    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC);
+    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC);
     haptics = SDL_GetHaptics(&num_haptics);
     SDL_Log("%d Haptic devices detected.\n", num_haptics);
     for (i = 0; i < num_haptics; ++i) {
diff --git a/test/testrumble.c b/test/testrumble.c
index 0b6d2c44cbacc..38acafd96905b 100644
--- a/test/testrumble.c
+++ b/test/testrumble.c
@@ -82,7 +82,7 @@ int main(int argc, char **argv)
     }
 
     /* Initialize the force feedbackness */
-    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC);
+    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC);
     haptics = SDL_GetHaptics(&num_haptics);
     SDL_Log("%d Haptic devices detected.\n", num_haptics);
     if (num_haptics == 0) {
diff --git a/test/testtimer.c b/test/testtimer.c
index 8c18ac06383cb..14f5ccbb07024 100644
--- a/test/testtimer.c
+++ b/test/testtimer.c
@@ -116,11 +116,6 @@ int main(int argc, char *argv[])
         i += consumed;
     }
 
-    if (!SDL_Init(SDL_INIT_TIMER)) {
-        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
-        return 1;
-    }
-
     if (SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "SDL_TESTS_QUICK") != NULL) {
         SDL_Log("Not running slower tests");
         SDL_Quit();