Last active
June 10, 2021 19:05
-
-
Save salexkidd/bcbea2372e92c6e5b04cbd7f48d9b204 to your computer and use it in GitHub Desktop.
NSScreen+someOfFunc.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
import Cocoa | |
extension NSScreen { | |
public var displayID: CGDirectDisplayID { | |
get { | |
return deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as! CGDirectDisplayID | |
} | |
} | |
public var displayName: String { | |
get { | |
var name = "Unknow" | |
var object : io_object_t | |
var serialPortIterator = io_iterator_t() | |
let matching = IOServiceMatching("IODisplayConnect") | |
let kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &serialPortIterator) | |
if KERN_SUCCESS == kernResult && serialPortIterator != 0 { | |
repeat { | |
object = IOIteratorNext(serialPortIterator) | |
let displayInfo = IODisplayCreateInfoDictionary(object, UInt32(kIODisplayOnlyPreferredName)).takeRetainedValue() as NSDictionary as! [String:AnyObject] | |
if (displayInfo[kDisplayVendorID] as? UInt32 == CGDisplayVendorNumber(displayID) && | |
displayInfo[kDisplayProductID] as? UInt32 == CGDisplayModelNumber(displayID) && | |
displayInfo[kDisplaySerialNumber] as? UInt32 ?? 0 == CGDisplaySerialNumber(displayID) | |
) { | |
if let productName = displayInfo["DisplayProductName"] as? [String:String], | |
let firstKey = Array(productName.keys).first { | |
name = productName[firstKey]! | |
break | |
} | |
} | |
} while object != 0 | |
} | |
IOObjectRelease(serialPortIterator) | |
return name | |
} | |
} | |
static public func getByDisplayID(displayID: CGDirectDisplayID) -> NSScreen? { | |
var result: NSScreen? = nil | |
for screen in NSScreen.screens { | |
if screen.displayID == displayID { | |
result = screen | |
break | |
} | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment