From 53a906bd9801d1b5e62796f5ce3ba160c7bdae07 Mon Sep 17 00:00:00 2001 From: Antoine Damhet Date: Tue, 10 Sep 2019 08:52:59 +0200 Subject: 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 --- brightnessctl.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/brightnessctl.c b/brightnessctl.c index 77dca5c..680dc4b 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; @@ -103,16 +107,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"); @@ -212,20 +213,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) @@ -366,7 +367,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) { @@ -393,9 +394,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); @@ -418,8 +419,6 @@ fail: return !errno; } -#endif - bool read_device(struct device *d, char *class, char *id) { DIR *dirp; FILE *f; -- cgit v1.2.3