SDL: Use SDLCALL for callbacks in public APIs (dbdf3)

From dbdf375a1259032041d3681c5b56e4e25eddfcf9 Mon Sep 17 00:00:00 2001
From: Cameron Cawley <[EMAIL REDACTED]>
Date: Wed, 18 May 2022 22:12:05 +0100
Subject: [PATCH] Use SDLCALL for callbacks in public APIs

---
 include/SDL_config_os2.h                    | 3 +++
 include/SDL_stdinc.h                        | 2 +-
 include/SDL_system.h                        | 2 +-
 src/audio/wasapi/SDL_wasapi_win32.c         | 2 +-
 src/joystick/hidapi/SDL_hidapi_rumble.c     | 2 +-
 src/joystick/windows/SDL_dinputjoystick.c   | 2 +-
 src/joystick/windows/SDL_rawinputjoystick.c | 2 +-
 src/joystick/windows/SDL_windowsjoystick.c  | 2 +-
 src/stdlib/SDL_malloc.c                     | 8 ++++----
 src/test/SDL_test_harness.c                 | 4 ++--
 src/video/SDL_video.c                       | 2 +-
 src/video/windows/SDL_windowsevents.c       | 1 +
 test/testqsort.c                            | 2 +-
 13 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/include/SDL_config_os2.h b/include/SDL_config_os2.h
index 1728bd772bc..c2b22cbe31d 100644
--- a/include/SDL_config_os2.h
+++ b/include/SDL_config_os2.h
@@ -105,7 +105,10 @@
 #define HAVE_GETENV 1
 #define HAVE_SETENV 1
 #define HAVE_PUTENV 1
+/* OpenWatcom requires specific calling conventions for qsort and bsearch */
+#ifndef __WATCOMC__
 #define HAVE_QSORT 1
+#endif
 #define HAVE_ABS 1
 #define HAVE_BCOPY 1
 #define HAVE_MEMSET 1
diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h
index 449e6445c46..741094c8041 100644
--- a/include/SDL_stdinc.h
+++ b/include/SDL_stdinc.h
@@ -462,7 +462,7 @@ extern DECLSPEC int SDLCALL SDL_GetNumAllocations(void);
 extern DECLSPEC char *SDLCALL SDL_getenv(const char *name);
 extern DECLSPEC int SDLCALL SDL_setenv(const char *name, const char *value, int overwrite);
 
-extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, int (*compare) (const void *, const void *));
+extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, int (SDLCALL *compare) (const void *, const void *));
 
 extern DECLSPEC int SDLCALL SDL_abs(int x);
 
diff --git a/include/SDL_system.h b/include/SDL_system.h
index 41563add429..c540e474851 100644
--- a/include/SDL_system.h
+++ b/include/SDL_system.h
@@ -195,7 +195,7 @@ extern DECLSPEC int SDLCALL SDL_LinuxSetThreadPriorityAndPolicy(Sint64 threadID,
  *
  * \sa SDL_iPhoneSetEventPump
  */
-extern DECLSPEC int SDLCALL SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam);
+extern DECLSPEC int SDLCALL SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (SDLCALL *callback)(void*), void *callbackParam);
 
 #define SDL_iOSSetEventPump(enabled) SDL_iPhoneSetEventPump(enabled)
 
diff --git a/src/audio/wasapi/SDL_wasapi_win32.c b/src/audio/wasapi/SDL_wasapi_win32.c
index 8e5c7e2a129..137f370138d 100644
--- a/src/audio/wasapi/SDL_wasapi_win32.c
+++ b/src/audio/wasapi/SDL_wasapi_win32.c
@@ -362,7 +362,7 @@ typedef struct
     WAVEFORMATEXTENSIBLE fmt;
 } EndpointItem;
 
