From b46200628006970eaa0e7958d1f83c9b1a628a3d 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 14:31:19 +0800 Subject: [PATCH] Chassis (Windows): add flag `--chassis-use-wmi` --- CHANGELOG.md | 4 +++- doc/json_schema.json | 18 ++++++++++---- src/data/help.txt | 1 + src/detection/chassis/chassis.h | 2 +- src/detection/chassis/chassis_bsd.c | 2 +- src/detection/chassis/chassis_linux.c | 2 +- src/detection/chassis/chassis_nosupport.c | 2 +- src/detection/chassis/chassis_windows.cpp | 6 ++--- src/modules/chassis/chassis.c | 29 +++++++++++++++++++++-- src/modules/chassis/option.h | 4 ++++ 10 files changed, 56 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c0204ab6..adb0cb2b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ Changes: * Display keys `percent*` and `size*` in JSON config are restructured. `{ "sizeNdigits": 1 }` is now `{ "size": { "ndigits": 1 } }` * With the introduction of `--migrate-config`, the old flag based config file is deprecated, and will be removed in 3.0.0 (next major version) * Support of `--gen-config conf` is deprecated accordingly, and will be removed in 2.3.0 (next minor version) -* A new flag `--packages-winget` is introduced to control whether `winget` packages count should be detected. It was controlled by `--allow-slow-operations` previously. Note it's a very slow operation, please enable it with caution. +* The global flag `--allow-slow-operations` is splitted into some explicit flags in differnet modules + * `--packages-winget`: control whether `winget` packages count should be detected. Note it's a very slow operation, please enable it with caution. + * `--chassis-use-wmi`: control whether `WMI` query should be used to detect chassis type, which detects more information, but slower. This flag only affects `--chassis-format` and `--format json`. Features: * Quirks for MIPS platforms (CPU, Linux) diff --git a/doc/json_schema.json b/doc/json_schema.json index dd82f1903..ee84616f0 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -644,10 +644,6 @@ "const": "board", "description": "Print mather board name and other info" }, - { - "const": "chassis", - "description": "Print chassis type (desktop, laptop, etc)" - }, { "const": "cursor", "description": "Print cursor style name" @@ -867,6 +863,20 @@ } } }, + { + "title": "Chassis", + "properties": { + "type": { + "const": "chassis", + "description": "Print chassis type (desktop, laptop, etc)" + }, + "useWmi": { + "description": "Set if WMI query should be used on Windows, which detects more information but slower", + "type": "boolean", + "default": false + } + } + }, { "title": "CPU", "properties": { diff --git a/src/data/help.txt b/src/data/help.txt index 2e8b91693..cf2c48760 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -125,6 +125,7 @@ Module specific options: --title-color-user : Set color of the user name (left part). Default is empty (use color of `--color-title`). --title-color-at : Set color of the @ symbol (middle part). Default is empty (use color of `--color-title`) --title-color-host : Set color of the host name (right part). Default is empty (use color of `--color-title`) + --chassis-use-wmi : Set if WMI query should be used on Windows, which detects more information but slower. Default is false --separator-string : Set the string printed by the separator module --os-file : Set the path to the file containing OS information. Linux only --disk-folders : A colon (semicolon on Windows) separated list of folder paths for the disk output. Default is "/:/home" ("C:\\;D:\\ ..." on Windows) diff --git a/src/detection/chassis/chassis.h b/src/detection/chassis/chassis.h index 876936c8d..534141666 100644 --- a/src/detection/chassis/chassis.h +++ b/src/detection/chassis/chassis.h @@ -12,7 +12,7 @@ typedef struct FFChassisResult FFstrbuf version; } FFChassisResult; -const char* ffDetectChassis(FFChassisResult* result); +const char* ffDetectChassis(FFChassisResult* result, FFChassisOptions* options); const char* ffChassisTypeToString(uint32_t type); #endif diff --git a/src/detection/chassis/chassis_bsd.c b/src/detection/chassis/chassis_bsd.c index c38b562c2..54a84c7e9 100644 --- a/src/detection/chassis/chassis_bsd.c +++ b/src/detection/chassis/chassis_bsd.c @@ -2,7 +2,7 @@ #include "common/settings.h" #include "util/smbiosHelper.h" -const char* ffDetectChassis(FFChassisResult* result) +const char* ffDetectChassis(FFChassisResult* result, FF_MAYBE_UNUSED FFChassisOptions* options) { // Unlike other platforms, `smbios.chassis.type` return display string directly on my machine ffSettingsGetFreeBSDKenv("smbios.chassis.type", &result->type); diff --git a/src/detection/chassis/chassis_linux.c b/src/detection/chassis/chassis_linux.c index 074753eb6..e8ce65c82 100644 --- a/src/detection/chassis/chassis_linux.c +++ b/src/detection/chassis/chassis_linux.c @@ -17,7 +17,7 @@ static void getSmbiosValue(const char* devicesPath, const char* classPath, FFstr ffStrbufClear(buffer); } -const char* ffDetectChassis(FFChassisResult* result) +const char* ffDetectChassis(FFChassisResult* result, FF_MAYBE_UNUSED FFChassisOptions* options) { getSmbiosValue("/sys/devices/virtual/dmi/id/chassis_type", "/sys/class/dmi/id/chassis_type", &result->type); getSmbiosValue("/sys/devices/virtual/dmi/id/chassis_vendor", "/sys/class/dmi/id/chassis_vendor", &result->vendor); diff --git a/src/detection/chassis/chassis_nosupport.c b/src/detection/chassis/chassis_nosupport.c index c2acd7579..6d83b90d1 100644 --- a/src/detection/chassis/chassis_nosupport.c +++ b/src/detection/chassis/chassis_nosupport.c @@ -1,6 +1,6 @@ #include "chassis.h" -const char* ffDetectChassis(FF_MAYBE_UNUSED FFChassisResult* result) +const char* ffDetectChassis(FF_MAYBE_UNUSED FFChassisResult* result, FF_MAYBE_UNUSED FFChassisOptions* options) { return "Not supported on this platform"; } diff --git a/src/detection/chassis/chassis_windows.cpp b/src/detection/chassis/chassis_windows.cpp index 05fe63ad1..9a3d37fcc 100644 --- a/src/detection/chassis/chassis_windows.cpp +++ b/src/detection/chassis/chassis_windows.cpp @@ -36,7 +36,7 @@ FF_MAYBE_UNUSED static const char* detectWithWmi(FFChassisResult* result) { auto [arr, len] = vtProp.get>(); if(len == 0) - return "ChassisTypes contain no data failed"; + return "ChassisTypes contain no data"; for (uint32_t i = 0; i < len; ++i) { if (i > 0) @@ -65,9 +65,9 @@ FF_MAYBE_UNUSED static const char* detectWithWmi(FFChassisResult* result) } extern "C" -const char* ffDetectChassis(FFChassisResult* result) +const char* ffDetectChassis(FFChassisResult* result, FFChassisOptions* options) { - if (instance.config.general.allowSlowOperations) + if (options->useWmi) return detectWithWmi(result); return detectWithRegistry(result); } diff --git a/src/modules/chassis/chassis.c b/src/modules/chassis/chassis.c index bd1b0c208..f4ca8af96 100644 --- a/src/modules/chassis/chassis.c +++ b/src/modules/chassis/chassis.c @@ -13,7 +13,7 @@ void ffPrintChassis(FFChassisOptions* options) ffStrbufInit(&result.vendor); ffStrbufInit(&result.version); - const char* error = ffDetectChassis(&result); + const char* error = ffDetectChassis(&result, options); if(error) { @@ -57,6 +57,14 @@ bool ffParseChassisCommandOptions(FFChassisOptions* options, const char* key, co if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs)) return true; + #ifdef _WIN32 + if (ffStrEqualsIgnCase(subKey, "use-wmi")) + { + options->useWmi = ffOptionParseBoolean(value); + return true; + } + #endif + return false; } @@ -73,6 +81,14 @@ void ffParseChassisJsonObject(FFChassisOptions* options, yyjson_val* module) if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) continue; + #ifdef _WIN32 + if (ffStrEqualsIgnCase(key, "useWmi")) + { + options->useWmi = yyjson_get_bool(val); + continue; + } + #endif + ffPrintError(FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } @@ -83,6 +99,11 @@ void ffGenerateChassisJsonConfig(FFChassisOptions* options, yyjson_mut_doc* doc, ffInitChassisOptions(&defaultOptions); ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs); + + #ifdef _WIN32 + if (options->useWmi != defaultOptions.useWmi) + yyjson_mut_obj_add_bool(doc, module, "useWmi", options->useWmi); + #endif } void ffGenerateChassisJsonResult(FF_MAYBE_UNUSED FFChassisOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) @@ -92,7 +113,7 @@ void ffGenerateChassisJsonResult(FF_MAYBE_UNUSED FFChassisOptions* options, yyjs ffStrbufInit(&result.vendor); ffStrbufInit(&result.version); - const char* error = ffDetectChassis(&result); + const char* error = ffDetectChassis(&result, options); if (error) { @@ -139,6 +160,10 @@ void ffInitChassisOptions(FFChassisOptions* options) ffGenerateChassisJsonConfig ); ffOptionInitModuleArg(&options->moduleArgs); + + #ifdef _WIN32 + options->useWmi = false; + #endif } void ffDestroyChassisOptions(FFChassisOptions* options) diff --git a/src/modules/chassis/option.h b/src/modules/chassis/option.h index de6338cd5..25d7cee65 100644 --- a/src/modules/chassis/option.h +++ b/src/modules/chassis/option.h @@ -8,4 +8,8 @@ typedef struct FFChassisOptions { FFModuleBaseInfo moduleInfo; FFModuleArgs moduleArgs; + + #ifdef _WIN32 + bool useWmi; + #endif } FFChassisOptions;