backlight: supprt ID_LEDS_CLAMP udev property for leds subsystem devices

Closes #30507.
This commit is contained in:
Yu Watanabe
2024-01-03 09:30:41 +09:00
parent e0feaedbd9
commit 778823fd52
2 changed files with 37 additions and 20 deletions
+16 -7
View File
@@ -31,13 +31,22 @@
<refsect1>
<title>Description</title>
<para><filename>systemd-backlight@.service</filename> is a service that restores the display backlight
brightness at early boot and saves it at shutdown. On disk, the backlight brightness is stored in
<filename>/var/lib/systemd/backlight/</filename>. During loading, if the udev property
<option>ID_BACKLIGHT_CLAMP</option> is not set to false, the brightness is clamped to a value of at least
1 or 5% of maximum brightness, whichever is greater. The percentage can be adjusted by specifying a
percentage (needs to be suffixed with <literal>%</literal>, e.g. <literal>30%</literal>) to the property
<option>ID_BACKLIGHT_CLAMP</option>.</para>
<para><filename>systemd-backlight@.service</filename> is a service that restores the brightness of
a display backlight or LED (e.g. keyboard backlight) device at early boot, and saves it at shutdown.
The brightness is stored in <filename>/var/lib/systemd/backlight/</filename>.</para>
<para>On restoring brightness of a display backlight device, <command>systemd-backlight</command> reads
<varname>ID_BACKLIGHT_CLAMP</varname> udev property, that takes a boolean value or a percentage (needs to
be suffixed with <literal>%</literal>, e.g. <literal>30%</literal>). When a percentage is specified, the
saved brightness is clamped to a value of at least 1 or the specified percentage of the maximum
brightness, whichever is greater. When unset or set to true, the brightness is clamped in the same way
with percentage 5%. When false, the saved brightness will not be clamped, and loaded as is.</para>
<para>On restoring brightness of a LED device, <command>systemd-backlight</command> reads
<varname>ID_LEDS_CLAMP</varname> udev property, that also takes a boolean value or a percentage. When a
percentage is specified, the saved brightness is clamped to the specified percentage of the maximum
brightness. When set to true, the brightness is clamped in the same way with percentage 5%. When unset or
set to false, the saved brightness will not be clamped, and loaded as is.</para>
</refsect1>
<refsect1>
+21 -13
View File
@@ -338,10 +338,9 @@ static int clamp_brightness(
* avoids preserving an unreadably dim screen, which would otherwise force the user to disable
* state restoration. */
min_brightness = (unsigned) ((double) max_brightness * percent / 100);
if (device_in_subsystem(device, "backlight"))
min_brightness = MAX(1U, (unsigned) ((double) max_brightness * percent / 100));
else
min_brightness = 0;
min_brightness = MAX(1U, min_brightness);
new_brightness = CLAMP(*brightness, min_brightness, max_brightness);
if (new_brightness != *brightness)
@@ -356,19 +355,28 @@ static int clamp_brightness(
return 0;
}
static bool shall_clamp(sd_device *d, unsigned *ret) {
const char *s;
static bool shall_clamp(sd_device *device, unsigned *ret) {
const char *property, *s;
unsigned default_percent;
int r;
assert(d);
assert(device);
assert(ret);
r = sd_device_get_property_value(d, "ID_BACKLIGHT_CLAMP", &s);
if (device_in_subsystem(device, "backlight")) {
property = "ID_BACKLIGHT_CLAMP";
default_percent = 5;
} else {
property = "ID_LEDS_CLAMP";
default_percent = 0;
}
r = sd_device_get_property_value(device, property, &s);
if (r < 0) {
if (r != -ENOENT)
log_device_debug_errno(d, r, "Failed to get ID_BACKLIGHT_CLAMP property, ignoring: %m");
*ret = 5; /* defaults to 5% */
return true;
log_device_debug_errno(device, r, "Failed to get %s property, ignoring: %m", property);
*ret = default_percent;
return default_percent > 0;
}
r = parse_boolean(s);
@@ -379,9 +387,9 @@ static bool shall_clamp(sd_device *d, unsigned *ret) {
r = parse_percent(s);
if (r < 0) {
log_device_debug_errno(d, r, "Failed to parse ID_BACKLIGHT_CLAMP property, ignoring: %m");
*ret = 5;
return true;
log_device_debug_errno(device, r, "Failed to parse %s property, ignoring: %m", property);
*ret = default_percent;
return default_percent > 0;
}
*ret = r;