From 9be73ed7c5d81f5bf5431285ab9a319836adac00 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 18 Jul 2024 17:29:24 -0700 Subject: [PATCH] SDL_GetMice() follows the SDL_GetStringRule --- include/SDL3/SDL_mouse.h | 9 +++++---- src/dynapi/SDL_dynapi_procs.h | 2 +- src/events/SDL_mouse.c | 4 ++-- src/video/windows/SDL_windowsevents.c | 3 +-- src/video/x11/SDL_x11xinput2.c | 3 +-- test/testhotplug.c | 2 +- 6 files changed, 11 insertions(+), 12 deletions(-) diff --git a/include/SDL3/SDL_mouse.h b/include/SDL3/SDL_mouse.h index 3a2cda67e..abcedc30c 100644 --- a/include/SDL3/SDL_mouse.h +++ b/include/SDL3/SDL_mouse.h @@ -135,9 +135,10 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasMouse(void); * 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 mice returned. - * \returns a 0 terminated array of mouse instance IDs which should be freed - * with SDL_free(), or NULL on failure; call SDL_GetError() for more + * The returned array follows the SDL_GetStringRule, and will be automatically freed later. + * + * \param count a pointer filled in with the number of mice returned, may be NULL. + * \returns a 0 terminated array of mouse instance IDs or NULL on failure; call SDL_GetError() for more * information. * * \since This function is available since SDL 3.0.0. @@ -145,7 +146,7 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasMouse(void); * \sa SDL_GetMouseNameForID * \sa SDL_HasMouse */ -extern SDL_DECLSPEC SDL_MouseID * SDLCALL SDL_GetMice(int *count); +extern SDL_DECLSPEC const SDL_MouseID * SDLCALL SDL_GetMice(int *count); /** * Get the name of a mouse. diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index f63be2b17..aa044d90f 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -379,7 +379,7 @@ SDL_DYNAPI_PROC(int,SDL_GetMasksForPixelFormat,(SDL_PixelFormat a, int *b, Uint3 SDL_DYNAPI_PROC(int,SDL_GetMaxHapticEffects,(SDL_Haptic *a),(a),return) SDL_DYNAPI_PROC(int,SDL_GetMaxHapticEffectsPlaying,(SDL_Haptic *a),(a),return) SDL_DYNAPI_PROC(void,SDL_GetMemoryFunctions,(SDL_malloc_func *a, SDL_calloc_func *b, SDL_realloc_func *c, SDL_free_func *d),(a,b,c,d),) -SDL_DYNAPI_PROC(SDL_MouseID*,SDL_GetMice,(int *a),(a),return) +SDL_DYNAPI_PROC(const SDL_MouseID*,SDL_GetMice,(int *a),(a),return) SDL_DYNAPI_PROC(SDL_Keymod,SDL_GetModState,(void),(),return) SDL_DYNAPI_PROC(SDL_Window*,SDL_GetMouseFocus,(void),(),return) SDL_DYNAPI_PROC(const char*,SDL_GetMouseNameForID,(SDL_MouseID a),(a),return) diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c index b84965280..9006da351 100644 --- a/src/events/SDL_mouse.c +++ b/src/events/SDL_mouse.c @@ -346,7 +346,7 @@ SDL_bool SDL_HasMouse(void) return (SDL_mouse_count > 0); } -SDL_MouseID *SDL_GetMice(int *count) +const SDL_MouseID *SDL_GetMice(int *count) { int i; SDL_MouseID *mice; @@ -367,7 +367,7 @@ SDL_MouseID *SDL_GetMice(int *count) } } - return mice; + return SDL_FreeLater(mice); } const char *SDL_GetMouseNameForID(SDL_MouseID instance_id) diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c index ee211f806..a05fedca8 100644 --- a/src/video/windows/SDL_windowsevents.c +++ b/src/video/windows/SDL_windowsevents.c @@ -871,7 +871,7 @@ void WIN_CheckKeyboardAndMouseHotplug(SDL_VideoDevice *_this, SDL_bool initial_c int new_keyboard_count = 0; SDL_KeyboardID *new_keyboards = NULL; int old_mouse_count = 0; - SDL_MouseID *old_mice = NULL; + const SDL_MouseID *old_mice = NULL; int new_mouse_count = 0; SDL_MouseID *new_mice = NULL; SDL_bool send_event = !initial_check; @@ -983,7 +983,6 @@ void WIN_CheckKeyboardAndMouseHotplug(SDL_VideoDevice *_this, SDL_bool initial_c } SDL_free(new_keyboards); - SDL_free(old_mice); SDL_free(new_mice); SetupDiDestroyDeviceInfoList(devinfo); diff --git a/src/video/x11/SDL_x11xinput2.c b/src/video/x11/SDL_x11xinput2.c index 3b33e6d4b..8ef4197cf 100644 --- a/src/video/x11/SDL_x11xinput2.c +++ b/src/video/x11/SDL_x11xinput2.c @@ -738,7 +738,7 @@ void X11_Xinput2UpdateDevices(SDL_VideoDevice *_this, SDL_bool initial_check) int new_keyboard_count = 0; SDL_KeyboardID *new_keyboards = NULL; int old_mouse_count = 0; - SDL_MouseID *old_mice = NULL; + const SDL_MouseID *old_mice = NULL; int new_mouse_count = 0; SDL_MouseID *new_mice = NULL; int old_touch_count = 0; @@ -840,7 +840,6 @@ void X11_Xinput2UpdateDevices(SDL_VideoDevice *_this, SDL_bool initial_check) } SDL_free(new_keyboards); - SDL_free(old_mice); SDL_free(new_mice); SDL_free(old_touch_devices); SDL_free(new_touch_devices); diff --git a/test/testhotplug.c b/test/testhotplug.c index 0f51c49e3..328ef76c1 100644 --- a/test/testhotplug.c +++ b/test/testhotplug.c @@ -83,7 +83,7 @@ int main(int argc, char *argv[]) SDL_GetKeyboards(&num_keyboards); SDL_Log("There are %d keyboards at startup\n", num_keyboards); - SDL_free(SDL_GetMice(&num_mice)); + SDL_GetMice(&num_mice); SDL_Log("There are %d mice at startup\n", num_mice); SDL_GetJoysticks(&num_joysticks);