I'm trying to get a display name from CGDirectDisplayID
in pure C++ (or C.)
I can do it in Objective-C++, similar to this:
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <IOKit/graphics/IOGraphicsLib.h>
#import <Cocoa/Cocoa.h>
#include <string>
bool getDisplayNameForDispID(CGDirectDisplayID dispID,
std::string& strOutName)
{
bool bRes = false;
strOutName.clear();
NSArray *screens = [NSScreen screens];
for (NSScreen *screen in screens)
{
NSDictionary* screenDictionary = [screen deviceDescription];
if(screenDictionary)
{
NSNumber* screenID = [screenDictionary objectForKey:@"NSScreenNumber"];
if(screenID)
{
CGDirectDisplayID aID = [screenID unsignedIntValue];
if(aID == dispID)
{
//Got it
NSString* pName = [screen localizedName];
strOutName.assign([pName UTF8String], [pName lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
bRes = true;
break;
}
}
}
}
return bRes;
}
but I don't want to bring this slow Objective-C code into my project just to do this little thing. There must be a way to do it in low-level C.
PS. I know that there's this whole thread dedicated to it. Please note that none of the solutions there work for macOS Ventura as CGDisplayIOServicePort
is deprecated and simply returns 0 and any subsequent calls do thing.