From 26e5790a78182a810eec31fd70c4d6dd478ee9d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 19 Sep 2023 13:25:50 +0800 Subject: [PATCH] Brightness: report more detailed values --- src/detection/brightness/brightness.h | 2 +- src/detection/brightness/brightness_apple.c | 8 ++++++-- src/detection/brightness/brightness_bsd.c | 13 ++++++++----- src/detection/brightness/brightness_linux.c | 9 ++++++--- src/detection/brightness/brightness_windows.cpp | 8 ++++++-- src/modules/brightness/brightness.c | 17 ++++++++++++----- 6 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/detection/brightness/brightness.h b/src/detection/brightness/brightness.h index d65843947..58aeed20a 100644 --- a/src/detection/brightness/brightness.h +++ b/src/detection/brightness/brightness.h @@ -9,7 +9,7 @@ typedef struct FFBrightnessResult { FFstrbuf name; - float value; + double min, max, current; } FFBrightnessResult; const char* ffDetectBrightness(FFlist* result); // list of FFBrightnessResult diff --git a/src/detection/brightness/brightness_apple.c b/src/detection/brightness/brightness_apple.c index ecbe60478..a3b1d3c8f 100644 --- a/src/detection/brightness/brightness_apple.c +++ b/src/detection/brightness/brightness_apple.c @@ -20,7 +20,9 @@ static const char* detectWithDisplayServices(const FFDisplayServerResult* displa if(DisplayServicesGetBrightness((CGDirectDisplayID) display->id, &value) == kCGErrorSuccess) { FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); - brightness->value = value * 100; + brightness->current = value; + brightness->max = 1; + brightness->min = 0; ffStrbufInitCopy(&brightness->name, &display->name); } } @@ -90,7 +92,9 @@ FF_MAYBE_UNUSED static const char* detectWithDdcci(FFlist* result) uint32_t max = ((uint32_t) i2cOut[6] << 8u) + (uint32_t) i2cOut[7]; FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); - brightness->value = (float) current * 100.f / max; + brightness->max = max; + brightness->min = 0; + brightness->current = current; ffStrbufInit(&brightness->name); uint8_t edid[128] = {}; diff --git a/src/detection/brightness/brightness_bsd.c b/src/detection/brightness/brightness_bsd.c index 8c6a89e2f..72f1e5476 100644 --- a/src/detection/brightness/brightness_bsd.c +++ b/src/detection/brightness/brightness_bsd.c @@ -26,13 +26,16 @@ const char* ffDetectBrightness(FFlist* result) if(ioctl(blfd, BACKLIGHTGETSTATUS, &status) < 0) continue; - FFBrightnessResult* display = (FFBrightnessResult*) ffListAdd(result); - ffStrbufInit(&display->name); - display->value = (float) status.brightness / BACKLIGHTMAXLEVELS; + FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); + ffStrbufInit(&brightness->name); + + brightness->max = BACKLIGHTMAXLEVELS; + brightness->min = 0; + brightness->current = status.brightness; struct backlight_info info; if(ioctl(blfd, BACKLIGHTGETINFO, &info) < 0) - ffStrbufAppendS(&display->name, info.name); + ffStrbufAppendS(&brightness->name, info.name); } return NULL; } @@ -44,4 +47,4 @@ const char* ffDetectBrightness(FF_MAYBE_UNUSED FFlist* result) return "Backlight is supported only on FreeBSD 13 and newer"; } -#endif \ No newline at end of file +#endif diff --git a/src/detection/brightness/brightness_linux.c b/src/detection/brightness/brightness_linux.c index 8ab4e5f86..b3e75b234 100644 --- a/src/detection/brightness/brightness_linux.c +++ b/src/detection/brightness/brightness_linux.c @@ -72,8 +72,9 @@ static const char* detectWithBacklight(FFlist* result) } else ffStrbufInitS(&brightness->name, entry->d_name); - double maxBrightness = ffStrbufToDouble(&buffer); - brightness->value = (float) (actualBrightness * 100 / maxBrightness); + brightness->max = ffStrbufToDouble(&buffer); + brightness->min = 0; + brightness->current = actualBrightness; } } ffStrbufSubstrBefore(&backlightDir, backlightDirLength); @@ -123,7 +124,9 @@ static const char* detectWithDdcci(FFlist* result) ffddca_free_any_vcp_value(vcpValue); FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); - brightness->value = (float) current * 100.f / (float) max; + brightness->max = max; + brightness->min = 0; + brightness->current = current; ffStrbufInitS(&brightness->name, display->model_name); } ffddca_close_display(handle); diff --git a/src/detection/brightness/brightness_windows.cpp b/src/detection/brightness/brightness_windows.cpp index 28f489b96..06c169017 100644 --- a/src/detection/brightness/brightness_windows.cpp +++ b/src/detection/brightness/brightness_windows.cpp @@ -20,7 +20,9 @@ static const char* detectWithWmi(FFlist* result) if(FFWmiVariant vtValue = record.get(L"CurrentBrightness")) { FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); - brightness->value = vtValue.get(); + brightness->max = 100; + brightness->min = 0; + brightness->current = vtValue.get(); ffStrbufInit(&brightness->name); if (FFWmiVariant vtName = record.get(L"InstanceName")) @@ -51,7 +53,9 @@ static char* detectWithDdcci(const FFDisplayServerResult* displayServer, FFlist* else ffStrbufInitWS(&brightness->name, physicalMonitor.szPhysicalMonitorDescription); - brightness->value = (float) (curr - min) * 100.f / (float) (max - min); + brightness->max = max; + brightness->min = min; + brightness->current = curr; } } } diff --git a/src/modules/brightness/brightness.c b/src/modules/brightness/brightness.c index 120459245..261122bb0 100644 --- a/src/modules/brightness/brightness.c +++ b/src/modules/brightness/brightness.c @@ -5,7 +5,7 @@ #include "modules/brightness/brightness.h" #include "util/stringUtils.h" -#define FF_BRIGHTNESS_NUM_FORMAT_ARGS 2 +#define FF_BRIGHTNESS_NUM_FORMAT_ARGS 5 void ffPrintBrightness(FFBrightnessOptions* options) { @@ -43,6 +43,8 @@ void ffPrintBrightness(FFBrightnessOptions* options) }); } + const double percent = (item->current - item->min) / (item->max - item->min) * 100; + if(options->moduleArgs.outputFormat.length == 0) { FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate(); @@ -50,7 +52,7 @@ void ffPrintBrightness(FFBrightnessOptions* options) if (instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - ffAppendPercentBar(&str, item->value, 0, 100, 100); + ffAppendPercentBar(&str, percent, 0, 100, 100); } if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) @@ -58,7 +60,7 @@ void ffPrintBrightness(FFBrightnessOptions* options) if(str.length > 0) ffStrbufAppendC(&str, ' '); - ffAppendPercentNum(&str, item->value, 10, 10, str.length > 0); + ffAppendPercentNum(&str, percent, 10, 10, str.length > 0); } ffStrbufPutTo(&str, stdout); @@ -66,10 +68,13 @@ void ffPrintBrightness(FFBrightnessOptions* options) else { FF_STRBUF_AUTO_DESTROY valueStr = ffStrbufCreate(); - ffAppendPercentNum(&valueStr, item->value, 10, 10, false); + ffAppendPercentNum(&valueStr, percent, 10, 10, false); ffPrintFormatString(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, FF_BRIGHTNESS_NUM_FORMAT_ARGS, (FFformatarg[]) { {FF_FORMAT_ARG_TYPE_STRBUF, &valueStr}, {FF_FORMAT_ARG_TYPE_STRBUF, &item->name}, + {FF_FORMAT_ARG_TYPE_DOUBLE, &item->max}, + {FF_FORMAT_ARG_TYPE_DOUBLE, &item->min}, + {FF_FORMAT_ARG_TYPE_DOUBLE, &item->current}, }); } @@ -142,7 +147,9 @@ void ffGenerateBrightnessJson(FF_MAYBE_UNUSED FFBrightnessOptions* options, yyjs { yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr); yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name); - yyjson_mut_obj_add_real(doc, obj, "value", item->value); + yyjson_mut_obj_add_real(doc, obj, "max", item->max); + yyjson_mut_obj_add_real(doc, obj, "min", item->min); + yyjson_mut_obj_add_real(doc, obj, "current", item->current); } FF_LIST_FOR_EACH(FFBrightnessResult, item, result)