Skip to content

Commit 433980a

Browse files
committed
pen: Offer the current window during promixity events on most platforms.
Fixes #12356.
1 parent f3d3981 commit 433980a

File tree

10 files changed

+34
-26
lines changed

10 files changed

+34
-26
lines changed

include/SDL3/SDL_events.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,9 @@ typedef struct SDL_PinchFingerEvent
818818
* is there." The pen touching and lifting off from the tablet while not
819819
* leaving the area are handled by SDL_EVENT_PEN_DOWN and SDL_EVENT_PEN_UP.
820820
*
821+
* Not all platforms have a window associated with the pen during proximity
822+
* events. Some wait until motion/button/etc events to offer this info.
823+
*
821824
* \since This struct is available since SDL 3.2.0.
822825
*/
823826
typedef struct SDL_PenProximityEvent

src/events/SDL_pen.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ SDL_PenCapabilityFlags SDL_GetPenCapabilityFromAxis(SDL_PenAxis axis)
218218
return 0; // oh well.
219219
}
220220

221-
SDL_PenID SDL_AddPenDevice(Uint64 timestamp, const char *name, const SDL_PenInfo *info, void *handle)
221+
SDL_PenID SDL_AddPenDevice(Uint64 timestamp, const char *name, SDL_Window *window, const SDL_PenInfo *info, void *handle)
222222
{
223223
SDL_assert(handle != NULL); // just allocate a Uint8 so you have a unique pointer if not needed!
224224
SDL_assert(SDL_FindPenByHandle(handle) == 0); // Backends shouldn't double-add pens!
@@ -262,13 +262,14 @@ SDL_PenID SDL_AddPenDevice(Uint64 timestamp, const char *name, const SDL_PenInfo
262262
event.pproximity.type = SDL_EVENT_PEN_PROXIMITY_IN;
263263
event.pproximity.timestamp = timestamp;
264264
event.pproximity.which = result;
265+
event.pproximity.windowID = window ? window->id : 0;
265266
SDL_PushEvent(&event);
266267
}
267268

268269
return result;
269270
}
270271

271-
void SDL_RemovePenDevice(Uint64 timestamp, SDL_PenID instance_id)
272+
void SDL_RemovePenDevice(Uint64 timestamp, SDL_Window *window, SDL_PenID instance_id)
272273
{
273274
if (!instance_id) {
274275
return;
@@ -306,6 +307,7 @@ void SDL_RemovePenDevice(Uint64 timestamp, SDL_PenID instance_id)
306307
event.pproximity.type = SDL_EVENT_PEN_PROXIMITY_OUT;
307308
event.pproximity.timestamp = timestamp;
308309
event.pproximity.which = instance_id;
310+
event.pproximity.windowID = window ? window->id : 0;
309311
SDL_PushEvent(&event);
310312
}
311313
}

src/events/SDL_pen_c.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ typedef struct SDL_PenInfo
6161
// Backend calls this when a new pen device is hotplugged, plus once for each pen already connected at startup.
6262
// Note that name and info are copied but currently unused; this is placeholder for a potentially more robust API later.
6363
// Both are allowed to be NULL.
64-
extern SDL_PenID SDL_AddPenDevice(Uint64 timestamp, const char *name, const SDL_PenInfo *info, void *handle);
64+
extern SDL_PenID SDL_AddPenDevice(Uint64 timestamp, const char *name, SDL_Window *window, const SDL_PenInfo *info, void *handle);
6565

6666
// Backend calls this when an existing pen device is disconnected during runtime. They must free their own stuff separately.
67-
extern void SDL_RemovePenDevice(Uint64 timestamp, SDL_PenID instance_id);
67+
extern void SDL_RemovePenDevice(Uint64 timestamp, SDL_Window *window, SDL_PenID instance_id);
6868

6969
// Backend can call this to remove all pens, probably during shutdown, with a callback to let them free their own handle.
7070
extern void SDL_RemoveAllPenDevices(void (*callback)(SDL_PenID instance_id, void *handle, void *userdata), void *userdata);

