Last active
July 9, 2020 15:56
-
-
Save nemolize/5274eba919f43a06cea77e73fc8693f8 to your computer and use it in GitHub Desktop.
This code demonstrates how display refresh rates can be retrieved in Cocoa framework with swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Prints refresh rates of displays. | |
func debugPrintDisplayRefreshRates() { | |
var displayCount: UInt32 = 0 | |
var result = CGGetActiveDisplayList(0, nil, &displayCount) | |
let allocated = Int(displayCount) | |
let activeDisplays = UnsafeMutablePointer<CGDirectDisplayID>.allocate(capacity: allocated) | |
defer { activeDisplays.deallocate() } | |
result = CGGetActiveDisplayList(displayCount, activeDisplays, &displayCount) | |
if result != .success { | |
print("error: \(result)") | |
return | |
} | |
for i in 0 ..< displayCount { | |
let displayId = activeDisplays[Int(i)] | |
if CGDisplayIsBuiltin(displayId) == 1 { | |
continue | |
} | |
// NOTE: For some reason or another, zero is printed for intrinsic display of my Macbook Pro :( | |
if let mode = CGDisplayCopyDisplayMode(displayId) { | |
debugPrint("[\(i)] - \(activeDisplays[Int(i)]) \(mode.refreshRate)Hz") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment