SDL: check dxgi1_6.h before enabling sdr/hdr code in SDL_windowsmodes.c

From 2b374e6c0f20bc06f1fc4c19bf520ed9bdbee9a1 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Tue, 28 May 2024 22:11:56 +0300
Subject: [PATCH] check dxgi1_6.h before enabling sdr/hdr code in
 SDL_windowsmodes.c

thanks @madebr
---
 CMakeLists.txt                                  |  1 +
 include/build_config/SDL_build_config.h.cmake   |  1 +
 include/build_config/SDL_build_config_windows.h |  1 +
 include/build_config/SDL_build_config_wingdk.h  |  1 +
 src/video/windows/SDL_windowsmodes.c            | 10 +++++++++-
 5 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 905ce36664743..d9805c8779bb3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1911,6 +1911,7 @@ elseif(WINDOWS)
   endif()
 
   # headers needed elsewhere
+  check_include_file(dxgi1_6.h HAVE_DXGI1_6_H)
   check_include_file(tpcshrd.h HAVE_TPCSHRD_H)
   check_include_file(roapi.h HAVE_ROAPI_H)
   check_include_file(mmdeviceapi.h HAVE_MMDEVICEAPI_H)
diff --git a/include/build_config/SDL_build_config.h.cmake b/include/build_config/SDL_build_config.h.cmake
index cf9e837fd66a1..348856a211ccf 100644
--- a/include/build_config/SDL_build_config.h.cmake
+++ b/include/build_config/SDL_build_config.h.cmake
@@ -231,6 +231,7 @@
 #cmakedefine HAVE_XINPUT_H @HAVE_XINPUT_H@
 #cmakedefine HAVE_WINDOWS_GAMING_INPUT_H @HAVE_WINDOWS_GAMING_INPUT_H@
 #cmakedefine HAVE_DXGI_H @HAVE_DXGI_H@
+#cmakedefine HAVE_DXGI1_6_H @HAVE_DXGI1_6_H@
 
 #cmakedefine HAVE_MMDEVICEAPI_H @HAVE_MMDEVICEAPI_H@
 #cmakedefine HAVE_AUDIOCLIENT_H @HAVE_AUDIOCLIENT_H@
diff --git a/include/build_config/SDL_build_config_windows.h b/include/build_config/SDL_build_config_windows.h
index b8434600fcd73..92ca5f11bfcae 100644
--- a/include/build_config/SDL_build_config_windows.h
+++ b/include/build_config/SDL_build_config_windows.h
@@ -86,6 +86,7 @@ typedef unsigned int uintptr_t;
 #define HAVE_DXGI_H 1
 #define HAVE_XINPUT_H 1
 #if defined(_WIN32_MAXVER) && _WIN32_MAXVER >= 0x0A00  /* Windows 10 SDK */
+#define HAVE_DXGI1_6_H 1
 #define HAVE_WINDOWS_GAMING_INPUT_H 1
 #endif
 #if defined(_WIN32_MAXVER) && _WIN32_MAXVER >= 0x0602  /* Windows 8 SDK */
diff --git a/include/build_config/SDL_build_config_wingdk.h b/include/build_config/SDL_build_config_wingdk.h
index 167d661f32305..2845b0ecc1479 100644
--- a/include/build_config/SDL_build_config_wingdk.h
+++ b/include/build_config/SDL_build_config_wingdk.h
@@ -37,6 +37,7 @@
 #define HAVE_DSOUND_H 1
 /* No SDK version checks needed for these because the SDK has to be new. */
 #define HAVE_DXGI_H 1
+#define HAVE_DXGI1_6_H 1
 #define HAVE_XINPUT_H 1
 #define HAVE_WINDOWS_GAMING_INPUT_H 1
 #define HAVE_D3D11_H 1
diff --git a/src/video/windows/SDL_windowsmodes.c b/src/video/windows/SDL_windowsmodes.c
index ca689d810d6a8..4926ff2af4d4e 100644
--- a/src/video/windows/SDL_windowsmodes.c
+++ b/src/video/windows/SDL_windowsmodes.c
@@ -25,8 +25,10 @@
 #include "SDL_windowsvideo.h"
 #include "../../events/SDL_displayevents_c.h"
 
+#ifdef HAVE_DXGI1_6_H
 #define COBJMACROS
 #include <dxgi1_6.h>
+#endif
 
 /* Windows CE compatibility */
 #ifndef CDS_FULLSCREEN
@@ -337,6 +339,7 @@ static char *WIN_GetDisplayNameVista(const WCHAR *deviceName)
     return NULL;
 }
 
+#ifdef HAVE_DXGI1_6_H
 static SDL_bool WIN_GetMonitorDESC1(HMONITOR hMonitor, DXGI_OUTPUT_DESC1 *desc)
 {
     typedef HRESULT (WINAPI * PFN_CREATE_DXGI_FACTORY)(REFIID riid, void **ppFactory);
@@ -353,7 +356,7 @@ static SDL_bool WIN_GetMonitorDESC1(HMONITOR hMonitor, DXGI_OUTPUT_DESC1 *desc)
     }
 #endif
     if (CreateDXGIFactoryFunc) {
-        static const GUID SDL_IID_IDXGIFactory1 = { 0x770aae78, 0xf26f, 0x4dba, { 0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87 } }; 
+        static const GUID SDL_IID_IDXGIFactory1 = { 0x770aae78, 0xf26f, 0x4dba, { 0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87 } };
         static const GUID SDL_IID_IDXGIOutput6 = { 0x068346e8, 0xaaec, 0x4b84, { 0xad, 0xd7, 0x13, 0x7f, 0x51, 0x3f, 0x77, 0xa1 } };
         IDXGIFactory1 *dxgiFactory;
 
@@ -488,6 +491,7 @@ static void WIN_GetHDRProperties(SDL_VideoDevice *_this, HMONITOR hMonitor, SDL_
         }
     }
 }
+#endif /* HAVE_DXGI1_6_H */
 
 static void WIN_AddDisplay(SDL_VideoDevice *_this, HMONITOR hMonitor, const MONITORINFOEXW *info, int *display_index)
 {
@@ -555,7 +559,9 @@ static void WIN_AddDisplay(SDL_VideoDevice *_this, HMONITOR hMonitor, const MONI
                 }
                 SDL_SendDisplayEvent(existing_display, SDL_EVENT_DISPLAY_ORIENTATION, current_orientation);
                 SDL_SetDisplayContentScale(existing_display, content_scale);
+#ifdef HAVE_DXGI1_6_H
                 WIN_GetHDRProperties(_this, hMonitor, &HDR);
+#endif
                 SDL_SetDisplayHDRProperties(existing_display, &HDR);
             }
             goto done;
@@ -588,7 +594,9 @@ static void WIN_AddDisplay(SDL_VideoDevice *_this, HMONITOR hMonitor, const MONI
     display.device = _this;
     display.driverdata = displaydata;
     WIN_GetDisplayBounds(_this, &display, &displaydata->bounds);
+#ifdef HAVE_DXGI1_6_H
     WIN_GetHDRProperties(_this, hMonitor, &display.HDR);
+#endif
     SDL_AddVideoDisplay(&display, SDL_FALSE);
     SDL_free(display.name);