mirror of
https://github.com/systemd/systemd.git
synced 2025-03-09 12:58:26 +03:00
backlight: split out read_saved_brightness()
No functional change, just refactoring.
This commit is contained in:
parent
69ba99f9f3
commit
78b4ff5df4
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user