2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
#include "sd-device.h"
|
2015-11-17 06:52:45 +01:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2018-08-28 09:05:35 +02:00
|
|
|
#include "device-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "escape.h"
|
2013-08-14 01:57:02 +02:00
|
|
|
#include "fileio.h"
|
2018-11-20 07:16:08 +01:00
|
|
|
#include "main-func.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "mkdir.h"
|
2015-10-27 00:06:29 +01:00
|
|
|
#include "parse-util.h"
|
2019-08-01 14:34:51 +02:00
|
|
|
#include "reboot-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2018-08-22 06:35:47 +02:00
|
|
|
#include "strv.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "util.h"
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) {
|
|
|
|
const char *subsystem, *sysname, *value;
|
|
|
|
sd_device *parent;
|
|
|
|
int r;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
|
|
|
assert(device);
|
2018-08-22 06:35:47 +02:00
|
|
|
assert(ret);
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_get_parent(device, &parent);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_get_subsystem(parent, &subsystem);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_get_sysname(parent, &sysname);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
|
|
|
if (streq(subsystem, "drm")) {
|
|
|
|
const char *c;
|
|
|
|
|
|
|
|
c = startswith(sysname, "card");
|
|
|
|
if (!c)
|
2018-08-22 06:35:47 +02:00
|
|
|
return -ENODATA;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2014-04-08 02:57:22 +02:00
|
|
|
c += strspn(c, DIGITS);
|
2013-10-14 02:12:52 +02:00
|
|
|
if (*c == '-') {
|
|
|
|
/* A connector DRM device, let's ignore all but LVDS and eDP! */
|
2018-11-23 16:30:23 +01:00
|
|
|
if (!STARTSWITH_SET(c, "-LVDS-", "-Embedded DisplayPort-"))
|
2018-08-22 06:35:47 +02:00
|
|
|
return -EOPNOTSUPP;
|
2013-10-14 02:12:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
} else if (streq(subsystem, "pci") &&
|
|
|
|
sd_device_get_sysattr_value(parent, "class", &value) >= 0) {
|
|
|
|
unsigned long class = 0;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = safe_atolu(value, &class);
|
|
|
|
if (r < 0)
|
|
|
|
return log_warning_errno(r, "Cannot parse PCI class '%s' of device %s:%s: %m",
|
|
|
|
value, subsystem, sysname);
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
/* Graphics card */
|
|
|
|
if (class == 0x30000) {
|
2018-09-06 04:58:29 +02:00
|
|
|
*ret = parent;
|
2018-08-22 06:35:47 +02:00
|
|
|
return 0;
|
2013-10-14 02:12:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
} else if (streq(subsystem, "platform")) {
|
2018-09-06 04:58:29 +02:00
|
|
|
*ret = parent;
|
2018-08-22 06:35:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
return find_pci_or_platform_parent(parent, ret);
|
2013-10-14 02:12:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
static int same_device(sd_device *a, sd_device *b) {
|
|
|
|
const char *a_val, *b_val;
|
|
|
|
int r;
|
|
|
|
|
2013-10-14 02:12:52 +02:00
|
|
|
assert(a);
|
|
|
|
assert(b);
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_get_subsystem(a, &a_val);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_device_get_subsystem(b, &b_val);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-09-01 16:03:22 +02:00
|
|
|
if (!streq(a_val, b_val))
|
2013-10-14 02:12:52 +02:00
|
|
|
return false;
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_get_sysname(a, &a_val);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_device_get_sysname(b, &b_val);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-09-01 16:03:22 +02:00
|
|
|
return streq(a_val, b_val);
|
2013-10-14 02:12:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
static int validate_device(sd_device *device) {
|
|
|
|
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *enumerate = NULL;
|
2013-10-14 02:12:52 +02:00
|
|
|
const char *v, *subsystem;
|
2018-09-06 04:58:29 +02:00
|
|
|
sd_device *parent, *other;
|
2013-10-14 02:12:52 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(device);
|
|
|
|
|
|
|
|
/* Verify whether we should actually care for a specific
|
|
|
|
* backlight device. For backlight devices there might be
|
|
|
|
* multiple ways to access the same control: "firmware"
|
|
|
|
* (i.e. ACPI), "platform" (i.e. via the machine's EC) and
|
|
|
|
* "raw" (via the graphics card). In general we should prefer
|
|
|
|
* "firmware" (i.e. ACPI) or "platform" access over "raw"
|
|
|
|
* access, in order not to confuse the BIOS/EC, and
|
|
|
|
* compatibility with possible low-level hotkey handling of
|
|
|
|
* screen brightness. The kernel will already make sure to
|
|
|
|
* expose only one of "firmware" and "platform" for the same
|
|
|
|
* device to userspace. However, we still need to make sure
|
|
|
|
* that we use "raw" only if no "firmware" or "platform"
|
|
|
|
* device for the same device exists. */
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_get_subsystem(device, &subsystem);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (!streq(subsystem, "backlight"))
|
2013-10-14 02:12:52 +02:00
|
|
|
return true;
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_get_sysattr_value(device, "type", &v);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (!streq(v, "raw"))
|
2013-10-14 02:12:52 +02:00
|
|
|
return true;
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = find_pci_or_platform_parent(device, &parent);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_get_subsystem(parent, &subsystem);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_enumerator_new(&enumerate);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_enumerator_allow_uninitialized(enumerate);
|
2013-10-14 02:12:52 +02:00
|
|
|
if (r < 0)
|
2018-08-22 06:35:47 +02:00
|
|
|
return r;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_enumerator_add_match_subsystem(enumerate, "backlight", true);
|
2013-10-14 02:12:52 +02:00
|
|
|
if (r < 0)
|
2018-08-22 06:35:47 +02:00
|
|
|
return r;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-28 09:05:35 +02:00
|
|
|
FOREACH_DEVICE(enumerate, other) {
|
2018-08-22 06:35:47 +02:00
|
|
|
const char *other_subsystem;
|
2018-09-06 04:58:29 +02:00
|
|
|
sd_device *other_parent;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
if (same_device(device, other) > 0)
|
2013-10-14 02:12:52 +02:00
|
|
|
continue;
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
if (sd_device_get_sysattr_value(other, "type", &v) < 0 ||
|
|
|
|
!STR_IN_SET(v, "platform", "firmware"))
|
2013-10-14 02:12:52 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* OK, so there's another backlight device, and it's a
|
|
|
|
* platform or firmware device, so, let's see if we
|
2018-08-22 06:35:47 +02:00
|
|
|
* can verify it belongs to the same device as ours. */
|
|
|
|
if (find_pci_or_platform_parent(other, &other_parent) < 0)
|
2013-10-14 02:12:52 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (same_device(parent, other_parent)) {
|
2018-08-22 06:35:47 +02:00
|
|
|
const char *device_sysname = NULL, *other_sysname = NULL;
|
|
|
|
|
|
|
|
/* Both have the same PCI parent, that means we are out. */
|
|
|
|
|
|
|
|
(void) sd_device_get_sysname(device, &device_sysname);
|
|
|
|
(void) sd_device_get_sysname(other, &other_sysname);
|
|
|
|
|
2014-04-08 02:57:22 +02:00
|
|
|
log_debug("Skipping backlight device %s, since device %s is on same PCI device and takes precedence.",
|
2018-08-22 06:35:47 +02:00
|
|
|
device_sysname, other_sysname);
|
2013-10-14 02:12:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
if (sd_device_get_subsystem(other_parent, &other_subsystem) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (streq(other_subsystem, "platform") && streq(subsystem, "pci")) {
|
|
|
|
const char *device_sysname = NULL, *other_sysname = NULL;
|
|
|
|
|
|
|
|
/* The other is connected to the platform bus and we are a PCI device, that also means we are out. */
|
|
|
|
|
|
|
|
(void) sd_device_get_sysname(device, &device_sysname);
|
|
|
|
(void) sd_device_get_sysname(other, &other_sysname);
|
|
|
|
|
2014-04-08 02:57:22 +02:00
|
|
|
log_debug("Skipping backlight device %s, since device %s is a platform device and takes precedence.",
|
2018-08-22 06:35:47 +02:00
|
|
|
device_sysname, other_sysname);
|
2013-10-14 02:12:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
static int get_max_brightness(sd_device *device, unsigned *ret) {
|
2014-03-12 05:16:33 +01:00
|
|
|
const char *max_brightness_str;
|
2014-03-27 23:41:59 +01:00
|
|
|
unsigned max_brightness;
|
2018-08-22 06:35:47 +02:00
|
|
|
int r;
|
2014-03-12 05:16:33 +01:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
assert(device);
|
|
|
|
assert(ret);
|
|
|
|
|
|
|
|
r = sd_device_get_sysattr_value(device, "max_brightness", &max_brightness_str);
|
|
|
|
if (r < 0)
|
2018-10-22 05:13:23 +02:00
|
|
|
return log_device_warning_errno(device, r, "Failed to read 'max_brightness' attribute: %m");
|
2014-03-12 05:16:33 +01:00
|
|
|
|
2014-03-27 23:41:59 +01:00
|
|
|
r = safe_atou(max_brightness_str, &max_brightness);
|
2018-08-22 06:35:47 +02:00
|
|
|
if (r < 0)
|
2018-10-22 05:13:23 +02:00
|
|
|
return log_device_warning_errno(device, r, "Failed to parse 'max_brightness' \"%s\": %m", max_brightness_str);
|
2014-04-23 06:55:54 +02:00
|
|
|
|
2020-06-30 12:28:50 +02:00
|
|
|
if (max_brightness <= 0)
|
|
|
|
return log_device_warning_errno(device, SYNTHETIC_ERRNO(EINVAL), "Maximum brightness is 0, ignoring device.");
|
2014-03-12 05:16:33 +01:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
*ret = max_brightness;
|
|
|
|
return 0;
|
2014-03-27 23:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Some systems turn the backlight all the way off at the lowest levels.
|
|
|
|
* clamp_brightness clamps the saved brightness to at least 1 or 5% of
|
2014-06-05 21:59:40 +02:00
|
|
|
* max_brightness in case of 'backlight' subsystem. This avoids preserving
|
|
|
|
* an unreadably dim screen, which would otherwise force the user to
|
|
|
|
* disable state restoration. */
|
2018-08-22 06:35:47 +02:00
|
|
|
static int clamp_brightness(sd_device *device, char **value, unsigned max_brightness) {
|
2014-05-07 11:01:01 +02:00
|
|
|
unsigned brightness, new_brightness, min_brightness;
|
2014-06-05 21:59:40 +02:00
|
|
|
const char *subsystem;
|
2018-03-23 15:20:34 +01:00
|
|
|
int r;
|
2014-03-27 23:41:59 +01:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
assert(value);
|
|
|
|
assert(*value);
|
|
|
|
|
2014-03-27 23:41:59 +01:00
|
|
|
r = safe_atou(*value, &brightness);
|
2018-08-22 06:35:47 +02:00
|
|
|
if (r < 0)
|
2018-10-22 05:13:23 +02:00
|
|
|
return log_device_warning_errno(device, r, "Failed to parse brightness \"%s\": %m", *value);
|
2014-03-12 05:16:33 +01:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_get_subsystem(device, &subsystem);
|
|
|
|
if (r < 0)
|
2018-10-22 05:13:23 +02:00
|
|
|
return log_device_warning_errno(device, r, "Failed to get device subsystem: %m");
|
2018-08-22 06:35:47 +02:00
|
|
|
|
|
|
|
if (streq(subsystem, "backlight"))
|
2014-06-05 21:59:40 +02:00
|
|
|
min_brightness = MAX(1U, max_brightness/20);
|
|
|
|
else
|
|
|
|
min_brightness = 0;
|
|
|
|
|
2014-05-07 11:01:01 +02:00
|
|
|
new_brightness = CLAMP(brightness, min_brightness, max_brightness);
|
2014-03-12 05:16:33 +01:00
|
|
|
if (new_brightness != brightness) {
|
2018-08-22 06:35:47 +02:00
|
|
|
char *new_value;
|
2014-03-12 05:16:33 +01:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = asprintf(&new_value, "%u", new_brightness);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
2014-03-12 05:16:33 +01:00
|
|
|
|
2018-10-22 05:13:23 +02:00
|
|
|
log_device_info(device, "Saved brightness %s %s to %s.", *value,
|
|
|
|
new_brightness > brightness ?
|
|
|
|
"too low; increasing" : "too high; decreasing",
|
|
|
|
new_value);
|
2014-05-07 11:01:01 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
free_and_replace(*value, new_value);
|
2014-03-12 05:16:33 +01:00
|
|
|
}
|
2018-08-22 06:35:47 +02:00
|
|
|
|
|
|
|
return 0;
|
2014-03-12 05:16:33 +01:00
|
|
|
}
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
static bool shall_clamp(sd_device *d) {
|
2018-03-28 13:32:27 +02:00
|
|
|
const char *s;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(d);
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_get_property_value(d, "ID_BACKLIGHT_CLAMP", &s);
|
2018-10-22 05:13:23 +02:00
|
|
|
if (r < 0) {
|
|
|
|
log_device_debug_errno(d, r, "Failed to get ID_BACKLIGHT_CLAMP property, ignoring: %m");
|
2018-03-28 13:32:27 +02:00
|
|
|
return true;
|
2018-10-22 05:13:23 +02:00
|
|
|
}
|
2018-03-28 13:32:27 +02:00
|
|
|
|
|
|
|
r = parse_boolean(s);
|
|
|
|
if (r < 0) {
|
2018-10-22 05:13:23 +02:00
|
|
|
log_device_debug_errno(d, r, "Failed to parse ID_BACKLIGHT_CLAMP property, ignoring: %m");
|
2018-03-28 13:32:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:41:39 +02:00
|
|
|
static int read_brightness(sd_device *device, const char **ret) {
|
|
|
|
const char *subsystem;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(device);
|
|
|
|
assert(ret);
|
|
|
|
|
|
|
|
r = sd_device_get_subsystem(device, &subsystem);
|
|
|
|
if (r < 0)
|
|
|
|
return log_device_debug_errno(device, r, "Failed to get subsystem: %m");
|
|
|
|
|
|
|
|
if (streq(subsystem, "backlight")) {
|
|
|
|
r = sd_device_get_sysattr_value(device, "actual_brightness", ret);
|
|
|
|
if (r >= 0)
|
|
|
|
return 0;
|
|
|
|
if (r != -ENOENT)
|
|
|
|
return log_device_debug_errno(device, r, "Failed to read 'actual_brightness' attribute: %m");
|
|
|
|
|
|
|
|
log_device_debug_errno(device, r, "Failed to read 'actual_brightness' attribute, fall back to use 'brightness' attribute: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_device_get_sysattr_value(device, "brightness", ret);
|
|
|
|
if (r < 0)
|
|
|
|
return log_device_debug_errno(device, r, "Failed to read 'brightness' attribute: %m");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-20 07:16:08 +01:00
|
|
|
static int run(int argc, char *argv[]) {
|
2018-08-22 06:35:47 +02:00
|
|
|
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
|
2018-03-23 15:20:34 +01:00
|
|
|
_cleanup_free_ char *escaped_ss = NULL, *escaped_sysname = NULL, *escaped_path_id = NULL;
|
|
|
|
const char *sysname, *path_id, *ss, *saved;
|
2014-03-27 23:41:59 +01:00
|
|
|
unsigned max_brightness;
|
2013-08-14 01:57:02 +02:00
|
|
|
int r;
|
|
|
|
|
2020-06-30 12:29:42 +02:00
|
|
|
log_setup_service();
|
|
|
|
|
2020-06-30 12:28:50 +02:00
|
|
|
if (argc != 3)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires two arguments.");
|
2013-08-14 01:57:02 +02:00
|
|
|
|
|
|
|
umask(0022);
|
|
|
|
|
2013-09-18 00:28:35 +02:00
|
|
|
r = mkdir_p("/var/lib/systemd/backlight", 0755);
|
2018-11-20 07:16:08 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to create backlight directory /var/lib/systemd/backlight: %m");
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2013-10-14 02:12:52 +02:00
|
|
|
sysname = strchr(argv[2], ':');
|
2020-06-30 12:28:50 +02:00
|
|
|
if (!sysname)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Requires a subsystem and sysname pair specifying a backlight device.");
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-03-23 15:20:34 +01:00
|
|
|
ss = strndupa(argv[2], sysname - argv[2]);
|
2013-10-14 02:12:52 +02:00
|
|
|
|
|
|
|
sysname++;
|
|
|
|
|
2020-06-30 12:28:50 +02:00
|
|
|
if (!STR_IN_SET(ss, "backlight", "leds"))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a backlight or LED device: '%s:%s'", ss, sysname);
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_new_from_subsystem_sysname(&device, ss, sysname);
|
2018-11-20 07:16:08 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to get backlight or LED device '%s:%s': %m", ss, sysname);
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2014-03-27 23:41:59 +01:00
|
|
|
/* If max_brightness is 0, then there is no actual backlight
|
|
|
|
* device. This happens on desktops with Asus mainboards
|
2018-08-22 06:35:47 +02:00
|
|
|
* that load the eeepc-wmi module. */
|
2018-11-20 07:16:08 +01:00
|
|
|
if (get_max_brightness(device, &max_brightness) < 0)
|
|
|
|
return 0;
|
2014-03-27 23:41:59 +01:00
|
|
|
|
2013-10-14 19:01:09 +02:00
|
|
|
escaped_ss = cescape(ss);
|
2018-11-20 07:16:08 +01:00
|
|
|
if (!escaped_ss)
|
|
|
|
return log_oom();
|
2013-10-14 19:01:09 +02:00
|
|
|
|
|
|
|
escaped_sysname = cescape(sysname);
|
2018-11-20 07:16:08 +01:00
|
|
|
if (!escaped_sysname)
|
|
|
|
return log_oom();
|
2013-10-14 19:01:09 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
if (sd_device_get_property_value(device, "ID_PATH", &path_id) >= 0) {
|
2013-10-14 19:01:09 +02:00
|
|
|
escaped_path_id = cescape(path_id);
|
2018-11-20 07:16:08 +01:00
|
|
|
if (!escaped_path_id)
|
|
|
|
return log_oom();
|
2013-10-14 19:01:09 +02:00
|
|
|
|
2018-03-23 15:20:34 +01:00
|
|
|
saved = strjoina("/var/lib/systemd/backlight/", escaped_path_id, ":", escaped_ss, ":", escaped_sysname);
|
2013-10-14 19:01:09 +02:00
|
|
|
} else
|
2018-03-23 15:20:34 +01:00
|
|
|
saved = strjoina("/var/lib/systemd/backlight/", escaped_ss, ":", escaped_sysname);
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2013-10-14 02:12:52 +02:00
|
|
|
/* 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
|
2014-02-17 03:37:13 +01:00
|
|
|
* with a specific backlight. To deal with this, we will
|
2013-10-14 02:12:52 +02:00
|
|
|
* actively delete backlight state files at shutdown (where
|
|
|
|
* device probing should be complete), so that the validity
|
|
|
|
* check at boot time doesn't have to be reliable. */
|
|
|
|
|
2014-09-11 00:49:36 +02:00
|
|
|
if (streq(argv[1], "load")) {
|
2013-08-14 01:57:02 +02:00
|
|
|
_cleanup_free_ char *value = NULL;
|
2018-03-28 13:32:27 +02:00
|
|
|
bool clamp;
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2015-11-02 23:07:58 +01:00
|
|
|
if (shall_restore_state() == 0)
|
2018-11-20 07:16:08 +01:00
|
|
|
return 0;
|
2014-09-11 00:49:36 +02:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
if (validate_device(device) == 0)
|
2018-11-20 07:16:08 +01:00
|
|
|
return 0;
|
2013-10-14 02:12:52 +02:00
|
|
|
|
2018-03-28 13:32:27 +02:00
|
|
|
clamp = shall_clamp(device);
|
|
|
|
|
2013-08-14 01:57:02 +02:00
|
|
|
r = read_one_line_file(saved, &value);
|
2019-01-31 18:07:06 +01:00
|
|
|
if (IN_SET(r, -ENOENT, 0)) {
|
2018-03-28 13:32:27 +02:00
|
|
|
const char *curval;
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2018-03-28 13:32:27 +02:00
|
|
|
/* Fallback to clamping current brightness or exit early if
|
|
|
|
* clamping is not supported/enabled. */
|
|
|
|
if (!clamp)
|
2018-11-20 07:16:08 +01:00
|
|
|
return 0;
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2020-06-30 12:41:39 +02:00
|
|
|
r = read_brightness(device, &curval);
|
2018-11-20 07:16:08 +01:00
|
|
|
if (r < 0)
|
2020-06-30 12:41:39 +02:00
|
|
|
return log_device_error_errno(device, r, "Failed to read current brightness: %m");
|
2018-03-28 13:32:27 +02:00
|
|
|
|
|
|
|
value = strdup(curval);
|
2018-11-20 07:16:08 +01:00
|
|
|
if (!value)
|
|
|
|
return log_oom();
|
|
|
|
} else if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to read %s: %m", saved);
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2018-03-28 13:32:27 +02:00
|
|
|
if (clamp)
|
2018-08-22 06:35:47 +02:00
|
|
|
(void) clamp_brightness(device, &value, max_brightness);
|
2014-03-12 05:16:33 +01:00
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
r = sd_device_set_sysattr_value(device, "brightness", value);
|
2018-11-20 07:16:08 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_device_error_errno(device, r, "Failed to write system 'brightness' attribute: %m");
|
2013-08-14 01:57:02 +02:00
|
|
|
|
|
|
|
} else if (streq(argv[1], "save")) {
|
|
|
|
const char *value;
|
|
|
|
|
2018-08-22 06:35:47 +02:00
|
|
|
if (validate_device(device) == 0) {
|
2019-03-27 14:36:36 +01:00
|
|
|
(void) unlink(saved);
|
2018-11-20 07:16:08 +01:00
|
|
|
return 0;
|
2013-10-14 02:12:52 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:41:39 +02:00
|
|
|
r = read_brightness(device, &value);
|
2018-11-20 07:16:08 +01:00
|
|
|
if (r < 0)
|
2020-06-30 12:41:39 +02:00
|
|
|
return log_device_error_errno(device, r, "Failed to read current brightness: %m");
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2015-07-07 01:19:25 +02:00
|
|
|
r = write_string_file(saved, value, WRITE_STRING_FILE_CREATE);
|
2018-11-20 07:16:08 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_device_error_errno(device, r, "Failed to write %s: %m", saved);
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2020-06-30 12:28:50 +02:00
|
|
|
} else
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown verb %s.", argv[1]);
|
2013-08-14 01:57:02 +02:00
|
|
|
|
2018-11-20 07:16:08 +01:00
|
|
|
return 0;
|
2013-08-14 01:57:02 +02:00
|
|
|
}
|
2018-11-20 07:16:08 +01:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|