aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorspv <spv@spv.sh>2025-09-20 22:59:36 -0400
committerspv <spv@spv.sh>2025-09-20 22:59:36 -0400
commit07cb5035b09f34e2f7927d4006261f5802517e27 (patch)
tree17dee81d66b29cdec518fa96860eb5564b48c2f7
janky kblightd
-rw-r--r--.gitignore1
-rw-r--r--Makefile30
-rw-r--r--README4
-rw-r--r--src/main.c138
4 files changed, 173 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ba077a4
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+bin
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..92d05e5
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,30 @@
+CC=arm-linux-gnueabi-gcc
+
+all: bin bin/kblightd
+
+clean:
+ rm -rf bin
+
+bin:
+ mkdir bin
+
+bin/kblightd: src/main.c
+ ${CC} -static $^ -o $@
+
+run_prep:
+ adb root
+
+run: all run_prep
+ make kill
+
+ adb push bin/kblightd /sbin/
+ adb shell "nohup /sbin/kblightd > /dev/null &" &
+
+debug: all run_prep
+ make kill
+
+ adb push bin/kblightd /sbin/
+ adb shell "/sbin/kblightd"
+
+kill: all
+ adb shell -x pkill kblightd
diff --git a/README b/README
new file mode 100644
index 0000000..5ec5d80
--- /dev/null
+++ b/README
@@ -0,0 +1,4 @@
+kblightd
+========
+
+jank wip keyboard backlight dsemon
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..d66f483
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,138 @@
+// WTFPL
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <sys/time.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+
+#define MS * 1000
+#define SEC * 1000000
+
+#define DEBOUNCE (10 MS)
+#define THREAD_WAIT (100 MS)
+#define BACKLIGHT_TIME (10 SEC)
+#define BACKLIGHT_FADE_FRAMETIME (50 MS)
+#define BACKLIGHT_FADE_TIME (1000 MS)
+#define BACKLIGHT_FADE_STEPS (BACKLIGHT_FADE_TIME / BACKLIGHT_FADE_FRAMETIME)
+
+#define KEYPRESS_SIZE 72
+#define KEYBOARD_FILE "/dev/input/event1"
+#define BACKLIGHT_FILE "/sys/class/leds/keyboard_light/brightness"
+
+//#define BACKLIGHT_FILE stdout
+
+#define MAX_BRIGHTNESS 255
+
+static struct timeval* last_press;
+static uint32_t curr_brightness;
+static FILE* backlight_fp;
+
+void write_to_backlight(int n) {
+ fprintf(backlight_fp, "%d", n);
+ fflush(backlight_fp);
+
+ curr_brightness = n;
+}
+
+#if 0
+void backlight_fade(void) {
+ short brightness_step = MAX_BRIGHTNESS / BACKLIGHT_FADE_STEPS;
+ short* brightness = malloc(sizeof(short));
+
+ *brightness = MAX_BRIGHTNESS;
+
+ fprintf(stderr, "ha, gay\n");
+
+ do {
+ fprintf(stderr, "%d %d %d\n", *brightness, brightness_step, 0);
+
+ write_to_backlight(*brightness);
+ *brightness -= brightness_step;
+
+ usleep(BACKLIGHT_FADE_FRAMETIME);
+ } while (*brightness >= brightness_step);
+}
+#endif
+
+void backlight_off(void) {
+ if (curr_brightness == 0) return;
+ write_to_backlight(0);
+
+// backlight_fade();
+}
+
+void backlight_on(void) {
+ curr_brightness = MAX_BRIGHTNESS;
+
+ write_to_backlight(MAX_BRIGHTNESS);
+ usleep(BACKLIGHT_TIME);
+}
+
+void light_thread(void) {
+ // check last_press vs time, do the thing
+ backlight_fp = fopen(BACKLIGHT_FILE, "w");
+
+// FILE* backlight_fp = BACKLIGHT_FILE;
+
+ struct timeval now;
+
+ uint64_t now_usec;
+ uint64_t press_usec;
+ uint64_t diff;
+
+ while (1) {
+ gettimeofday(&now, NULL);
+
+ now_usec = now.tv_sec * 1000000L;
+ now_usec += now.tv_usec;
+
+ press_usec = last_press->tv_sec * 1000000L;
+ press_usec += last_press->tv_usec;
+
+ diff = now_usec - press_usec;
+
+ if (diff > BACKLIGHT_TIME) backlight_off();
+ else backlight_on();
+
+ usleep(THREAD_WAIT);
+
+ fflush(stdout);
+ }
+}
+
+void on_press(void) {
+ gettimeofday(last_press, NULL);
+}
+
+void daemonize(void) {
+ daemon(0, 0);
+}
+
+int main(int argc, const char* argv[]) {
+ FILE* fp = fopen(KEYBOARD_FILE, "r");
+ fseek(fp, 0, SEEK_END);
+
+ void* discard = malloc(KEYPRESS_SIZE);
+
+ // shared mem b/c new proc doesn't share mem space
+ last_press = mmap(NULL, sizeof(last_press), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+
+ if (fork() == 0) {
+ daemonize();
+ light_thread();
+ } else daemonize();
+
+ while (1) {
+ fseek(fp, 0, SEEK_END);
+ fread(discard, 1, KEYPRESS_SIZE, fp);
+
+ on_press();
+
+ usleep(DEBOUNCE);
+ }
+
+ return 0;
+}