From c28ced9dcb3b55cb8a4b8432752a52fe6398f135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 8 Aug 2023 20:23:26 +0800 Subject: [PATCH] NativeResolution (Linux): add support --- CMakeLists.txt | 3 +- .../nativeresolution/nativeresolution_linux.c | 60 +++++++++++++++++++ src/util/edidHelper.c | 8 +++ src/util/edidHelper.h | 10 ++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/detection/nativeresolution/nativeresolution_linux.c create mode 100644 src/util/edidHelper.c create mode 100644 src/util/edidHelper.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1792122dc..4dc60b91e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -356,6 +356,7 @@ set(LIBFASTFETCH_SRC src/modules/wifi/wifi.c src/modules/wm/wm.c src/modules/wmtheme/wmtheme.c + src/util/edidHelper.c src/util/FFlist.c src/util/FFstrbuf.c src/util/platform/FFPlatform.c @@ -394,7 +395,7 @@ if(LINUX) src/detection/gamepad/gamepad_linux.c src/detection/media/media_linux.c src/detection/memory/memory_linux.c - src/detection/nativeresolution/nativeresolution_nosupport.c + src/detection/nativeresolution/nativeresolution_linux.c src/detection/opengl/opengl_linux.c src/detection/os/os_linux.c src/detection/packages/packages_linux.c diff --git a/src/detection/nativeresolution/nativeresolution_linux.c b/src/detection/nativeresolution/nativeresolution_linux.c new file mode 100644 index 000000000..bb7e5a5e1 --- /dev/null +++ b/src/detection/nativeresolution/nativeresolution_linux.c @@ -0,0 +1,60 @@ +#include "nativeresolution.h" + +#include "common/io/io.h" +#include "util/edidHelper.h" +#include "util/stringUtils.h" + +#include + +const char* ffDetectNativeResolution(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; + ffEdidGetNativeResolution(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; + } + + FFNativeResolutionResult* display = (FFNativeResolutionResult*) ffListAdd(results); + display->width = width; + display->height = height; + ffStrbufInitS(&display->name, plainName); + } + + ffStrbufSubstrBefore(&drmDir, drmDirLength); + } + + closedir(dirp); + return NULL; +} diff --git a/src/util/edidHelper.c b/src/util/edidHelper.c new file mode 100644 index 000000000..fa765d55f --- /dev/null +++ b/src/util/edidHelper.c @@ -0,0 +1,8 @@ +#include "edidHelper.h" + +void ffEdidGetNativeResolution(uint8_t edid[128], uint32_t* width, uint32_t* height) +{ + const int dtd = 54; + *width = ((edid[dtd + 4] >> 4) << 8) | edid[dtd + 2]; + *height = ((edid[dtd + 7] >> 4) << 8) | edid[dtd + 5]; +} diff --git a/src/util/edidHelper.h b/src/util/edidHelper.h new file mode 100644 index 000000000..8a9540860 --- /dev/null +++ b/src/util/edidHelper.h @@ -0,0 +1,10 @@ +#pragma once + +#ifndef FF_INCLUDED_EDID_HELPER_H +#define FF_INCLUDED_EDID_HELPER_H + +#include + +void ffEdidGetNativeResolution(uint8_t edid[128], uint32_t* width, uint32_t* height); + +#endif