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 4571

Summary: OpenGL window doesn't produce SDL_WINDOWEVENT_FOCUS_LOST or SDL_APP_DIDENTERBACKGROUND
Product: SDL Reporter: Anthony @ POW Games <ant>
Component: renderAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: icculus
Version: HG 2.0Keywords: target-2.0.10
Hardware: All   
OS: Windows 10   

Description Anthony @ POW Games 2019-03-28 23:02:14 UTC
This is a new bug introduced in 2.0.9 or hg.

When making an opengl or opengles 2D renderer there is no event generated (the first time) when focus is lost. When returning to the app and leaving focus again for a second time, appropriate events are then generated.

It happens on Windows, but I can't yet tell if it's happening on Android.

There is no problem with Software and Direct3D renderers. It also used to work in 2.0.8.
Comment 1 Ryan C. Gordon 2019-05-18 18:48:55 UTC
Tagging a bunch of bugs with "target-2.0.10" so we have a clear list of things to address before a 2.0.10 release.

Please note that "addressing" one of these bugs might mean deciding to defer on it until after 2.0.10, or resolving it as WONTFIX, etc. This is just here to tell us we should look at it carefully, and soon.

If you have new information or feedback on this issue, this is a good time to add it to the conversation, as we're likely to be paying attention to this specific report in the next few days/weeks.

Thanks!

--ryan.
Comment 2 Ryan C. Gordon 2019-06-10 18:12:50 UTC
Haven't looked further, but bisecting says this is the culprit:

https://hg.libsdl.org/SDL/rev/fbfacc66c65c

--ryan.
Comment 3 Ryan C. Gordon 2019-06-11 05:16:06 UTC
Documenting this because it took awhile to track down, but the tl;dr is this bug is fixed in revision control.

Okay, so this happens because we ignore WM_ACTIVATE messages when the window isn't visible, but specifically for the OpenGL renderer, we'll call SDL_RecreateWindow() if the window doesn't have the SDL_WINDOW_OPENGL flag already (this is by design; you aren't supposed to supply this flag even if you explicitly plan to use the GL renderer backend). This is why you don't see this problem with Direct3D.

SDL_RecreateWindow() explicitly hides the window while it does its magic, and when hiding a window, Win32 sends a WM_ACTIVATE which we ignore. Ignoring that means we never reset what window we think has the keyboard focus, etc, causing this problem, and why it fixes itself after the first time: once we stop ignoring WM_ACTIVATE calls, the state gets unconfused after a few of these messages arrive.

What I ended up doing was only ignore activate messages when we are becoming active while still hidden, instead of ignoring them all. I'm not sure this is _correct_ fix, and I'm not sure if ever ignoring WM_ACTIVATE is a good idea, but I think this at least resolves this issue without regressing on the reason the ignoring was done in the first place.

So...fixed in https://hg.libsdl.org/SDL/rev/2f882d435abf as far as I can tell.

--ryan.