SDL: Add basic touch/finger support to `testpen.c`

From a961066d0bd3540d9c6bdc1f93f69bf6a400e639 Mon Sep 17 00:00:00 2001
From: Susko3 <[EMAIL REDACTED]>
Date: Tue, 26 Dec 2023 20:16:05 +0100
Subject: [PATCH] Add basic touch/finger support to `testpen.c`

Reuses the pen code as much as possible.

Seems like the right place, see
https://github.com/libsdl-org/SDL/pull/8749#issuecomment-1869671334.
---
 test/testpen.c | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/test/testpen.c b/test/testpen.c
index aa92f7f54de6..a193e0840856 100644
--- a/test/testpen.c
+++ b/test/testpen.c
@@ -308,6 +308,15 @@ static void update_axes(float *axes)
     last_rotation = axes[SDL_PEN_AXIS_ROTATION];
 }
 
+static void update_axes_from_touch(const float pressure)
+{
+    last_xtilt = 0;
+    last_ytilt = 0;
+    last_pressure = pressure;
+    last_distance = 0;
+    last_rotation = 0;
+}
+
 static void process_event(SDL_Event event)
 {
     SDLTest_CommonEvent(state, &event, &quitting);
@@ -341,7 +350,7 @@ static void process_event(SDL_Event event)
         }
     }
 #endif
-    if (event.motion.which != SDL_PEN_MOUSEID) {
+    if (event.motion.which != SDL_PEN_MOUSEID && event.motion.which != SDL_TOUCH_MOUSEID) {
         SDL_ShowCursor();
     } break;
 
@@ -444,6 +453,31 @@ static void process_event(SDL_Event event)
         break;
 #endif
 
+    case SDL_EVENT_FINGER_DOWN:
+    case SDL_EVENT_FINGER_MOTION:
+    case SDL_EVENT_FINGER_UP:
+    {
+        SDL_TouchFingerEvent *ev = &event.tfinger;
+        int w, h;
+        SDL_HideCursor();
+        SDL_GetWindowSize(SDL_GetWindowFromID(ev->windowID), &w, &h);
+        last_x = ev->x * w;
+        last_y = ev->y * h;
+        update_axes_from_touch(ev->pressure);
+        last_was_eraser = SDL_FALSE;
+        last_button = 0;
+        last_touching = (ev->type != SDL_EVENT_FINGER_UP);
+#if VERBOSE
+        SDL_Log("[%lu] finger %s: %s (touchId: %" SDL_PRIs64 ", fingerId: %" SDL_PRIs64 ") at (%.4f, %.4f); pressure=%.3f\n",
+                (unsigned long) ev->timestamp,
+                ev->type == SDL_EVENT_FINGER_DOWN ? "down" : (ev->type == SDL_EVENT_FINGER_MOTION ? "motion" : "up"),
+                SDL_GetTouchDeviceName(ev->touchId),
+                ev->touchId,
+                ev->fingerId,
+                last_x, last_y, last_pressure);
+#endif
+    } break;
+
     default:
         break;
     }