From ba5ff4b54633a3f049823d384c3eaeb7a1997f39 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Thu, 20 Sep 2018 20:38:42 +0000 Subject: [PATCH 1/1] Add initial hwkeys program. --- .gitignore | 4 ++ hwkeys.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ makefile | 14 +++++++ 3 files changed, 123 insertions(+) create mode 100644 .gitignore create mode 100644 hwkeys.c create mode 100644 makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5a9e4e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*~ +*.o +*.swp +hwkeys diff --git a/hwkeys.c b/hwkeys.c new file mode 100644 index 0000000..9807081 --- /dev/null +++ b/hwkeys.c @@ -0,0 +1,105 @@ +#include +#include + +#include +#include +#include +#include +#include + +static void screenlight(int up) +{ + static const char *path = "/sys/class/backlight/gmux_backlight/brightness"; + static int prev, next, tmp; + static double val; + static FILE *fd; + + // Open brightness file + if (fd == NULL) + fd = fopen(path, "r+"); + if (fd == NULL) { + printf("error opening file\n"); + return; + } + + // Reset if someone else changed the value + fseek(fd, 0, SEEK_SET); + fscanf(fd, "%d", &tmp); + if (tmp != prev) + val = pow(tmp/1024.0, 1/10.0); + //if (tmp != prev) + // printf("convert: %d -> %d -> %f\n", + // prev, tmp, val); + + // Update the current value + val += 0.01 * (up?1:-1); + if (val < 0) val = 0; + if (val > 1) val = 1; + + // Write it back to the file + next = pow(val, 10) * 1024; + if (next == prev) + next += (up?1:-1); + else + prev = next; + + //printf("saving: %d\n", next); + fprintf(fd, "%d", next); + fflush(fd); +} + +static void keylight(int up) +{ + //static const char *path = "/sys/class/leds/smc::kbd_backlight/brightness"; +} + +int main(int argc, char **argv) +{ + struct input_event ev; + int fd, len, debug=0; + + //if ((fd = open("/dev/input/event4", O_RDONLY)) < 0) { + //if ((fd = open("/dev/input/event6", O_RDONLY)) < 0) { + if ((fd = open("/dev/input/event10", O_RDONLY)) < 0) { + printf("Error\n"); + return 0; + } + + while ((len = read(fd, &ev, sizeof(ev)))) { + if (len != sizeof(ev)) { + printf("Error\n"); + sleep(1); + continue; + } + + if (debug) { + printf("%ld.%06lu: type=%04hx code=%04hx val=%08x\n", + ev.time.tv_sec, + ev.time.tv_usec, + ev.type, ev.code, ev.value); + fflush(stdout); + } + + if (ev.type != EV_KEY) + continue; + if (ev.value == 0) // key up + continue; + + switch (ev.code) { + case KEY_BRIGHTNESSDOWN: + screenlight(0); + break; + case KEY_BRIGHTNESSUP: + screenlight(1); + break; + case KEY_KBDILLUMDOWN: + keylight(0); + break; + case KEY_KBDILLUMUP: + keylight(1); + break; + } + fflush(stdout); + } + return 0; +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..b72730c --- /dev/null +++ b/makefile @@ -0,0 +1,14 @@ +CC ?= gcc +CFLAGS ?= -Wall -g +LDFLAGS ?= -lm + +run: hwkeys + ./hwkeys + +all: hwkeys + +hwkeys: hwkeys.o + $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $+ -- 2.43.2