Skip to content

Commit e9c2a43

Browse files
committed
Brightness: add --brightness-ddcci-sleep to set the sleep times (in ms) when sending DDC/CI requests
Fix #580
1 parent af94596 commit e9c2a43

File tree

11 files changed

+53
-20
lines changed

11 files changed

+53
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Features:
66
* Add `--cpu-freq-ndigits` to set number of digits for CPU frequency (CPU)
77
* New module to detect physical disk I/O usage (DiskIO)
88
* Add `--cpuusage-separate` to display CPU usage per CPU logical core
9+
* Add `--brightness-ddcci-sleep` to set the sleep times (in ms) when sending DDC/CI requests (Brightness, #580)
910

1011
Bugfixes:
1112
* Fix possible crashes on Windows 7 (Disk, Windows)

doc/json_schema.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,10 +623,6 @@
623623
"const": "board",
624624
"description": "Print mather board name and other info"
625625
},
626-
{
627-
"const": "brightness",
628-
"description": "Print brightness of your monitors"
629-
},
630626
{
631627
"const": "chassis",
632628
"description": "Print chassis type (desktop, laptop, etc)"
@@ -838,6 +834,22 @@
838834
},
839835
"additionalProperties": false
840836
},
837+
{
838+
"title": "Brightness",
839+
"properties": {
840+
"type": {
841+
"const": "brightness",
842+
"description": "Print current brightness / luminance of your monitors"
843+
},
844+
"ddcciSleep": {
845+
"type": "integer",
846+
"description": "Set the sleep times (in ms) when sending DDC/CI requests.\nSee <https://www.ddcutil.com/performance_options/#option-sleep-multiplier> for detail",
847+
"minimum": 0,
848+
"maximum": 400,
849+
"default": 10
850+
}
851+
}
852+
},
841853
{
842854
"title": "CPU",
843855
"properties": {

src/data/help.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ Module specific options:
137137
--display-compact-type: <?string>: Set if all displays should be printed in one line. Default is none
138138
--display-detect-name: <?value>: Set if display name should be detected and printed (if supported). Default is false
139139
--display-precise-refresh-rate: <?value>:Set if decimal refresh rates should not be rounded into integers when printing. Default is true
140+
--brightness-ddcci-sleep: <num> Set the sleep times (in ms) when sending DDC/CI requests. See <https://www.ddcutil.com/performance_options/#option-sleep-multiplier> for detail. Default is 10
140141
--sound-type: <value>: Set what type of sound devices should be printed. Should be either main, active or all. Default is main
141142
--battery-dir <folder>: The directory where the battery folders are. Standard: /sys/class/power_supply/
142143
--cpu-temp <?value>: Detect and display CPU temperature if supported. Default is false

src/detection/brightness/brightness.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ typedef struct FFBrightnessResult
1212
double min, max, current;
1313
} FFBrightnessResult;
1414

15-
const char* ffDetectBrightness(FFlist* result); // list of FFBrightnessResult
15+
const char* ffDetectBrightness(FFBrightnessOptions* options, FFlist* result); // list of FFBrightnessResult
1616

1717
#endif

src/detection/brightness/brightness_apple.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static const char* detectWithDisplayServices(const FFDisplayServerResult* displa
5252
#ifdef __aarch64__
5353
// https://github.com/waydabber/m1ddc
5454
// Works for Apple Silicon and USB-C adapter connection ( but not HTMI )
55-
static const char* detectWithDdcci(FF_MAYBE_UNUSED const FFDisplayServerResult* displayServer, FFlist* result)
55+
static const char* detectWithDdcci(FF_MAYBE_UNUSED const FFDisplayServerResult* displayServer, FFBrightnessOptions* options, FFlist* result)
5656
{
5757
if (!IOAVServiceCreate || !IOAVServiceReadI2C)
5858
return "IOAVService is not available";
@@ -93,7 +93,7 @@ static const char* detectWithDdcci(FF_MAYBE_UNUSED const FFDisplayServerResult*
9393
for (uint32_t i = 0; i < 2; ++i)
9494
{
9595
IOAVServiceWriteI2C(service, 0x37, 0x51, i2cIn, sizeof(i2cIn));
96-
usleep(10000);
96+
usleep(options->ddcciSleep * 1000);
9797
}
9898
}
9999

@@ -121,7 +121,7 @@ static const char* detectWithDdcci(FF_MAYBE_UNUSED const FFDisplayServerResult*
121121
return NULL;
122122
}
123123
#else
124-
static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, FFlist* result)
124+
static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, FFBrightnessOptions* options, FFlist* result)
125125
{
126126
if (!CGSServiceForDisplayNumber) return "CGSServiceForDisplayNumber is not available";
127127

@@ -156,7 +156,7 @@ static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, F
156156
.sendTransactionType = kIOI2CSimpleTransactionType,
157157
.sendBuffer = (vm_address_t) i2cIn,
158158
.sendBytes = sizeof(i2cIn) / sizeof(i2cIn[0]),
159-
.minReplyDelay = 10,
159+
.minReplyDelay = options->ddcciSleep,
160160
.replyAddress = 0x6F,
161161
.replySubAddress = 0x51,
162162
.replyTransactionType = kIOI2CDDCciReplyTransactionType,
@@ -186,14 +186,14 @@ static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, F
186186
}
187187
#endif
188188

189-
const char* ffDetectBrightness(FFlist* result)
189+
const char* ffDetectBrightness(FFBrightnessOptions* options, FFlist* result)
190190
{
191191
const FFDisplayServerResult* displayServer = ffConnectDisplayServer();
192192

193193
detectWithDisplayServices(displayServer, result);
194194

195195
if (displayServer->displays.length > result->length)
196-
detectWithDdcci(displayServer, result);
196+
detectWithDdcci(displayServer, options, result);
197197

198198
return NULL;
199199
}

src/detection/brightness/brightness_bsd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <sys/fcntl.h>
1010
#include <unistd.h>
1111

12-
const char* ffDetectBrightness(FFlist* result)
12+
const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FFlist* result)
1313
{
1414
// https://man.freebsd.org/cgi/man.cgi?query=backlight&sektion=9
1515
char path[] = "/dev/backlight/backlight0";
@@ -42,7 +42,7 @@ const char* ffDetectBrightness(FFlist* result)
4242

4343
#else
4444

45-
const char* ffDetectBrightness(FF_MAYBE_UNUSED FFlist* result)
45+
const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FF_MAYBE_UNUSED FFlist* result)
4646
{
4747
return "Backlight is supported only on FreeBSD 13 and newer";
4848
}

