d22a5aa
From 2d4460027359d91f442d2677e9ae42ea951e2430 Mon Sep 17 00:00:00 2001
d22a5aa
From: =?UTF-8?q?Thomas=20B=C3=A4chler?= <thomas@archlinux.org>
d22a5aa
Date: Thu, 27 Mar 2014 23:41:59 +0100
d22a5aa
Subject: [PATCH] backlight: do nothing if max_brightness is 0
d22a5aa
d22a5aa
On virtually any newer Asus mainboard, the eeepc-wmi driver is loaded.
d22a5aa
It exposes a backlight device despite the lack of any physical backlight
d22a5aa
devices. This fake backlight device has max_brightness set to 0. Since
d22a5aa
the introduction of the clamp_brightness function, systemd-backlight
d22a5aa
tries to write '1' to brightness and fails.
d22a5aa
d22a5aa
This patch changes systemd-backlight to exit gracefully when
d22a5aa
max_brightness is 0 before performing any action. This affects
d22a5aa
both the load and save actions.
d22a5aa
d22a5aa
(cherry picked from commit 3cadce7d33e263ec7a6a83c00c11144930258b22)
d22a5aa
(cherry picked from commit 555853f8ad1b1db461c45843310bc324bca394de)
d22a5aa
---
d22a5aa
 src/backlight/backlight.c | 44 ++++++++++++++++++++++++++++++--------------
d22a5aa
 1 file changed, 30 insertions(+), 14 deletions(-)
d22a5aa
d22a5aa
diff --git a/src/backlight/backlight.c b/src/backlight/backlight.c
d22a5aa
index 2740b80..84bf51d 100644
d22a5aa
--- a/src/backlight/backlight.c
d22a5aa
+++ b/src/backlight/backlight.c
d22a5aa
@@ -192,30 +192,37 @@ static bool validate_device(struct udev *udev, struct udev_device *device) {
d22a5aa
         return true;
d22a5aa
 }
d22a5aa
 
d22a5aa
-/* Some systems turn the backlight all the way off at the lowest levels.
d22a5aa
- * clamp_brightness clamps the saved brightness to at least 1 or 5% of
d22a5aa
- * max_brightness.  This avoids preserving an unreadably dim screen, which
d22a5aa
- * would otherwise force the user to disable state restoration. */
d22a5aa
-static void clamp_brightness(struct udev_device *device, char **value) {
d22a5aa
+static unsigned get_max_brightness(struct udev_device *device) {
d22a5aa
         int r;
d22a5aa
         const char *max_brightness_str;
d22a5aa
-        unsigned brightness, max_brightness, new_brightness;
d22a5aa
+        unsigned max_brightness;
d22a5aa
 
d22a5aa
         max_brightness_str = udev_device_get_sysattr_value(device, "max_brightness");
d22a5aa
         if (!max_brightness_str) {
d22a5aa
-                log_warning("Failed to read max_brightness attribute; not checking saved brightness");
d22a5aa
-                return;
d22a5aa
+                log_warning("Failed to read max_brightness attribute");
d22a5aa
+                return 0;
d22a5aa
         }
d22a5aa
 
d22a5aa
-        r = safe_atou(*value, &brightness);
d22a5aa
+        r = safe_atou(max_brightness_str, &max_brightness);
d22a5aa
         if (r < 0) {
d22a5aa
-                log_warning("Failed to parse brightness \"%s\": %s", *value, strerror(-r));
d22a5aa
-                return;
d22a5aa
+                log_warning("Failed to parse max_brightness \"%s\": %s", max_brightness_str, strerror(-r));
d22a5aa
+                return 0;
d22a5aa
         }
d22a5aa
 
d22a5aa
-        r = safe_atou(max_brightness_str, &max_brightness);
d22a5aa
+        return max_brightness;
d22a5aa
+}
d22a5aa
+
d22a5aa
+/* Some systems turn the backlight all the way off at the lowest levels.
d22a5aa
+ * clamp_brightness clamps the saved brightness to at least 1 or 5% of
d22a5aa
+ * max_brightness.  This avoids preserving an unreadably dim screen, which
d22a5aa
+ * would otherwise force the user to disable state restoration. */
d22a5aa
+static void clamp_brightness(struct udev_device *device, char **value, unsigned max_brightness) {
d22a5aa
+        int r;
d22a5aa
+        unsigned brightness, new_brightness;
d22a5aa
+
d22a5aa
+        r = safe_atou(*value, &brightness);
d22a5aa
         if (r < 0) {
d22a5aa
-                log_warning("Failed to parse max_brightness \"%s\": %s", max_brightness_str, strerror(-r));
d22a5aa
+                log_warning("Failed to parse brightness \"%s\": %s", *value, strerror(-r));
d22a5aa
                 return;
d22a5aa
         }
d22a5aa
 
d22a5aa
@@ -239,6 +246,7 @@ int main(int argc, char *argv[]) {
d22a5aa
         _cleanup_udev_device_unref_ struct udev_device *device = NULL;
d22a5aa
         _cleanup_free_ char *saved = NULL, *ss = NULL, *escaped_ss = NULL, *escaped_sysname = NULL, *escaped_path_id = NULL;
d22a5aa
         const char *sysname, *path_id;
d22a5aa
+        unsigned max_brightness;
d22a5aa
         int r;
d22a5aa
 
d22a5aa
         if (argc != 3) {
d22a5aa
@@ -294,6 +302,14 @@ int main(int argc, char *argv[]) {
d22a5aa
                 return EXIT_FAILURE;
d22a5aa
         }
d22a5aa
 
d22a5aa
+        /* If max_brightness is 0, then there is no actual backlight
d22a5aa
+         * device. This happens on desktops with Asus mainboards
d22a5aa
+         * that load the eeepc-wmi module.
d22a5aa
+         */
d22a5aa
+        max_brightness = get_max_brightness(device);
d22a5aa
+        if (max_brightness == 0)
d22a5aa
+                return EXIT_SUCCESS;
d22a5aa
+
d22a5aa
         escaped_ss = cescape(ss);
d22a5aa
         if (!escaped_ss) {
d22a5aa
                 log_oom();
d22a5aa
@@ -348,7 +364,7 @@ int main(int argc, char *argv[]) {
d22a5aa
                         return EXIT_FAILURE;
d22a5aa
                 }
d22a5aa
 
d22a5aa
-                clamp_brightness(device, &value);
d22a5aa
+                clamp_brightness(device, &value, max_brightness);
d22a5aa
 
d22a5aa
                 r = udev_device_set_sysattr_value(device, "brightness", value);
d22a5aa
                 if (r < 0) {