sdl2-compat: Update for the removal of SDL_SetWindowInputFocus and SDL_EVENT_WINDOW_TAKE_FOCUS from SDL3

From 255ac94893b5dc37ae515464114a6f35250d181d Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Wed, 3 Jul 2024 14:37:45 -0400
Subject: [PATCH] Update for the removal of SDL_SetWindowInputFocus and
 SDL_EVENT_WINDOW_TAKE_FOCUS from SDL3

Removes the now-defunct event and remaps SDL_SetWindowInputFocus() to SDL_RaiseWindow()
---
 src/sdl2_compat.c          | 27 +++++++++++++++++++++++++--
 src/sdl3_include_wrapper.h |  1 -
 src/sdl3_syms.h            |  1 -
 3 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 3760ca7..c6a21b1 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -1852,6 +1852,22 @@ SDL_PushEvent(SDL2_Event *event2)
 
 static int Display_IDToIndex(SDL_DisplayID displayID);
 
+static Uint8
+WindowEventType3To2(Uint32 event_type3)
+{
+    Uint8 event_type2 = (Uint8) (event_type3 - (Uint32) SDL_EVENT_WINDOW_SHOWN + 1);
+
+    /* SDL_EVENT_WINDOW_TAKE_FOCUS (formerly event 528, now SDL_EVENT_WINDOW_HIT_TEST) was removed from SDL3.
+     * SDL_EVENT_WINDOW_HIT_TEST and higher need the offset adjusted by an additional +1 when mapping to SDL2
+     * to account for the difference.
+     */
+    if (event_type3 >= SDL_EVENT_WINDOW_HIT_TEST) {
+        event_type2 += 1;
+    }
+
+    return event_type2;
+}
+
 static int SDLCALL
 EventFilter3to2(void *userdata, SDL_Event *event3)
 {
@@ -1919,15 +1935,16 @@ EventFilter3to2(void *userdata, SDL_Event *event3)
         case SDL_EVENT_WINDOW_FOCUS_GAINED:
         case SDL_EVENT_WINDOW_FOCUS_LOST:
         case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
-        case SDL_EVENT_WINDOW_TAKE_FOCUS:
         case SDL_EVENT_WINDOW_HIT_TEST:
         case SDL_EVENT_WINDOW_ICCPROF_CHANGED:
         case SDL_EVENT_WINDOW_DISPLAY_CHANGED:
             if (SDL3_EventEnabled(SDL2_WINDOWEVENT)) {
+
+
                 event2.window.type = SDL2_WINDOWEVENT;
                 event2.window.timestamp = (Uint32) SDL_NS_TO_MS(event3->window.timestamp);
                 event2.window.windowID = event3->window.windowID;
-                event2.window.event = (Uint8) ((event3->type - ((Uint32) SDL_EVENT_WINDOW_SHOWN)) + 1);
+                event2.window.event = WindowEventType3To2(event3->type);
                 event2.window.padding1 = 0;
                 event2.window.padding2 = 0;
                 event2.window.padding3 = 0;
@@ -7133,6 +7150,12 @@ SDL_RaiseWindow(SDL_Window *window)
     SDL3_RaiseWindow(window);
 }
 
+SDL_DECLSPEC int SDLCALL
+SDL_SetWindowInputFocus(SDL_Window *window)
+{
+    return SDL3_RaiseWindow(window);
+}
+
 SDL_DECLSPEC void SDLCALL
 SDL_MaximizeWindow(SDL_Window *window)
 {
diff --git a/src/sdl3_include_wrapper.h b/src/sdl3_include_wrapper.h
index 47413b2..ad82333 100644
--- a/src/sdl3_include_wrapper.h
+++ b/src/sdl3_include_wrapper.h
@@ -794,7 +794,6 @@
 #define SDL_SetWindowFullscreenMode IGNORE_THIS_VERSION_OF_SDL_SetWindowFullscreenMode
 #define SDL_SetWindowHitTest IGNORE_THIS_VERSION_OF_SDL_SetWindowHitTest
 #define SDL_SetWindowIcon IGNORE_THIS_VERSION_OF_SDL_SetWindowIcon
-#define SDL_SetWindowInputFocus IGNORE_THIS_VERSION_OF_SDL_SetWindowInputFocus
 #define SDL_SetWindowKeyboardGrab IGNORE_THIS_VERSION_OF_SDL_SetWindowKeyboardGrab
 #define SDL_SetWindowMaximumSize IGNORE_THIS_VERSION_OF_SDL_SetWindowMaximumSize
 #define SDL_SetWindowMinimumSize IGNORE_THIS_VERSION_OF_SDL_SetWindowMinimumSize
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index 1a030d1..c6043f6 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -529,7 +529,6 @@ SDL3_SYM(int,GetDisplayUsableBounds,(SDL_DisplayID a, SDL_Rect *b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(int,GetWindowBordersSize,(SDL_Window *a, int *b, int *c, int *d, int *e),(a,b,c,d,e),return)
 SDL3_SYM_PASSTHROUGH(int,SetWindowOpacity,(SDL_Window *a, float b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(int,GetWindowOpacity,(SDL_Window *a, float *b),(a,b),return)
-SDL3_SYM_PASSTHROUGH(int,SetWindowInputFocus,(SDL_Window *a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,SetWindowModalFor,(SDL_Window *a, SDL_Window *b),(a,b),return)
 SDL3_SYM(int,SetWindowResizable,(SDL_Window *a, SDL_bool b),(a,b),return)
 SDL3_SYM(SDL_bool,GetHintBoolean,(const char *a, SDL_bool b),(a,b),return)