Browse Source

SDL_GetTouchFingers() follows the SDL_GetStringRule

Sam Lantinga 9 months ago
parent
commit
0079b6d45f

+ 4 - 3
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

+ 1 - 1
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)

+ 3 - 3
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)

+ 2 - 3
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);
         }
     }