From 6ca18ed0e5f903b1b6f66b2689480cea90d112e9 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 18 Jul 2024 17:18:56 -0700 Subject: [PATCH] SDL_GetKeyboards() follows the SDL_GetStringRule --- include/SDL3/SDL_keyboard.h | 9 +++++---- src/dynapi/SDL_dynapi_procs.h | 2 +- src/events/SDL_keyboard.c | 4 ++-- src/video/windows/SDL_windowsevents.c | 5 ++--- src/video/x11/SDL_x11xinput2.c | 5 ++--- test/testhotplug.c | 2 +- 6 files changed, 13 insertions(+), 14 deletions(-) diff --git a/include/SDL3/SDL_keyboard.h b/include/SDL3/SDL_keyboard.h index cfa6b10f6d..f8c5067f1b 100644 --- a/include/SDL3/SDL_keyboard.h +++ b/include/SDL3/SDL_keyboard.h @@ -73,9 +73,10 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasKeyboard(void); * power buttons, etc. You should wait for input from a device before you * consider it actively in use. * - * \param count a pointer filled in with the number of keyboards returned. - * \returns a 0 terminated array of keyboards instance IDs which should be - * freed with SDL_free(), or NULL on failure; call SDL_GetError() for + * The returned array follows the SDL_GetStringRule, and will be automatically freed later. + * + * \param count a pointer filled in with the number of keyboards returned, may be NULL. + * \returns a 0 terminated array of keyboards instance IDs or NULL on failure; call SDL_GetError() for * more information. * * \since This function is available since SDL 3.0.0. @@ -83,7 +84,7 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasKeyboard(void); * \sa SDL_GetKeyboardNameForID * \sa SDL_HasKeyboard */ -extern SDL_DECLSPEC SDL_KeyboardID * SDLCALL SDL_GetKeyboards(int *count); +extern SDL_DECLSPEC const SDL_KeyboardID * SDLCALL SDL_GetKeyboards(int *count); /** * Get the name of a keyboard. diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index d1d90589dc..f63be2b173 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -372,7 +372,7 @@ SDL_DYNAPI_PROC(const char*,SDL_GetKeyName,(SDL_Keycode a),(a),return) SDL_DYNAPI_PROC(SDL_Window*,SDL_GetKeyboardFocus,(void),(),return) SDL_DYNAPI_PROC(const char*,SDL_GetKeyboardNameForID,(SDL_KeyboardID a),(a),return) SDL_DYNAPI_PROC(const Uint8*,SDL_GetKeyboardState,(int *a),(a),return) -SDL_DYNAPI_PROC(SDL_KeyboardID*,SDL_GetKeyboards,(int *a),(a),return) +SDL_DYNAPI_PROC(const SDL_KeyboardID*,SDL_GetKeyboards,(int *a),(a),return) SDL_DYNAPI_PROC(void,SDL_GetLogOutputFunction,(SDL_LogOutputFunction *a, void **b),(a,b),) SDL_DYNAPI_PROC(SDL_LogPriority,SDL_GetLogPriority,(int a),(a),return) SDL_DYNAPI_PROC(int,SDL_GetMasksForPixelFormat,(SDL_PixelFormat a, int *b, Uint32 *c, Uint32 *d, Uint32 *e, Uint32 *f),(a,b,c,d,e,f),return) diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c index 4ef6a04da4..1a426e0131 100644 --- a/src/events/SDL_keyboard.c +++ b/src/events/SDL_keyboard.c @@ -177,7 +177,7 @@ SDL_bool SDL_HasKeyboard(void) return (SDL_keyboard_count > 0); } -SDL_KeyboardID *SDL_GetKeyboards(int *count) +const SDL_KeyboardID *SDL_GetKeyboards(int *count) { int i; SDL_KeyboardID *keyboards; @@ -198,7 +198,7 @@ SDL_KeyboardID *SDL_GetKeyboards(int *count) } } - return keyboards; + return SDL_FreeLater(keyboards); } const char *SDL_GetKeyboardNameForID(SDL_KeyboardID instance_id) diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c index 048223ffef..ee211f8067 100644 --- a/src/video/windows/SDL_windowsevents.c +++ b/src/video/windows/SDL_windowsevents.c @@ -824,7 +824,7 @@ static void AddDeviceID(Uint32 deviceID, Uint32 **list, int *count) *list = new_list; } -static SDL_bool HasDeviceID(Uint32 deviceID, Uint32 *list, int count) +static SDL_bool HasDeviceID(Uint32 deviceID, const Uint32 *list, int count) { for (int i = 0; i < count; ++i) { if (deviceID == list[i]) { @@ -867,7 +867,7 @@ void WIN_CheckKeyboardAndMouseHotplug(SDL_VideoDevice *_this, SDL_bool initial_c PRAWINPUTDEVICELIST raw_devices = NULL; UINT raw_device_count = 0; int old_keyboard_count = 0; - SDL_KeyboardID *old_keyboards = NULL; + const SDL_KeyboardID *old_keyboards = NULL; int new_keyboard_count = 0; SDL_KeyboardID *new_keyboards = NULL; int old_mouse_count = 0; @@ -982,7 +982,6 @@ void WIN_CheckKeyboardAndMouseHotplug(SDL_VideoDevice *_this, SDL_bool initial_c } } - SDL_free(old_keyboards); SDL_free(new_keyboards); SDL_free(old_mice); SDL_free(new_mice); diff --git a/src/video/x11/SDL_x11xinput2.c b/src/video/x11/SDL_x11xinput2.c index 24025da127..3b33e6d4b0 100644 --- a/src/video/x11/SDL_x11xinput2.c +++ b/src/video/x11/SDL_x11xinput2.c @@ -715,7 +715,7 @@ static void AddDeviceID(Uint32 deviceID, Uint32 **list, int *count) *list = new_list; } -static SDL_bool HasDeviceID(Uint32 deviceID, Uint32 *list, int count) +static SDL_bool HasDeviceID(Uint32 deviceID, const Uint32 *list, int count) { for (int i = 0; i < count; ++i) { if (deviceID == list[i]) { @@ -734,7 +734,7 @@ void X11_Xinput2UpdateDevices(SDL_VideoDevice *_this, SDL_bool initial_check) XIDeviceInfo *info; int ndevices; int old_keyboard_count = 0; - SDL_KeyboardID *old_keyboards = NULL; + const SDL_KeyboardID *old_keyboards = NULL; int new_keyboard_count = 0; SDL_KeyboardID *new_keyboards = NULL; int old_mouse_count = 0; @@ -839,7 +839,6 @@ void X11_Xinput2UpdateDevices(SDL_VideoDevice *_this, SDL_bool initial_check) } } - SDL_free(old_keyboards); SDL_free(new_keyboards); SDL_free(old_mice); SDL_free(new_mice); diff --git a/test/testhotplug.c b/test/testhotplug.c index 78e980fea8..0f51c49e35 100644 --- a/test/testhotplug.c +++ b/test/testhotplug.c @@ -80,7 +80,7 @@ int main(int argc, char *argv[]) //SDL_CreateWindow("Dummy", 128, 128, 0); */ - SDL_free(SDL_GetKeyboards(&num_keyboards)); + SDL_GetKeyboards(&num_keyboards); SDL_Log("There are %d keyboards at startup\n", num_keyboards); SDL_free(SDL_GetMice(&num_mice));