backlight.c revision 934ae16baf543af03f3f521277d14524ca772d17
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "libudev.h"
#include "alloc-util.h"
#include "def.h"
#include "escape.h"
#include "fileio.h"
#include "mkdir.h"
#include "parse-util.h"
#include "proc-cmdline.h"
#include "string-util.h"
#include "udev-util.h"
#include "util.h"
struct udev_device *parent;
if (!parent)
return NULL;
if (!subsystem)
return NULL;
if (!sysname)
return NULL;
const char *c;
if (!c)
return NULL;
if (*c == '-') {
/* A connector DRM device, let's ignore all but LVDS and eDP! */
if (!startswith(c, "-LVDS-") &&
!startswith(c, "-Embedded DisplayPort-"))
return NULL;
}
const char *value;
if (value) {
unsigned long class = 0;
log_warning("Cannot parse PCI class %s of device %s:%s.",
return NULL;
}
/* Graphics card */
if (class == 0x30000)
return parent;
}
return parent;
return find_pci_or_platform_parent(parent);
}
assert(a);
assert(b);
return false;
return false;
return true;
}
struct udev_device *parent;
const char *v, *subsystem;
int r;
/* 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"
* 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. */
return true;
if (!streq_ptr(v, "raw"))
return true;
if (!parent)
return true;
if (!subsystem)
return true;
if (!enumerate)
return true;
if (r < 0)
return true;
if (r < 0)
return true;
struct udev_device *other_parent;
const char *other_subsystem;
if (!other)
return true;
continue;
continue;
/* OK, so there's another backlight device, and it's a
* platform or firmware device, so, let's see if we
* can verify it belongs to the same device as
* ours. */
if (!other_parent)
continue;
/* Both have the same PCI parent, that means
* we are out. */
log_debug("Skipping backlight device %s, since device %s is on same PCI device and takes precedence.",
return false;
}
/* The other is connected to the platform bus
* and we are a PCI device, that also means we
* are out. */
log_debug("Skipping backlight device %s, since device %s is a platform device and takes precedence.",
return false;
}
}
return true;
}
int r;
const char *max_brightness_str;
unsigned max_brightness;
if (!max_brightness_str) {
log_warning("Failed to read 'max_brightness' attribute.");
return 0;
}
if (r < 0) {
return 0;
}
if (max_brightness <= 0) {
log_warning("Maximum brightness is 0, ignoring device.");
return 0;
}
return max_brightness;
}
/* 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
* max_brightness in case of 'backlight' subsystem. This avoids preserving
* an unreadably dim screen, which would otherwise force the user to
* disable state restoration. */
int r;
const char *subsystem;
if (r < 0) {
return;
}
else
min_brightness = 0;
if (new_brightness != brightness) {
if (r < 0) {
log_oom();
return;
}
"too low; increasing" : "too high; decreasing",
*value);
}
}
_cleanup_free_ char *saved = NULL, *ss = NULL, *escaped_ss = NULL, *escaped_sysname = NULL, *escaped_path_id = NULL;
unsigned max_brightness;
int r;
if (argc != 3) {
log_error("This program requires two arguments.");
return EXIT_FAILURE;
}
log_open();
umask(0022);
if (r < 0) {
log_error_errno(r, "Failed to create backlight directory /var/lib/systemd/backlight: %m");
return EXIT_FAILURE;
}
if (!udev) {
log_oom();
return EXIT_FAILURE;
}
if (!sysname) {
log_error("Requires a subsystem and sysname pair specifying a backlight device.");
return EXIT_FAILURE;
}
if (!ss) {
log_oom();
return EXIT_FAILURE;
}
sysname++;
return EXIT_FAILURE;
}
errno = 0;
if (!device) {
if (errno != 0)
else
log_oom();
return EXIT_FAILURE;
}
/* If max_brightness is 0, then there is no actual backlight
* device. This happens on desktops with Asus mainboards
* that load the eeepc-wmi module.
*/
if (max_brightness == 0)
return EXIT_SUCCESS;
if (!escaped_ss) {
log_oom();
return EXIT_FAILURE;
}
if (!escaped_sysname) {
log_oom();
return EXIT_FAILURE;
}
if (path_id) {
if (!escaped_path_id) {
log_oom();
return EXIT_FAILURE;
}
saved = strjoin("/var/lib/systemd/backlight/", escaped_path_id, ":", escaped_ss, ":", escaped_sysname, NULL);
} else
if (!saved) {
log_oom();
return EXIT_FAILURE;
}
/* 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 this, we will
* 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. */
const char *clamp;
if (shall_restore_state() == 0)
return EXIT_SUCCESS;
return EXIT_SUCCESS;
if (r < 0) {
if (r == -ENOENT)
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
if (r < 0) {
log_error_errno(r, "Failed to write system 'brightness' attribute: %m");
return EXIT_FAILURE;
}
const char *value;
return EXIT_SUCCESS;
}
if (!value) {
log_error("Failed to read system 'brightness' attribute");
return EXIT_FAILURE;
}
if (r < 0) {
return EXIT_FAILURE;
}
} else {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}