[systemd-devel] [PATCH 2/2] backlight: Avoid restoring brightness to an unreadably dim level

Josh Triplett josh at joshtriplett.org
Tue Mar 11 18:55:48 PDT 2014


Some systems turn the backlight all the way off at the lowest levels.
Clamp saved brightness to at least 1 or 5% of max_brightness.  This
avoids preserving an unreadably dim screen, which would otherwise force
the user to disable state restoration.
---
 src/backlight/backlight.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/src/backlight/backlight.c b/src/backlight/backlight.c
index 81470b3..b776ab5 100644
--- a/src/backlight/backlight.c
+++ b/src/backlight/backlight.c
@@ -292,6 +292,7 @@ int main(int argc, char *argv[]) {
 
         if (streq(argv[1], "load") && shall_restore_state()) {
                 _cleanup_free_ char *value = NULL;
+                const char *max_brightness_str;
 
                 if (!validate_device(udev, device))
                         return EXIT_SUCCESS;
@@ -306,6 +307,44 @@ int main(int argc, char *argv[]) {
                         return EXIT_FAILURE;
                 }
 
+                /* Some systems turn the backlight all the way off at the
+                 * lowest levels.  Clamp saved brightness to at least 1 or 5%
+                 * of max_brightness.  This avoids preserving an unreadably dim
+                 * screen, which would otherwise force the user to disable
+                 * state restoration.
+                 */
+                max_brightness_str = udev_device_get_sysattr_value(device, "max_brightness");
+                if (!max_brightness_str) {
+                        log_warning("Failed to read max_brightness attribute; not checking saved brightness");
+                } else {
+                        unsigned long long brightness, max_brightness, new_brightness;
+
+                        r = safe_atollu(value, &brightness);
+                        if (r < 0) {
+                                log_error("Failed to parse brightness \"%s\": %s", value, strerror(-r));
+                                return EXIT_FAILURE;
+                        }
+
+                        r = safe_atollu(max_brightness_str, &max_brightness);
+                        if (r < 0) {
+                                log_error("Failed to parse max_brightness \"%s\": %s", max_brightness_str, strerror(-r));
+                                return EXIT_FAILURE;
+                        }
+
+                        new_brightness = MAX3(brightness, 1, max_brightness/20);
+                        if (new_brightness != brightness) {
+                                _cleanup_free_ char *old_value = value;
+
+                                value = asprintf("%llu", new_brightness);
+                                if (!value) {
+                                        log_oom();
+                                        return EXIT_FAILURE;
+                                }
+
+                                log_warning("Saved brightness %s too low; increasing to %s", old_value, value);
+                        }
+                }
+
                 r = udev_device_set_sysattr_value(device, "brightness", value);
                 if (r < 0) {
                         log_error("Failed to write system attribute: %s", strerror(-r));
-- 
1.9.0



More information about the systemd-devel mailing list