1
0
forked from pool/brightnessctl
brightnessctl/0003-Make-the-use-of-SetBrightness-dynamic.patch

116 lines
3.2 KiB
Diff

From 658d5d058cb03a94269f8eec5b7ac4f1e8c43880 Mon Sep 17 00:00:00 2001
From: Antoine Damhet <antoine.damhet@lse.epita.fr>
Date: Tue, 10 Sep 2019 08:52:59 +0200
Subject: [PATCH 3/4] Make the use of `SetBrightness` dynamic
It will only be used if the user do not have the permission to directly
write to the backlight device.
Signed-off-by: Antoine Damhet <antoine.damhet@lse.epita.fr>
---
brightnessctl.c | 31 +++++++++++++++----------------
1 file changed, 15 insertions(+), 16 deletions(-)
diff --git a/brightnessctl.c b/brightnessctl.c
index 3fec6ec..576e591 100644
--- a/brightnessctl.c
+++ b/brightnessctl.c
@@ -38,7 +38,7 @@ static char *class_path(char *);
static void apply_value(struct device *, struct value *);
static int apply_operation(struct device *, enum operation, struct value *);
static bool parse_value(struct value *, char *);
-static bool write_device(struct device *);
+static bool do_write_device(struct device *);
static bool read_device(struct device *, char *, char *);
static int read_class(struct device **, char *);
static int read_devices(struct device **);
@@ -50,6 +50,10 @@ static bool restore_device_data(struct device *);
static bool ensure_dir(char *);
#define ensure_run_dir() ensure_dir(run_dir)
+#ifdef ENABLE_SYSTEMD
+static bool logind_set_brightness(struct device *);
+#endif
+
struct device {
char *class;
char *id;
@@ -101,16 +105,13 @@ static const struct option options[] = {
{NULL,}
};
+static bool (*write_device)(struct device *) = do_write_device;
+
int main(int argc, char **argv) {
struct device *devs[255];
struct device *dev;
struct utsname name;
- char *dev_name, *sys_run_dir;
-
-#ifndef ENABLE_SYSTEMD
- char *file_path;
-#endif
-
+ char *dev_name, *file_path, *sys_run_dir;
int n, c, phelp = 0;
if (uname(&name))
fail("Unable to determine current OS. Exiting!\n");
@@ -203,20 +204,20 @@ int main(int argc, char **argv) {
fail("Invalid value given");
if (!(dev = find_device(devs, dev_name)))
fail("Device '%s' not found.\n", dev_name);
-
-#ifndef ENABLE_SYSTEMD
if ((p.operation == SET || p.restore) && !p.pretend && geteuid()) {
errno = 0;
file_path = cat_with('/', path, dev->class, dev->id, "brightness");
if (access(file_path, W_OK)) {
+#ifdef ENABLE_SYSTEMD
+ write_device = logind_set_brightness;
+#else
perror("Can't modify brightness");
fail("\nYou should run this program with root privileges.\n"
"Alternatively, get write permissions for device files.\n");
+#endif
}
free(file_path);
}
-#endif
-
if ((sys_run_dir = getenv("XDG_RUNTIME_DIR")))
run_dir = dir_child(sys_run_dir, "brightnessctl");
if (p.save)
@@ -340,7 +341,7 @@ apply:
#ifdef ENABLE_SYSTEMD
-bool write_device(struct device *d) {
+bool logind_set_brightness(struct device *d) {
sd_bus *bus = NULL;
int r = sd_bus_default_system(&bus);
if (r < 0) {
@@ -367,9 +368,9 @@ bool write_device(struct device *d) {
return r >= 0;
}
-#else
+#endif
-bool write_device(struct device *d) {
+bool do_write_device(struct device *d) {
FILE *f;
char c[16];
size_t s = sprintf(c, "%u", d->curr_brightness);
@@ -392,8 +393,6 @@ fail:
return !errno;
}
-#endif
-
bool read_device(struct device *d, char *class, char *id) {
DIR *dirp;
FILE *f;
--
2.25.0