]> Pileus Git - ~andy/hwkeys/commitdiff
Add initial hwkeys program. master
authorAndy Spencer <andy753421@gmail.com>
Thu, 20 Sep 2018 20:38:42 +0000 (20:38 +0000)
committerAndy Spencer <andy753421@gmail.com>
Thu, 20 Sep 2018 20:38:42 +0000 (20:38 +0000)
.gitignore [new file with mode: 0644]
hwkeys.c [new file with mode: 0644]
makefile [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..5a9e4e9
--- /dev/null
@@ -0,0 +1,4 @@
+*~
+*.o
+*.swp
+hwkeys
diff --git a/hwkeys.c b/hwkeys.c
new file mode 100644 (file)
index 0000000..9807081
--- /dev/null
+++ b/hwkeys.c
@@ -0,0 +1,105 @@
+#include <math.h>
+#include <stdio.h>
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <linux/input.h>
+
+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 (file)
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 $@ $+