diff --git a/CHANGELOG.md b/CHANGELOG.md index 1867814d1..18ab8db19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Features: * Add `--cpu-freq-ndigits` to set number of digits for CPU frequency (CPU) * New module to detect physical disk I/O usage (DiskIO) * Add `--cpuusage-separate` to display CPU usage per CPU logical core +* Add `--brightness-ddcci-sleep` to set the sleep times (in ms) when sending DDC/CI requests (Brightness, #580) Bugfixes: * Fix possible crashes on Windows 7 (Disk, Windows) diff --git a/doc/json_schema.json b/doc/json_schema.json index 3c4b031e7..e309dee87 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -623,10 +623,6 @@ "const": "board", "description": "Print mather board name and other info" }, - { - "const": "brightness", - "description": "Print brightness of your monitors" - }, { "const": "chassis", "description": "Print chassis type (desktop, laptop, etc)" @@ -838,6 +834,22 @@ }, "additionalProperties": false }, + { + "title": "Brightness", + "properties": { + "type": { + "const": "brightness", + "description": "Print current brightness / luminance of your monitors" + }, + "ddcciSleep": { + "type": "integer", + "description": "Set the sleep times (in ms) when sending DDC/CI requests.\nSee for detail", + "minimum": 0, + "maximum": 400, + "default": 10 + } + } + }, { "title": "CPU", "properties": { diff --git a/src/data/help.txt b/src/data/help.txt index a9ea8aab6..a83a81b8e 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -137,6 +137,7 @@ Module specific options: --display-compact-type: : Set if all displays should be printed in one line. Default is none --display-detect-name: : Set if display name should be detected and printed (if supported). Default is false --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/ --cpu-temp : Detect and display CPU temperature if supported. Default is false diff --git a/src/detection/brightness/brightness.h b/src/detection/brightness/brightness.h index 58aeed20a..b32e5d15c 100644 --- a/src/detection/brightness/brightness.h +++ b/src/detection/brightness/brightness.h @@ -12,6 +12,6 @@ typedef struct FFBrightnessResult double min, max, current; } FFBrightnessResult; -const char* ffDetectBrightness(FFlist* result); // list of FFBrightnessResult +const char* ffDetectBrightness(FFBrightnessOptions* options, FFlist* result); // list of FFBrightnessResult #endif diff --git a/src/detection/brightness/brightness_apple.c b/src/detection/brightness/brightness_apple.c index 7a05ce87f..4c58b5e6a 100644 --- a/src/detection/brightness/brightness_apple.c +++ b/src/detection/brightness/brightness_apple.c @@ -52,7 +52,7 @@ static const char* detectWithDisplayServices(const FFDisplayServerResult* displa #ifdef __aarch64__ // https://github.com/waydabber/m1ddc // Works for Apple Silicon and USB-C adapter connection ( but not HTMI ) -static const char* detectWithDdcci(FF_MAYBE_UNUSED const FFDisplayServerResult* displayServer, FFlist* result) +static const char* detectWithDdcci(FF_MAYBE_UNUSED const FFDisplayServerResult* displayServer, FFBrightnessOptions* options, FFlist* result) { if (!IOAVServiceCreate || !IOAVServiceReadI2C) return "IOAVService is not available"; @@ -93,7 +93,7 @@ static const char* detectWithDdcci(FF_MAYBE_UNUSED const FFDisplayServerResult* for (uint32_t i = 0; i < 2; ++i) { IOAVServiceWriteI2C(service, 0x37, 0x51, i2cIn, sizeof(i2cIn)); - usleep(10000); + usleep(options->ddcciSleep * 1000); } } @@ -121,7 +121,7 @@ static const char* detectWithDdcci(FF_MAYBE_UNUSED const FFDisplayServerResult* return NULL; } #else -static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, FFlist* result) +static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, FFBrightnessOptions* options, FFlist* result) { if (!CGSServiceForDisplayNumber) return "CGSServiceForDisplayNumber is not available"; @@ -156,7 +156,7 @@ static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, F .sendTransactionType = kIOI2CSimpleTransactionType, .sendBuffer = (vm_address_t) i2cIn, .sendBytes = sizeof(i2cIn) / sizeof(i2cIn[0]), - .minReplyDelay = 10, + .minReplyDelay = options->ddcciSleep, .replyAddress = 0x6F, .replySubAddress = 0x51, .replyTransactionType = kIOI2CDDCciReplyTransactionType, @@ -186,14 +186,14 @@ static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, F } #endif -const char* ffDetectBrightness(FFlist* result) +const char* ffDetectBrightness(FFBrightnessOptions* options, FFlist* result) { const FFDisplayServerResult* displayServer = ffConnectDisplayServer(); detectWithDisplayServices(displayServer, result); if (displayServer->displays.length > result->length) - detectWithDdcci(displayServer, result); + detectWithDdcci(displayServer, options, result); return NULL; } diff --git a/src/detection/brightness/brightness_bsd.c b/src/detection/brightness/brightness_bsd.c index 72f1e5476..71bf8c910 100644 --- a/src/detection/brightness/brightness_bsd.c +++ b/src/detection/brightness/brightness_bsd.c @@ -9,7 +9,7 @@ #include #include -const char* ffDetectBrightness(FFlist* result) +const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FFlist* result) { // https://man.freebsd.org/cgi/man.cgi?query=backlight&sektion=9 char path[] = "/dev/backlight/backlight0"; @@ -42,7 +42,7 @@ const char* ffDetectBrightness(FFlist* result) #else -const char* ffDetectBrightness(FF_MAYBE_UNUSED FFlist* result) +const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FF_MAYBE_UNUSED FFlist* result) { return "Backlight is supported only on FreeBSD 13 and newer"; } diff --git a/src/detection/brightness/brightness_linux.c b/src/detection/brightness/brightness_linux.c index d46599a85..85713f225 100644 --- a/src/detection/brightness/brightness_linux.c +++ b/src/detection/brightness/brightness_linux.c @@ -92,7 +92,7 @@ static const char* detectWithBacklight(FFlist* result) #include -static const char* detectWithDdcci(FFlist* result) +static const char* detectWithDdcci(FFBrightnessOptions* options, FFlist* result) { FF_LIBRARY_LOAD(libddcutil, &instance.config.libDdcutil, "dlopen ddcutil failed", "libddcutil" FF_LIBRARY_EXTENSION, 5); FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libddcutil, ddca_get_display_info_list2) @@ -100,8 +100,11 @@ static const char* detectWithDdcci(FFlist* result) FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libddcutil, ddca_get_any_vcp_value_using_explicit_type) FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libddcutil, ddca_free_any_vcp_value) FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libddcutil, ddca_close_display) + FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libddcutil, ddca_set_default_sleep_multiplier) libddcutil = NULL; // Don't dlclose libddcutil. See https://github.com/rockowitz/ddcutil/issues/330 + ffddca_set_default_sleep_multiplier(options->ddcciSleep / 40.0); + FF_AUTO_FREE DDCA_Display_Info_List* infoList = NULL; if (__builtin_expect(ffddca_get_display_info_list2(false, &infoList) < 0, 0)) return "ddca_get_display_info_list2(false, &infoList) failed"; @@ -137,14 +140,14 @@ static const char* detectWithDdcci(FFlist* result) } #endif -const char* ffDetectBrightness(FFlist* result) +const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FFlist* result) { detectWithBacklight(result); #ifdef FF_HAVE_DDCUTIL const FFDisplayServerResult* displayServer = ffConnectDisplayServer(); if (result->length < displayServer->displays.length) - detectWithDdcci(result); + detectWithDdcci(options, result); #endif return NULL; diff --git a/src/detection/brightness/brightness_nosupport.c b/src/detection/brightness/brightness_nosupport.c index 4bd83c4de..3ad99ed33 100644 --- a/src/detection/brightness/brightness_nosupport.c +++ b/src/detection/brightness/brightness_nosupport.c @@ -1,6 +1,6 @@ #include "brightness.h" -const char* ffDetectBrightness(FF_MAYBE_UNUSED FFlist* result) +const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FF_MAYBE_UNUSED FFlist* result) { return "Not supported on this platform"; } diff --git a/src/detection/brightness/brightness_windows.cpp b/src/detection/brightness/brightness_windows.cpp index 06c169017..01e3c0314 100644 --- a/src/detection/brightness/brightness_windows.cpp +++ b/src/detection/brightness/brightness_windows.cpp @@ -73,7 +73,7 @@ static bool hasBuiltinDisplay(const FFDisplayServerResult* displayServer) } extern "C" -const char* ffDetectBrightness(FFlist* result) +const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FFlist* result) { const FFDisplayServerResult* displayServer = ffConnectDisplayServer(); diff --git a/src/modules/brightness/brightness.c b/src/modules/brightness/brightness.c index f65c30d21..eaedf10a9 100644 --- a/src/modules/brightness/brightness.c +++ b/src/modules/brightness/brightness.c @@ -11,7 +11,7 @@ void ffPrintBrightness(FFBrightnessOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFBrightnessResult)); - const char* error = ffDetectBrightness(&result); + const char* error = ffDetectBrightness(options, &result); if(error) { @@ -88,6 +88,8 @@ void ffInitBrightnessOptions(FFBrightnessOptions* options) { ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_BRIGHTNESS_MODULE_NAME, ffParseBrightnessCommandOptions, ffParseBrightnessJsonObject, ffPrintBrightness, ffGenerateBrightnessJson, ffPrintBrightnessHelpFormat); ffOptionInitModuleArg(&options->moduleArgs); + + options->ddcciSleep = 10; } bool ffParseBrightnessCommandOptions(FFBrightnessOptions* options, const char* key, const char* value) @@ -97,6 +99,12 @@ bool ffParseBrightnessCommandOptions(FFBrightnessOptions* options, const char* k if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs)) return true; + if (ffStrEqualsIgnCase(key, "ddcci-sleep")) + { + options->ddcciSleep = ffOptionParseUInt32(key, value); + return true; + } + return false; } @@ -118,6 +126,12 @@ void ffParseBrightnessJsonObject(FFBrightnessOptions* options, yyjson_val* modul if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) continue; + if (ffStrEqualsIgnCase(key, "ddcciSleep")) + { + options->ddcciSleep = (uint32_t) yyjson_get_uint(val); + continue; + } + ffPrintError(FF_BRIGHTNESS_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } @@ -126,7 +140,7 @@ void ffGenerateBrightnessJson(FF_MAYBE_UNUSED FFBrightnessOptions* options, yyjs { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFBrightnessResult)); - const char* error = ffDetectBrightness(&result); + const char* error = ffDetectBrightness(options, &result); if (error) { diff --git a/src/modules/brightness/option.h b/src/modules/brightness/option.h index aa67ac190..cad3e7ac8 100644 --- a/src/modules/brightness/option.h +++ b/src/modules/brightness/option.h @@ -8,4 +8,6 @@ typedef struct FFBrightnessOptions { FFModuleBaseInfo moduleInfo; FFModuleArgs moduleArgs; + + uint32_t ddcciSleep; // ms } FFBrightnessOptions;