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 3015

Summary: grab mouse inconsistent state
Product: SDL Reporter: Martin Gerhardy <martin.gerhardy>
Component: videoAssignee: Ryan C. Gordon <icculus>
Status: ASSIGNED --- QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: icculus, yv.sharma
Version: HG 2.0Keywords: target-2.0.4
Hardware: x86_64   
OS: Linux   

Description Martin Gerhardy 2015-06-15 06:45:04 UTC
Not sure - but I think there might be a logic flaw in SDL_SetWindowGrab.

The problem here is that this modifies the window flags and e.g. sets 
SDL_WINDOW_INPUT_GRABBED - but the _this->grabbed_window pointer is not 
yet set.

Then in SDL_UpdateWindowGrab the _this->grabbed_window pointer is only 
set if the function pointer _this->SetWindowGrab is not NULL. But if 
this is NULL, the _this->grabbed_window pointer is never set, but every 
future call to any of the grab functions include an assert for:  
SDL_assert(!_this->grabbed_window || ((_this->grabbed_window->flags & 
SDL_WINDOW_INPUT_GRABBED) != 0));

That means the first call works, the second always fails and triggers 
the assert.
Comment 1 Sam Lantinga 2015-06-16 06:44:54 UTC
Okay, I think this is fixed:
https://hg.libsdl.org/SDL/rev/885b6b5c8426

I don't have an environment where I can easily test this, can you verify the fix?

Thanks!
Comment 2 Martin Gerhardy 2015-06-16 07:43:29 UTC
Still triggered.

Assertion failure at SDL_GetGrabbedWindow_REAL (/home/mgerhardy/dev/caveexpress/src/libs/SDL/src/video/SDL_video.c:2180), triggered 1 time:
  '!_this->grabbed_window || ((_this->grabbed_window->flags & SDL_WINDOW_INPUT_GRABBED) != 0)'
Comment 3 Martin Gerhardy 2015-06-16 07:44:02 UTC
reopen - see my previous comment
Comment 4 Martin Gerhardy 2015-06-16 07:48:20 UTC
looks like SDL_SetWindowGrab and SDL_UpdateWindowGrab compute different "SDL_bool grabbed" states
Comment 5 Ryan C. Gordon 2015-06-16 14:42:30 UTC
This one is my bug, taking it.

--ryan.
Comment 6 Yash 2015-07-21 04:40:35 UTC
Hi Ryan

Any fix so far ?
Comment 7 Ryan C. Gordon 2015-12-31 08:54:32 UTC
Actually, I'm trying to find a way to trigger that assertion and can't.

Martin, is this still happening to you?

--ryan.
Comment 8 Martin Gerhardy 2015-12-31 10:22:28 UTC
hm. I can't reproduce the assert anymore - but for my code the grabbing and ungrabbing doesn't work at all anymore. I always have the mouse grabbed (software renderer - as there might be grabbing issues... e.g. I only receive mouse move event in software render mode after I at least once alt+tabbed away)

will try a different renderer to double check.
Comment 9 Martin Gerhardy 2015-12-31 10:59:47 UTC
Assertion failure at SDL_GetGrabbedWindow_REAL (/home/mgerhardy/dev/caveexpress/src/libs/sdl2/src/video/SDL_video.c:2283), triggered 1 time:
  '!_this->grabbed_window || ((_this->grabbed_window->flags & SDL_WINDOW_INPUT_GRABBED) != 0)'

still happens on latest hg rev.

void SDLFrontend::toggleGrabMouse () {
#if SDL_VERSION_ATLEAST(2, 0, 4)
	const bool grabMouse = SDL_GetGrabbedWindow() == _window;
#else
	const bool grabMouse = Config.isGrabMouse();
#endif
	if (grabMouse) {
		SDL_SetWindowGrab(_window, SDL_FALSE);
		Log::info(LOG_GFX, "Mouse grab is now deactivated");
		Config.setGrabMouse(false);
	} else {
		SDL_SetWindowGrab(_window, SDL_TRUE);
		Log::info(LOG_GFX, "Mouse grab is now activated");
		Config.setGrabMouse(true);
	}
}

this is the code that I use to trigger it.