SDL icon indicating copy to clipboard operation
SDL copied to clipboard

SDL3: SDL_MouseMotionEvent and SDL_MouseButtonEvent not reporting a valid SDL_MouseId.

Open Oliver-makes-code opened this issue 1 year ago • 14 comments

I'm setting up an input handler for my game engine, and I'm setting it up to support different devices (for local multiplayer), and the events SDL_MouseButtonEvent and SDL_MouseMotionEvent are returning 0 for .which. SDL_MouseWheelEvent is unaffected.

Here's the debug log output from my engine:

main: [DEBUG] SDL event: 1024
main: [WARN] Failed to find Rendering::Input::Mouse device with index 0
main: [DEBUG] SDL event: 1025
main: [WARN] Failed to find Rendering::Input::Mouse device with index 0
main: [DEBUG] SDL event: 1026
main: [WARN] Failed to find Rendering::Input::Mouse device with index 0

I made sure I was on the latest commit from the main branch before reporting.

Oliver-makes-code avatar Oct 18 '24 05:10 Oliver-makes-code

I'm getting the same. It's been this way for as long as I remember. I just assumed the ID wasn't available. I'd be interested in this working.

AntTheAlchemist avatar Oct 18 '24 11:10 AntTheAlchemist

Oddly, I get a correct ID when in relative mode.

AntTheAlchemist avatar Oct 18 '24 11:10 AntTheAlchemist

I'm not sure where best to document it, but the mouse ID is only available in relative mode on desktop platforms.

slouken avatar Oct 18 '24 14:10 slouken

I'm not sure where best to document it, but the mouse ID is only available in relative mode on desktop platforms.

Could there be a hint to treat it as an arbitrary ID? SDL_SetHint(SDL_HINT_GLOBAL_MOUSE_ID, "1");

Oliver-makes-code avatar Oct 18 '24 18:10 Oliver-makes-code

Or you can just treat mouse ID 0 as your default mouse?

slouken avatar Oct 18 '24 18:10 slouken

I'm not sure where best to document it, but the mouse ID is only available in relative mode on desktop platforms.

I would put it right beside the other comment. Something short like "relative-mode only". https://github.com/libsdl-org/SDL/blob/2a05b63580b3962fd19a9fcac77c5ba9c6fa0276/include/SDL3/SDL_events.h#L425

And that for every struct member with a mouse ID that only works in relative mode.

This is the most likely place where users look for what the members do.

Sackzement avatar Oct 30 '24 01:10 Sackzement

Or you can just treat mouse ID 0 as your default mouse?

For a wheel event I always get a mouse ID of 7, for every other event I get 0.

Sackzement avatar Oct 30 '24 02:10 Sackzement

Or you can just treat mouse ID 0 as your default mouse?

The documentation makes it very clear that 0 is an invalid SDL_MouseID, so using it is incorrect.

https://github.com/libsdl-org/SDL/blob/bdf16628fb664b32e24859ed4c5cec0df6204642/include/SDL3/SDL_mouse.h#L48

For fixing this bug, maybe SDL can reserve an ID for "unknown mouse" and use that instead of 0.

Susko3 avatar Oct 30 '24 16:10 Susko3

Another reason why 0 shouldn't be the default mouse ID is that SDL_GetMice() claims to return a 0 terminated list of mouse IDs. https://github.com/libsdl-org/SDL/blob/6c10446a6cda1820fd19fa081ddb937aefd11060/include/SDL3/SDL_mouse.h#L158

Also: Other ID types use 0 as an invalid ID. Using it as a valid ID for mice, would be a minor but significant style break.

Sackzement avatar Oct 30 '24 17:10 Sackzement

Zero in the mouse event field seems like a reasonable way to say "this is mouse input but the system doesn't report different mice so we have no more information."

icculus avatar Oct 30 '24 19:10 icculus

Yeah but why overload SDL_MouseID 0 when you can easily add #define SDL_MOUSEID_UNKNOWN -3. (Well, it's not so easy when you give it a proper ID as you need to consider the mouse added events and whatnot.)

Having 0 be a valid mouse ID complicates applications since they cannot use 0 as "no mouse". See the your first-party woodeneye-008 example that bugs out when the mouse id is 0, as it assumes 0 is "no mouse" in application logic.

Susko3 avatar Oct 30 '24 19:10 Susko3

I'll go ahead and write up official documentation for the multi-mouse/keyboard feature for SDL 3.2.0, including the various caveats discussed here.

slouken avatar Dec 04 '24 19:12 slouken

What are SDL_GLOBAL_MOUSE_ID and SDL_DEFAULT_MOUSE_ID supposed to be used for respectively? There doesn’t seem to be any consistent pattern in the codebase for when events are sent from one or the other, perhaps some unification/cleanup might be overdue?

The global mouse ID is used when we can't associate the events with a specific mouse device, and is defined as 0. The default mouse ID is used for events generated from a specific mouse device that doesn't otherwise have a platform-specific ID.

slouken avatar Dec 24 '24 01:12 slouken

I've gone ahead and documented that you only get a non-zero mouse ID for touch emulated events or when relative mode is enabled.

slouken avatar Dec 24 '24 01:12 slouken