SDL_gesture: Updated SDL_gesture for SDL3 changes

From 0878dcd8b10a1a7d2a3028debe9a45767fd57c91 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 26 May 2024 12:18:28 -0700
Subject: [PATCH] Updated SDL_gesture for SDL3 changes

Also removed SDL2 support from testgesture.c, since SDL2 already includes it.
---
 SDL_gesture.h       | 121 ++++++++++++++++++++++----------------------
 test/CMakeLists.txt |  27 ++--------
 test/testgesture.c  |  67 ++++++++++++------------
 3 files changed, 97 insertions(+), 118 deletions(-)

diff --git a/SDL_gesture.h b/SDL_gesture.h
index d3bf44e..9f3f06e 100644
--- a/SDL_gesture.h
+++ b/SDL_gesture.h
@@ -64,7 +64,7 @@ typedef struct Gesture_MultiGestureEvent
 {
     Uint32 type;
     Uint32 timestamp;
-    SDL_TouchID touchId;
+    SDL_TouchID touchID;
     float dTheta;
     float dDist;
     float x;
@@ -77,7 +77,7 @@ typedef struct Gesture_DollarGestureEvent
 {
     Uint32 type;
     Uint32 timestamp;
-    SDL_TouchID touchId;
+    SDL_TouchID touchID;
     Gesture_ID gestureId;
     Uint32 numFingers;
     float error;
@@ -103,18 +103,18 @@ extern void SDLCALL Gesture_Quit(void);
 /**
  * Begin recording a gesture on a specified touch device or all touch devices.
  *
- * If the parameter `touchId` is -1 (i.e., all devices), this function will
+ * If the parameter `touchID` is -1 (i.e., all devices), this function will
  * always return 1, regardless of whether there actually are any devices.
  *
- * \param touchId the touch device id, or -1 for all touch devices
+ * \param touchID the touch device id, or -1 for all touch devices
  * \returns 1 on success or 0 if the specified device could not be found.
  */
-extern int SDLCALL Gesture_RecordGesture(SDL_TouchID touchId);
+extern int SDLCALL Gesture_RecordGesture(SDL_TouchID touchID);
 
 /**
  * Save all currently loaded Dollar Gesture templates.
  *
- * \param dst a SDL_RWops to save to
+ * \param dst a SDL_IOStream to save to
  * \returns the number of saved templates on success or 0 on failure; call
  *          SDL_GetError() for more information.
  *
@@ -123,13 +123,13 @@ extern int SDLCALL Gesture_RecordGesture(SDL_TouchID touchId);
  * \sa Gesture_LoadDollarTemplates
  * \sa Gesture_SaveDollarTemplate
  */
-extern int SDLCALL Gesture_SaveAllDollarTemplates(SDL_RWops *dst);
+extern int SDLCALL Gesture_SaveAllDollarTemplates(SDL_IOStream *dst);
 
 /**
  * Save a currently loaded Dollar Gesture template.
  *
  * \param gestureId a gesture id
- * \param dst a SDL_RWops to save to
+ * \param dst a SDL_IOStream to save to
  * \returns 1 on success or 0 on failure; call SDL_GetError() for more
  *          information.
  *
@@ -138,13 +138,13 @@ extern int SDLCALL Gesture_SaveAllDollarTemplates(SDL_RWops *dst);
  * \sa SDL_LoadDollarTemplates
  * \sa SDL_SaveAllDollarTemplates
  */
-extern int SDLCALL Gesture_SaveDollarTemplate(Gesture_ID gestureId, SDL_RWops *dst);
+extern int SDLCALL Gesture_SaveDollarTemplate(Gesture_ID gestureId, SDL_IOStream *dst);
 
 /**
  * Load Dollar Gesture templates from a file.
  *
- * \param touchId a touch id
- * \param src a SDL_RWops to load from
+ * \param touchID a touch id
+ * \param src a SDL_IOStream to load from
  * \returns the number of loaded templates on success or a negative error code
  *          (or 0) on failure; call SDL_GetError() for more information.
  *
@@ -153,7 +153,7 @@ extern int SDLCALL Gesture_SaveDollarTemplate(Gesture_ID gestureId, SDL_RWops *d
  * \sa SDL_SaveAllDollarTemplates
  * \sa SDL_SaveDollarTemplate
  */
-extern int SDLCALL Gesture_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src);
+extern int SDLCALL Gesture_LoadDollarTemplates(SDL_TouchID touchID, SDL_IOStream *src);
 
 /* Ends C function definitions when using C++ */
 #ifdef __cplusplus
@@ -182,7 +182,7 @@ typedef struct
 
 typedef struct
 {
-    SDL_TouchID touchId;
+    SDL_TouchID touchID;
     SDL_FPoint centroid;
     GestureDollarPath dollarPath;
     Uint16 numDownFingers;
@@ -212,7 +212,7 @@ int Gesture_Init(void)
 }
 
 
-static GestureTouch *GestureAddTouch(const SDL_TouchID touchId)
+static GestureTouch *GestureAddTouch(const SDL_TouchID touchID)
 {
     GestureTouch *gestureTouch = (GestureTouch *)SDL_realloc(GestureTouches, (GestureNumTouches + 1) * sizeof(GestureTouch));
     if (gestureTouch == NULL) {
@@ -222,15 +222,15 @@ static GestureTouch *GestureAddTouch(const SDL_TouchID touchId)
 
     GestureTouches = gestureTouch;
     SDL_zero(GestureTouches[GestureNumTouches]);
-    GestureTouches[GestureNumTouches].touchId = touchId;
+    GestureTouches[GestureNumTouches].touchID = touchID;
     return &GestureTouches[GestureNumTouches++];
 }
 
-static int GestureDelTouch(const SDL_TouchID touchId)
+static int GestureDelTouch(const SDL_TouchID touchID)
 {
     int i;
     for (i = 0; i < GestureNumTouches; i++) {
-        if (GestureTouches[i].touchId == touchId) {
+        if (GestureTouches[i].touchID == touchID) {
             break;
         }
     }
@@ -250,40 +250,41 @@ static int GestureDelTouch(const SDL_TouchID touchId)
     return 0;
 }
 
-static GestureTouch *GestureGetTouch(const SDL_TouchID touchId)
+static GestureTouch *GestureGetTouch(const SDL_TouchID touchID)
 {
     int i;
     for (i = 0; i < GestureNumTouches; i++) {
-        /* printf("%i ?= %i\n",GestureTouches[i].touchId,touchId); */
-        if (GestureTouches[i].touchId == touchId) {
+        /* printf("%i ?= %i\n",GestureTouches[i].touchID,touchID); */
+        if (GestureTouches[i].touchID == touchID) {
             return &GestureTouches[i];
         }
     }
     return NULL;
 }
 
-int Gesture_RecordGesture(SDL_TouchID touchId)
+int Gesture_RecordGesture(SDL_TouchID touchID)
 {
-    const int numtouchdevs = SDL_GetNumTouchDevices();
+	SDL_TouchID *devices;
     int i;
 
-    /* make sure we know about all the devices SDL3 knows about, since we aren't connected as tightly as we were in SDL2. */
-    for (i = 0; i < numtouchdevs; i++) {
-        const SDL_TouchID thistouch = SDL_GetTouchDevice(i);
-        if (!GestureGetTouch(thistouch)) {
-            if (!GestureAddTouch(thistouch)) {
-                return 0;  /* uhoh, out of memory */
-            }
-        }
-    }
-
-    if (touchId < 0) {
+	devices = SDL_GetTouchDevices(NULL);
+	if (devices) {
+		/* make sure we know about all the devices SDL3 knows about, since we aren't connected as tightly as we were in SDL2. */
+		for (i = 0; devices[i]; i++) {
+			if (!GestureGetTouch(devices[i])) {
+				GestureAddTouch(devices[i]);
+			}
+		}
+		SDL_free(devices);
+	}
+
+    if (touchID != 0) {
         GestureRecordAll = SDL_TRUE;  /* !!! FIXME: this is never set back to SDL_FALSE anywhere, that's probably a bug. */
         for (i = 0; i < GestureNumTouches; i++) {
             GestureTouches[i].recording = SDL_TRUE;
         }
     } else {
-        GestureTouch *touch = GestureGetTouch(touchId);
+        GestureTouch *touch = GestureGetTouch(touchID);
         if (!touch) {
             return 0;  /* bogus touchid */
         }
@@ -313,7 +314,7 @@ static unsigned long GestureHashDollar(SDL_FPoint *points)
     return hash;
 }
 
-static int GestureSaveTemplate(GestureDollarTemplate *templ, SDL_RWops *dst)
+static int GestureSaveTemplate(GestureDollarTemplate *templ, SDL_IOStream *dst)
 {
     const Sint64 bytes = sizeof(templ->path[0]) * GESTURE_DOLLARNPOINTS;
 
@@ -322,10 +323,10 @@ static int GestureSaveTemplate(GestureDollarTemplate *templ, SDL_RWops *dst)
     }
 
     /* No Longer storing the Hash, rehash on load */
-    /* if (SDL_RWops.write(dst, &(templ->hash), sizeof(templ->hash)) != sizeof(templ->hash)) return 0; */
+    /* if (SDL_IOWrite(dst, &(templ->hash), sizeof(templ->hash)) != sizeof(templ->hash)) return 0; */
 
 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
-    if (SDL_RWwrite(dst, templ->path, bytes) != bytes) {
+    if (SDL_WriteIO(dst, templ->path, bytes) != bytes) {
         return 0;
     }
 #else
@@ -338,7 +339,7 @@ static int GestureSaveTemplate(GestureDollarTemplate *templ, SDL_RWops *dst)
             p->y = SDL_SwapFloatLE(p->y);
         }
 
-        if (SDL_RWwrite(dst, copy.path, bytes) != bytes) {
+        if (SDL_WriteIO(dst, copy.path, bytes) != bytes) {
             return 0;
         }
     }
@@ -347,8 +348,8 @@ static int GestureSaveTemplate(GestureDollarTemplate *templ, SDL_RWops *dst)
     return 1;
 }
 
-DECLSPEC int SDLCALL
-Gesture_SaveAllDollarTemplates(SDL_RWops *dst)
+SDL_DECLSPEC int SDLCALL
+Gesture_SaveAllDollarTemplates(SDL_IOStream *dst)
 {
     int i, j, rtrn = 0;
     for (i = 0; i < GestureNumTouches; i++) {
@@ -360,8 +361,8 @@ Gesture_SaveAllDollarTemplates(SDL_RWops *dst)
     return rtrn;
 }
 
-DECLSPEC int SDLCALL
-Gesture_SaveDollarTemplate(Gesture_ID gestureId, SDL_RWops *dst)
+SDL_DECLSPEC int SDLCALL
+Gesture_SaveDollarTemplate(Gesture_ID gestureId, SDL_IOStream *dst)
 {
     int i, j;
     for (i = 0; i < GestureNumTouches; i++) {
@@ -419,17 +420,17 @@ static int GestureAddDollar(GestureTouch *inTouch, SDL_FPoint *path)
     return GestureAddDollar_one(inTouch, path);
 }
 
-DECLSPEC int SDLCALL
-Gesture_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src)
+SDL_DECLSPEC int SDLCALL
+Gesture_LoadDollarTemplates(SDL_TouchID touchID, SDL_IOStream *src)
 {
     int i, loaded = 0;
     GestureTouch *touch = NULL;
     if (src == NULL) {
         return 0;
     }
-    if (touchId >= 0) {
+    if (touchID >= 0) {
         for (i = 0; i < GestureNumTouches; i++) {
-            if (GestureTouches[i].touchId == touchId) {
+            if (GestureTouches[i].touchID == touchID) {
                 touch = &GestureTouches[i];
             }
         }
@@ -442,7 +443,7 @@ Gesture_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src)
         GestureDollarTemplate templ;
         const Sint64 bytes = sizeof(templ.path[0]) * GESTURE_DOLLARNPOINTS;
 
-        if (SDL_RWread(src, templ.path, bytes) < bytes) {
+        if (SDL_ReadIO(src, templ.path, bytes) < bytes) {
             if (loaded == 0) {
                 return SDL_SetError("could not read any dollar gesture from rwops");
             }
@@ -457,7 +458,7 @@ Gesture_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src)
         }
 #endif
 
-        if (touchId >= 0) {
+        if (touchID >= 0) {
             /* printf("Adding loaded gesture to 1 touch\n"); */
             if (GestureAddDollar(touch, templ.path) >= 0) {
                 loaded++;
@@ -651,11 +652,11 @@ static float GestureDollarRecognize(const GestureDollarPath *path, int *bestTemp
 
 static void GestureSendMulti(GestureTouch *touch, float dTheta, float dDist)
 {
-    if (SDL_GetEventState(GESTURE_MULTIGESTURE) == SDL_ENABLE) {
+    if (SDL_EventEnabled(GESTURE_MULTIGESTURE)) {
         Gesture_MultiGestureEvent mgesture;
         mgesture.type = GESTURE_MULTIGESTURE;
         mgesture.timestamp = 0;
-        mgesture.touchId = touch->touchId;
+        mgesture.touchID = touch->touchID;
         mgesture.x = touch->centroid.x;
         mgesture.y = touch->centroid.y;
         mgesture.dTheta = dTheta;
@@ -667,11 +668,11 @@ static void GestureSendMulti(GestureTouch *touch, float dTheta, float dDist)
 
 static void GestureSendDollar(GestureTouch *touch, Gesture_ID gestureId, float error)
 {
-    if (SDL_GetEventState(GESTURE_DOLLARGESTURE) == SDL_ENABLE) {
+    if (SDL_EventEnabled(GESTURE_DOLLARGESTURE)) {
         Gesture_DollarGestureEvent dgesture;
         dgesture.type = GESTURE_DOLLARGESTURE;
         dgesture.timestamp = 0;
-        dgesture.touchId = touch->touchId;
+        dgesture.touchID = touch->touchID;
         dgesture.x = touch->centroid.x;
         dgesture.y = touch->centroid.y;
         dgesture.gestureId = gestureId;
@@ -684,11 +685,11 @@ static void GestureSendDollar(GestureTouch *touch, Gesture_ID gestureId, float e
 
 static void GestureSendDollarRecord(GestureTouch *touch, Gesture_ID gestureId)
 {
-    if (SDL_GetEventState(GESTURE_DOLLARRECORD) == SDL_ENABLE) {
+    if (SDL_EventEnabled(GESTURE_DOLLARRECORD)) {
         Gesture_DollarGestureEvent dgesture;
         dgesture.type = GESTURE_DOLLARRECORD;
         dgesture.timestamp = 0;
-        dgesture.touchId = touch->touchId;
+        dgesture.touchID = touch->touchID;
         dgesture.gestureId = gestureId;
         SDL_PushEvent((SDL_Event*)&dgesture);
     }
@@ -707,11 +708,11 @@ static void GestureProcessEvent(const SDL_Event *event)
     float dtheta;
     float dDist;
 
-    if (event->type == SDL_FINGERMOTION || event->type == SDL_FINGERDOWN || event->type == SDL_FINGERUP) {
-        GestureTouch *inTouch = GestureGetTouch(event->tfinger.touchId);
+    if (event->type == SDL_EVENT_FINGER_MOTION || event->type == SDL_EVENT_FINGER_DOWN || event->type == SDL_EVENT_FINGER_UP) {
+        GestureTouch *inTouch = GestureGetTouch(event->tfinger.touchID);
 
         if (inTouch == NULL) {  /* we maybe didn't see this one before. */
-            inTouch = GestureAddTouch(event->tfinger.touchId);
+            inTouch = GestureAddTouch(event->tfinger.touchID);
             if (!inTouch) {
                 return;  /* oh well. */
             }
@@ -721,7 +722,7 @@ static void GestureProcessEvent(const SDL_Event *event)
         y = event->tfinger.y;
 
         /* Finger Up */
-        if (event->type == SDL_FINGERUP) {
+        if (event->type == SDL_EVENT_FINGER_UP) {
             SDL_FPoint path[GESTURE_DOLLARNPOINTS];
             inTouch->numDownFingers--;
 
@@ -759,7 +760,7 @@ static void GestureProcessEvent(const SDL_Event *event)
                 inTouch->centroid.x = (inTouch->centroid.x * (inTouch->numDownFingers + 1) - x) / inTouch->numDownFingers;
                 inTouch->centroid.y = (inTouch->centroid.y * (inTouch->numDownFingers + 1) - y) / inTouch->numDownFingers;
             }
-        } else if (event->type == SDL_FINGERMOTION) {
+        } else if (event->type == SDL_EVENT_FINGER_MOTION) {
             const float dx = event->tfinger.dx;
             const float dy = event->tfinger.dy;
             GestureDollarPath *path = &inTouch->dollarPath;
@@ -828,7 +829,7 @@ static void GestureProcessEvent(const SDL_Event *event)
             inTouch->gestureLast[j].f.p.y = y;
             break;
             pressure? */
-        } else if (event->type == SDL_FINGERDOWN) {
+        } else if (event->type == SDL_EVENT_FINGER_DOWN) {
             inTouch->numDownFingers++;
             inTouch->centroid.x = (inTouch->centroid.x * (inTouch->numDownFingers - 1) +
                                    x) /
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 417d124..6056f83 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -4,33 +4,15 @@ project(SDL_gesture_tests C)
 option(SDLGESTURE_TESTS_STATIC "Link to static SDLx library" OFF)
 
 if(SDLGESTURE_TESTS_STATIC)
-    set(SDL2_COMPONENTS "SDL2-static")
-    set(SDL2_TARGET "SDL2::SDL2-static")
-
     set(SDL3_COMPONENTS "SDL3-static")
     set(SDL3_TARGET "SDL3::SDL3-static")
 else()
-    set(SDL2_COMPONENTS "SDL2")
-    set(SDL2_TARGET "SDL2::SDL2")
-
     set(SDL3_COMPONENTS "SDL3")
     set(SDL3_TARGET "SDL3::SDL3")
 endif()
 
 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../cmake")
 
-find_package(SDL2 CONFIG COMPONENTS ${SDL2_COMPONENTS} SDL2main)
-if(NOT SDL2_FOUND OR NOT TARGET SDL2::SDL2)
-    find_package(PrivateSDL2 MODULE REQUIRED)
-    set(SDL2_TARGET "PrivateSDL2::PrivateSDL2")
-endif()
-if(NOT TARGET SDL2::SDL2main)
-    find_package(SDL2main MODULE QUIET)
-    if(NOT SDL2main_FOUND)
-        add_library(SDL2::SDL2main INTERFACE)
-    endif()
-endif()
-
 find_package(SDL3 QUIET CONFIG COMPONENTS ${SDL3_COMPONENTS})
 if(NOT SDL3_FOUND)
     message(STATUS "SDL3 not found -> not testing SDL_gesture with SDL3")
@@ -40,11 +22,8 @@ if(NOT TARGET SDL_gesture::SDL_gesture)
     find_package(SDL_gesture REQUIRED)
 endif()
 
-add_executable(testgesture_sdl2 testgesture.c)
-target_link_libraries(testgesture_sdl2 PRIVATE SDL_gesture::SDL_gesture SDL2::SDL2main  ${SDL2_TARGET})
-
 if(TARGET ${SDL3_TARGET})
-    add_executable(testgesture_sdl3 testgesture.c)
-    target_compile_definitions(testgesture_sdl3 PRIVATE TESTGESTURE_SDL3)
-    target_link_libraries(testgesture_sdl3 PRIVATE SDL_gesture::SDL_gesture ${SDL3_TARGET})
+    add_executable(testgesture testgesture.c)
+    target_compile_definitions(testgesture PRIVATE TESTGESTURE_SDL3)
+    target_link_libraries(testgesture PRIVATE SDL_gesture::SDL_gesture ${SDL3_TARGET})
 endif()
diff --git a/test/testgesture.c b/test/testgesture.c
index 6fe17d1..1ff32d1 100644
--- a/test/testgesture.c
+++ b/test/testgesture.c
@@ -19,11 +19,7 @@ static const char *usage = "\n\
     v: enable virtual touch. Touch events are synthetized when Mouse events occur\n\
 ";
 
-#if defined(TESTGESTURE_SDL3)
 #include <SDL3/SDL.h>
-#else
-#include "SDL.h"
-#endif
 
 #define SDL_GESTURE_IMPLEMENTATION 1
 #include "SDL_gesture.h"
@@ -78,7 +74,7 @@ setpix(SDL_Surface *screen, float _x, float _y, unsigned int col)
 
     pixmem32 = (Uint32 *)screen->pixels + y * screen->pitch / BPP + x;
 
-    SDL_memcpy(&colour, pixmem32, screen->format->BytesPerPixel);
+    SDL_memcpy(&colour, pixmem32, screen->format->bytes_per_pixel);
 
     SDL_GetRGB(colour, screen->format, &r, &g, &b);
 
@@ -143,7 +139,7 @@ DrawScreen(SDL_Window *window)
         return;
     }
 
-    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 75, 75, 75));
+    SDL_FillSurfaceRect(screen, NULL, SDL_MapRGB(screen->format, 75, 75, 75));
 
     /* draw Touch History */
     for (i = eventWrite; i < eventWrite + EVENT_BUF_SIZE; ++i) {
@@ -152,19 +148,19 @@ DrawScreen(SDL_Window *window)
         float x, y;
         unsigned int c, col;
 
-        if ((event->type == SDL_FINGERMOTION) ||
-            (event->type == SDL_FINGERDOWN) ||
-            (event->type == SDL_FINGERUP)) {
+        if ((event->type == SDL_EVENT_FINGER_MOTION) ||
+            (event->type == SDL_EVENT_FINGER_DOWN) ||
+            (event->type == SDL_EVENT_FINGER_UP)) {
             x = event->tfinger.x;
             y = event->tfinger.y;
 
             /* draw the touch: */
-            c = colors[event->tfinger.fingerId % 7];
+            c = colors[event->tfinger.fingerID % 7];
             col = ((unsigned int)(c * (0.1f + 0.85f))) | (unsigned int)(0xFF * age) << 24;
 
-            if (event->type == SDL_FINGERMOTION) {
+            if (event->type == SDL_EVENT_FINGER_MOTION) {
                 drawCircle(screen, x * screen->w, y * screen->h, 5, col);
-            } else if (event->type == SDL_FINGERDOWN) {
+            } else if (event->type == SDL_EVENT_FINGER_DOWN) {
                 drawCircle(screen, x * screen->w, y * screen->h, -10, col);
             }
         }
@@ -185,7 +181,7 @@ loop(void)
         Gesture_MultiGestureEvent mgesture;
         Gesture_DollarGestureEvent dgesture;
     } u_event;
-    SDL_RWops *stream;
+    SDL_IOStream *stream;
     int i;
 
     while (SDL_PollEvent(&u_event.event)) {
@@ -195,11 +191,11 @@ loop(void)
         eventWrite++;
 
         switch (u_event.event.type) {
-        case SDL_QUIT:
+        case SDL_EVENT_QUIT:
            quitting = 1;
            break;
 
-        case SDL_KEYDOWN:
+        case SDL_EVENT_KEY_DOWN:
             switch (u_event.event.key.keysym.sym) {
             case SDLK_ESCAPE:
                quitting = 1;
@@ -207,14 +203,17 @@ loop(void)
 
             case SDLK_i:
             {
-                for (i = 0; i < SDL_GetNumTouchDevices(); ++i) {
-                    const SDL_TouchID id = SDL_GetTouchDevice(i);
-#if SDL_VERSION_ATLEAST(2, 22, 0)
-                    const char *name = SDL_GetTouchName(i);
-#else
-                    const char *name = "<unknown>";
-#endif
-                    SDL_Log("Fingers Down on device %" SDL_PRIs64 " (%s): %d", id, name, SDL_GetNumTouchFingers(id));
+                SDL_TouchID *devices = SDL_GetTouchDevices(NULL);
+                if (devices) {
+                    for (i = 0; devices[i]; ++i) {
+                        const SDL_TouchID id = devices[i];
+                        const char *name = SDL_GetTouchDeviceName(id);
+                        int num_fingers;
+                        SDL_Finger **fingers = SDL_GetTouchFingers(id, &num_fingers);
+                        SDL_free(fingers);
+                        SDL_Log("Fingers Down on device %" SDL_PRIs64 " (%s): %d", id, name, num_fingers);
+                    }
+                    SDL_free(devices);
                 }
                 break;
             }
@@ -224,16 +223,16 @@ loop(void)
                 break;
 
             case SDLK_s:
-                stream = SDL_RWFromFile("gestureSave", "w");
+                stream = SDL_IOFromFile("gestureSave", "w");
                 SDL_Log("Wrote %i templates", Gesture_SaveAllDollarTemplates(stream));
-                SDL_RWclose(stream);
+                SDL_CloseIO(stream);
                 break;
 
             case SDLK_l:
-                stream = SDL_RWFromFile("gestureSave", "r");
+                stream = SDL_IOFromFile("gestureSave", "r");
                 if (stream) {
                     SDL_Log("Loaded: %i", Gesture_LoadDollarTemplates(-1, stream));
-                    SDL_RWclose(stream);
+                    SDL_CloseIO(stream);
                 } else {
                     SDL_Log("Cannot load 'gestureSave' file");
                 }
@@ -249,19 +248,19 @@ loop(void)
             break;
 
 #if VERBOSE
-        case SDL_FINGERMOTION:
-            SDL_Log("Finger: %" SDL_PRIs64 ", x: %f, y: %f", u_event.event.tfinger.fingerId,
+        case SDL_EVENT_FINGER_MOTION:
+            SDL_Log("Finger: %" SDL_PRIs64 ", x: %f, y: %f", u_event.event.tfinger.fingerID,
                     u_event.event.tfinger.x, u_event.event.tfinger.y);
             break;
 
-        case SDL_FINGERDOWN:
+        case SDL_EVENT_FINGER_DOWN:
             SDL_Log("Finger: %" SDL_PRIs64 " down - x: %f, y: %f",
-                    u_event.event.tfinger.fingerId, u_event.event.tfinger.x, u_event.event.tfinger.y);
+                    u_event.event.tfinger.fingerID, u_event.event.tfinger.x, u_event.event.tfinger.y);
             break;
 
-        case SDL_FINGERUP:
+        case SDL_EVENT_FINGER_UP:
             SDL_Log("Finger: %" SDL_PRIs64 " up - x: %f, y: %f",
-                    u_event.event.tfinger.fingerId, u_event.event.tfinger.x, u_event.event.tfinger.y);
+                    u_event.event.tfinger.fingerID, u_event.event.tfinger.x, u_event.event.tfinger.y);
             break;
 #endif
 
@@ -303,7 +302,7 @@ int main(int argc, char *argv[])
 {
     SDL_Log("%s", usage);
 
-    g_window = SDL_CreateWindow("test gesture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, 0);
+    g_window = SDL_CreateWindow("test gesture", WIDTH, HEIGHT, 0);
     if (g_window == NULL) {
        return -1;
     }