+
+ return name;
+}
+
+/* Returns the name for the screen that DID came from, or NULL.
+ Caller must free return value.
+*/
+
+static char *
+ns_screen_name (CGDirectDisplayID did)
+{
+ char *name = NULL;
+
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9
+ mach_port_t masterPort;
+ io_iterator_t it;
+ io_object_t obj;
+
+ // CGDisplayIOServicePort is deprecated. Do it another (harder) way.
+
+ if (IOMasterPort (MACH_PORT_NULL, &masterPort) != kIOReturnSuccess
+ || IOServiceGetMatchingServices (masterPort,
+ IOServiceMatching ("IONDRVDevice"),
+ &it) != kIOReturnSuccess)
+ return name;
+
+ /* Must loop until we find a name. Many devices can have the same unit
+ number (represents different GPU parts), but only one has a name. */
+ while (! name && (obj = IOIteratorNext (it)))
+ {
+ CFMutableDictionaryRef props;
+ const void *val;
+
+ if (IORegistryEntryCreateCFProperties (obj,
+ &props,
+ kCFAllocatorDefault,
+ kNilOptions) == kIOReturnSuccess
+ && props != nil
+ && (val = CFDictionaryGetValue(props, @"IOFBDependentIndex")))
+ {
+ unsigned nr = [(NSNumber *)val unsignedIntegerValue];
+ if (nr == CGDisplayUnitNumber (did))
+ name = ns_get_name_from_ioreg (obj);
+ }
+
+ CFRelease (props);
+ IOObjectRelease (obj);
+ }
+
+ IOObjectRelease (it);
+
+#else
+
+ name = ns_get_name_from_ioreg (CGDisplayIOServicePort (did));
+
+#endif