Skip to content

Instantly share code, notes, and snippets.

@nemolize
Last active July 9, 2020 15:56
Show Gist options
  • Save nemolize/5274eba919f43a06cea77e73fc8693f8 to your computer and use it in GitHub Desktop.
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
// 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