From ccef5de86e9b5c1e7754715bfefd956d75fdc60a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 22 Mar 2023 16:04:48 +0800 Subject: [PATCH] Display (macOS, Windows): support display name detection --- src/detection/displayserver/displayserver.c | 3 +- src/detection/displayserver/displayserver.h | 3 +- .../displayserver/displayserver_apple.c | 19 ++++++++- .../displayserver/displayserver_windows.c | 8 ++-- src/detection/displayserver/linux/wayland.c | 3 +- src/detection/displayserver/linux/xcb.c | 18 +++++---- src/detection/displayserver/linux/xlib.c | 18 +++++---- src/modules/display.c | 39 +++++++++++++++---- 8 files changed, 82 insertions(+), 29 deletions(-) diff --git a/src/detection/displayserver/displayserver.c b/src/detection/displayserver/displayserver.c index d727e8bfa..eeaad5dfc 100644 --- a/src/detection/displayserver/displayserver.c +++ b/src/detection/displayserver/displayserver.c @@ -1,7 +1,7 @@ #include "displayserver.h" #include "detection/internal.h" -bool ffdsAppendDisplay(FFDisplayServerResult* result, uint32_t width, uint32_t height, double refreshRate, uint32_t scaledWidth, uint32_t scaledHeight) +bool ffdsAppendDisplay(FFDisplayServerResult* result, uint32_t width, uint32_t height, double refreshRate, uint32_t scaledWidth, uint32_t scaledHeight, FFstrbuf* name) { if(width == 0 || height == 0) return false; @@ -12,6 +12,7 @@ bool ffdsAppendDisplay(FFDisplayServerResult* result, uint32_t width, uint32_t h display->refreshRate = refreshRate; display->scaledWidth = scaledWidth; display->scaledHeight = scaledHeight; + ffStrbufInitMove(&display->name, name); return true; } diff --git a/src/detection/displayserver/displayserver.h b/src/detection/displayserver/displayserver.h index 55c268919..fde40a4c6 100644 --- a/src/detection/displayserver/displayserver.h +++ b/src/detection/displayserver/displayserver.h @@ -41,6 +41,7 @@ typedef struct FFDisplayResult double refreshRate; uint32_t scaledWidth; uint32_t scaledHeight; + FFstrbuf name; } FFDisplayResult; typedef struct FFDisplayServerResult @@ -55,6 +56,6 @@ typedef struct FFDisplayServerResult } FFDisplayServerResult; const FFDisplayServerResult* ffConnectDisplayServer(const FFinstance* instance); -bool ffdsAppendDisplay(FFDisplayServerResult* result, uint32_t width, uint32_t height, double refreshRate, uint32_t scaledWidth, uint32_t scaledHeight); +bool ffdsAppendDisplay(FFDisplayServerResult* result, uint32_t width, uint32_t height, double refreshRate, uint32_t scaledWidth, uint32_t scaledHeight, FFstrbuf* name); #endif diff --git a/src/detection/displayserver/displayserver_apple.c b/src/detection/displayserver/displayserver_apple.c index e13e925e7..6db604114 100644 --- a/src/detection/displayserver/displayserver_apple.c +++ b/src/detection/displayserver/displayserver_apple.c @@ -1,5 +1,6 @@ #include "displayserver.h" #include "common/sysctl.h" +#include "util/apple/cf_helpers.h" #include #include @@ -8,6 +9,8 @@ #include #include +extern CFDictionaryRef CoreDisplay_DisplayCreateInfoDictionary(CGDirectDisplayID display) __attribute__((weak_import)); + static void detectDisplays(FFDisplayServerResult* ds) { CGDirectDisplayID screens[128]; @@ -36,12 +39,26 @@ static void detectDisplays(FFDisplayServerResult* ds) } } + FF_STRBUF_AUTO_DESTROY name; + ffStrbufInit(&name); + if(CoreDisplay_DisplayCreateInfoDictionary) + { + CFDictionaryRef FF_CFTYPE_AUTO_RELEASE displayInfo = CoreDisplay_DisplayCreateInfoDictionary(screen); + if(displayInfo) + { + CFDictionaryRef productNames; + if(!ffCfDictGetDict(displayInfo, CFSTR(kDisplayProductName), &productNames)) + ffCfDictGetString(productNames, CFSTR("en_US"), &name); + } + } + ffdsAppendDisplay(ds, (uint32_t)CGDisplayModeGetPixelWidth(mode), (uint32_t)CGDisplayModeGetPixelHeight(mode), refreshRate, (uint32_t)CGDisplayModeGetWidth(mode), - (uint32_t)CGDisplayModeGetHeight(mode) + (uint32_t)CGDisplayModeGetHeight(mode), + &name ); CGDisplayModeRelease(mode); } diff --git a/src/detection/displayserver/displayserver_windows.c b/src/detection/displayserver/displayserver_windows.c index 71df5da61..bbd40e38e 100644 --- a/src/detection/displayserver/displayserver_windows.c +++ b/src/detection/displayserver/displayserver_windows.c @@ -92,18 +92,18 @@ void ffConnectDisplayServerImpl(FFDisplayServerResult* ds, const FFinstance* ins }, }; - FFstrbuf name; + FF_STRBUF_AUTO_DESTROY name; ffStrbufInit(&name); if(SUCCEEDED(DisplayConfigGetDeviceInfo(&targetName.header)) && targetName.flags.friendlyNameFromEdid) ffStrbufSetWS(&name, targetName.monitorFriendlyDeviceName); - ffdsAppendDisplay( - ds, + ffdsAppendDisplay(ds, mode->targetMode.targetVideoSignalInfo.totalSize.cx, mode->targetMode.targetVideoSignalInfo.totalSize.cy, mode->targetMode.targetVideoSignalInfo.vSyncFreq.Numerator / (double) mode->targetMode.targetVideoSignalInfo.vSyncFreq.Denominator, data.width, - data.height); + data.height, + &name); } } diff --git a/src/detection/displayserver/linux/wayland.c b/src/detection/displayserver/linux/wayland.c index 938d46b3c..55f7ea46f 100644 --- a/src/detection/displayserver/linux/wayland.c +++ b/src/detection/displayserver/linux/wayland.c @@ -125,7 +125,8 @@ static void waylandOutputHandler(WaylandData* wldata, struct wl_registry* regist (uint32_t) display.height, display.refreshRate / 1000.0, (uint32_t) (display.width / display.scale), - (uint32_t) (display.height / display.scale) + (uint32_t) (display.height / display.scale), + NULL ); ffThreadMutexUnlock(&mutex); diff --git a/src/detection/displayserver/linux/xcb.c b/src/detection/displayserver/linux/xcb.c index 3b9680301..e89b32545 100644 --- a/src/detection/displayserver/linux/xcb.c +++ b/src/detection/displayserver/linux/xcb.c @@ -112,13 +112,13 @@ void ffdsConnectXcb(const FFinstance* instance, FFDisplayServerResult* result) while(iterator.rem > 0) { - ffdsAppendDisplay( - result, + ffdsAppendDisplay(result, (uint32_t) iterator.data->width_in_pixels, (uint32_t) iterator.data->height_in_pixels, 0, (uint32_t) iterator.data->width_in_pixels, - (uint32_t) iterator.data->height_in_pixels + (uint32_t) iterator.data->height_in_pixels, + NULL ); ffxcb_screen_next(&iterator); } @@ -182,7 +182,8 @@ static bool xcbRandrHandleModeInfo(XcbRandrData* data, xcb_randr_mode_info_t* mo (uint32_t) modeInfo->height, refreshRate == 0 ? data->defaultRefreshRate : refreshRate, (uint32_t) modeInfo->width, - (uint32_t) modeInfo->height + (uint32_t) modeInfo->height, + NULL ); } @@ -219,7 +220,8 @@ static bool xcbRandrHandleCrtc(XcbRandrData* data, xcb_randr_crtc_t crtc) (uint32_t) crtcInfoReply->height, data->defaultRefreshRate, (uint32_t) crtcInfoReply->width, - (uint32_t) crtcInfoReply->height + (uint32_t) crtcInfoReply->height, + NULL ); free(crtcInfoReply); @@ -264,7 +266,8 @@ static bool xcbRandrHandleMonitor(XcbRandrData* data, xcb_randr_monitor_info_t* (uint32_t) monitor->height, data->defaultRefreshRate, (uint32_t) monitor->width, - (uint32_t) monitor->height + (uint32_t) monitor->height, + NULL ); } @@ -324,7 +327,8 @@ static void xcbRandrHandleScreen(XcbRandrData* data, xcb_screen_t* screen) (uint32_t) screen->height_in_pixels, data->defaultRefreshRate, (uint32_t) screen->width_in_pixels, - (uint32_t) screen->height_in_pixels + (uint32_t) screen->height_in_pixels, + NULL ); } diff --git a/src/detection/displayserver/linux/xlib.c b/src/detection/displayserver/linux/xlib.c index bcce80f52..0df2bb350 100644 --- a/src/detection/displayserver/linux/xlib.c +++ b/src/detection/displayserver/linux/xlib.c @@ -77,13 +77,13 @@ void ffdsConnectXlib(const FFinstance* instance, FFDisplayServerResult* result) for(int i = 0; i < ScreenCount(display); i++) { Screen* screen = ScreenOfDisplay(display, i); - ffdsAppendDisplay( - result, + ffdsAppendDisplay(result, (uint32_t) WidthOfScreen(screen), (uint32_t) HeightOfScreen(screen), 0, (uint32_t) WidthOfScreen(screen), - (uint32_t) HeightOfScreen(screen) + (uint32_t) HeightOfScreen(screen), + NULL ); } @@ -141,7 +141,8 @@ static bool xrandrHandleModeInfo(XrandrData* data, XRRModeInfo* modeInfo) (uint32_t) modeInfo->height, refreshRate == 0 ? data->defaultRefreshRate : refreshRate, (uint32_t) modeInfo->width, - (uint32_t) modeInfo->height + (uint32_t) modeInfo->height, + NULL ); } @@ -172,7 +173,8 @@ static bool xrandrHandleCrtc(XrandrData* data, RRCrtc crtc) (uint32_t) crtcInfo->height, data->defaultRefreshRate, (uint32_t) crtcInfo->width, - (uint32_t) crtcInfo->height + (uint32_t) crtcInfo->height, + NULL ); data->ffXRRFreeCrtcInfo(crtcInfo); @@ -208,7 +210,8 @@ static bool xrandrHandleMonitor(XrandrData* data, XRRMonitorInfo* monitorInfo) (uint32_t) monitorInfo->height, data->defaultRefreshRate, (uint32_t) monitorInfo->width, - (uint32_t) monitorInfo->height + (uint32_t) monitorInfo->height, + NULL ); } @@ -262,7 +265,8 @@ static void xrandrHandleScreen(XrandrData* data, Screen* screen) (uint32_t) HeightOfScreen(screen), data->defaultRefreshRate, (uint32_t) WidthOfScreen(screen), - (uint32_t) HeightOfScreen(screen) + (uint32_t) HeightOfScreen(screen), + NULL ); } diff --git a/src/modules/display.c b/src/modules/display.c index a129b4077..3e364a3fc 100644 --- a/src/modules/display.c +++ b/src/modules/display.c @@ -2,18 +2,21 @@ #include "common/printing.h" #include "detection/displayserver/displayserver.h" -#define FF_RESOLUTION_MODULE_NAME "Display" -#define FF_RESOLUTION_NUM_FORMAT_ARGS 5 +#define FF_DISPLAY_MODULE_NAME "Display" +#define FF_DISPLAY_NUM_FORMAT_ARGS 6 void ffPrintDisplay(FFinstance* instance) { #ifdef __ANDROID__ - ffPrintError(instance, FF_RESOLUTION_MODULE_NAME, 0, &instance->config.display, "Display detection is not supported on Android"); + ffPrintError(instance, FF_DISPLAY_MODULE_NAME, 0, &instance->config.display, "Display detection is not supported on Android"); return; #endif const FFDisplayServerResult* dsResult = ffConnectDisplayServer(instance); + FF_STRBUF_AUTO_DESTROY key; + ffStrbufInit(&key); + for(uint32_t i = 0; i < dsResult->displays.length; i++) { FFDisplayResult* result = ffListGet(&dsResult->displays, i); @@ -21,7 +24,26 @@ void ffPrintDisplay(FFinstance* instance) if(instance->config.display.outputFormat.length == 0) { - ffPrintLogoAndKey(instance, FF_RESOLUTION_MODULE_NAME, moduleIndex, &instance->config.display.key); + if(result->name.length) + { + ffStrbufClear(&key); + if(instance->config.display.key.length == 0) + { + ffStrbufAppendF(&key, "%s (%s)", FF_DISPLAY_MODULE_NAME, result->name.chars); + } + else + { + ffParseFormatString(&key, &instance->config.display.key, 1, (FFformatarg[]){ + {FF_FORMAT_ARG_TYPE_STRBUF, &result->name} + }); + } + ffPrintLogoAndKey(instance, key.chars, 0, NULL); + } + else + { + ffPrintLogoAndKey(instance, FF_DISPLAY_MODULE_NAME, moduleIndex, &instance->config.display.key); + } + printf("%ix%i", result->width, result->height); if(result->refreshRate > 0) @@ -36,16 +58,19 @@ void ffPrintDisplay(FFinstance* instance) } else { - ffPrintFormat(instance, FF_RESOLUTION_MODULE_NAME, moduleIndex, &instance->config.display, FF_RESOLUTION_NUM_FORMAT_ARGS, (FFformatarg[]) { + ffPrintFormat(instance, FF_DISPLAY_MODULE_NAME, moduleIndex, &instance->config.display, FF_DISPLAY_NUM_FORMAT_ARGS, (FFformatarg[]) { {FF_FORMAT_ARG_TYPE_UINT, &result->width}, {FF_FORMAT_ARG_TYPE_UINT, &result->height}, {FF_FORMAT_ARG_TYPE_DOUBLE, &result->refreshRate}, {FF_FORMAT_ARG_TYPE_UINT, &result->scaledWidth}, - {FF_FORMAT_ARG_TYPE_UINT, &result->scaledHeight} + {FF_FORMAT_ARG_TYPE_UINT, &result->scaledHeight}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->name}, }); } + + ffStrbufDestroy(&result->name); } if(dsResult->displays.length == 0) - ffPrintError(instance, FF_RESOLUTION_MODULE_NAME, 0, &instance->config.display, "Couldn't detect display"); + ffPrintError(instance, FF_DISPLAY_MODULE_NAME, 0, &instance->config.display, "Couldn't detect display"); }