From 5da9d4ecc2b163487a5d82cc2aac188bb61340a6 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 18 Jan 2025 17:01:46 -0500 Subject: [PATCH] cocoa: Slightly better display hotplugging detection. Fixes #12016. --- src/video/cocoa/SDL_cocoamodes.m | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/video/cocoa/SDL_cocoamodes.m b/src/video/cocoa/SDL_cocoamodes.m index 7de75729c1746..b4e151b916f5a 100644 --- a/src/video/cocoa/SDL_cocoamodes.m +++ b/src/video/cocoa/SDL_cocoamodes.m @@ -405,8 +405,15 @@ static void Cocoa_DisplayReconfigurationCallback(CGDirectDisplayID displayid, CG } if ((flags & kCGDisplayAddFlag) && (flags & kCGDisplayRemoveFlag)) { - // both adding _and_ removing? Treat it as a remove exclusively. This can happen if a display is unmirroring because it's being disabled, etc. - flags &= ~kCGDisplayAddFlag; + // sometimes you get a removed device that gets Add and Remove flags at the same time but the display dimensions are 0x0 or 1x1, hence the `> 1` test. + // Mirrored things are always removed, since they don't represent a discrete display in this state. + if (((flags & kCGDisplayMirrorFlag) == 0) && (CGDisplayPixelsWide(displayid) > 1)) { + // Final state is connected + flags &= ~kCGDisplayRemoveFlag; + } else { + // Final state is disconnected + flags &= ~kCGDisplayAddFlag; + } } if (flags & kCGDisplayAddFlag) {