Skip to content

Instantly share code, notes, and snippets.

@4np
Created January 24, 2017 23:34
Show Gist options
  • Save 4np/3a82c1818bc27abadfe96df2482207da to your computer and use it in GitHub Desktop.
Save 4np/3a82c1818bc27abadfe96df2482207da to your computer and use it in GitHub Desktop.
Access Mac OS Displays
private func getActiveDisplays() -> [CGDirectDisplayID] {
var displayCount: UInt32 = 0
// get the number of active displays
guard CGGetActiveDisplayList(0, nil, &displayCount) == .success else {
return []
}
// get the active displays
let allocated = Int(displayCount)
let activeDisplays = UnsafeMutablePointer<CGDirectDisplayID>.allocate(capacity: allocated)
guard CGGetActiveDisplayList(displayCount, activeDisplays, &displayCount) == .success else {
return []
}
debugPrint("display count: \(displayCount)")
debugPrint("displays: \(activeDisplays)")
// transform unsafe mutable pointer
var displays = [CGDirectDisplayID]()
for i in 0..<allocated {
let display = activeDisplays[i]
// get display brightness
let model = CGDisplayModelNumber(display)
debugPrint("display: \(display), model: \(model)")
displays.append(display)
}
// deallocate
activeDisplays.deallocate(capacity: allocated)
return displays
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment