SDL: Set the hint value before calling hint callbacks

From e643d28d9c3ab5249caa0ec6ed0b665320f942ad Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 28 Jun 2023 08:53:48 -0700
Subject: [PATCH] Set the hint value before calling hint callbacks

This allows the hint callbacks to perform more complicated logic involving multiple hints without worrying about which hint has been changed.
---
 docs/README-migration.md |  2 ++
 src/SDL_hints.c          | 10 +++++++---
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/docs/README-migration.md b/docs/README-migration.md
index 61c8aadf3175..4c34f365e30c 100644
--- a/docs/README-migration.md
+++ b/docs/README-migration.md
@@ -386,6 +386,8 @@ functionality to your app and aid migration. That is located in the
 
 SDL_AddHintCallback() now returns a standard int result instead of void, returning 0 if the function succeeds or a negative error code if there was an error.
 
+Calling SDL_GetHint() with the name of the hint being changed from within a hint callback will now return the new value rather than the old value. The old value is still passed as a parameter to the hint callback.
+
 The following hints have been removed:
 * SDL_HINT_VIDEO_HIGHDPI_DISABLED - high DPI support is always enabled
 * SDL_HINT_IDLE_TIMER_DISABLED - use SDL_DisableScreenSaver instead
diff --git a/src/SDL_hints.c b/src/SDL_hints.c
index e813a6f97ac3..9ee0bc31c91c 100644
--- a/src/SDL_hints.c
+++ b/src/SDL_hints.c
@@ -65,14 +65,18 @@ SDL_bool SDL_SetHintWithPriority(const char *name, const char *value, SDL_HintPr
             }
             if (hint->value != value &&
                 (value == NULL || !hint->value || SDL_strcmp(hint->value, value) != 0)) {
+                char *old_value = hint->value;
+
+                hint->value = value ? SDL_strdup(value) : NULL;
                 for (entry = hint->callbacks; entry;) {
                     /* Save the next entry in case this one is deleted */
                     SDL_HintWatch *next = entry->next;
-                    entry->callback(entry->userdata, name, hint->value, value);
+                    entry->callback(entry->userdata, name, old_value, value);
                     entry = next;
                 }
-                SDL_free(hint->value);
-                hint->value = value ? SDL_strdup(value) : NULL;
+                if (old_value) {
+                    SDL_free(old_value);
+                }
             }
             hint->priority = priority;
             return SDL_TRUE;