From 55b41144dbe9f8e8dc9f660b0e2b88064b09f00f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 30 Oct 2023 16:34:35 +0800 Subject: [PATCH] WM: add flag `--wm-detect-plugin` --- CMakeLists.txt | 3 ++ .../displayserver/displayserver_apple.c | 41 ------------------ .../displayserver/displayserver_windows.c | 6 ++- src/detection/wmde/wmde.h | 6 +++ src/detection/wmde/wmde_apple.c | 42 +++++++++++++++++++ src/detection/wmde/wmde_linux.c | 11 +++++ src/detection/wmde/wmde_nosupport.c | 11 +++++ src/modules/wm/option.h | 2 + src/modules/wm/wm.c | 27 ++++++++++++ 9 files changed, 106 insertions(+), 43 deletions(-) create mode 100644 src/detection/wmde/wmde.h create mode 100644 src/detection/wmde/wmde_apple.c create mode 100644 src/detection/wmde/wmde_linux.c create mode 100644 src/detection/wmde/wmde_nosupport.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 96842bd7f..3ab1453be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -421,6 +421,7 @@ if(LINUX) src/detection/users/users_linux.c src/detection/wallpaper/wallpaper_linux.c src/detection/wifi/wifi_linux.c + src/detection/wmde/wmde_linux.c src/detection/wmtheme/wmtheme_linux.c src/util/platform/FFPlatform_unix.c ) @@ -528,6 +529,7 @@ elseif(BSD) src/detection/users/users_linux.c src/detection/wallpaper/wallpaper_linux.c src/detection/wifi/wifi_bsd.c + src/detection/wmde/wmde_linux.c src/detection/wmtheme/wmtheme_linux.c src/util/platform/FFPlatform_unix.c ) @@ -578,6 +580,7 @@ elseif(APPLE) src/detection/users/users_linux.c src/detection/wallpaper/wallpaper_apple.c src/detection/wifi/wifi_apple.m + src/detection/wmde/wmde_apple.c src/detection/wmtheme/wmtheme_apple.m src/util/apple/cf_helpers.c src/util/apple/osascript.m diff --git a/src/detection/displayserver/displayserver_apple.c b/src/detection/displayserver/displayserver_apple.c index 1bc386c52..cb1583270 100644 --- a/src/detection/displayserver/displayserver_apple.c +++ b/src/detection/displayserver/displayserver_apple.c @@ -1,10 +1,7 @@ #include "displayserver.h" -#include "common/sysctl.h" #include "util/apple/cf_helpers.h" -#include "util/mallocHelper.h" #include -#include #include #include #include @@ -70,35 +67,6 @@ static void detectDisplays(FFDisplayServerResult* ds) } } -static void detectWMPlugin(FFstrbuf* name) -{ - int request[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; - u_int requestLength = sizeof(request) / sizeof(*request); - - size_t length = 0; - FF_AUTO_FREE struct kinfo_proc* processes = ffSysctlGetData(request, requestLength, &length); - if(processes == NULL) - return; - - for(size_t i = 0; i < length / sizeof(struct kinfo_proc); i++) - { - const char* comm = processes[i].kp_proc.p_comm; - - if( - strcasecmp(comm, "spectacle") != 0 && - strcasecmp(comm, "amethyst") != 0 && - strcasecmp(comm, "kwm") != 0 && - strcasecmp(comm, "chunkwm") != 0 && - strcasecmp(comm, "yabai") != 0 && - strcasecmp(comm, "rectangle") != 0 - ) continue; - - ffStrbufAppendS(name, comm); - name->chars[0] = (char) toupper(name->chars[0]); - break; - } -} - void ffConnectDisplayServerImpl(FFDisplayServerResult* ds) { { @@ -116,15 +84,6 @@ void ffConnectDisplayServerImpl(FFDisplayServerResult* ds) } ffStrbufInit(&ds->wmProtocolName); - if(instance.config.general.allowSlowOperations) - { - FF_STRBUF_AUTO_DESTROY name; - ffStrbufInit(&name); - detectWMPlugin(&name); - if(name.length) - ffStrbufAppendF(&ds->wmPrettyName, " (with %s)", name.chars); - } - ffStrbufInit(&ds->deProcessName); ffStrbufInitStatic(&ds->dePrettyName, "Aqua"); ffStrbufInit(&ds->deVersion); diff --git a/src/detection/displayserver/displayserver_windows.c b/src/detection/displayserver/displayserver_windows.c index b4945436b..b8e2b54a8 100644 --- a/src/detection/displayserver/displayserver_windows.c +++ b/src/detection/displayserver/displayserver_windows.c @@ -143,15 +143,17 @@ static void detectDisplays(FFDisplayServerResult* ds) void ffConnectDisplayServerImpl(FFDisplayServerResult* ds) { BOOL enabled; - if(SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled == TRUE) + if(SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled) { ffStrbufInitStatic(&ds->wmProcessName, "dwm.exe"); ffStrbufInitStatic(&ds->wmPrettyName, "Desktop Window Manager"); } else { + // `explorer.exe` only provides a subset of WM functions, as well as the taskbar and desktop icons. + // While a window itself is drawn by kernel (GDI). Killing `explorer.exe` won't affect how windows are displayed generally. ffStrbufInitStatic(&ds->wmProcessName, "explorer.exe"); - ffStrbufInitStatic(&ds->wmPrettyName, "Windows Explorer"); + ffStrbufInitStatic(&ds->wmPrettyName, "Internal"); } ffStrbufInit(&ds->wmProtocolName); ffStrbufInit(&ds->deProcessName); diff --git a/src/detection/wmde/wmde.h b/src/detection/wmde/wmde.h new file mode 100644 index 000000000..2e944991e --- /dev/null +++ b/src/detection/wmde/wmde.h @@ -0,0 +1,6 @@ +#pragma once + +#include "fastfetch.h" + +const char* ffDetectWMPlugin(FFstrbuf* pluginName); +const char* ffDetectDEVersion(); diff --git a/src/detection/wmde/wmde_apple.c b/src/detection/wmde/wmde_apple.c new file mode 100644 index 000000000..feb7a611f --- /dev/null +++ b/src/detection/wmde/wmde_apple.c @@ -0,0 +1,42 @@ +#include "wmde.h" + +#include "common/sysctl.h" +#include "util/mallocHelper.h" + +#include + +const char* ffDetectWMPlugin(FFstrbuf* pluginName) +{ + int request[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; + u_int requestLength = sizeof(request) / sizeof(*request); + + size_t length = 0; + FF_AUTO_FREE struct kinfo_proc* processes = ffSysctlGetData(request, requestLength, &length); + if(processes == NULL) + return "sysctl(CTL_KERN, KERN_PROC, KERN_PROC_ALL) failed"; + + for(size_t i = 0; i < length / sizeof(struct kinfo_proc); i++) + { + const char* comm = processes[i].kp_proc.p_comm; + + if( + strcasecmp(comm, "spectacle") != 0 && + strcasecmp(comm, "amethyst") != 0 && + strcasecmp(comm, "kwm") != 0 && + strcasecmp(comm, "chunkwm") != 0 && + strcasecmp(comm, "yabai") != 0 && + strcasecmp(comm, "rectangle") != 0 + ) continue; + + ffStrbufAppendS(pluginName, comm); + pluginName->chars[0] = (char) toupper(pluginName->chars[0]); + break; + } + + return NULL; +} + +const char* ffDetectDEVersion() +{ + return "No support"; +} diff --git a/src/detection/wmde/wmde_linux.c b/src/detection/wmde/wmde_linux.c new file mode 100644 index 000000000..047fd5905 --- /dev/null +++ b/src/detection/wmde/wmde_linux.c @@ -0,0 +1,11 @@ +#include "wmde.h" + +const char* ffDetectWMPlugin(FFstrbuf* pluginName) +{ + return "No support"; +} + +const char* ffDetectDEVersion() +{ + +} diff --git a/src/detection/wmde/wmde_nosupport.c b/src/detection/wmde/wmde_nosupport.c new file mode 100644 index 000000000..047fd5905 --- /dev/null +++ b/src/detection/wmde/wmde_nosupport.c @@ -0,0 +1,11 @@ +#include "wmde.h" + +const char* ffDetectWMPlugin(FFstrbuf* pluginName) +{ + return "No support"; +} + +const char* ffDetectDEVersion() +{ + +} diff --git a/src/modules/wm/option.h b/src/modules/wm/option.h index 24c16f311..b38ab7c3a 100644 --- a/src/modules/wm/option.h +++ b/src/modules/wm/option.h @@ -8,4 +8,6 @@ typedef struct FFWMOptions { FFModuleBaseInfo moduleInfo; FFModuleArgs moduleArgs; + + bool detectPlugin; } FFWMOptions; diff --git a/src/modules/wm/wm.c b/src/modules/wm/wm.c index 374e981f6..6c1e185f3 100644 --- a/src/modules/wm/wm.c +++ b/src/modules/wm/wm.c @@ -1,6 +1,7 @@ #include "common/printing.h" #include "common/jsonconfig.h" #include "detection/displayserver/displayserver.h" +#include "detection/wmde/wmde.h" #include "modules/wm/wm.h" #include "util/stringUtils.h" @@ -16,6 +17,10 @@ void ffPrintWM(FFWMOptions* options) return; } + FF_STRBUF_AUTO_DESTROY pluginName = ffStrbufCreate(); + if(options->detectPlugin) + ffDetectWMPlugin(&pluginName); + if(options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(FF_WM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); @@ -29,6 +34,13 @@ void ffPrintWM(FFWMOptions* options) putchar(')'); } + if(pluginName.length > 0) + { + fputs(" (with ", stdout); + ffStrbufWriteTo(&pluginName, stdout); + putchar(')'); + } + putchar('\n'); } else @@ -48,6 +60,12 @@ bool ffParseWMCommandOptions(FFWMOptions* options, const char* key, const char* if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs)) return true; + if (ffStrEqualsIgnCase(subKey, "detect-plugin")) + { + options->detectPlugin = ffOptionParseBoolean(value); + return true; + } + return false; } @@ -64,6 +82,12 @@ void ffParseWMJsonObject(FFWMOptions* options, yyjson_val* module) if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) continue; + if (ffStrEqualsIgnCase(key, "detectPlugin")) + { + options->detectPlugin = yyjson_get_bool(val); + continue; + } + ffPrintError(FF_WM_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } @@ -74,6 +98,9 @@ void ffGenerateWMJsonConfig(FFWMOptions* options, yyjson_mut_doc* doc, yyjson_mu ffInitWMOptions(&defaultOptions); ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs); + + if (options->detectPlugin != defaultOptions.detectPlugin) + yyjson_mut_obj_add_bool(doc, module, "detectPlugin", options->detectPlugin); } void ffGenerateWMJsonResult(FF_MAYBE_UNUSED FFWMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)