From b9317734b9870fc208ef184ac0b268a24302ec2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 31 May 2025 10:02:45 +0800 Subject: [PATCH] WM: support CTWM version detection --- CMakeLists.txt | 2 +- src/detection/wm/wm_linux.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 80904490f..376f47783 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -802,7 +802,7 @@ elseif(NetBSD) src/detection/users/users_linux.c src/detection/wallpaper/wallpaper_linux.c src/detection/wifi/wifi_nbsd.c - src/detection/wm/wm_nosupport.c + src/detection/wm/wm_linux.c src/detection/de/de_linux.c src/detection/wmtheme/wmtheme_linux.c src/detection/camera/camera_linux.c diff --git a/src/detection/wm/wm_linux.c b/src/detection/wm/wm_linux.c index 60ff5a3b8..6cdc3e9f7 100644 --- a/src/detection/wm/wm_linux.c +++ b/src/detection/wm/wm_linux.c @@ -143,6 +143,39 @@ static const char* getWslg(FFstrbuf* result) return NULL; } +static bool extractCtwmVersion(const char* line, FF_MAYBE_UNUSED uint32_t len, void *userdata) +{ + int count = 0; + sscanf(line, "%*d.%*d.%*d%n", &count); + if (count == 0) return true; + + ffStrbufSetNS((FFstrbuf*) userdata, len, line); + return false; +} + +static const char* getCtwm(FFstrbuf* result) +{ + FF_STRBUF_AUTO_DESTROY path = ffStrbufCreate(); + const char* error = ffFindExecutableInPath("ctwm", &path); + if (error) return "Failed to find ctwm executable path"; + + ffBinaryExtractStrings(path.chars, extractCtwmVersion, result, (uint32_t) strlen("0.0.0")); + if (result->length > 0) return NULL; + + if (ffProcessAppendStdOut(result, (char* const[]){ + path.chars, + "--version", + NULL + }) == NULL) + { // ctwm version 4.0.1\n... + ffStrbufSubstrBeforeFirstC(result, '\n'); + ffStrbufSubstrAfterLastC(result, ' '); + return NULL; + } + + return "Failed to run command `ctwm --version`"; +} + const char* ffDetectWMVersion(const FFstrbuf* wmName, FFstrbuf* result, FF_MAYBE_UNUSED FFWMOptions* options) { if (!wmName) @@ -157,5 +190,8 @@ const char* ffDetectWMVersion(const FFstrbuf* wmName, FFstrbuf* result, FF_MAYBE if (ffStrbufEqualS(wmName, "WSLg")) return getWslg(result); + if (ffStrbufEqualS(wmName, "ctwm")) + return getCtwm(result); + return "Unsupported WM"; }