Cleanup of CGDisplayIOServicePort replacement.

master
Camilla Berglund ago%!(EXTRA string=12 years)
parent bebae14223
commit 43095307da
  1. 1
      README.md
  2. 144
      src/cocoa_monitor.m

@ -123,6 +123,7 @@ skills.
- Marcus Geelnard - Marcus Geelnard
- Stefan Gustavson - Stefan Gustavson
- Sylvain Hellegouarch - Sylvain Hellegouarch
- Matthew Henry
- heromyth - heromyth
- Paul Holden - Paul Holden
- Toni Jovanoski - Toni Jovanoski

@ -36,109 +36,73 @@
#include <CoreVideo/CVDisplayLink.h> #include <CoreVideo/CVDisplayLink.h>
// Returns the io_service_t corresponding to a CG display ID, or 0 on failure. // Returns the name of the specified display
// The io_service_t should be released with IOObjectRelease when not needed.
// //
static io_service_t IOServicePortFromCGDisplayID(CGDirectDisplayID displayID) static char* getDisplayName(CGDirectDisplayID displayID)
{ {
io_iterator_t iter; io_iterator_t iter;
io_service_t serv, servicePort = 0; io_service_t service;
char* result = NULL;
CFMutableDictionaryRef matching = IOServiceMatching("IODisplayConnect");
const uint32_t modelNumber = CGDisplayModelNumber(displayID);
// releases matching for us const uint32_t vendorNumber = CGDisplayVendorNumber(displayID);
kern_return_t err = IOServiceGetMatchingServices(kIOMasterPortDefault,
matching, if (modelNumber == kDisplayProductIDGeneric || modelNumber == 0xffffffff ||
&iter); vendorNumber == kDisplayVendorIDUnknown || vendorNumber == 0xffffffff)
if (err)
{ {
return 0; return NULL;
} }
while ((serv = IOIteratorNext(iter)) != 0) CFMutableDictionaryRef matching = IOServiceMatching("IODisplayConnect");
if (IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &iter) != 0)
return NULL;
while ((service = IOIteratorNext(iter)) != 0)
{ {
CFDictionaryRef info; CFIndex size, vendorID, productID;
CFIndex vendorID, productID; CFDictionaryRef info, names;
CFNumberRef vendorIDRef, productIDRef; CFStringRef name;
Boolean success;
info = IODisplayCreateInfoDictionary(service, kIODisplayOnlyPreferredName);
info = IODisplayCreateInfoDictionary(serv,
kIODisplayOnlyPreferredName); CFNumberGetValue(CFDictionaryGetValue(info, CFSTR(kDisplayVendorID)),
kCFNumberCFIndexType, &vendorID);
vendorIDRef = CFDictionaryGetValue(info, CFNumberGetValue(CFDictionaryGetValue(info, CFSTR(kDisplayProductID)),
CFSTR(kDisplayVendorID)); kCFNumberCFIndexType, &productID);
productIDRef = CFDictionaryGetValue(info,
CFSTR(kDisplayProductID)); if (vendorNumber != vendorID || modelNumber != productID)
success = CFNumberGetValue(vendorIDRef, kCFNumberCFIndexType,
&vendorID);
success &= CFNumberGetValue(productIDRef, kCFNumberCFIndexType,
&productID);
if (!success)
{ {
CFRelease(info); CFRelease(info);
continue; continue;
} }
if (CGDisplayVendorNumber(displayID) != vendorID || names = CFDictionaryGetValue(info, CFSTR(kDisplayProductName));
CGDisplayModelNumber(displayID) != productID) if (!names)
{ {
// This may happen if a desktop Mac is running headless
CFRelease(info); CFRelease(info);
continue; continue;
} }
// we're a match
servicePort = serv;
CFRelease(info);
break;
}
IOObjectRelease(iter);
return servicePort;
}
// Get the name of the specified display if (!CFDictionaryGetValueIfPresent(names, CFSTR("en_US"),
// (const void**) &name))
static const char* getDisplayName(CGDirectDisplayID displayID) {
{ CFRelease(info);
char* name; continue;
CFDictionaryRef info, names; }
CFStringRef value;
CFIndex size; size = CFStringGetMaximumSizeForEncoding(CFStringGetLength(name),
kCFStringEncodingUTF8);
io_service_t serv = IOServicePortFromCGDisplayID(displayID); result = calloc(size + 1, sizeof(char));
if (!serv) CFStringGetCString(name, result, size, kCFStringEncodingUTF8);
{
return strdup("Unknown");
}
info = IODisplayCreateInfoDictionary(serv,
kIODisplayOnlyPreferredName);
IOObjectRelease(serv);
names = CFDictionaryGetValue(info, CFSTR(kDisplayProductName));
if (!names || !CFDictionaryGetValueIfPresent(names, CFSTR("en_US"),
(const void**) &value))
{
// This may happen if a desktop Mac is running headless
_glfwInputError(GLFW_PLATFORM_ERROR, "Failed to retrieve display name");
CFRelease(info); CFRelease(info);
return strdup("Unknown");
} }
size = CFStringGetMaximumSizeForEncoding(CFStringGetLength(value),
kCFStringEncodingUTF8);
name = calloc(size + 1, sizeof(char));
CFStringGetCString(value, name, size, kCFStringEncodingUTF8);
CFRelease(info);
return name;
}
IOObjectRelease(iter);
return result;
}
// Check whether the display mode should be included in enumeration // Check whether the display mode should be included in enumeration
// //
@ -332,10 +296,16 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count)
{ {
int j; int j;
const CGSize size = CGDisplayScreenSize(displays[i]); const CGSize size = CGDisplayScreenSize(displays[i]);
char* name = getDisplayName(displays[i]);
monitors[found] = _glfwAllocMonitor(getDisplayName(displays[i]), if (!name)
size.width, size.height); {
_glfwInputError(GLFW_PLATFORM_ERROR,
"Cocoa: Failed to retrieve display name");
name = strdup("Unknown");
}
monitors[found] = _glfwAllocMonitor(name, size.width, size.height);
monitors[found]->ns.displayID = displays[i]; monitors[found]->ns.displayID = displays[i];
for (j = 0; j < [screens count]; j++) for (j = 0; j < [screens count]; j++)

Loading…
Cancel
Save