macOS: Make sure the desktop's display mode is always in SDL's list of display modes.
authorAlex Szpakowski <slime73@gmail.com>
Fri, 29 Dec 2017 22:13:40 -0400
changeset 11786677c76668fe1
parent 11785 6d85907059ff
child 11787 feaf1b45ae88
macOS: Make sure the desktop's display mode is always in SDL's list of display modes.
src/video/cocoa/SDL_cocoamodes.m
     1.1 --- a/src/video/cocoa/SDL_cocoamodes.m	Tue Dec 19 11:19:10 2017 -0800
     1.2 +++ b/src/video/cocoa/SDL_cocoamodes.m	Fri Dec 29 22:13:40 2017 -0400
     1.3 @@ -340,29 +340,53 @@
     1.4  Cocoa_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
     1.5  {
     1.6      SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
     1.7 -    CFArrayRef modes = CGDisplayCopyAllDisplayModes(data->display, NULL);
     1.8 +    CVDisplayLinkRef link = NULL;
     1.9 +    CGDisplayModeRef desktopmoderef;
    1.10 +    SDL_DisplayMode desktopmode;
    1.11 +    CFArrayRef modes;
    1.12 +
    1.13 +    CVDisplayLinkCreateWithCGDisplay(data->display, &link);
    1.14 +
    1.15 +    desktopmoderef = CGDisplayCopyDisplayMode(data->display);
    1.16 +
    1.17 +    /* CopyAllDisplayModes won't always contain the desktop display mode (if
    1.18 +     * NULL is passed in) - for example on a retina 15" MBP, System Preferences
    1.19 +     * allows choosing 1920x1200 but it's not in the list. AddDisplayMode makes
    1.20 +     * sure there are no duplicates so it's safe to always add the desktop mode
    1.21 +     * even in cases where it is in the CopyAllDisplayModes list.
    1.22 +     */
    1.23 +    if (desktopmoderef && GetDisplayMode(_this, desktopmoderef, link, &desktopmode)) {
    1.24 +        if (!SDL_AddDisplayMode(display, &desktopmode)) {
    1.25 +            CGDisplayModeRelease(desktopmoderef);
    1.26 +            SDL_free(desktopmode.driverdata);
    1.27 +        }
    1.28 +    } else {
    1.29 +        CGDisplayModeRelease(desktopmoderef);
    1.30 +    }
    1.31 +
    1.32 +    modes = CGDisplayCopyAllDisplayModes(data->display, NULL);
    1.33  
    1.34      if (modes) {
    1.35 -        CVDisplayLinkRef link = NULL;
    1.36 +        CFIndex i;
    1.37          const CFIndex count = CFArrayGetCount(modes);
    1.38 -        CFIndex i;
    1.39 -
    1.40 -        CVDisplayLinkCreateWithCGDisplay(data->display, &link);
    1.41  
    1.42          for (i = 0; i < count; i++) {
    1.43              CGDisplayModeRef moderef = (CGDisplayModeRef) CFArrayGetValueAtIndex(modes, i);
    1.44              SDL_DisplayMode mode;
    1.45 +
    1.46              if (GetDisplayMode(_this, moderef, link, &mode)) {
    1.47 -                CGDisplayModeRetain(moderef);
    1.48 -                if (!SDL_AddDisplayMode(display, &mode)) {
    1.49 +                if (SDL_AddDisplayMode(display, &mode)) {
    1.50 +                    CGDisplayModeRetain(moderef);
    1.51 +                } else {
    1.52                      SDL_free(mode.driverdata);
    1.53                  }
    1.54              }
    1.55          }
    1.56  
    1.57 -        CVDisplayLinkRelease(link);
    1.58          CFRelease(modes);
    1.59      }
    1.60 +
    1.61 +    CVDisplayLinkRelease(link);
    1.62  }
    1.63  
    1.64  int