-static int sort_endpoints(const void *_a, const void *_b)
+static int SDLCALL sort_endpoints(const void *_a, const void *_b)
 {
     LPWSTR a = ((const EndpointItem *) _a)->devid;
     LPWSTR b = ((const EndpointItem *) _b)->devid;
diff --git a/src/joystick/hidapi/SDL_hidapi_rumble.c b/src/joystick/hidapi/SDL_hidapi_rumble.c
index bf376277ec9..9cbdb959937 100644
--- a/src/joystick/hidapi/SDL_hidapi_rumble.c
+++ b/src/joystick/hidapi/SDL_hidapi_rumble.c
@@ -53,7 +53,7 @@ typedef struct SDL_HIDAPI_RumbleContext
 
 static SDL_HIDAPI_RumbleContext rumble_context;
 
-static int SDL_HIDAPI_RumbleThread(void *data)
+static int SDLCALL SDL_HIDAPI_RumbleThread(void *data)
 {
     SDL_HIDAPI_RumbleContext *ctx = (SDL_HIDAPI_RumbleContext *)data;
 
diff --git a/src/joystick/windows/SDL_dinputjoystick.c b/src/joystick/windows/SDL_dinputjoystick.c
index c2f53a93fe6..f2e15ca11da 100644
--- a/src/joystick/windows/SDL_dinputjoystick.c
+++ b/src/joystick/windows/SDL_dinputjoystick.c
@@ -683,7 +683,7 @@ EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE pDeviceObject, LPVOID pContext)
 /* Sort using the data offset into the DInput struct.
  * This gives a reasonable ordering for the inputs.
  */
-static int
+static int SDLCALL
 SortDevFunc(const void *a, const void *b)
 {
     const input_t *inputA = (const input_t*)a;
diff --git a/src/joystick/windows/SDL_rawinputjoystick.c b/src/joystick/windows/SDL_rawinputjoystick.c
index df40232df45..f3de6046f73 100644
--- a/src/joystick/windows/SDL_rawinputjoystick.c
+++ b/src/joystick/windows/SDL_rawinputjoystick.c
@@ -1053,7 +1053,7 @@ RAWINPUT_JoystickGetDeviceInstanceID(int device_index)
     return RAWINPUT_GetDeviceByIndex(device_index)->joystick_id;
 }
 
-static int
+static int SDLCALL
 RAWINPUT_SortValueCaps(const void *A, const void *B)
 {
     HIDP_VALUE_CAPS *capsA = (HIDP_VALUE_CAPS *)A;
diff --git a/src/joystick/windows/SDL_windowsjoystick.c b/src/joystick/windows/SDL_windowsjoystick.c
index 7dae16d9f2f..e051083922c 100644
--- a/src/joystick/windows/SDL_windowsjoystick.c
+++ b/src/joystick/windows/SDL_windowsjoystick.c
@@ -330,7 +330,7 @@ SDL_WaitForDeviceNotification(SDL_DeviceNotificationData *data, SDL_mutex *mutex
 static SDL_DeviceNotificationData s_notification_data;
 
 /* Function/thread to scan the system for joysticks. */
-static int
+static int SDLCALL
 SDL_JoystickThread(void *_data)
 {
 #if SDL_JOYSTICK_XINPUT
diff --git a/src/stdlib/SDL_malloc.c b/src/stdlib/SDL_malloc.c
index 7dc5a4211d3..0a51dddce1c 100644
--- a/src/stdlib/SDL_malloc.c
+++ b/src/stdlib/SDL_malloc.c
@@ -733,7 +733,7 @@ extern "C"
   maximum supported value of n differs across systems, but is in all
   cases less than the maximum representable value of a size_t.
 */
-    void *dlmalloc(size_t);
+    void * SDLCALL dlmalloc(size_t);
 
 /*
   free(void* p)
@@ -742,14 +742,14 @@ extern "C"
   It has no effect if p is null. If p was not malloced or already
   freed, free(p) will by default cause the current program to abort.
 */
-    void dlfree(void *);
+    void SDLCALL dlfree(void *);
 
 /*
   calloc(size_t n_elements, size_t element_size);
   Returns a pointer to n_elements * element_size bytes, with all locations
   set to zero.
 */
-    void *dlcalloc(size_t, size_t);
+    void * SDLCALL dlcalloc(size_t, size_t);
 
 /*
   realloc(void* p, size_t n)
@@ -774,7 +774,7 @@ extern "C"
   to be used as an argument to realloc is not supported.
 */
 
-    void *dlrealloc(void *, size_t);
+    void * SDLCALL dlrealloc(void *, size_t);
 
 /*
   memalign(size_t alignment, size_t n);
diff --git a/src/test/SDL_test_harness.c b/src/test/SDL_test_harness.c
index 8f10f3e5c7e..3a1f432b0b7 100644
--- a/src/test/SDL_test_harness.c
+++ b/src/test/SDL_test_harness.c
@@ -169,7 +169,7 @@ SDLTest_GenerateExecKey(const char *runSeed, const char *suiteName, const char *
 * \return Timer id or -1 on failure.
 */
 static SDL_TimerID
-SDLTest_SetTestTimeout(int timeout, void (*callback)(void))
+SDLTest_SetTestTimeout(int timeout, void (SDLCALL *callback)(void))
 {
     Uint32 timeoutInMilliseconds;
     SDL_TimerID timerID;
@@ -209,7 +209,7 @@ SDLTest_SetTestTimeout(int timeout, void (*callback)(void))
 #if defined(__WATCOMC__)
 #pragma aux SDLTest_BailOut aborts;
 #endif
-static SDL_NORETURN void
+static SDL_NORETURN void SDLCALL
 SDLTest_BailOut(void)
 {
     SDLTest_LogError("TestCaseTimeout timer expired. Aborting test run.");
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index c415fc29e87..b4a1df179bd 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -339,7 +339,7 @@ SDL_DestroyWindowTexture(SDL_VideoDevice *unused, SDL_Window * window)
     SDL_free(data);
 }
 
-static int
+static int SDLCALL
 cmpmodes(const void *A, const void *B)
 {
     const SDL_DisplayMode *a = (const SDL_DisplayMode *) A;
diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c
index ebd0dd1f956..cf6a7c54274 100644
--- a/src/video/windows/SDL_windowsevents.c
+++ b/src/video/windows/SDL_windowsevents.c
@@ -29,6 +29,7 @@
 #include "SDL_timer.h"
 #include "SDL_vkeys.h"
 #include "SDL_hints.h"
+#include "SDL_main.h"
 #include "../../events/SDL_events_c.h"
 #include "../../events/SDL_touch_c.h"
 #include "../../events/scancodes_windows.h"
diff --git a/test/testqsort.c b/test/testqsort.c
index d28daebd41a..29ea056275b 100644
--- a/test/testqsort.c
+++ b/test/testqsort.c
@@ -12,7 +12,7 @@
 
 #include "SDL_test.h"
 
-static int
+static int SDLCALL
 num_compare(const void *_a, const void *_b)
 {
     const int a = *((const int *) _a);