Last active
January 16, 2019 09:06
-
-
Save thorikawa/2dd7d070e73b66dd1f658c135b6da0f0 to your computer and use it in GitHub Desktop.
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
static char* getDisplayName(CGDirectDisplayID displayID) | |
{ | |
io_iterator_t it; | |
io_service_t service; | |
CFDictionaryRef info; | |
if (IOServiceGetMatchingServices(kIOMasterPortDefault, | |
IOServiceMatching("IODisplayConnect"), | |
&it) != 0) | |
{ | |
// This may happen if a desktop Mac is running headless | |
return NULL; | |
} | |
while ((service = IOIteratorNext(it)) != 0) | |
{ | |
info = IODisplayCreateInfoDictionary(service, | |
kIODisplayOnlyPreferredName); | |
CFNumberRef vendorIDRef = | |
(CFNumberRef)CFDictionaryGetValue(info, CFSTR(kDisplayVendorID)); | |
CFNumberRef productIDRef = | |
(CFNumberRef)CFDictionaryGetValue(info, CFSTR(kDisplayProductID)); | |
if (!vendorIDRef || !productIDRef) | |
{ | |
CFRelease(info); | |
continue; | |
} | |
unsigned int vendorID, productID; | |
CFNumberGetValue(vendorIDRef, kCFNumberIntType, &vendorID); | |
CFNumberGetValue(productIDRef, kCFNumberIntType, &productID); | |
if (CGDisplayVendorNumber(displayID) == vendorID && | |
CGDisplayModelNumber(displayID) == productID) | |
{ | |
// Info dictionary is used and freed below | |
break; | |
} | |
CFRelease(info); | |
} | |
IOObjectRelease(it); | |
if (!service) | |
{ | |
ofLogWarning() << "Cocoa: Failed to find service port for display"; | |
return NULL; | |
} | |
CFDictionaryRef names = | |
(CFDictionaryRef)CFDictionaryGetValue(info, CFSTR(kDisplayProductName)); | |
CFStringRef nameRef; | |
if (!names || !CFDictionaryGetValueIfPresent(names, CFSTR("en_US"), | |
(const void**) &nameRef)) | |
{ | |
// This may happen if a desktop Mac is running headless | |
CFRelease(info); | |
return NULL; | |
} | |
const CFIndex size = | |
CFStringGetMaximumSizeForEncoding(CFStringGetLength(nameRef), | |
kCFStringEncodingUTF8); | |
char* name = (char*)calloc(size + 1, 1); | |
CFStringGetCString(nameRef, name, size, kCFStringEncodingUTF8); | |
CFRelease(info); | |
return name; | |
} | |
uint32_t i, j, monitorCount, disconnectedCount; | |
CGDirectDisplayID* monitors; | |
CGGetOnlineDisplayList(0, NULL, &monitorCount); | |
monitors = (CGDirectDisplayID*)calloc(monitorCount, sizeof(CGDirectDisplayID)); | |
CGGetOnlineDisplayList(monitorCount, monitors, &monitorCount); | |
ofLog() << "monitor count:" << monitorCount; | |
for(int i = 0; i < monitorCount; i++) { | |
CGDisplayUnitNumber(monitors[i]); | |
char* name = HM::getDisplayName(monitors[i]); | |
const CGSize size = CGDisplayScreenSize(monitors[i]); | |
CGDisplayModeRef displayMode; | |
CVDisplayLinkRef link; | |
CVDisplayLinkCreateWithCGDisplay(monitors[i], &link); | |
displayMode = CGDisplayCopyDisplayMode(monitors[i]); | |
auto width = (int) CGDisplayModeGetWidth(displayMode); | |
auto height = (int) CGDisplayModeGetHeight(displayMode); | |
ostringstream oss; | |
oss << i << ":" << name << " " << width << "*" << height; | |
options.push_back(oss.str()); | |
free(name); | |
CGDisplayModeRelease(displayMode); | |
CVDisplayLinkRelease(link); | |
} | |
free(monitors); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment