SDL icon indicating copy to clipboard operation
SDL copied to clipboard

`SDL_GetGlobalMouseState` and `SDL_GetWindowPosition` inconsistency still present on Emscripten

Open vittorioromeo opened this issue 9 months ago • 5 comments

Follow up from #12667.

This still seems to be broken for me after merging both #12669 and #12575. The relative mouse position is reported incorrectly -- see this GIF:

https://i.imgur.com/sqBYbwe.mp4

This is my logic:

////////////////////////////////////////////////////////////
Vector2i getPosition()
{
    Vector2f result;
    SDL_GetGlobalMouseState(&result.x, &result.y);
    return result.toVector2i();
}


////////////////////////////////////////////////////////////
Vector2i getPosition(const WindowBase& relativeTo)
{
    return getPosition() - relativeTo.getPosition();
//                         ^~~~~~~~~~~~~~~~~~~~~~~~
//                       uses SDL_GetWindowPosition() internally 
}

@Temdog007: my guess here is that SDL_GetWindowPosition is not returning the correct position of the canvas relative to the parent web page shell, but I might be incorrect.

vittorioromeo avatar Apr 07 '25 15:04 vittorioromeo

A call to SDL_SyncWindow is necessary to ensure that the window position is sync'd with position in the DOM.

Calling SDL_SetHint(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, "1"); before calling SDL_Init will ensure that the window position is always sync'd after the position or size of the window changes.

The functions in SDL where this occurs:

  • SDL_SetWindowFullscreenMode
  • SDL_SetWindowPosition
  • SDL_SetWindowSize
  • SDL_MaximizeWindow
  • SDL_MinimizeWindow
  • SDL_RestoreWindow
  • SDL_SetWindowFullscreen

If the canvas is moved outside of SDL, the window position won't be sync'd without explicitly calling SDL_SyncWindow.

Temdog007 avatar Apr 07 '25 22:04 Temdog007

A call to SDL_SyncWindow is necessary to ensure that the window position is sync'd with position in the DOM.

Calling SDL_SetHint(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, "1"); before calling SDL_Init will ensure that the window position is always sync'd after the position or size of the window changes.

If the canvas is moved outside of SDL, the window position won't be sync'd without explicitly calling SDL_SyncWindow.

Even with SDL_SetHint(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, "1") I am getting inconsistent results. When I open the browser at first, the relative position is screwed up as usual. If I resize the window once, the position seems to fix itself, but stops being correct again as soon as I resize the browser window.

Is there a chance I am doing something wrong? How can I debug this and figure out what is the culprit?

vittorioromeo avatar Apr 08 '25 18:04 vittorioromeo

A call to SDL_SyncWindow is necessary to ensure that the window position is sync'd with position in the DOM.

Calling SDL_SetHint(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, "1"); before calling SDL_Init will ensure that the window position is always sync'd after the position or size of the window changes.

If the canvas is moved outside of SDL, the window position won't be sync'd without explicitly calling SDL_SyncWindow.

So, calling SDL_SetHint before SDL_Init doesn't work, but calling SDL_SyncWindow every frame does. I will experiment further.

vittorioromeo avatar Apr 08 '25 19:04 vittorioromeo

The hint only makes sure the window is sync'd (by literally calling SDL_SyncWindow internally) when you do things through the SDL API to alter the window (like SDL_SetWindowSize, etc). It doesn't call it every frame or when pumping events, etc...it's more meant to be "I expected changing the window size to be an immediate operation but it's actually asynchronous on many platforms, so block until the change I explicitly requested is done."

I'm surprised explicitly calling SDL_SyncWindow works for you on Emscripten, too...it doesn't appear to be wired up at all on this platform (which would cause SDL_SyncWindow to just return true without doing anything at all).

icculus avatar Nov 14 '25 01:11 icculus

This conversation was in regards to the PR #12575. I had added Emscripten_SyncWindow function to be the hook to SDL_SyncWindow on Emscripten. This functionality is not currently merged into SDL.

Temdog007 avatar Nov 15 '25 01:11 Temdog007