From 3dee60d65f84cee0cafe3fd6581c9a5b3d38d643 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Thu, 20 Nov 2025 16:49:50 -0500
Subject: [PATCH] pen: Only allow one pen on Emscripten.
Same deal as the current Windows code: this "fixes" proximity but limits you
to a single pen device. But this is probably okay for most reasonable use
cases.
---
src/video/emscripten/SDL_emscriptenevents.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/video/emscripten/SDL_emscriptenevents.c b/src/video/emscripten/SDL_emscriptenevents.c
index e378ff8cc77cb..66cb71c2b1861 100644
--- a/src/video/emscripten/SDL_emscriptenevents.c
+++ b/src/video/emscripten/SDL_emscriptenevents.c
@@ -763,7 +763,7 @@ static void Emscripten_UpdateTouchFromEvent(SDL_WindowData *window_data, const E
static void Emscripten_UpdatePenFromEvent(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
{
SDL_assert(event->pointer_type == PTRTYPE_PEN);
- const SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) event->pointerid);
+ const SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) 1); // something > 0 for the single pen handle.
if (pen) {
// rescale (in case canvas is being scaled)
double client_w, client_h;
@@ -849,9 +849,10 @@ static void Emscripten_HandlePenEnter(SDL_WindowData *window_data, const Emscrip
{
SDL_assert(event->pointer_type == PTRTYPE_PEN);
-SDL_Log("PEN ENTER pointerid=%d", event->pointerid);
+ // event->pointerid is one continuous interaction; it doesn't necessarily track a specific tool over time, like the same finger's ID changed on each new touch event.
+ // as such, we only expose a single pen, and when the touch ends, we say it lost proximity instead of the calling SDL_RemovePenDevice().
- SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) event->pointerid);
+ SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) 1); // something > 0 for the single pen handle.
if (pen) {
SDL_SendPenProximity(0, pen, window_data->window, true);
} else {
@@ -862,7 +863,7 @@ SDL_Log("PEN ENTER pointerid=%d", event->pointerid);
peninfo.max_tilt = 90.0f;
peninfo.num_buttons = 2;
peninfo.subtype = SDL_PEN_TYPE_PEN;
- SDL_AddPenDevice(0, NULL, window_data->window, &peninfo, (void *) (size_t) event->pointerid, true);
+ SDL_AddPenDevice(0, NULL, window_data->window, &peninfo, (void *) (size_t) 1, true);
}
Emscripten_UpdatePenFromEvent(window_data, event);
@@ -884,8 +885,7 @@ EMSCRIPTEN_KEEPALIVE void Emscripten_HandlePointerEnter(SDL_WindowData *window_d
static void Emscripten_HandlePenLeave(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
{
-SDL_Log("PEN LEAVE pointerid=%d", event->pointerid);
- const SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) event->pointerid);
+ const SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) 1); // something > 0 for the single pen handle.
if (pen) {
Emscripten_UpdatePointerFromEvent(window_data, event); // last data updates?
SDL_SendPenProximity(0, pen, window_data->window, false);