cocoadisplay: Round refresh rates when choosing display mode on macOS

Closes #1144

Co-authored-by: rdb <git@rdb.name>
This commit is contained in:
Ben Humphries 2021-06-01 10:56:55 +02:00 committed by rdb
parent 2386e80448
commit 0415a08416

View File

@ -1233,7 +1233,7 @@ find_display_modes(int width, int height) {
// Get the current refresh rate and pixel encoding. // Get the current refresh rate and pixel encoding.
CFStringRef current_pixel_encoding; CFStringRef current_pixel_encoding;
int refresh_rate; double refresh_rate;
mode = CGDisplayCopyDisplayMode(_display); mode = CGDisplayCopyDisplayMode(_display);
// First check if the current mode is adequate. // First check if the current mode is adequate.
@ -1267,7 +1267,7 @@ find_display_modes(int width, int height) {
// the mode width and height but also actual pixel widh and height. // the mode width and height but also actual pixel widh and height.
if (CGDisplayModeGetWidth(mode) == width && if (CGDisplayModeGetWidth(mode) == width &&
CGDisplayModeGetHeight(mode) == height && CGDisplayModeGetHeight(mode) == height &&
CGDisplayModeGetRefreshRate(mode) == refresh_rate && (int)(CGDisplayModeGetRefreshRate(mode) + 0.5) == (int)(refresh_rate + 0.5) &&
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1080 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
(floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_14 || (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_14 ||
(CGDisplayModeGetPixelWidth(mode) == expected_pixel_width && (CGDisplayModeGetPixelWidth(mode) == expected_pixel_width &&
@ -1275,7 +1275,12 @@ find_display_modes(int width, int height) {
#endif #endif
CFStringCompare(pixel_encoding, current_pixel_encoding, 0) == kCFCompareEqualTo) { CFStringCompare(pixel_encoding, current_pixel_encoding, 0) == kCFCompareEqualTo) {
CFArrayAppendValue(valid_modes, mode); if (CGDisplayModeGetRefreshRate(mode) == refresh_rate) {
// Exact match for refresh rate, prioritize this.
CFArrayInsertValueAtIndex(valid_modes, 0, mode);
} else {
CFArrayAppendValue(valid_modes, mode);
}
} }
CFRelease(pixel_encoding); CFRelease(pixel_encoding);
} }