Skip to content

Commit a48b06a

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

File tree

10 files changed

+21
-15
lines changed

10 files changed

+21
-15
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: 2 additions & 1 deletion
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,6 +262,7 @@ 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

src/events/SDL_pen_c.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ 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.
6767
extern void SDL_RemovePenDevice(Uint64 timestamp, SDL_PenID instance_id);

src/video/android/SDL_androidpen.c

Lines changed: 1 addition & 1 deletion
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;

src/video/cocoa/SDL_cocoapen.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ 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
}

src/video/emscripten/SDL_emscriptenevents.c

Lines changed: 1 addition & 1 deletion
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

src/video/uikit/SDL_uikitpen.m

Lines changed: 9 additions & 7 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,18 +188,20 @@ 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
}

src/video/wayland/SDL_waylandevents.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3439,7 +3439,7 @@ static void tablet_tool_handle_frame(void *data, struct zwp_tablet_tool_v2 *tool
34393439
if (sdltool->frame.have_proximity_in) {
34403440
SDL_assert(sdltool->instance_id == 0); // shouldn't be added at this point.
34413441
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);
3442+
sdltool->instance_id = SDL_AddPenDevice(timestamp, NULL, window, &sdltool->info, sdltool);
34433443
Wayland_TabletToolUpdateCursor(sdltool);
34443444
}
34453445
}

src/video/windows/SDL_windowsevents.c

Lines changed: 1 addition & 1 deletion
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

src/video/x11/SDL_x11pen.c

Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
 (0)