changeset 0:73a371432d36

misc: initial import
author David Demelier <markand@malikania.fr>
date Mon, 18 Jul 2016 14:58:32 +0200
parents
children 4402b74acda0
files .editorconfig LICENSE.md Makefile README.md main.c
diffstat 5 files changed, 320 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.editorconfig	Mon Jul 18 14:58:32 2016 +0200
@@ -0,0 +1,13 @@
+root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+insert_final_newline = true
+indent_style = space
+indent_size = 4
+trim_trailing_whitespace = true
+
+[Makefile]
+indent_style = tab
+indent_size = 8
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LICENSE.md	Mon Jul 18 14:58:32 2016 +0200
@@ -0,0 +1,16 @@
+backlight LICENSE
+=================
+
+Copyright (c) 2010-2016 David Demelier <markand@malikania.fr>
+
+Permission to use, copy, modify, and/or 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.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Mon Jul 18 14:58:32 2016 +0200
@@ -0,0 +1,43 @@
+#
+# Makefile for backlight
+#
+# Copyright (c) 2010-2016 David Demelier <markand@malikania.fr>
+#
+# 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.
+#
+
+PREFIX=		/usr/local/bin
+
+CC=		gcc
+CFLAGS=		-Wall -Wextra -ansi -pedantic -DNDEBUG
+
+ECHO=		echo
+RM=		rm -f
+INSTALL=	install -o root -g wheel -m 4755
+
+all: backlight
+
+main.o: main.c
+	@echo CC $<
+	@${CC} ${CFLAGS} -c -o $@ $<
+
+backlight: main.o
+	@echo LD $@
+	@${CC} ${CFLAGS} -o $@ $<
+
+clean:
+	${RM} *.o backlight
+
+install: backlight
+	${INSTALL} $< ${PREFIX}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Mon Jul 18 14:58:32 2016 +0200
@@ -0,0 +1,39 @@
+backlight
+=========
+
+This tool adjust your laptop brightness using ACPI.
+
+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.
+
+Example:
+
+    backlight up
+    backlight down
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.c	Mon Jul 18 14:58:32 2016 +0200
@@ -0,0 +1,209 @@
+/*
+ * main.c -- adjust laptop backlight using ACPI
+ *
+ * Copyright (c) 2010-2016 David Demelier <markand@malikania.fr>
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+enum {
+    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 */
+
+#ifdef __FreeBSD__
+#  include <sys/types.h>
+#  include <sys/sysctl.h>
+
+static int
+set(int 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", &current, &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", &current, &len, &next, nextlen) == -1) {
+        perror("sysctl");
+        return -1;
+    }
+
+    return next;
+}
+
+#endif
+
+/* }}} */
+
+/* {{{ Support for Linux */
+
+#ifdef __linux__
+
+static int
+read_int(const char *path)
+{
+    FILE *fp;
+    int value;
+
+    if ((fp = fopen(path, "r")) == NULL) {
+        perror("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 int
+set(int type)
+{
+    int current, max, next;
+
+    /* Read actual */
+    current = read_int("/sys/class/backlight/acpi_video0/actual_brightness");
+    if (current < 0)
+        return -1;
+
+    /* Read max */
+    max = read_int("/sys/class/backlight/acpi_video0/max_brightness");
+    if (max < 0)
+        return -1;
+
+    if (type == Up) {
+        next = ++ current;
+
+        if (next > max)
+            return max;
+    } else {
+        next = -- current;
+
+        if (next < 0)
+            return 0;
+    }
+
+    return write_int("/sys/class/backlight/acpi_video0/brightness", next);
+}
+
+#else
+
+#include <errno.h>
+
+/* Not supported */
+static int
+set(int type)
+{
+    (void)type;
+
+    fprintf(stderr, "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) {
+        printf("failed to set brightness to %d\n", st);
+        return 1;
+    }
+
+    printf("switching to %d\n", st);
+
+    return 0;
+}
+
+