sdl2-compat: Check for WinRT before using the windows SDL_Thread hack

From 6e86c928e26db41b2e4bd332e94f20c3487149bb Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Mon, 6 Feb 2023 21:10:00 +0300
Subject: [PATCH] Check for WinRT before using the windows SDL_Thread hack

Because the SDL_PASSED_BEGINTHREAD_ENDTHREAD magic seems to be only for
WIN32 desktop and GDK, and WINRT is not included for it -- see platform
defines for __WIN32__, __GDK__ and __WINRT__, compare with SDL_thread.h

Closes https://github.com/libsdl-org/sdl2-compat/issues/40
---
 CMakeLists.txt             |  5 +++++
 src/sdl3_include_wrapper.h | 13 ++++++++++---
 src/sdl3_syms.h            |  2 +-
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8038f75..c727026 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -204,6 +204,11 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
   endif()
 endif()
 
+check_include_file("winapifamily.h" HAVE_WINAPIFAMILY_H)
+if(HAVE_WINAPIFAMILY_H)
+  set(EXTRA_CFLAGS "${EXTRA_CFLAGS} -DSDL_HAVE_WINAPIFAMILY_H")
+endif()
+
 # This isn't needed for compat libSDL2 who use SDL3 headers.
 # SDL2_test and SDL2_main (and the tes programs) still need
 # this, because they use SDL2 headers.
diff --git a/src/sdl3_include_wrapper.h b/src/sdl3_include_wrapper.h
index 592e4e6..95c341d 100644
--- a/src/sdl3_include_wrapper.h
+++ b/src/sdl3_include_wrapper.h
@@ -868,8 +868,15 @@
 #define SDL_LogSetOutputFunction IGNORE_THIS_VERSION_OF_SDL_LogSetOutputFunction
 
 /* *** HACK HACK HACK:
- * *** Avoid including SDL_thread.h: it defines SDL_CreateThread() as a macro */
-#ifdef _WIN32
+ * *** Avoid including SDL_thread.h: it defines SDL_CreateThread() as a macro
+ * *** for Win32 Desktop and GDK, but not for WinRT. */
+#ifdef SDL_HAVE_WINAPIFAMILY_H
+#include <winapifamily.h>
+#if (!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP))
+#define SDL2COMPAT_WINRT
+#endif
+#endif
+#if defined(_WIN32) && !defined(SDL2COMPAT_WINRT)
 #define _SDL_thread_h
 #define SDL_thread_h_
 #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
@@ -890,7 +897,7 @@
 #include <SDL3/SDL_vulkan.h>
 
 /* Missing SDL_thread.h stuff (see above) */
-#ifdef _WIN32
+#if defined(_WIN32) && !defined(SDL2COMPAT_WINRT)
 #ifndef WIN32_LEAN_AND_MEAN
 #define WIN32_LEAN_AND_MEAN 1
 #endif
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index a619778..5a7fb12 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -659,7 +659,7 @@ SDL3_SYM_PASSTHROUGH(SDL_bool,IsTablet,(void),(),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__)
+#if (defined(__WIN32__) || defined(__GDK__)) && !defined(__WINRT__)
 SDL3_SYM_PASSTHROUGH(SDL_Thread*,CreateThreadWithStackSize,(SDL_ThreadFunction a, const char *b, const size_t c, void *d, pfnSDL_CurrentBeginThread e, pfnSDL_CurrentEndThread f),(a,b,c,d,e,f),return)
 #else
 SDL3_SYM_PASSTHROUGH(SDL_Thread*,CreateThreadWithStackSize,(SDL_ThreadFunction a, const char *b, const size_t c, void *d),(a,b,c,d),return)