Files
fastfetch/src/detection/monitor/monitor_apple.m
T

138 lines
5.4 KiB
Objective-C
Raw Normal View History

2023-08-09 16:31:24 +08:00
#include "monitor.h"
2023-08-09 10:11:24 +08:00
#include "detection/displayserver/displayserver.h"
#include "util/apple/cf_helpers.h"
#include "util/apple/ddcci.h"
#include "util/edidHelper.h"
2023-08-11 16:45:16 +08:00
#import <AppKit/NSScreen.h>
#import <Foundation/Foundation.h>
2023-08-09 10:11:24 +08:00
extern CFDictionaryRef CoreDisplay_DisplayCreateInfoDictionary(CGDirectDisplayID display) __attribute__((weak_import));
static const char* detectWithDisplayServices(const FFDisplayServerResult* displayServer, FFlist* results)
{
if(!CoreDisplay_DisplayCreateInfoDictionary) return "CoreDisplay_DisplayCreateInfoDictionary is not available";
FF_LIST_FOR_EACH(FFDisplayResult, display, displayServer->displays)
{
2023-08-11 14:29:24 +08:00
if (display->type == FF_DISPLAY_TYPE_BUILTIN)
2023-08-09 10:11:24 +08:00
{
CFDictionaryRef FF_CFTYPE_AUTO_RELEASE displayInfo = CoreDisplay_DisplayCreateInfoDictionary((CGDirectDisplayID) display->id);
if(displayInfo)
{
int width, height;
2023-08-11 14:29:24 +08:00
if (ffCfDictGetInt(displayInfo, CFSTR("kCGDisplayPixelWidth"), &width) || // Default resolution (limited by connectors, GPUs, etc.)
2023-08-09 10:11:24 +08:00
ffCfDictGetInt(displayInfo, CFSTR("kCGDisplayPixelHeight"), &height) ||
width <= 0 || height <= 0)
continue;
2023-08-09 16:31:24 +08:00
FFMonitorResult* monitor = (FFMonitorResult*) ffListAdd(results);
monitor->width = (uint32_t) width;
monitor->height = (uint32_t) height;
2023-08-11 14:29:24 +08:00
ffStrbufInitCopy(&monitor->name, &display->name);
2023-08-09 10:52:12 +08:00
CGSize size = CGDisplayScreenSize((CGDirectDisplayID) display->id);
2023-08-09 16:31:24 +08:00
monitor->physicalWidth = (uint32_t) (size.width + 0.5);
monitor->physicalHeight = (uint32_t) (size.height + 0.5);
2023-08-11 16:45:16 +08:00
monitor->hdrCompatible = false;
if (CFDictionaryContainsKey(displayInfo, CFSTR("ReferencePeakHDRLuminance")))
monitor->hdrCompatible = true;
else
{
NSScreen* mainScreen = NSScreen.mainScreen;
if (display->primary)
monitor->hdrCompatible = mainScreen.maximumPotentialExtendedDynamicRangeColorComponentValue > 1;
else
{
for (NSScreen* screen in NSScreen.screens)
{
if (screen == mainScreen) continue;
NSNumber* screenNumber = [screen.deviceDescription valueForKey:@"NSScreenNumber"];
2023-08-11 19:37:23 +08:00
if (screenNumber && screenNumber.longValue == (long) display->id)
2023-08-11 16:45:16 +08:00
{
monitor->hdrCompatible = screen.maximumPotentialExtendedDynamicRangeColorComponentValue > 1;
break;
}
}
continue;
}
}
2023-08-09 10:11:24 +08:00
}
}
}
return NULL;
}
static const char* detectWithDdcci(FFlist* results)
{
if (!IOAVServiceCreate || !IOAVServiceReadI2C)
return "IOAVService is not available";
CFMutableDictionaryRef matchDict = IOServiceMatching("DCPAVServiceProxy");
if (matchDict == NULL)
return "IOServiceMatching(\"DCPAVServiceProxy\") failed";
io_iterator_t iterator;
if(IOServiceGetMatchingServices(MACH_PORT_NULL, matchDict, &iterator) != kIOReturnSuccess)
return "IOServiceGetMatchingServices() failed";
FF_STRBUF_AUTO_DESTROY location = ffStrbufCreate();
io_registry_entry_t registryEntry;
while((registryEntry = IOIteratorNext(iterator)) != 0)
{
CFMutableDictionaryRef properties;
if(IORegistryEntryCreateCFProperties(registryEntry, &properties, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
{
IOObjectRelease(registryEntry);
continue;
}
ffStrbufClear(&location);
if(ffCfDictGetString(properties, CFSTR("Location"), &location) || ffStrbufEqualS(&location, "Embedded"))
{
// Builtin display should be handled by DisplayServices
IOObjectRelease(registryEntry);
continue;
}
FF_CFTYPE_AUTO_RELEASE IOAVServiceRef service = IOAVServiceCreateWithService(kCFAllocatorDefault, (io_service_t) registryEntry);
IOObjectRelease(registryEntry);
if (!service) continue;
FF_CFTYPE_AUTO_RELEASE CFDataRef edid = NULL;
2023-08-11 14:29:24 +08:00
if (IOAVServiceCopyEDID(service, &edid) != KERN_SUCCESS)
2023-08-09 10:11:24 +08:00
continue;
if (CFDataGetLength(edid) < 128)
continue;
uint32_t width, height;
2023-08-09 10:52:12 +08:00
const uint8_t* edidData = CFDataGetBytePtr(edid);
2023-08-09 13:25:22 +08:00
ffEdidGetPhysicalResolution(edidData, &width, &height);
2023-08-09 10:11:24 +08:00
if (width == 0 || height == 0) continue;
2023-08-09 16:31:24 +08:00
FFMonitorResult* display = (FFMonitorResult*) ffListAdd(results);
2023-08-09 10:11:24 +08:00
display->width = width;
display->height = height;
ffStrbufInit(&display->name);
2023-08-09 10:52:12 +08:00
ffEdidGetName(edidData, &display->name);
2023-08-09 13:25:22 +08:00
ffEdidGetPhysicalSize(edidData, &display->physicalWidth, &display->physicalHeight);
2023-08-11 19:37:23 +08:00
display->hdrCompatible = false;
2023-08-09 10:11:24 +08:00
}
return NULL;
}
2023-08-09 16:31:24 +08:00
const char* ffDetectMonitor(FFlist* results)
2023-08-09 10:11:24 +08:00
{
const FFDisplayServerResult* displayServer = ffConnectDisplayServer();
detectWithDisplayServices(displayServer, results);
if (displayServer->displays.length > results->length)
detectWithDdcci(results);
return NULL;
}