From 0079b6d45fcc2ab288249abedab160ee68caffa5 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 19 Jul 2024 12:39:34 -0700 Subject: [PATCH] SDL_GetTouchFingers() follows the SDL_GetStringRule --- include/SDL3/SDL_touch.h | 7 ++++--- src/dynapi/SDL_dynapi_procs.h | 2 +- src/events/SDL_touch.c | 6 +++--- src/video/cocoa/SDL_cocoawindow.m | 5 ++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/SDL3/SDL_touch.h b/include/SDL3/SDL_touch.h index bb8e36647..995690260 100644 --- a/include/SDL3/SDL_touch.h +++ b/include/SDL3/SDL_touch.h @@ -122,16 +122,17 @@ extern SDL_DECLSPEC SDL_TouchDeviceType SDLCALL SDL_GetTouchDeviceType(SDL_Touch /** * Get a list of active fingers for a given touch device. * + * This returns temporary memory which will be automatically freed later, and can be claimed with SDL_ClaimTemporaryMemory(). + * * \param touchID the ID of a touch device. * \param count a pointer filled in with the number of fingers returned, can * be NULL. - * \returns a NULL terminated array of SDL_Finger pointers which should be - * freed with SDL_free(), or NULL on failure; call SDL_GetError() for + * \returns a NULL terminated array of SDL_Finger pointers or NULL on failure; call SDL_GetError() for * more information. * * \since This function is available since SDL 3.0.0. */ -extern SDL_DECLSPEC SDL_Finger ** SDLCALL SDL_GetTouchFingers(SDL_TouchID touchID, int *count); +extern SDL_DECLSPEC const SDL_Finger * const * SDLCALL SDL_GetTouchFingers(SDL_TouchID touchID, int *count); /* Ends C function definitions when using C++ */ #ifdef __cplusplus diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 9fdf202d7..e3d288403 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -501,7 +501,7 @@ SDL_DYNAPI_PROC(Uint64,SDL_GetTicksNS,(void),(),return) SDL_DYNAPI_PROC(const char*,SDL_GetTouchDeviceName,(SDL_TouchID a),(a),return) SDL_DYNAPI_PROC(SDL_TouchDeviceType,SDL_GetTouchDeviceType,(SDL_TouchID a),(a),return) SDL_DYNAPI_PROC(const SDL_TouchID*,SDL_GetTouchDevices,(int *a),(a),return) -SDL_DYNAPI_PROC(SDL_Finger**,SDL_GetTouchFingers,(SDL_TouchID a, int *b),(a,b),return) +SDL_DYNAPI_PROC(const SDL_Finger* const*,SDL_GetTouchFingers,(SDL_TouchID a, int *b),(a,b),return) SDL_DYNAPI_PROC(const char*,SDL_GetUserFolder,(SDL_Folder a),(a),return) SDL_DYNAPI_PROC(int,SDL_GetVersion,(void),(),return) SDL_DYNAPI_PROC(const char*,SDL_GetVideoDriver,(int a),(a),return) diff --git a/src/events/SDL_touch.c b/src/events/SDL_touch.c index c2cada056..9bb14ed64 100644 --- a/src/events/SDL_touch.c +++ b/src/events/SDL_touch.c @@ -134,7 +134,7 @@ static SDL_Finger *SDL_GetFinger(const SDL_Touch *touch, SDL_FingerID id) return touch->fingers[index]; } -SDL_Finger **SDL_GetTouchFingers(SDL_TouchID touchID, int *count) +const SDL_Finger * const * SDL_GetTouchFingers(SDL_TouchID touchID, int *count) { SDL_Finger **fingers; SDL_Finger *finger_data; @@ -153,7 +153,7 @@ SDL_Finger **SDL_GetTouchFingers(SDL_TouchID touchID, int *count) if (!fingers) { return NULL; } - finger_data = (SDL_Finger *)((Uint8 *)fingers + (touch->num_fingers + 1) * sizeof(*fingers)); + finger_data = (SDL_Finger *)(fingers + (touch->num_fingers + 1)); for (int i = 0; i < touch->num_fingers; ++i) { fingers[i] = &finger_data[i]; @@ -164,7 +164,7 @@ SDL_Finger **SDL_GetTouchFingers(SDL_TouchID touchID, int *count) if (count) { *count = touch->num_fingers; } - return fingers; + return SDL_FreeLater(fingers); } int SDL_AddTouch(SDL_TouchID touchID, SDL_TouchDeviceType type, const char *name) diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m index e3cfbf050..01b46f19b 100644 --- a/src/video/cocoa/SDL_cocoawindow.m +++ b/src/video/cocoa/SDL_cocoawindow.m @@ -1735,11 +1735,11 @@ static int Cocoa_SendMouseButtonClicks(SDL_Mouse *mouse, NSEvent *theEvent, SDL_ } if (existingTouchCount == 0) { int numFingers; - SDL_Finger **fingers = SDL_GetTouchFingers(touchID, &numFingers); + const SDL_Finger * const *fingers = SDL_GetTouchFingers(touchID, &numFingers); if (fingers) { DLog("Reset Lost Fingers: %d", numFingers); for (--numFingers; numFingers >= 0; --numFingers) { - SDL_Finger *finger = fingers[numFingers]; + const SDL_Finger *finger = fingers[numFingers]; /* trackpad touches have no window. If we really wanted one we could * use the window that has mouse or keyboard focus. * Sending a null window currently also prevents synthetic mouse @@ -1748,7 +1748,6 @@ static int Cocoa_SendMouseButtonClicks(SDL_Mouse *mouse, NSEvent *theEvent, SDL_ SDL_Window *window = NULL; SDL_SendTouch(Cocoa_GetEventTimestamp([theEvent timestamp]), touchID, finger->id, window, SDL_FALSE, 0, 0, 0); } - SDL_free(fingers); } }