summaryrefslogtreecommitdiffstats
path: root/src/backlight
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2023-12-21 20:29:54 +0100
committerYu Watanabe <watanabe.yu+github@gmail.com>2023-12-22 19:53:28 +0100
commit78b4ff5df454e1911576b2dd86f437d26185ba6f (patch)
tree56d80cbd5a9367a8eac735b6c8a7bbe9153c2976 /src/backlight
parentbacklight: split out device_new_from_arg() (diff)
downloadsystemd-78b4ff5df454e1911576b2dd86f437d26185ba6f.tar.xz
systemd-78b4ff5df454e1911576b2dd86f437d26185ba6f.zip
backlight: split out read_saved_brightness()
No functional change, just refactoring.
Diffstat (limited to 'src/backlight')
-rw-r--r--src/backlight/backlight.c66
1 files changed, 40 insertions, 26 deletions
diff --git a/src/backlight/backlight.c b/src/backlight/backlight.c
index c988a7bd14..4298654479 100644
--- a/src/backlight/backlight.c
+++ b/src/backlight/backlight.c
@@ -510,6 +510,37 @@ static int build_save_file_path(sd_device *device, char **ret) {
return 0;
}
+static int read_saved_brightness(sd_device *device, unsigned *ret) {
+ _cleanup_free_ char *path = NULL, *value = NULL;
+ int r;
+
+ assert(device);
+ assert(ret);
+
+ r = build_save_file_path(device, &path);
+ if (r < 0)
+ return r;
+
+ r = read_one_line_file(path, &value);
+ if (r < 0) {
+ if (r != -ENOENT)
+ log_device_error_errno(device, r, "Failed to read %s: %m", path);
+ return r;
+ }
+
+ r = safe_atou(value, ret);
+ if (r < 0) {
+ log_device_warning_errno(device, r,
+ "Failed to parse saved brightness '%s', removing %s.",
+ value, path);
+ (void) unlink(path);
+ return r;
+ }
+
+ log_device_debug(device, "Using saved brightness %u.", *ret);
+ return 0;
+}
+
static int device_new_from_arg(const char *s, sd_device **ret) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
_cleanup_free_ char *subsystem = NULL;
@@ -552,7 +583,6 @@ static int device_new_from_arg(const char *s, sd_device **ret) {
static int run(int argc, char *argv[]) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
- _cleanup_free_ char *saved = NULL;
unsigned max_brightness, brightness;
int r;
@@ -581,10 +611,6 @@ static int run(int argc, char *argv[]) {
if (r <= 0)
return r;
- r = build_save_file_path(device, &saved);
- if (r < 0)
- return r;
-
/* If there are multiple conflicting backlight devices, then their probing at boot-time might
* happen in any order. This means the validity checking of the device then is not reliable,
* since it might not see other devices conflicting with a specific backlight. To deal with
@@ -592,7 +618,6 @@ static int run(int argc, char *argv[]) {
* be complete), so that the validity check at boot time doesn't have to be reliable. */
if (streq(argv[1], "load")) {
- _cleanup_free_ char *value = NULL;
unsigned percent;
bool clamp;
@@ -604,25 +629,7 @@ static int run(int argc, char *argv[]) {
clamp = shall_clamp(device, &percent);
- r = read_one_line_file(saved, &value);
- if (r < 0 && r != -ENOENT)
- return log_error_errno(r, "Failed to read %s: %m", saved);
- if (r > 0) {
- r = safe_atou(value, &brightness);
- if (r < 0) {
- log_warning_errno(r, "Failed to parse saved brightness '%s', removing %s.",
- value, saved);
- (void) unlink(saved);
- } else {
- log_debug("Using saved brightness %u.", brightness);
- if (clamp)
- (void) clamp_brightness(device, percent, /* saved = */ true, max_brightness, &brightness);
-
- /* Do not fall back to read current brightness below. */
- r = 1;
- }
- }
- if (r <= 0) {
+ if (read_saved_brightness(device, &brightness) < 0) {
/* Fallback to clamping current brightness or exit early if clamping is not
* supported/enabled. */
if (!clamp)
@@ -633,13 +640,20 @@ static int run(int argc, char *argv[]) {
return log_device_error_errno(device, r, "Failed to read current brightness: %m");
(void) clamp_brightness(device, percent, /* saved = */ false, max_brightness, &brightness);
- }
+ } else if (clamp)
+ (void) clamp_brightness(device, percent, /* saved = */ true, max_brightness, &brightness);
r = sd_device_set_sysattr_valuef(device, "brightness", "%u", brightness);
if (r < 0)
return log_device_error_errno(device, r, "Failed to write system 'brightness' attribute: %m");
} else if (streq(argv[1], "save")) {
+ _cleanup_free_ char *saved = NULL;
+
+ r = build_save_file_path(device, &saved);
+ if (r < 0)
+ return r;
+
if (validate_device(device) == 0) {
(void) unlink(saved);
return 0;