From c823f26f184e6e92f9f5ec3187a9533e5f3b5683 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 4 Jan 2023 13:51:40 -0800
Subject: [PATCH] Revert "Removed public joystick locking API"
This reverts commit a515f51ac0001d9173dfc5bb338c536ec1eb14bc.
We still need joystick locking to protect the gamepad mappings
---
docs/README-migration.md | 4 +---
include/SDL3/SDL_joystick.h | 21 +++++++++++++++++++++
src/dynapi/SDL_dynapi.sym | 2 ++
src/dynapi/SDL_dynapi_overrides.h | 2 ++
src/dynapi/SDL_dynapi_procs.h | 2 ++
src/joystick/SDL_joystick_c.h | 10 ----------
6 files changed, 28 insertions(+), 13 deletions(-)
diff --git a/docs/README-migration.md b/docs/README-migration.md
index a1aa4be88979..e6892816dfc7 100644
--- a/docs/README-migration.md
+++ b/docs/README-migration.md
@@ -349,7 +349,6 @@ The following symbols have been renamed:
* SDL_JOYSTICK_TYPE_GAMECONTROLLER => SDL_JOYSTICK_TYPE_GAMEPAD
The following functions have been removed:
-* SDL_LockJoysticks()
* SDL_JoystickEventState() - replaced with SDL_SetJoystickEventsEnabled() and SDL_JoystickEventsEnabled()
* SDL_JoystickGetDeviceGUID() - replaced with SDL_GetJoystickInstanceGUID()
* SDL_JoystickGetDeviceInstanceID()
@@ -360,8 +359,7 @@ The following functions have been removed:
* SDL_JoystickGetDeviceVendor() - replaced with SDL_GetJoystickInstanceVendor()
* SDL_JoystickNameForIndex() - replaced with SDL_GetJoystickInstanceName()
* SDL_JoystickPathForIndex() - replaced with SDL_GetJoystickInstancePath()
-* SDL_NumJoysticks() - replaced with SDL_GetJoysticks()
-* SDL_UnlockJoysticks()
+* SDL_NumJoysticks - replaced with SDL_GetJoysticks()
## SDL_keycode.h
diff --git a/include/SDL3/SDL_joystick.h b/include/SDL3/SDL_joystick.h
index 214f90255a09..5e75df8874a1 100644
--- a/include/SDL3/SDL_joystick.h
+++ b/include/SDL3/SDL_joystick.h
@@ -64,6 +64,9 @@ extern "C" {
/**
* The joystick structure used to identify an SDL joystick
*/
+#ifdef SDL_THREAD_SAFETY_ANALYSIS
+extern SDL_mutex *SDL_joystick_lock;
+#endif
struct SDL_Joystick;
typedef struct SDL_Joystick SDL_Joystick;
@@ -115,6 +118,24 @@ typedef enum
/* Function prototypes */
+/**
+ * Locking for atomic access to the joystick API
+ *
+ * The SDL joystick functions are thread-safe, however you can lock the joysticks
+ * while processing to guarantee that the joystick list won't change and joystick
+ * and gamepad events will not be delivered.
+ *
+ * \since This function is available since SDL 3.0.0.
+ */
+extern DECLSPEC void SDLCALL SDL_LockJoysticks(void) SDL_ACQUIRE(SDL_joystick_lock);
+
+/**
+ * Unlocking for atomic access to the joystick API
+ *
+ * \since This function is available since SDL 3.0.0.
+ */
+extern DECLSPEC void SDLCALL SDL_UnlockJoysticks(void) SDL_RELEASE(SDL_joystick_lock);
+
/**
* Get a list of currently connected joysticks.
*
diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym
index deac2f901171..f780c5c5fb0a 100644
--- a/src/dynapi/SDL_dynapi.sym
+++ b/src/dynapi/SDL_dynapi.sym
@@ -450,6 +450,7 @@ SDL3_0.0.0 {
SDL_LoadWAV_RW;
SDL_LockAudio;
SDL_LockAudioDevice;
+ SDL_LockJoysticks;
SDL_LockMutex;
SDL_LockSurface;
SDL_LockTexture;
@@ -666,6 +667,7 @@ SDL3_0.0.0 {
SDL_UnloadObject;
SDL_UnlockAudio;
SDL_UnlockAudioDevice;
+ SDL_UnlockJoysticks;
SDL_UnlockMutex;
SDL_UnlockSurface;
SDL_UnlockTexture;
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 08a974093ebd..b322d3822bd2 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -476,6 +476,7 @@
#define SDL_LoadWAV_RW SDL_LoadWAV_RW_REAL
#define SDL_LockAudio SDL_LockAudio_REAL
#define SDL_LockAudioDevice SDL_LockAudioDevice_REAL
+#define SDL_LockJoysticks SDL_LockJoysticks_REAL
#define SDL_LockMutex SDL_LockMutex_REAL
#define SDL_LockSurface SDL_LockSurface_REAL
#define SDL_LockTexture SDL_LockTexture_REAL
@@ -692,6 +693,7 @@
#define SDL_UnloadObject SDL_UnloadObject_REAL
#define SDL_UnlockAudio SDL_UnlockAudio_REAL
#define SDL_UnlockAudioDevice SDL_UnlockAudioDevice_REAL
+#define SDL_UnlockJoysticks SDL_UnlockJoysticks_REAL
#define SDL_UnlockMutex SDL_UnlockMutex_REAL
#define SDL_UnlockSurface SDL_UnlockSurface_REAL
#define SDL_UnlockTexture SDL_UnlockTexture_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 4a5fd0ec5b1c..ff3276eb7727 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -543,6 +543,7 @@ SDL_DYNAPI_PROC(void*,SDL_LoadObject,(const char *a),(a),return)
SDL_DYNAPI_PROC(SDL_AudioSpec*,SDL_LoadWAV_RW,(SDL_RWops *a, int b, SDL_AudioSpec *c, Uint8 **d, Uint32 *e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(void,SDL_LockAudio,(void),(),)
SDL_DYNAPI_PROC(void,SDL_LockAudioDevice,(SDL_AudioDeviceID a),(a),)
+SDL_DYNAPI_PROC(void,SDL_LockJoysticks,(void),(),)
SDL_DYNAPI_PROC(int,SDL_LockMutex,(SDL_mutex *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_LockSurface,(SDL_Surface *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_LockTexture,(SDL_Texture *a, const SDL_Rect *b, void **c, int *d),(a,b,c,d),return)
@@ -746,6 +747,7 @@ SDL_DYNAPI_PROC(int,SDL_TryLockMutex,(SDL_mutex *a),(a),return)
SDL_DYNAPI_PROC(void,SDL_UnloadObject,(void *a),(a),)
SDL_DYNAPI_PROC(void,SDL_UnlockAudio,(void),(),)
SDL_DYNAPI_PROC(void,SDL_UnlockAudioDevice,(SDL_AudioDeviceID a),(a),)
+SDL_DYNAPI_PROC(void,SDL_UnlockJoysticks,(void),(),)
SDL_DYNAPI_PROC(int,SDL_UnlockMutex,(SDL_mutex *a),(a),return)
SDL_DYNAPI_PROC(void,SDL_UnlockSurface,(SDL_Surface *a),(a),)
SDL_DYNAPI_PROC(void,SDL_UnlockTexture,(SDL_Texture *a),(a),)
diff --git a/src/joystick/SDL_joystick_c.h b/src/joystick/SDL_joystick_c.h
index d01fafbd6211..976b1a0daff0 100644
--- a/src/joystick/SDL_joystick_c.h
+++ b/src/joystick/SDL_joystick_c.h
@@ -43,16 +43,6 @@ extern SDL_bool SDL_JoysticksInitialized(void);
/* Return whether the joystick system is shutting down */
extern SDL_bool SDL_JoysticksQuitting(void);
-#ifdef SDL_THREAD_SAFETY_ANALYSIS
-extern SDL_mutex *SDL_joystick_lock;
-#endif
-
-/* Locking for atomic access to the joystick API */
-extern void SDL_LockJoysticks(void) SDL_ACQUIRE(SDL_joystick_lock);
-
-/* Unlocking for atomic access to the joystick API */
-extern void SDL_UnlockJoysticks(void) SDL_RELEASE(SDL_joystick_lock);
-
/* Return whether the joysticks are currently locked */
extern SDL_bool SDL_JoysticksLocked(void);