mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-05 12:44:19 +01:00
PhysicalDisplay: rename module to Monitor
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
typedef struct FFMonitorResult
|
||||
{
|
||||
FFstrbuf name;
|
||||
uint32_t width; // native / maximum resolution, in pixels
|
||||
uint32_t height; // native / maximum resolution, in pixels
|
||||
uint32_t physicalWidth; // in mm
|
||||
uint32_t physicalHeight; // in mm
|
||||
} FFMonitorResult;
|
||||
|
||||
const char* ffDetectMonitor(FFlist* results);
|
||||
@@ -0,0 +1,113 @@
|
||||
#include "monitor.h"
|
||||
#include "detection/displayserver/displayserver.h"
|
||||
#include "util/apple/cf_helpers.h"
|
||||
#include "util/apple/ddcci.h"
|
||||
#include "util/edidHelper.h"
|
||||
|
||||
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)
|
||||
{
|
||||
if (display->type == FF_DISPLAY_TYPE_BUILTIN || display->type == FF_DISPLAY_TYPE_UNKNOWN)
|
||||
{
|
||||
CFDictionaryRef FF_CFTYPE_AUTO_RELEASE displayInfo = CoreDisplay_DisplayCreateInfoDictionary((CGDirectDisplayID) display->id);
|
||||
if(displayInfo)
|
||||
{
|
||||
int width, height;
|
||||
if (ffCfDictGetInt(displayInfo, CFSTR("kCGDisplayPixelWidth"), &width) ||
|
||||
ffCfDictGetInt(displayInfo, CFSTR("kCGDisplayPixelHeight"), &height) ||
|
||||
width <= 0 || height <= 0)
|
||||
continue;
|
||||
|
||||
FFMonitorResult* monitor = (FFMonitorResult*) ffListAdd(results);
|
||||
monitor->width = (uint32_t) width;
|
||||
monitor->height = (uint32_t) height;
|
||||
ffStrbufInit(&monitor->name);
|
||||
|
||||
CFDictionaryRef productNames;
|
||||
if(!ffCfDictGetDict(displayInfo, CFSTR(kDisplayProductName), &productNames))
|
||||
ffCfDictGetString(productNames, CFSTR("en_US"), &monitor->name);
|
||||
|
||||
CGSize size = CGDisplayScreenSize((CGDirectDisplayID) display->id);
|
||||
monitor->physicalWidth = (uint32_t) (size.width + 0.5);
|
||||
monitor->physicalHeight = (uint32_t) (size.height + 0.5);
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
if (IOAVServiceCopyEDID(service, &edid) != KERN_SUCCESS )
|
||||
continue;
|
||||
|
||||
if (CFDataGetLength(edid) < 128)
|
||||
continue;
|
||||
|
||||
uint32_t width, height;
|
||||
const uint8_t* edidData = CFDataGetBytePtr(edid);
|
||||
ffEdidGetPhysicalResolution(edidData, &width, &height);
|
||||
if (width == 0 || height == 0) continue;
|
||||
|
||||
FFMonitorResult* display = (FFMonitorResult*) ffListAdd(results);
|
||||
display->width = width;
|
||||
display->height = height;
|
||||
ffStrbufInit(&display->name);
|
||||
ffEdidGetName(edidData, &display->name);
|
||||
ffEdidGetPhysicalSize(edidData, &display->physicalWidth, &display->physicalHeight);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* ffDetectMonitor(FFlist* results)
|
||||
{
|
||||
const FFDisplayServerResult* displayServer = ffConnectDisplayServer();
|
||||
|
||||
detectWithDisplayServices(displayServer, results);
|
||||
|
||||
if (displayServer->displays.length > results->length)
|
||||
detectWithDdcci(results);
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "monitor.h"
|
||||
|
||||
#include "common/io/io.h"
|
||||
#include "util/edidHelper.h"
|
||||
#include "util/stringUtils.h"
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
const char* ffDetectMonitor(FFlist* results)
|
||||
{
|
||||
const char* drmDirPath = "/sys/class/drm/";
|
||||
|
||||
DIR* dirp = opendir(drmDirPath);
|
||||
if(dirp == NULL)
|
||||
return "opendir(drmDirPath) == NULL";
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY drmDir = ffStrbufCreateA(64);
|
||||
ffStrbufAppendS(&drmDir, drmDirPath);
|
||||
|
||||
uint32_t drmDirLength = drmDir.length;
|
||||
|
||||
struct dirent* entry;
|
||||
while((entry = readdir(dirp)) != NULL)
|
||||
{
|
||||
if(ffStrEquals(entry->d_name, ".") || ffStrEquals(entry->d_name, ".."))
|
||||
continue;
|
||||
|
||||
ffStrbufAppendS(&drmDir, entry->d_name);
|
||||
ffStrbufAppendS(&drmDir, "/edid");
|
||||
|
||||
uint8_t edidData[128];
|
||||
if(ffReadFileData(drmDir.chars, sizeof(edidData), edidData) != sizeof(edidData))
|
||||
{
|
||||
ffStrbufSubstrBefore(&drmDir, drmDirLength);
|
||||
continue;
|
||||
}
|
||||
|
||||
uint32_t width, height;
|
||||
ffEdidGetPhysicalResolution(edidData, &width, &height);
|
||||
if (width != 0 && height != 0)
|
||||
{
|
||||
const char* plainName = entry->d_name;
|
||||
if (ffStrStartsWith(plainName, "card"))
|
||||
{
|
||||
const char* tmp = strchr(plainName + strlen("card"), '-');
|
||||
if (tmp) plainName = tmp + 1;
|
||||
}
|
||||
|
||||
FFMonitorResult* display = (FFMonitorResult*) ffListAdd(results);
|
||||
display->width = width;
|
||||
display->height = height;
|
||||
ffStrbufInitS(&display->name, plainName);
|
||||
ffEdidGetPhysicalSize(edidData, &display->physicalWidth, &display->physicalHeight);
|
||||
}
|
||||
|
||||
ffStrbufSubstrBefore(&drmDir, drmDirLength);
|
||||
}
|
||||
|
||||
closedir(dirp);
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "monitor.h"
|
||||
|
||||
const char* ffDetectMonitor(FF_MAYBE_UNUSED FFlist* results)
|
||||
{
|
||||
return "Not supported on this platform";
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "monitor.h"
|
||||
|
||||
#include "common/io/io.h"
|
||||
#include "util/edidHelper.h"
|
||||
#include "util/stringUtils.h"
|
||||
#include "util/windows/registry.h"
|
||||
#include "util/windows/unicode.h"
|
||||
|
||||
#include <windows.h>
|
||||
#include <setupapi.h>
|
||||
#include <devguid.h>
|
||||
|
||||
static inline void wrapSetupDiDestroyDeviceInfoList(HDEVINFO* hdev)
|
||||
{
|
||||
if(*hdev)
|
||||
SetupDiDestroyDeviceInfoList(*hdev);
|
||||
}
|
||||
|
||||
const char* ffDetectMonitor(FFlist* results)
|
||||
{
|
||||
//https://learn.microsoft.com/en-us/windows/win32/power/enumerating-battery-devices
|
||||
HDEVINFO hdev __attribute__((__cleanup__(wrapSetupDiDestroyDeviceInfoList))) =
|
||||
SetupDiGetClassDevsW(&GUID_DEVCLASS_MONITOR, 0, 0, DIGCF_PRESENT);
|
||||
if(hdev == INVALID_HANDLE_VALUE)
|
||||
return "SetupDiGetClassDevsW(&GUID_DEVCLASS_MONITOR) failed";
|
||||
|
||||
SP_DEVINFO_DATA did = { .cbSize = sizeof(did) };
|
||||
for (DWORD idev = 0; SetupDiEnumDeviceInfo(hdev, idev, &did); ++idev)
|
||||
{
|
||||
FF_HKEY_AUTO_DESTROY hKey = SetupDiOpenDevRegKey(hdev, &did, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
|
||||
if (!hKey) continue;
|
||||
|
||||
uint8_t edidData[256] = {};
|
||||
if (!ffRegReadData(hKey, L"EDID", edidData, sizeof(edidData), NULL)) continue;
|
||||
uint32_t width, height;
|
||||
ffEdidGetPhysicalResolution(edidData, &width, &height);
|
||||
if (width == 0 || height == 0) continue;
|
||||
|
||||
FFMonitorResult* display = (FFMonitorResult*) ffListAdd(results);
|
||||
display->width = width;
|
||||
display->height = height;
|
||||
ffStrbufInit(&display->name);
|
||||
ffEdidGetName(edidData, &display->name);
|
||||
ffEdidGetPhysicalSize(edidData, &display->physicalWidth, &display->physicalHeight);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user