sdl2-compat: Use SDL_DisplayID

From 5d5a3f0bb4982a64a38066e4114f19d9ad2bcbee Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Tue, 31 Jan 2023 10:09:11 +0100
Subject: [PATCH] Use SDL_DisplayID

---
 src/sdl2_compat.c          | 138 +++++++++++++++++++++++++++++++++++--
 src/sdl3_include_wrapper.h |  25 -------
 src/sdl3_syms.h            |  29 ++++----
 3 files changed, 148 insertions(+), 44 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 4a07ef2..f642b6a 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -288,6 +288,9 @@ static void OS_GetExeName(char *buf, const unsigned maxpath) {
 }
 #endif
 
+
+static int Display_IDToIndex(SDL_DisplayID displayID);
+
 static const char *
 SDL2Compat_GetExeName(void)
 {
@@ -1340,7 +1343,7 @@ EventFilter3to2(void *userdata, SDL_Event *event3)
             if (SDL3_EventEnabled(SDL2_DISPLAYEVENT)) {
                 event2.display.type = SDL2_DISPLAYEVENT;
                 event2.display.timestamp = (Uint32) SDL_NS_TO_MS(event3->display.timestamp);
-                event2.display.display = event3->display.display;
+                event2.display.display = Display_IDToIndex(event3->display.displayID);
                 event2.display.event = (Uint8) ((event3->type - ((Uint32) SDL_EVENT_DISPLAY_ORIENTATION)) + 1);
                 event2.display.padding1 = 0;
                 event2.display.padding2 = 0;
@@ -3239,11 +3242,136 @@ DisplayMode_3to2(const SDL_DisplayMode *in, SDL2_DisplayMode *out) {
     }
 }
 
+
+static SDL_DisplayID Display_IndexToID(int displayIndex)
+{
+    SDL_DisplayID displayID = 0;
+    int count = 0;
+    SDL_DisplayID *list = NULL;
+
+    list = SDL3_GetDisplays(&count);
+
+    if (list == NULL || list == 0) {
+        SDL3_SetError("no displays");
+        SDL_free(list);
+        return SDL3_GetPrimaryDisplay();
+    }
+
+    if (displayIndex < 0 || displayIndex >= count) {
+        SDL3_SetError("invalid displayIndex");
+        SDL_free(list);
+        return SDL3_GetPrimaryDisplay();
+    }
+
+    displayID = list[displayIndex];
+    SDL_free(list);
+    return displayID;
+}
+
+DECLSPEC int SDLCALL
+SDL_GetNumVideoDisplays(void)
+{
+    int count = 0;
+    SDL_DisplayID *list = NULL;
+    list = SDL3_GetDisplays(&count);
+    SDL_free(list);
+    return count;
+}
+
+DECLSPEC int SDLCALL SDL_GetWindowDisplayIndex(SDL_Window * window)
+{
+    SDL_DisplayID displayID = SDL3_GetDisplayForWindow(window);
+    return Display_IDToIndex(displayID);
+}
+
+DECLSPEC int SDLCALL SDL_GetPointDisplayIndex(const SDL_Point * point)
+{
+    SDL_DisplayID displayID = SDL3_GetDisplayForPoint(point);
+    return Display_IDToIndex(displayID);
+}
+
+DECLSPEC int SDLCALL SDL_GetRectDisplayIndex(const SDL_Rect * rect)
+{
+    SDL_DisplayID displayID = SDL3_GetDisplayForRect(rect);
+    return Display_IDToIndex(displayID);
+}
+
+static int Display_IDToIndex(SDL_DisplayID displayID)
+{
+    int displayIndex = 0;
+    int count = 0, i;
+    SDL_DisplayID *list = NULL;
+
+    if (displayID == 0) {
+        SDL3_SetError("invalid displayID");
+        return 0;
+    }
+
+    list = SDL3_GetDisplays(&count);
+
+    if (list == NULL || list == 0) {
+        SDL3_SetError("no displays");
+        SDL_free(list);
+        return 0;
+    }
+
+    for (i = 0; i < count; i++) {
+        if (list[i] == displayID) {
+            displayIndex = i;
+            break;
+        }
+    }
+    SDL_free(list);
+    return displayIndex;
+}
+
+DECLSPEC const char * SDLCALL
+SDL_GetDisplayName(int displayIndex)
+{
+    SDL_DisplayID displayID = Display_IndexToID(displayIndex);
+    return SDL3_GetDisplayName(displayID);
+}
+
+DECLSPEC int SDLCALL
+SDL_GetDisplayBounds(int displayIndex, SDL_Rect * rect)
+{
+    SDL_DisplayID displayID = Display_IndexToID(displayIndex);
+    return SDL3_GetDisplayBounds(displayID, rect);
+}
+
+DECLSPEC int SDLCALL
+SDL_GetNumDisplayModes(int displayIndex)
+{
+    SDL_DisplayID displayID = Display_IndexToID(displayIndex);
+    return SDL3_GetNumDisplayModes(displayID);
+}
+
+DECLSPEC int SDLCALL
+SDL_GetDisplayDPI(int displayIndex, float * ddpi, float * hdpi, float * vdpi)
+{
+    SDL_DisplayID displayID = Display_IndexToID(displayIndex);
+    return SDL3_GetDisplayPhysicalDPI(displayID, ddpi, hdpi, vdpi);
+}
+
+DECLSPEC int SDLCALL
+SDL_GetDisplayUsableBounds(int displayIndex, SDL_Rect * rect)
+{
+    SDL_DisplayID displayID = Display_IndexToID(displayIndex);
+    return SDL3_GetDisplayUsableBounds(displayID, rect);
+}
+
+DECLSPEC SDL_DisplayOrientation SDLCALL
+SDL_GetDisplayOrientation(int displayIndex)
+{
+    SDL_DisplayID displayID = Display_IndexToID(displayIndex);
+    return SDL3_GetDisplayOrientation(displayID);
+}
+
 DECLSPEC int SDLCALL
 SDL_GetDisplayMode(int displayIndex, int modeIndex, SDL2_DisplayMode *mode)
 {
     SDL_DisplayMode dp;
-    int ret = SDL3_GetDisplayMode(displayIndex, modeIndex, mode ? &dp : NULL);
+    int ret = SDL3_GetDisplayMode(Display_IndexToID(displayIndex), modeIndex, mode ? &dp : NULL);
     DisplayMode_3to2(&dp, mode);
     return ret;
 }
@@ -3252,7 +3380,7 @@ DECLSPEC int SDLCALL
 SDL_GetCurrentDisplayMode(int displayIndex, SDL2_DisplayMode *mode)
 {
     SDL_DisplayMode dp;
-    int ret = SDL3_GetCurrentDisplayMode(displayIndex, mode ? &dp : NULL);
+    int ret = SDL3_GetCurrentDisplayMode(Display_IndexToID(displayIndex), mode ? &dp : NULL);
     DisplayMode_3to2(&dp, mode);
     return ret;
 }
@@ -3261,7 +3389,7 @@ DECLSPEC int SDLCALL
 SDL_GetDesktopDisplayMode(int displayIndex, SDL2_DisplayMode *mode)
 {
     SDL_DisplayMode dp;
-    int ret = SDL3_GetDesktopDisplayMode(displayIndex, mode ? &dp : NULL);
+    int ret = SDL3_GetDesktopDisplayMode(Display_IndexToID(displayIndex), mode ? &dp : NULL);
     DisplayMode_3to2(&dp, mode);
     return ret;
 }
@@ -3283,7 +3411,7 @@ SDL_GetClosestDisplayMode(int displayIndex, const SDL2_DisplayMode * mode, SDL2_
     SDL_DisplayMode *ret;
     static SDL2_DisplayMode ret2;  /* FIXME alloc ?? */
     DisplayMode_2to3(closest, &closest3);
-    ret = SDL3_GetClosestDisplayMode(displayIndex, mode ? &dp : NULL, closest ? &closest3 : NULL);
+    ret = SDL3_GetClosestDisplayMode(Display_IndexToID(displayIndex), mode ? &dp : NULL, closest ? &closest3 : NULL);
     DisplayMode_3to2(ret, &ret2);
     return &ret2;
 }
diff --git a/src/sdl3_include_wrapper.h b/src/sdl3_include_wrapper.h
index 8f33b1b..d838825 100644
--- a/src/sdl3_include_wrapper.h
+++ b/src/sdl3_include_wrapper.h
@@ -433,20 +433,15 @@
 #define SDL_GetNumVideoDrivers IGNORE_THIS_VERSION_OF_SDL_GetNumVideoDrivers
 #define SDL_GetVideoDriver IGNORE_THIS_VERSION_OF_SDL_GetVideoDriver
 #define SDL_GetCurrentVideoDriver IGNORE_THIS_VERSION_OF_SDL_GetCurrentVideoDriver
-#define SDL_GetNumVideoDisplays IGNORE_THIS_VERSION_OF_SDL_GetNumVideoDisplays
 #define SDL_GetDisplayName IGNORE_THIS_VERSION_OF_SDL_GetDisplayName
 #define SDL_GetDisplayBounds IGNORE_THIS_VERSION_OF_SDL_GetDisplayBounds
 #define SDL_GetDisplayUsableBounds IGNORE_THIS_VERSION_OF_SDL_GetDisplayUsableBounds
-#define SDL_GetDisplayPhysicalDPI IGNORE_THIS_VERSION_OF_SDL_GetDisplayPhysicalDPI
 #define SDL_GetDisplayOrientation IGNORE_THIS_VERSION_OF_SDL_GetDisplayOrientation
 #define SDL_GetNumDisplayModes IGNORE_THIS_VERSION_OF_SDL_GetNumDisplayModes
 #define SDL_GetDisplayMode IGNORE_THIS_VERSION_OF_SDL_GetDisplayMode
 #define SDL_GetDesktopDisplayMode IGNORE_THIS_VERSION_OF_SDL_GetDesktopDisplayMode
 #define SDL_GetCurrentDisplayMode IGNORE_THIS_VERSION_OF_SDL_GetCurrentDisplayMode
 #define SDL_GetClosestDisplayMode IGNORE_THIS_VERSION_OF_SDL_GetClosestDisplayMode
-#define SDL_GetDisplayIndexForPoint IGNORE_THIS_VERSION_OF_SDL_GetDisplayIndexForPoint
-#define SDL_GetDisplayIndexForRect IGNORE_THIS_VERSION_OF_SDL_GetDisplayIndexForRect
-#define SDL_GetWindowDisplayIndex IGNORE_THIS_VERSION_OF_SDL_GetWindowDisplayIndex
 #define SDL_SetWindowDisplayMode IGNORE_THIS_VERSION_OF_SDL_SetWindowDisplayMode
 #define SDL_GetWindowDisplayMode IGNORE_THIS_VERSION_OF_SDL_GetWindowDisplayMode
 #define SDL_GetWindowICCProfile IGNORE_THIS_VERSION_OF_SDL_GetWindowICCProfile
@@ -2536,10 +2531,6 @@ typedef void (__cdecl *pfnSDL_CurrentEndThread) (unsigned);
 #undef SDL_GetCurrentVideoDriver
 #endif
 
-#ifdef SDL_GetNumVideoDisplays
-#undef SDL_GetNumVideoDisplays
-#endif
-
 #ifdef SDL_GetDisplayName
 #undef SDL_GetDisplayName
 #endif
@@ -2552,10 +2543,6 @@ typedef void (__cdecl *pfnSDL_CurrentEndThread) (unsigned);
 #undef SDL_GetDisplayUsableBounds
 #endif
 
-#ifdef SDL_GetDisplayPhysicalDPI
-#undef SDL_GetDisplayPhysicalDPI
-#endif
-
 #ifdef SDL_GetDisplayOrientation
 #undef SDL_GetDisplayOrientation
 #endif
@@ -2580,18 +2567,6 @@ typedef void (__cdecl *pfnSDL_CurrentEndThread) (unsigned);
 #undef SDL_GetClosestDisplayMode
 #endif
 
-#ifdef SDL_GetDisplayIndexForPoint
-#undef SDL_GetDisplayIndexForPoint
-#endif
-
-#ifdef SDL_GetDisplayIndexForRect
-#undef SDL_GetDisplayIndexForRect
-#endif
-
-#ifdef SDL_GetWindowDisplayIndex
-#undef SDL_GetWindowDisplayIndex
-#endif
-
 #ifdef SDL_SetWindowDisplayMode
 #undef SDL_SetWindowDisplayMode
 #endif
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index 0db6969..2351d62 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -480,15 +480,13 @@ SDL3_SYM_PASSTHROUGH(const char*,GetRevision,(void),(),return)
 SDL3_SYM_PASSTHROUGH(int,GetNumVideoDrivers,(void),(),return)
 SDL3_SYM_PASSTHROUGH(const char*,GetVideoDriver,(int a),(a),return)
 SDL3_SYM_PASSTHROUGH(const char*,GetCurrentVideoDriver,(void),(),return)
-SDL3_SYM_PASSTHROUGH(int,GetNumVideoDisplays,(void),(),return)
-SDL3_SYM_PASSTHROUGH(const char*,GetDisplayName,(int a),(a),return)
-SDL3_SYM_PASSTHROUGH(int,GetDisplayBounds,(int a, SDL_Rect *b),(a,b),return)
-SDL3_SYM_PASSTHROUGH(int,GetNumDisplayModes,(int a),(a),return)
-SDL3_SYM(int,GetDisplayMode,(int a, int b, SDL_DisplayMode *c),(a,b,c),return)
-SDL3_SYM(int,GetDesktopDisplayMode,(int a, SDL_DisplayMode *b),(a,b),return)
-SDL3_SYM(int,GetCurrentDisplayMode,(int a, SDL_DisplayMode *b),(a,b),return)
-SDL3_SYM(SDL_DisplayMode*,GetClosestDisplayMode,(int a, const SDL_DisplayMode *b, SDL_DisplayMode *c),(a,b,c),return)
-SDL3_SYM_PASSTHROUGH(int,GetWindowDisplayIndex,(SDL_Window *a),(a),return)
+SDL3_SYM(const char*,GetDisplayName,(SDL_DisplayID a),(a),return)
+SDL3_SYM(int,GetDisplayBounds,(SDL_DisplayID a, SDL_Rect *b),(a,b),return)
+SDL3_SYM(int,GetNumDisplayModes,(SDL_DisplayID a),(a),return)
+SDL3_SYM(int,GetDisplayMode,(SDL_DisplayID a, int b, SDL_DisplayMode *c),(a,b,c),return)
+SDL3_SYM(int,GetDesktopDisplayMode,(SDL_DisplayID a, SDL_DisplayMode *b),(a,b),return)
+SDL3_SYM(int,GetCurrentDisplayMode,(SDL_DisplayID a, SDL_DisplayMode *b),(a,b),return)
+SDL3_SYM(SDL_DisplayMode*,GetClosestDisplayMode,(SDL_DisplayID a, const SDL_DisplayMode *b, SDL_DisplayMode *c),(a,b,c),return)
 SDL3_SYM(int,SetWindowDisplayMode,(SDL_Window *a, const SDL_DisplayMode *b),(a,b),return)
 SDL3_SYM(int,GetWindowDisplayMode,(SDL_Window *a, SDL_DisplayMode *b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(Uint32,GetWindowPixelFormat,(SDL_Window *a),(a),return)
@@ -571,11 +569,11 @@ SDL3_SYM_PASSTHROUGH(SDL_Window*,GetGrabbedWindow,(void),(),return)
 #if defined(__WIN32__) || defined(__GDK__)
 SDL3_SYM_PASSTHROUGH(void,SetWindowsMessageHook,(SDL_WindowsMessageHook a, void *b),(a,b),)
 #endif
-SDL3_SYM_RENAMED(int,GetDisplayDPI,GetDisplayPhysicalDPI,(int a, float *b, float *c, float *d),(a,b,c,d),return)
+SDL3_SYM(int,GetDisplayPhysicalDPI,(SDL_DisplayID, float *b, float *c, float *d),(a,b,c,d),return)
 SDL3_SYM_RENAMED(SDL_JoystickPowerLevel,JoystickCurrentPowerLevel,GetJoystickPowerLevel,(SDL_Joystick *a),(a),return)
 SDL3_SYM_RENAMED(SDL_GameController*,GameControllerFromInstanceID,GetGamepadFromInstanceID,(SDL_JoystickID a),(a),return)
 SDL3_SYM_RENAMED(SDL_Joystick*,JoystickFromInstanceID,GetJoystickFromInstanceID,(SDL_JoystickID a),(a),return)
-SDL3_SYM_PASSTHROUGH(int,GetDisplayUsableBounds,(int a, SDL_Rect *b),(a,b),return)
+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)
@@ -668,7 +666,7 @@ SDL3_SYM_RENAMED(int,SensorGetData,GetSensorData,(SDL_Sensor *a, float *b, int c
 SDL3_SYM_RENAMED(void,SensorClose,CloseSensor,(SDL_Sensor *a),(a),)
 SDL3_SYM_RENAMED(void,SensorUpdate,UpdateSensors,(void),(),)
 SDL3_SYM_PASSTHROUGH(SDL_bool,IsTablet,(void),(),return)
-SDL3_SYM_PASSTHROUGH(SDL_DisplayOrientation,GetDisplayOrientation,(int a),(a),return)
+SDL3_SYM(SDL_DisplayOrientation,GetDisplayOrientation,(SDL_DisplayID a),(a),return)
 SDL3_SYM_RENAMED(SDL_bool,HasColorKey,SurfaceHasColorKey,(SDL_Surface *a),(a),return)
 
 #if defined(__WIN32__) || defined(__GDK__)
@@ -873,8 +871,6 @@ SDL3_SYM_PASSTHROUGH(int,GDKRunApp,(SDL_main_func a, void *b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(void,GetOriginalMemoryFunctions,(SDL_malloc_func *a, SDL_calloc_func *b, SDL_realloc_func *c, SDL_free_func *d),(a,b,c,d),)
 SDL3_SYM_PASSTHROUGH(void,ResetKeyboard,(void),(),)
 SDL3_SYM_PASSTHROUGH(int,GetDefaultAudioInfo,(char **a, SDL_AudioSpec *b, int c),(a,b,c),return)
-SDL3_SYM_RENAMED(int,GetPointDisplayIndex,GetDisplayIndexForPoint,(const SDL_Point *a),(a),return)
-SDL3_SYM_RENAMED(int,GetRectDisplayIndex,GetDisplayIndexForRect,(const SDL_Rect *a),(a),return)
 SDL3_SYM(SDL_bool,ResetHint,(const char *a),(a),return)
 SDL3_SYM_PASSTHROUGH(Uint16,crc16,(Uint16 a, const void *b, size_t c),(a,b,c),return)
 SDL3_SYM_PASSTHROUGH(void,GetWindowSizeInPixels,(SDL_Window *a, int *b, int *c),(a,b,c),)
@@ -918,6 +914,11 @@ SDL3_SYM(int,GetSensorInstanceNonPortableType,(SDL_SensorID a),(a),return)
 SDL3_SYM(SDL_Sensor *,OpenSensor,(SDL_SensorID a),(a),return)
 SDL3_SYM(double,modf,(double a, double *b),(a,b),return)
 SDL3_SYM(float,modff,(float a, float *b),(a,b),return)
+SDL3_SYM(SDL_DisplayID*,GetDisplays,(int *a),(a),return)
+SDL3_SYM(SDL_DisplayID,GetDisplayForWindow,(SDL_Window *a),(a),return)
+SDL3_SYM(SDL_DisplayID,GetDisplayForPoint,(const SDL_Point *a),(a),return)
+SDL3_SYM(SDL_DisplayID,GetDisplayForRect,(const SDL_Rect *a),(a),return)
+SDL3_SYM(SDL_DisplayID,GetPrimaryDisplay,(),(),return)
 
 #undef SDL3_SYM
 #undef SDL3_SYM_PASSTHROUGH