Created
January 24, 2017 23:34
-
-
Save 4np/3a82c1818bc27abadfe96df2482207da to your computer and use it in GitHub Desktop.
Access Mac OS Displays
This file contains hidden or 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
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