# HG changeset patch # User David Demelier # Date 1568639301 -7200 # Node ID 77ea2359f3e4e309ace26cb230bc38c16fe76622 # Parent 867f73a28c79c63ae6d7d6ccc863830bec2ff58b misc: refactor diff -r 867f73a28c79 -r 77ea2359f3e4 50-brightness.rules --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/50-brightness.rules Mon Sep 16 15:08:21 2019 +0200 @@ -0,0 +1,2 @@ +ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness" +ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chmod g+w /sys/class/backlight/%k/brightness" diff -r 867f73a28c79 -r 77ea2359f3e4 INSTALL.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/INSTALL.md Mon Sep 16 15:08:21 2019 +0200 @@ -0,0 +1,49 @@ +backlight INSTALL +================= + +Supported systems +----------------- + +- Linux: requires video ACPI module, +- FreeBSD: requires the `acpi_video(4)` loaded. + +Installation on Linux +--------------------- + +First, build the program: + + make + +On Linux, adjusting the brightness can be done as normal user if you have write +access to the appropriate files which are usually under */sys*. + +The recommandation is to install the udev rule which change the ownership of the +brightness file to `video` group and writable by users in this group. Use the +`install-udev` for that purpose. Then don't forget to check if you are part of +the video group. + + make install + make install-udev + +Otherwise, as alternative you can simply install the binary as setuid if you +don't run udev. + + make install-setuid + +Installation on FreeBSD +----------------------- + +In FreeBSD, it's required to have the tool in setuid as there is no way to +change sysctl values from a regular user. + +Also, you will need the ACPI video module, add the following to your +*/boot/loader.conf*: + + acpi_video_load=YES + +Alternatively, you can compile this module into the kernel. + +Then install using: + + make + make install-setuid diff -r 867f73a28c79 -r 77ea2359f3e4 LICENSE.md --- a/LICENSE.md Thu Oct 18 12:50:02 2018 +0200 +++ b/LICENSE.md Mon Sep 16 15:08:21 2019 +0200 @@ -1,7 +1,7 @@ backlight LICENSE ================= -Copyright (c) 2010-2018 David Demelier +Copyright (c) 2010-2019 David Demelier Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above diff -r 867f73a28c79 -r 77ea2359f3e4 Makefile --- a/Makefile Thu Oct 18 12:50:02 2018 +0200 +++ b/Makefile Mon Sep 16 15:08:21 2019 +0200 @@ -1,7 +1,7 @@ # # Makefile for backlight # -# Copyright (c) 2010-2018 David Demelier +# Copyright (c) 2010-2019 David Demelier # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -16,27 +16,41 @@ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # -PREFIX= /usr/local/bin +CC= gcc +CFLAGS= -Wall -Wextra -ansi -pedantic -std=c99 -DNDEBUG ${EXTRA_CFLAGS} +LDFLAGS= ${EXTRA_LDFLAGS} +LDLIBS= ${EXTRA_LDLIBS} -CC= gcc -CFLAGS= -Wall -Wextra -ansi -pedantic -std=c99 -DNDEBUG - -ECHO= echo -RM= rm -f -INSTALL= install -o root -g wheel -m 4755 +PREFIX= /usr/local +BINDIR= ${PREFIX}/bin +MANDIR= ${PREFIX}/share/man +LIBDIR= ${PREFIX}/lib -all: backlight +PROG= backlight +SRCS= backlight.c +MAN1= backlight.1 +OBJS= ${SRCS:.c=.o} -main.o: main.c - @echo CC $< - @${CC} ${CFLAGS} -c -o $@ $< +all: ${PROG} -backlight: main.o - @echo LD $@ - @${CC} ${CFLAGS} -o $@ $< +.c.o: + ${CC} ${CFLAGS} -c -o $@ $< + +${PROG}: ${OBJS} + ${CC} -o ${PROG} ${OBJS} ${LDFLAGS} ${LDLIBS} clean: - ${RM} *.o backlight + rm -f ${OBJS} ${PROG} + +install: ${PROG} + install -dm0755 ${DESTDIR}${MANDIR}/man1 + install -m0644 ${MAN1} ${DESTDIR}${MANDIR}/man1 -install: backlight - ${INSTALL} $< ${PREFIX} +install-udev: + install -Dm0755 ${PROG} ${DESTDIR}${BINDIR}/${PROG} + install -Dm0755 50-brightness.rules ${DESTDIR}${LIBDIR}/udev.d/rules.d/50-brightness.rules + +install-setuid: backlight + install -Dm4755 ${PROG} ${DESTDIR}${BINDIR}/${PROG} + +.PHONY: all clean install install-udev install-setuid diff -r 867f73a28c79 -r 77ea2359f3e4 README.md --- a/README.md Thu Oct 18 12:50:02 2018 +0200 +++ b/README.md Mon Sep 16 15:08:21 2019 +0200 @@ -6,29 +6,8 @@ It has been designed for broken ACPI (e.g. HP) where fn-keys do not work, instead, you can map these keys to call this tool. -Supported systems -================= - - - Linux, requires video ACPI module, - - FreeBSD, requires the acpi_video(4) loaded. - -Installation -============ - -It is recommended to install this tool using the setuid to allow non-root users -to change the laptop backlight. - - make - sudo make install - -For FreeBSD, add the following to your **/boot/loader.conf**: - - acpi_video_load=YES - -Alternatively, you can compile this module into the kernel. - Usage -===== +----- The backlight tool supports two commands, `up` and `down` which increment and decrement backlight respectively. @@ -36,4 +15,4 @@ Example: backlight up - backlight down \ No newline at end of file + backlight down diff -r 867f73a28c79 -r 77ea2359f3e4 main.c --- a/main.c Thu Oct 18 12:50:02 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,237 +0,0 @@ -/* - * main.c -- adjust laptop backlight using ACPI - * - * Copyright (c) 2010-2018 David Demelier - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include - -enum action { - Up, - Down -}; - -static void -usage(const char *name) -{ - fprintf(stderr, "usage: %s down\n", name); - fprintf(stderr, " %s up\n", name); - exit(1); -} - -/* {{{ Support for FreeBSD */ - -#if defined(__FreeBSD__) - -#include -#include - -static int -set(enum action type) -{ - int current, next, i; - int msg[100]; - size_t len, nextlen; - - /* First, get all values availables */ - len = sizeof (msg); - - if (sysctlbyname("hw.acpi.video.lcd0.levels", msg, &len, NULL, 0) == -1) { - perror("sysctl"); - return -1; - } - - /* Get the current value */ - len = sizeof (current); - - if (sysctlbyname("hw.acpi.video.lcd0.brightness", ¤t, &len, NULL, 0) == -1) { - perror("sysctl"); - return -1; - } - - /* First find the index of 0 */ - for (i = 0; msg[i] != 0; ++i) - continue; - - /* Find the current value index in msg */ - for (; msg[i] != current; ++i) - continue; - - if (type == Up) { - if (msg[i] >= 100) - return msg[i]; - - next = msg[i + 1]; - } else { - if (msg[i] == 0) - return (0); - - next = msg[i - 1]; - } - - nextlen = sizeof (next); - - if (sysctlbyname("hw.acpi.video.lcd0.brightness", ¤t, &len, &next, nextlen) == -1) { - perror("sysctl"); - return -1; - } - - return next; -} - -#endif - -/* }}} */ - -/* {{{ Support for Linux */ - -#if defined(__linux__) - -#include -#include - -static int -read_int(const char *path) -{ - FILE *fp; - int value; - - if ((fp = fopen(path, "r")) == NULL) { - perror("abort: open"); - return -1; - } - - if (fscanf(fp, "%d", &value) != 1) - value = -1; - fclose(fp); - - return value; -} - -static int -write_int(const char *path, int value) -{ - FILE *fp; - - if ((fp = fopen(path, "w+")) == NULL) { - perror("open"); - return -1; - } - - fprintf(fp, "%d", value); - - return value; -} - -static const char * -find_card(void) -{ - static const char *list[] = { - "/sys/class/backlight/acpi_video0", - "/sys/class/backlight/intel_backlight", - NULL - }; - - struct stat st; - - for (const char **ptr = list; *ptr != NULL; ++ptr) - if (stat(*ptr, &st) >= 0) - return *ptr; - - return NULL; -} - -static int -set(int type) -{ - int current, max; - const char *card = find_card(); - char file[BUFSIZ]; - - /* Find a card adaptor */ - if (!card) { - fprintf(stderr, "abort: could not find card adaptor\n"); - return -1; - } - - /* Read actual */ - snprintf(file, sizeof (file), "%s/actual_brightness", card); - current = read_int(file); - if (current < 0) - return -1; - - /* Read max */ - snprintf(file, sizeof (file), "%s/max_brightness", card); - max = read_int(file); - if (max < 0) - return -1; - - if (type == Up) { - current += max / 100; - - if (current > max) - return max; - } else { - current -= max / 100; - - if (current < 0) - return 0; - } - - snprintf(file, sizeof (file), "%s/brightness", card); - - return write_int(file, current); -} - -#else - -/* Not supported */ -static int -set(int type) -{ - (void)type; - - fprintf(stderr, "abort: backlight is not supported on this system"); - exit(1); -} - -#endif - -/* }}} */ - -int -main(int argc, char *argv[]) -{ - int st, type; - - if (argc < 2) - usage(argv[0]); - - if (strcmp(argv[1], "up") == 0) - type = Up; - else if (strcmp(argv[1], "down") == 0) - type = Down; - else - usage(argv[0]); - - if ((st = set(type)) < 0) - return 1; - - printf("switching to %d\n", st); - - return 0; -}