We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3974

Summary: [PATCH] Fix SDL_WarpMouseInWindow on both KMSDRM and RaspberryPi drivers
Product: SDL Reporter: Manuel Alfayate Corchete <redwindwanderer>
Component: eventsAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: 2.0.7   
Hardware: All   
OS: Linux   
Attachments: Small patch that fixes SDL_WarpMouseInWindow() on KMSDRM and Raspberry Pi graphic backends
Patch that fixes sticky cursor on programs were mouse coords are tracked but cursor area is smaller than total screen area

Description Manuel Alfayate Corchete 2017-11-20 11:50:36 UTC
Created attachment 3094 [details]
Small patch that fixes SDL_WarpMouseInWindow() on KMSDRM and Raspberry Pi graphic backends

Hi,

This patch fixes SDL_WarpMouseInWindow() in both the KMSDRM and Raspberry Pi graphic backends.

When we warp a mouse cursor, we also MUST set the new mouse coordinates, not only move the cursor graphic position. This simple patch does precisely that.
Not doing so causes the mouse to have internal coordinates bigger than on-screen position, thus causing the cursor to get "sticky" on the edges when the game area is smaller than the total window size (which is the whole physical resolution on x-less, fullscreen systems)

It fixes the bug report I previously did here: 
https://bugzilla.libsdl.org/show_bug.cgi?id=3970

Now, for example, the current Scummvm GIT version (due for release soon) works as expected on the Pi and KMSDRM embedded-like enviroments.

Please, merge ASAP, it's a very simple yet important fix.
Comment 1 Sam Lantinga 2017-11-22 05:42:38 UTC
Typically drivers call SDL_SendMouseMotion() instead of setting the mouse variables directly. This is standard behavior and apps should be waiting for a mouse motion event with the coordinates that they just called warp with.

Can you make that change for both drivers and test it there?

Thanks!
Comment 2 Manuel Alfayate Corchete 2017-11-22 20:46:08 UTC
(In reply to Sam Lantinga from comment #1)
> Typically drivers call SDL_SendMouseMotion() instead of setting the mouse
> variables directly. This is standard behavior and apps should be waiting for
> a mouse motion event with the coordinates that they just called warp with.
> 
> Can you make that change for both drivers and test it there?
> 
> Thanks!

That's not such a good idea as things are: tried to use SDL_SendMouseMotion() instead, and that causes a segfault when the cursor is hidden.
Something must be done in SDL_SendMouseMotion() sub-functions to correct that, but I don't have the time.
If you get it to operate safely when the cursor is hidden (yes, that makes sense and is used in Scummvm) we could use SDL_SendMouseMotion() instead if you want.
But as things are now, manually updating the position is the only working way.
Comment 3 Sam Lantinga 2017-11-24 11:02:55 UTC
I don't have a KMSDRM or Raspberry Pi testing environment, can you investigate why it's crashing when the mouse is hidden? That sounds like a bug - it should be safe to call it anytime.
Comment 4 Manuel Alfayate Corchete 2017-11-25 21:58:03 UTC
Created attachment 3101 [details]
Patch that fixes sticky cursor on programs were mouse coords are tracked but cursor area is smaller than total screen area
Comment 5 Manuel Alfayate Corchete 2017-11-25 21:59:17 UTC
(In reply to Sam Lantinga from comment #3)
> I don't have a KMSDRM or Raspberry Pi testing environment, can you
> investigate why it's crashing when the mouse is hidden? That sounds like a
> bug - it should be safe to call it anytime.

In the end, it was a recursion bug: 
KMSDRM_MoveCursor() was calling
KMSDRM_WarpMouse() was calling
SDL_SendMouseMotion() was calling
SDL_PrivateSendMouseMotion() was calling
KMSDRM_MoveCursor().... Boom!!! :D

So calling SDL_WarpMouse in KMSDRM_MoveCursor was plain wrong. Cursor HAS to be moved, but not calling KMSDRM_WarpMouse()... bad, bad idea.

Anyway, here's what I hope to be the final patch on this. It works by callin SendMouseMotion as intended, and works perfectly well on both the Raspberry Pi and KMSDRM. Please merge: I really want this fixed. (The patch is in the previous message)
Comment 6 Sam Lantinga 2017-12-05 04:37:23 UTC
Fixed, thanks!
https://hg.libsdl.org/SDL/rev/73826bd39748