src/video/android/SDL_androidpen.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void Android_OnPen(SDL_Window *window, int pen_id_in, SDL_PenDeviceType device_t
5151
peninfo.num_buttons = 2;
5252
peninfo.subtype = SDL_PEN_TYPE_PEN;
5353
peninfo.device_type = device_type;
54-
pen = SDL_AddPenDevice(0, NULL, &peninfo, (void *) (size_t) pen_id_in);
54+
pen = SDL_AddPenDevice(0, NULL, window, &peninfo, (void *) (size_t) pen_id_in);
5555
if (!pen) {
5656
SDL_Log("error: can't add a pen device %d", pen_id_in);
5757
return;
@@ -78,7 +78,7 @@ void Android_OnPen(SDL_Window *window, int pen_id_in, SDL_PenDeviceType device_t
7878
switch (action) {
7979
case ACTION_CANCEL:
8080
case ACTION_HOVER_EXIT:
81-
SDL_RemovePenDevice(0, pen);
81+
SDL_RemovePenDevice(0, window, pen);
8282
break;
8383

8484
case ACTION_DOWN:

src/video/cocoa/SDL_cocoapen.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ static void Cocoa_HandlePenProximityEvent(SDL_CocoaWindowData *_data, NSEvent *e
105105
handle->deviceid = devid;
106106
handle->toolid = toolid;
107107
handle->is_eraser = is_eraser;
108-
handle->pen = SDL_AddPenDevice(Cocoa_GetEventTimestamp([event timestamp]), NULL, &peninfo, handle);
108+
handle->pen = SDL_AddPenDevice(Cocoa_GetEventTimestamp([event timestamp]), NULL, _data.window, &peninfo, handle);
109109
if (!handle->pen) {
110110
SDL_free(handle); // oh well.
111111
}
112112
} else { // old pen leaving!
113113
Cocoa_PenHandle *handle = Cocoa_FindPenByDeviceID(devid, toolid);
114114
if (handle) {
115-
SDL_RemovePenDevice(Cocoa_GetEventTimestamp([event timestamp]), handle->pen);
115+
SDL_RemovePenDevice(Cocoa_GetEventTimestamp([event timestamp]), _data.window, handle->pen);
116116
SDL_free(handle);
117117
}
118118
}

src/video/emscripten/SDL_emscriptenevents.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ static void Emscripten_HandlePenEnter(SDL_WindowData *window_data, const Emscrip
855855
peninfo.max_tilt = 90.0f;
856856
peninfo.num_buttons = 2;
857857
peninfo.subtype = SDL_PEN_TYPE_PEN;
858-
SDL_AddPenDevice(0, NULL, &peninfo, (void *) (size_t) event->pointerid);
858+
SDL_AddPenDevice(0, NULL, window_data->window, &peninfo, (void *) (size_t) event->pointerid);
859859
Emscripten_UpdatePenFromEvent(window_data, event);
860860
}
861861

@@ -878,7 +878,7 @@ static void Emscripten_HandlePenLeave(SDL_WindowData *window_data, const Emscrip
878878
const SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) event->pointerid);
879879
if (pen) {
880880
Emscripten_UpdatePointerFromEvent(window_data, event); // last data updates?
881-
SDL_RemovePenDevice(0, pen);
881+
SDL_RemovePenDevice(0, window_data->window, pen);
882882
}
883883
}
884884

src/video/uikit/SDL_uikitpen.m

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ bool UIKit_InitPen(SDL_VideoDevice *_this)
6363
// we only have one Apple Pencil at a time, and it must be paired to the iOS device.
6464
// We only know about its existence when it first sends an event, so add an single SDL pen
6565
// device here if we haven't already.
66-
static SDL_PenID UIKit_AddPenIfNecesary()
66+
static SDL_PenID UIKit_AddPenIfNecesary(SDL_Window *window)
6767
{
6868
if (!apple_pencil_id) {
6969
SDL_PenInfo info;
@@ -86,7 +86,7 @@ static SDL_PenID UIKit_AddPenIfNecesary()
8686
// so we can't use it for tangential pressure.
8787

8888
// There's only ever one Apple Pencil at most, so we just pass a non-zero value for the handle.
89-
apple_pencil_id = SDL_AddPenDevice(0, "Apple Pencil", &info, (void *) (size_t) 0x1);
89+
apple_pencil_id = SDL_AddPenDevice(0, "Apple Pencil", window, &info, (void *) (size_t) 0x1);
9090
}
9191

9292
return apple_pencil_id;
@@ -95,7 +95,7 @@ static SDL_PenID UIKit_AddPenIfNecesary()
9595
static void UIKit_HandlePenAxes(SDL_Window *window, NSTimeInterval nstimestamp, float zOffset, const CGPoint *point, float force,
9696
float maximumPossibleForce, float azimuthAngleInView, float altitudeAngle, float rollAngle)
9797
{
98-
const SDL_PenID penId = UIKit_AddPenIfNecesary();
98+
const SDL_PenID penId = UIKit_AddPenIfNecesary(window);
9999
if (penId) {
100100
const Uint64 timestamp = UIKit_GetEventTimestamp(nstimestamp);
101101
const float radians_to_degrees = 180.0f / SDL_PI_F;
@@ -188,26 +188,28 @@ void UIKit_HandlePenMotion(SDL_uikitview *view, UITouch *pencil)
188188

189189
void UIKit_HandlePenPress(SDL_uikitview *view, UITouch *pencil)
190190
{
191-
const SDL_PenID penId = UIKit_AddPenIfNecesary();
191+
SDL_Window *window = [view getSDLWindow];
192+
const SDL_PenID penId = UIKit_AddPenIfNecesary(window);
192193
if (penId) {
193194
UIKit_HandlePenAxesFromUITouch(view, pencil);
194-
SDL_SendPenTouch(UIKit_GetEventTimestamp([pencil timestamp]), penId, [view getSDLWindow], false, true);
195+
SDL_SendPenTouch(UIKit_GetEventTimestamp([pencil timestamp]), penId, window, false, true);
195196
}
196197
}
197198

198199
void UIKit_HandlePenRelease(SDL_uikitview *view, UITouch *pencil)
199200
{
200-
const SDL_PenID penId = UIKit_AddPenIfNecesary();
201+
SDL_Window *window = [view getSDLWindow];
202+
const SDL_PenID penId = UIKit_AddPenIfNecesary(window);
201203
if (penId) {
202-
SDL_SendPenTouch(UIKit_GetEventTimestamp([pencil timestamp]), penId, [view getSDLWindow], false, false);
204+
SDL_SendPenTouch(UIKit_GetEventTimestamp([pencil timestamp]), penId, window, false, false);
203205
UIKit_HandlePenAxesFromUITouch(view, pencil);
204206
}
205207
}
206208

207209
void UIKit_QuitPen(SDL_VideoDevice *_this)
208210
{
209211
if (apple_pencil_id) {
210-
SDL_RemovePenDevice(0, apple_pencil_id);
212+
SDL_RemovePenDevice(0, NULL, apple_pencil_id);
211213
apple_pencil_id = 0;
212214
}
213215
}

src/video/wayland/SDL_waylandevents.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3306,7 +3306,8 @@ static void tablet_tool_handle_removed(void *data, struct zwp_tablet_tool_v2 *to
33063306
{
33073307
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
33083308
if (sdltool->instance_id) {
3309-
SDL_RemovePenDevice(0, sdltool->instance_id);
3309+
SDL_Window *window = sdltool->focus ? sdltool->focus->sdlwindow : NULL;
3310+
SDL_RemovePenDevice(0, window, sdltool->instance_id);
33103311
}
33113312

33123313
Wayland_CursorStateRelease(&sdltool->cursor_state);
@@ -3439,7 +3440,7 @@ static void tablet_tool_handle_frame(void *data, struct zwp_tablet_tool_v2 *tool
34393440
if (sdltool->frame.have_proximity_in) {
34403441
SDL_assert(sdltool->instance_id == 0); // shouldn't be added at this point.
34413442
if (sdltool->info.subtype != SDL_PEN_TYPE_UNKNOWN) { // don't tell SDL about it if we don't know its role.
3442-
sdltool->instance_id = SDL_AddPenDevice(timestamp, NULL, &sdltool->info, sdltool);
3443+
sdltool->instance_id = SDL_AddPenDevice(timestamp, NULL, window, &sdltool->info, sdltool);
34433444
Wayland_TabletToolUpdateCursor(sdltool);
34443445
}
34453446
}
@@ -3487,7 +3488,7 @@ static void tablet_tool_handle_frame(void *data, struct zwp_tablet_tool_v2 *tool
34873488
if (sdltool->frame.have_proximity_out) {
34883489
sdltool->focus = NULL;
34893490
Wayland_TabletToolUpdateCursor(sdltool);
3490-
SDL_RemovePenDevice(timestamp, sdltool->instance_id);
3491+
SDL_RemovePenDevice(timestamp, window, sdltool->instance_id);
34913492
sdltool->instance_id = 0;
34923493
}
34933494

@@ -3656,7 +3657,7 @@ void Wayland_DisplayRemoveWindowReferencesFromSeats(SDL_VideoData *display, SDL_
36563657
tool->focus = NULL;
36573658
Wayland_TabletToolUpdateCursor(tool);
36583659
if (tool->instance_id) {
3659-
SDL_RemovePenDevice(0, tool->instance_id);
3660+
SDL_RemovePenDevice(0, window->sdlwindow, tool->instance_id);
36603661
tool->instance_id = 0;
36613662
}
36623663
}

src/video/windows/SDL_windowsevents.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ LRESULT CALLBACK WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
12771277
info.max_tilt = 90.0f;
12781278
info.num_buttons = 1;
12791279
info.subtype = SDL_PEN_TYPE_PENCIL;
1280-
SDL_AddPenDevice(0, NULL, &info, hpointer);
1280+
SDL_AddPenDevice(0, NULL, data->window, &info, hpointer);
12811281
returnCode = 0;
12821282
} break;
12831283

@@ -1293,7 +1293,7 @@ LRESULT CALLBACK WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
12931293

12941294
// if this just left the _window_, we don't care. If this is no longer visible to the tablet, time to remove it!
12951295
if ((msg == WM_POINTERCAPTURECHANGED) || !IS_POINTER_INCONTACT_WPARAM(wParam)) {
1296-
SDL_RemovePenDevice(WIN_GetEventTimestamp(), pen);
1296+
SDL_RemovePenDevice(WIN_GetEventTimestamp(), data->window, pen);
12971297
}
12981298
returnCode = 0;
12991299
} break;

src/video/x11/SDL_x11pen.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static X11_PenHandle *X11_MaybeAddPen(SDL_VideoDevice *_this, const XIDeviceInfo
272272
handle->is_eraser = is_eraser;
273273
handle->x11_deviceid = dev->deviceid;
274274

275-
handle->pen = SDL_AddPenDevice(0, dev->name, &peninfo, handle);
275+
handle->pen = SDL_AddPenDevice(0, dev->name, NULL, &peninfo, handle);
276276
if (!handle->pen) {
277277
SDL_free(handle);
278278
return NULL;
@@ -301,7 +301,7 @@ void X11_RemovePenByDeviceID(int deviceid)
301301
{
302302
X11_PenHandle *handle = X11_FindPenByDeviceID(deviceid);
303303
if (handle) {
304-
SDL_RemovePenDevice(0, handle->pen);
304+
SDL_RemovePenDevice(0, NULL, handle->pen);
305305
SDL_free(handle);
306306
}
307307
}

0 commit comments

Comments
 (0)