backlight: add intel_backlight and adjust next value

Thu, 05 Jan 2017 21:20:18 +0100

author
David Demelier <markand@malikania.fr>
date
Thu, 05 Jan 2017 21:20:18 +0100
changeset 2
a81001c9e45a
parent 1
4402b74acda0
child 3
7174d183e592

backlight: add intel_backlight and adjust next value

Makefile file | annotate | diff | comparison | revisions
main.c file | annotate | diff | comparison | revisions
--- a/Makefile	Mon Jan 02 16:34:05 2017 +0100
+++ b/Makefile	Thu Jan 05 21:20:18 2017 +0100
@@ -19,7 +19,7 @@
 PREFIX=		/usr/local/bin
 
 CC=		gcc
-CFLAGS=		-Wall -Wextra -ansi -pedantic -DNDEBUG
+CFLAGS=		-Wall -Wextra -ansi -pedantic -std=c99 -DNDEBUG
 
 ECHO=		echo
 RM=		rm -f
--- a/main.c	Mon Jan 02 16:34:05 2017 +0100
+++ b/main.c	Thu Jan 05 21:20:18 2017 +0100
@@ -100,6 +100,9 @@
 
 #ifdef __linux__
 
+#include <sys/types.h>
+#include <sys/stat.h>
+
 static int
 read_int(const char *path)
 {
@@ -133,34 +136,64 @@
     return value;
 }
 
+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, next;
+    int current, max;
+    const char *card = find_card();
+    char file[BUFSIZ];
+
+    /* Find a card adaptor */
+    if (!card) {
+        fprintf(stderr, "could not find card adaptor\n");
+        return -1;
+    }
 
     /* Read actual */
-    current = read_int("/sys/class/backlight/acpi_video0/actual_brightness");
+    snprintf(file, sizeof (file), "%s/actual_brightness", card);
+    current = read_int(file);
     if (current < 0)
         return -1;
 
     /* Read max */
-    max = read_int("/sys/class/backlight/acpi_video0/max_brightness");
+    snprintf(file, sizeof (file), "%s/max_brightness", card);
+    max = read_int(file);
     if (max < 0)
         return -1;
 
     if (type == Up) {
-        next = ++ current;
+        current += max / 100;
 
-        if (next > max)
+        if (current > max)
             return max;
     } else {
-        next = -- current;
+        current -= max / 100;
 
-        if (next < 0)
+        if (current < 0)
             return 0;
     }
 
-    return write_int("/sys/class/backlight/acpi_video0/brightness", next);
+    snprintf(file, sizeof (file), "%s/brightness", card);
+
+    return write_int(file, current);
 }
 
 #else

mercurial