From ae3420824720e8e7a2dc8007f5bf42fcc0669467 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:49:00 +0800 Subject: [PATCH] Battery (Windows): add flag `--battery-use-setup-api` --- CHANGELOG.md | 1 + doc/json_schema.json | 5 +++++ src/data/help.txt | 3 ++- src/detection/battery/battery_windows.c | 2 +- src/modules/battery/battery.c | 23 +++++++++++++++++++++++ src/modules/battery/option.h | 2 ++ 6 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adb0cb2b9..6b0ca4aae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Changes: * 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`. + * `--battery-use-setup-api`: control whether `SetupAPI` should be used on Windows to detect battery info, which supports multi batteries, but slower. Features: * Quirks for MIPS platforms (CPU, Linux) diff --git a/doc/json_schema.json b/doc/json_schema.json index ee84616f0..bed20cbe5 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -800,6 +800,11 @@ "title": "The directory where the battery folders are. Standard: `/sys/class/power_supply/`. Linux only", "type": "string" }, + "useSetupApi": { + "title": "Set if `SetupAPI` should be used on Windows to detect battery info, which supports multi batteries, but slower. Windows only", + "type": "boolean", + "default": false + }, "temp": { "title": "Detect and display Battery temperature if supported", "type": "boolean", diff --git a/src/data/help.txt b/src/data/help.txt index cf2c48760..dd6ff6de7 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -144,7 +144,8 @@ Module specific options: --display-precise-refresh-rate: :Set if decimal refresh rates should not be rounded into integers when printing. Default is true --brightness-ddcci-sleep: Set the sleep times (in ms) when sending DDC/CI requests. See for detail. Default is 10 --sound-type: : Set what type of sound devices should be printed. Should be either main, active or all. Default is main - --battery-dir : The directory where the battery folders are. Standard: /sys/class/power_supply/ + --battery-dir : The directory where the battery folders are. Standard: `/sys/class/power_supply/`. Linux only + --battery-use-setup-api : Set if `SetupAPI` should be used on Windows to detect battery info, which supports multi batteries, but slower. Windows only --cpu-temp : Detect and display CPU temperature if supported. Default is false --cpu-freq-ndigits : Set the number of digits to keep after the decimal point when printing CPU frequency. Default is 2 --cpuusage-separate : Display CPU usage per CPU logical core, instead of an average result. Default is false diff --git a/src/detection/battery/battery_windows.c b/src/detection/battery/battery_windows.c index e75baccbc..ea30df791 100644 --- a/src/detection/battery/battery_windows.c +++ b/src/detection/battery/battery_windows.c @@ -32,7 +32,7 @@ static inline void wrapSetupDiDestroyDeviceInfoList(HDEVINFO* hdev) const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results) { - if(instance.config.general.allowSlowOperations) + if(options->useSetupApi) { //https://learn.microsoft.com/en-us/windows/win32/power/enumerating-battery-devices HDEVINFO hdev __attribute__((__cleanup__(wrapSetupDiDestroyDeviceInfoList))) = diff --git a/src/modules/battery/battery.c b/src/modules/battery/battery.c index 7318289fd..afa0e2e60 100644 --- a/src/modules/battery/battery.c +++ b/src/modules/battery/battery.c @@ -122,6 +122,14 @@ bool ffParseBatteryCommandOptions(FFBatteryOptions* options, const char* key, co } #endif + #ifdef _WIN32 + if (ffStrEqualsIgnCase(subKey, "use-setup-api")) + { + options->useSetupApi = ffOptionParseBoolean(value); + return true; + } + #endif + return false; } @@ -146,6 +154,14 @@ void ffParseBatteryJsonObject(FFBatteryOptions* options, yyjson_val* module) } #endif + #ifdef _WIN32 + if (ffStrEqualsIgnCase(key, "useSetupApi")) + { + options->useSetupApi = yyjson_get_bool(val); + continue; + } + #endif + if (ffStrEqualsIgnCase(key, "temp")) { options->temp = yyjson_get_bool(val); @@ -168,6 +184,11 @@ void ffGenerateBatteryJsonConfig(FFBatteryOptions* options, yyjson_mut_doc* doc, yyjson_mut_obj_add_strbuf(doc, module, "dir", &options->dir); #endif + #ifdef _WIN32 + if (defaultOptions.useSetupApi != options->useSetupApi) + yyjson_mut_obj_add_bool(doc, module, "useSetupApi", options->useSetupApi); + #endif + if (options->temp != defaultOptions.temp) yyjson_mut_obj_add_bool(doc, module, "temp", options->temp); } @@ -233,6 +254,8 @@ void ffInitBatteryOptions(FFBatteryOptions* options) #ifdef __linux__ ffStrbufInit(&options->dir); + #elif defined(_WIN32) + options->useSetupApi = false; #endif } diff --git a/src/modules/battery/option.h b/src/modules/battery/option.h index 51bbb0c01..89f05bab4 100644 --- a/src/modules/battery/option.h +++ b/src/modules/battery/option.h @@ -13,5 +13,7 @@ typedef struct FFBatteryOptions #ifdef __linux__ FFstrbuf dir; + #elif defined(_WIN32) + bool useSetupApi; #endif } FFBatteryOptions;