SDL icon indicating copy to clipboard operation
SDL copied to clipboard

SDL2 window sometimes fails to gain input focus on macOS at startup

Open ffbh123456 opened this issue 3 months ago • 32 comments

On macOS, an SDL2 window usually (75%+) gains input focus and receives mouse and keyboard input automatically at startup. However, occasionally it does not, and only receives events after being minimized and restored. In those cases, clicking the window cannot give it focus.

Platform: macOS[13.2], SDL2 [2.32.8]

SDL_Window* CreateWindow()
{
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
        return nullptr;
    }

    SDL_Window* window = SDL_CreateWindow(
        "MyApp",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        960, 540,
        SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI
    );

    if (!window) {
        SDL_Quit();
        return nullptr;
    }

    SDL_RaiseWindow(window);
    SDL_SetWindowInputFocus(window);

    return window;
}

ffbh123456 avatar Sep 10 '25 16:09 ffbh123456

Are you seeing this with SDL3 as well?

slouken avatar Sep 10 '25 16:09 slouken

A few months ago, I tried SDL3 (3.2.18) but couldn’t get mouse or keyboard input to work, which is why I’m using SDL2 for now. At the time, I hadn’t tried the minimize-and-restore solution, so it might have been the same issue. Will try the latest SDL3 when I have time, though it probably won’t be very soon.

ffbh123456 avatar Sep 11 '25 02:09 ffbh123456

same issue on mac - SDL_WINDOW_ALWAYS_ON_TOP seemed to make it happen less often - but still does. Does that help your case a bit? (SDL2)

goblinhack avatar Sep 29 '25 08:09 goblinhack

(Assigning to myself, but I'm not likely to get to this for SDL2 for awhile. If someone says this is happening in SDL3 also, though, it goes to the top of the queue.)

icculus avatar Sep 29 '25 14:09 icculus

I noticed it happens when I move the mouse whilst starting the game. Possibly iTerm2 steals the focus while the game is starting.

goblinhack avatar Sep 29 '25 16:09 goblinhack

This is possibly fixed by b2c3e6fadec4be4f16febf1b5046fe0cc950addb, which should be in 2.32.10. If anyone can upgrade and verify, I'd appreciate it.

icculus avatar Sep 30 '25 05:09 icculus

Alas, for me I'm on 2.32.10 already. I have a workaround, check if SDL_GetKeyboardFocus is null and if so, restart the program :) Not a great workaround I'll admit. I tried quitting the SDL video/destroying the window, but on the mac these things don't seen to do anything w.r.t closing the window immediately. I'll try SDL3 at some point too

goblinhack avatar Sep 30 '25 07:09 goblinhack

I just upgraded to SDL3. I started and stopped my application about 10 times, and I didn’t encounter this issue. I’ll comment if I manage to reproduce it again.

However, I do have two other issues in SDL3. https://github.com/libsdl-org/SDL/issues/14107 https://github.com/libsdl-org/SDL/issues/14108

ffbh123456 avatar Oct 02 '25 04:10 ffbh123456

same issue on mac - SDL_WINDOW_ALWAYS_ON_TOP seemed to make it happen less often - but still does. Does that help your case a bit? (SDL2)

After creating this issue, it occurs very rarely (less than 5%) in SDL2. I can’t remember whether it was caused by my code changes or by upgrading to the newest SDL2. Also, I did not use SDL_WINDOW_ALWAYS_ON_TOP.

ffbh123456 avatar Oct 02 '25 04:10 ffbh123456

@icculus This issue still exists in the latest SDL3. Although it doesn’t occur very often, it works correctly most of the time (over 95%).

ffbh123456 avatar Oct 06 '25 16:10 ffbh123456

Also seeing this issue in latest SDL3.

kevinw avatar Oct 07 '25 10:10 kevinw

Just checking: if you move the mouse while launching the app, does it reproduce 100% of the time?

icculus avatar Oct 07 '25 20:10 icculus

yes!

I have my game restart in a loop if it detects the lack of focus. I noticed if I keep moving the mouse during restart, then the issue happens over and over.

If I stop moving the mouse during startup, the issue is gone

So 100% related to mouse movement

thanks

On Tue, 7 Oct 2025 at 21:29, Ryan C. Gordon @.***> wrote:

icculus left a comment (libsdl-org/SDL#13920) https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3378600406

Just checking: if you move the mouse while launching the app, does it reproduce 100% of the time?

— Reply to this email directly, view it on GitHub https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3378600406, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADA3H4BFPSYX57VXL77ETL3WQPA7AVCNFSM6AAAAACGE2Z2HSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTGNZYGYYDANBQGY . You are receiving this because you commented.Message ID: @.***>

goblinhack avatar Oct 07 '25 20:10 goblinhack

Okay, we have something to go on here, then. Let me dig in a little deeper over here, and I'll report back.

icculus avatar Oct 09 '25 17:10 icculus

Possibly iTerm2 steals the focus while the game is starting.

iTerm2 updated when I booted the Mac today (but this update might have been many days old, this isn't my primary development system).

Today I'm noticing that I'm always failing to bring the app to the foreground now with the latest SDL3. I'm also using iTerm2. Running examples/render-clear from iTerm2 never brings it to the foreground until I seek it out on the Dock. I double-clicked the examples/render-clear.app from the Finder, it doesn't have this problem. Launched the non-app-bundled examples/renderer-clear binary from the standard Terminal app...and it works correctly.

Are we all using iTerm2? Could this be the culprit?

icculus avatar Oct 20 '25 20:10 icculus

(Restarting iTerm2 fixed this specific problem, fwiw.)

icculus avatar Oct 20 '25 20:10 icculus

I'm having a similar problem with FNA + SDL3 and Rider. When i launch the app from Rider it was not automatically showing up but hiding in the dock instead.

When launching from iTerm2 the app window open behind the terminal + not having focus instead of showing on top of the terminal like normal.

chutne9 avatar Nov 02 '25 15:11 chutne9

This issue started to happen from SDL 3.2.18, as older versions like 3.2.16 works fine for me.

chutne9 avatar Nov 03 '25 02:11 chutne9

This issue started to happen from SDL 3.2.18, as older versions like 3.2.16 works fine for me.

Can you use git bisect to identify where it broke for you?

slouken avatar Nov 03 '25 02:11 slouken

Guessing it's going to be https://github.com/libsdl-org/SDL/commit/b2c3e6fadec4be4f16febf1b5046fe0cc950addb

icculus avatar Nov 03 '25 02:11 icculus

Guessing it's going to be b2c3e6f

yes it is!, the log points to another commit but the code this the same: 17656d051b750b945fdd8b4608f9524c7879bc1b

chutne9 avatar Nov 03 '25 02:11 chutne9

I don't know what it does but removing this line background_app_default = true; /* by default, don't explicitly activate the dock and then us again to force to foreground */ in SDL_cocoaevents.m makes it focus normally on launch again.

chutne9 avatar Nov 03 '25 02:11 chutne9

Do we have a contact at Apple still, @slouken? It would be useful if someone could tell us why this Dock trick was necessary and why nothing else needs this.

Like, what are we doing that we don't get focused on launch by default?

icculus avatar Nov 08 '25 23:11 icculus

Like, is this more "oh, you didn't use a .nib file?!" fallout...?

icculus avatar Nov 08 '25 23:11 icculus

Like, is this more "oh, you didn't use a .nib file?!" fallout...?

Yeah, I think so. Launching from terminal has different finder behavior than launching from an app bundle.

slouken avatar Nov 08 '25 23:11 slouken

I found a workaround for my purposes: after creating the window, I flush the event loop completely. Then I call SDL_RaiseWindow, and flush the event loop one more time. This consistently focuses the window on startup--hope that helps any fixes here!

kevinw avatar Nov 09 '25 10:11 kevinw

Kevin, you mean?

SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); SDL_RaiseWindow(sdl.window); SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);

didn't work for me (SDL2)

thanks

On Sun, 9 Nov 2025 at 10:48, Kevin Watters @.***> wrote:

kevinw left a comment (libsdl-org/SDL#13920) https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3507953632

I found a workaround for my purposes: after creating the window, I flush the event loop completely. Then I call SDL_RaiseWindow, and flush the event loop one more time. This consistently focuses the window on startup--hope that helps any fixes here!

— Reply to this email directly, view it on GitHub https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3507953632, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADA3H2AD2Y4CV5PCGIEDO3334LY7AVCNFSM6AAAAACGE2Z2HSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTKMBXHE2TGNRTGI . You are receiving this because you commented.Message ID: @.***>

goblinhack avatar Nov 09 '25 16:11 goblinhack

@goblinhack not quite (and as I mentioned, I'm on SDL3) -- I just use while SDL_PollEvent(&evt) {}

kevinw avatar Nov 11 '25 11:11 kevinw

That worked! (SDL2)

#if APPLE SDL_Event evt; // // Work around macos focus issue, possibly caused by iTerm // while (SDL_PollEvent(&evt)) {} #endif

SDL_RaiseWindow(sdl.window);

On Tue, 11 Nov 2025 at 11:42, Kevin Watters @.***> wrote:

kevinw left a comment (libsdl-org/SDL#13920) https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3516481474

@goblinhack https://github.com/goblinhack not quite (and as I mentioned, I'm on SDL3) -- I just use while SDL_PollEvent(&evt) {}

— Reply to this email directly, view it on GitHub https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3516481474, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADA3HZ7HXSE43OBUZ2DKQ334HDTNAVCNFSM6AAAAACGE2Z2HSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTKMJWGQ4DCNBXGQ . You are receiving this because you were mentioned.Message ID: @.***>

goblinhack avatar Nov 11 '25 13:11 goblinhack

Okay, so it sounds to me like maybe SDL just needs to wait until a certain event comes through from Cocoa, sometime after applicationDidFinishLaunching, and then do some final efforts there. I'll see if I can isolate it.

icculus avatar Nov 11 '25 20:11 icculus