From 09ff0aadf9f6ac2097121c522ece66e89fd0b06b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 4 Oct 2023 21:46:26 +0900 Subject: [PATCH] backlight: support to specify percentage of minimum brightness Closes #29328. --- man/systemd-backlight@.service.xml | 17 +++++++--------- src/backlight/backlight.c | 32 +++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/man/systemd-backlight@.service.xml b/man/systemd-backlight@.service.xml index 08e14ad09ca..431b27bbb09 100644 --- a/man/systemd-backlight@.service.xml +++ b/man/systemd-backlight@.service.xml @@ -31,16 +31,13 @@ Description - systemd-backlight@.service 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 /var/lib/systemd/backlight/. During - loading, if the udev property is - not set to false, the brightness is clamped to a value of at - least 1 or 5% of maximum brightness, whichever is greater. This - restriction will be removed when the kernel allows user space to - reliably set a brightness value which does not turn off the - display. + systemd-backlight@.service 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 + /var/lib/systemd/backlight/. During loading, if the udev property + 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 %, e.g. 30%) to the property + . diff --git a/src/backlight/backlight.c b/src/backlight/backlight.c index 24459628faf..5ac9f904a92 100644 --- a/src/backlight/backlight.c +++ b/src/backlight/backlight.c @@ -13,6 +13,7 @@ #include "main-func.h" #include "mkdir.h" #include "parse-util.h" +#include "percent-util.h" #include "pretty-print.h" #include "process-util.h" #include "reboot-util.h" @@ -326,7 +327,13 @@ static int get_max_brightness(sd_device *device, unsigned *ret) { return 0; } -static int clamp_brightness(sd_device *device, bool saved, unsigned max_brightness, unsigned *brightness) { +static int clamp_brightness( + sd_device *device, + unsigned percent, + bool saved, + unsigned max_brightness, + unsigned *brightness) { + unsigned new_brightness, min_brightness; const char *subsystem; int r; @@ -344,7 +351,7 @@ static int clamp_brightness(sd_device *device, bool saved, unsigned max_brightne return log_device_warning_errno(device, r, "Failed to get device subsystem: %m"); if (streq(subsystem, "backlight")) - min_brightness = MAX(1U, max_brightness/20); + min_brightness = MAX(1U, (unsigned) ((double) max_brightness * percent / 100)); else min_brightness = 0; @@ -361,26 +368,36 @@ static int clamp_brightness(sd_device *device, bool saved, unsigned max_brightne return 0; } -static bool shall_clamp(sd_device *d) { +static bool shall_clamp(sd_device *d, unsigned *ret) { const char *s; int r; assert(d); + assert(ret); r = sd_device_get_property_value(d, "ID_BACKLIGHT_CLAMP", &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; } r = parse_boolean(s); + if (r >= 0) { + *ret = r ? 5 : 0; + return r; + } + + 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; } - return r; + *ret = r; + return true; } static int read_brightness(sd_device *device, unsigned max_brightness, unsigned *ret_brightness) { @@ -526,6 +543,7 @@ static int run(int argc, char *argv[]) { if (streq(argv[1], "load")) { _cleanup_free_ char *value = NULL; + unsigned percent; bool clamp; if (!shall_restore_state()) @@ -534,7 +552,7 @@ static int run(int argc, char *argv[]) { if (validate_device(device) == 0) return 0; - clamp = shall_clamp(device); + clamp = shall_clamp(device, &percent); r = read_one_line_file(saved, &value); if (r < 0 && r != -ENOENT) @@ -548,7 +566,7 @@ static int run(int argc, char *argv[]) { } else { log_debug("Using saved brightness %u.", brightness); if (clamp) - (void) clamp_brightness(device, true, max_brightness, &brightness); + (void) clamp_brightness(device, percent, /* saved = */ true, max_brightness, &brightness); /* Do not fall back to read current brightness below. */ r = 1; @@ -564,7 +582,7 @@ static int run(int argc, char *argv[]) { if (r < 0) return log_device_error_errno(device, r, "Failed to read current brightness: %m"); - (void) clamp_brightness(device, false, max_brightness, &brightness); + (void) clamp_brightness(device, percent, /* saved = */ false, max_brightness, &brightness); } r = sd_device_set_sysattr_valuef(device, "brightness", "%u", brightness);