src/detection/brightness/brightness_linux.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,19 @@ static const char* detectWithBacklight(FFlist* result)
9292

9393
#include <ddcutil_c_api.h>
9494

95-
static const char* detectWithDdcci(FFlist* result)
95+
static const char* detectWithDdcci(FFBrightnessOptions* options, FFlist* result)
9696
{
9797
FF_LIBRARY_LOAD(libddcutil, &instance.config.libDdcutil, "dlopen ddcutil failed", "libddcutil" FF_LIBRARY_EXTENSION, 5);
9898
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libddcutil, ddca_get_display_info_list2)
9999
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libddcutil, ddca_open_display2)
100100
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libddcutil, ddca_get_any_vcp_value_using_explicit_type)
101101
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libddcutil, ddca_free_any_vcp_value)
102102
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libddcutil, ddca_close_display)
103+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libddcutil, ddca_set_default_sleep_multiplier)
103104
libddcutil = NULL; // Don't dlclose libddcutil. See https://github.com/rockowitz/ddcutil/issues/330
104105

106+
ffddca_set_default_sleep_multiplier(options->ddcciSleep / 40.0);
107+
105108
FF_AUTO_FREE DDCA_Display_Info_List* infoList = NULL;
106109
if (__builtin_expect(ffddca_get_display_info_list2(false, &infoList) < 0, 0))
107110
return "ddca_get_display_info_list2(false, &infoList) failed";
@@ -137,14 +140,14 @@ static const char* detectWithDdcci(FFlist* result)
137140
}
138141
#endif
139142

140-
const char* ffDetectBrightness(FFlist* result)
143+
const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FFlist* result)
141144
{
142145
detectWithBacklight(result);
143146

144147
#ifdef FF_HAVE_DDCUTIL
145148
const FFDisplayServerResult* displayServer = ffConnectDisplayServer();
146149
if (result->length < displayServer->displays.length)
147-
detectWithDdcci(result);
150+
detectWithDdcci(options, result);
148151
#endif
149152

150153
return NULL;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "brightness.h"
22

3-
const char* ffDetectBrightness(FF_MAYBE_UNUSED FFlist* result)
3+
const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FF_MAYBE_UNUSED FFlist* result)
44
{
55
return "Not supported on this platform";
66
}

src/detection/brightness/brightness_windows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static bool hasBuiltinDisplay(const FFDisplayServerResult* displayServer)
7373
}
7474

7575
extern "C"
76-
const char* ffDetectBrightness(FFlist* result)
76+
const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FFlist* result)
7777
{
7878
const FFDisplayServerResult* displayServer = ffConnectDisplayServer();
7979

src/modules/brightness/brightness.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void ffPrintBrightness(FFBrightnessOptions* options)
1111
{
1212
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFBrightnessResult));
1313

14-
const char* error = ffDetectBrightness(&result);
14+
const char* error = ffDetectBrightness(options, &result);
1515

1616
if(error)
1717
{
@@ -88,6 +88,8 @@ void ffInitBrightnessOptions(FFBrightnessOptions* options)
8888
{
8989
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_BRIGHTNESS_MODULE_NAME, ffParseBrightnessCommandOptions, ffParseBrightnessJsonObject, ffPrintBrightness, ffGenerateBrightnessJson, ffPrintBrightnessHelpFormat);
9090
ffOptionInitModuleArg(&options->moduleArgs);
91+
92+
options->ddcciSleep = 10;
9193
}
9294

9395
bool ffParseBrightnessCommandOptions(FFBrightnessOptions* options, const char* key, const char* value)
@@ -97,6 +99,12 @@ bool ffParseBrightnessCommandOptions(FFBrightnessOptions* options, const char* k
9799
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
98100
return true;
99101

102+
if (ffStrEqualsIgnCase(key, "ddcci-sleep"))
103+
{
104+
options->ddcciSleep = ffOptionParseUInt32(key, value);
105+
return true;
106+
}
107+
100108
return false;
101109
}
102110

@@ -118,6 +126,12 @@ void ffParseBrightnessJsonObject(FFBrightnessOptions* options, yyjson_val* modul
118126
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
119127
continue;
120128

129+
if (ffStrEqualsIgnCase(key, "ddcciSleep"))
130+
{
131+
options->ddcciSleep = (uint32_t) yyjson_get_uint(val);
132+
continue;
133+
}
134+
121135
ffPrintError(FF_BRIGHTNESS_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
122136
}
123137
}
@@ -126,7 +140,7 @@ void ffGenerateBrightnessJson(FF_MAYBE_UNUSED FFBrightnessOptions* options, yyjs
126140
{
127141
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFBrightnessResult));
128142

129-
const char* error = ffDetectBrightness(&result);
143+
const char* error = ffDetectBrightness(options, &result);
130144

131145
if (error)
132146
{

0 commit comments

Comments
 (0)