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;
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;
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
* 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;
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)
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;