Fixed problem with no existing serial number for built-in display

This commit is contained in:
Moritz Pflanzer 2015-05-12 17:13:00 +01:00
parent e0a6772e5e
commit 8e3c423ed4

View File

@ -57,50 +57,53 @@ static io_service_t IOServicePortFromCGDisplayID(CGDirectDisplayID displayID)
while((serv = IOIteratorNext(iter)) != 0)
{
CFDictionaryRef info;
CFIndex vendorID, productID, serialNumber;
CFNumberRef vendorIDRef, productIDRef, serialNumberRef;
CFDictionaryRef displayInfo;
CFNumberRef vendorIDRef;
CFNumberRef productIDRef;
CFNumberRef serialNumberRef;
NSNumber *vendorID;
NSNumber *productID;
NSNumber *serialNumber;
Boolean success;
info = IODisplayCreateInfoDictionary(serv,
kIODisplayOnlyPreferredName);
displayInfo = IODisplayCreateInfoDictionary(serv, kIODisplayOnlyPreferredName);
vendorIDRef = CFDictionaryGetValue(info,
CFSTR(kDisplayVendorID));
productIDRef = CFDictionaryGetValue(info,
CFSTR(kDisplayProductID));
serialNumberRef = CFDictionaryGetValue(info,
CFSTR(kDisplaySerialNumber));
success = CFNumberGetValue(vendorIDRef, kCFNumberCFIndexType,
&vendorID);
success &= CFNumberGetValue(productIDRef, kCFNumberCFIndexType,
&productID);
success &= CFNumberGetValue(serialNumberRef, kCFNumberCFIndexType,
&serialNumber);
success = CFDictionaryGetValueIfPresent(displayInfo, CFSTR(kDisplayVendorID), (const void**)&vendorIDRef);
success &= CFDictionaryGetValueIfPresent(displayInfo, CFSTR(kDisplayProductID), (const void**)&productIDRef);
if(!success)
{
CFRelease(info);
CFRelease(displayInfo);
continue;
}
vendorID = (__bridge NSNumber*)vendorIDRef;
productID = (__bridge NSNumber*)productIDRef;
// If a serial number is found use it
// Otherwise serial number will be nil (= 0) which will match with the output of 'CGDisplaySerialNumber'
if(CFDictionaryGetValueIfPresent(displayInfo, CFSTR(kDisplaySerialNumber), (const void**)&serialNumberRef))
{
serialNumber = (__bridge NSNumber*)serialNumberRef;
}
// If the vendor and product id along with the serial don't match
// then we are not looking at the correct monitor.
// NOTE: The serial number is important in cases where two monitors
// are the exact same.
if (CGDisplayVendorNumber(displayID) != vendorID ||
CGDisplayModelNumber(displayID) != productID ||
CGDisplaySerialNumber(displayID) != serialNumber)
if(CGDisplayVendorNumber(displayID) != vendorID.unsignedIntValue ||
CGDisplayModelNumber(displayID) != productID.unsignedIntValue ||
CGDisplaySerialNumber(displayID) != serialNumber.unsignedIntValue)
{
CFRelease(info);
CFRelease(displayInfo);
continue;
}
// The VendorID, Product ID, and the Serial Number all Match Up!
// Therefore we have found the appropriate display io_service
servicePort = serv;
CFRelease(info);
CFRelease(displayInfo);
break;
}