Browse Source

cocoa: Slightly better display hotplugging detection.

Fixes #12016.
Ryan C. Gordon 3 months ago
parent
commit
5da9d4ecc2
1 changed files with 9 additions and 2 deletions
  1. 9 2
      src/video/cocoa/SDL_cocoamodes.m

+ 9 - 2
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) {