aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--README6
-rw-r--r--src/backlight.c28
-rw-r--r--src/backlight.h7
-rw-r--r--src/common.h14
-rw-r--r--src/config.h22
-rw-r--r--src/consts.h9
-rw-r--r--src/keypress_thread.c32
-rw-r--r--src/light_thread.c42
-rw-r--r--src/main.c126
10 files changed, 163 insertions, 125 deletions
diff --git a/Makefile b/Makefile
index 92d05e5..def9342 100644
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ clean:
bin:
mkdir bin
-bin/kblightd: src/main.c
+bin/kblightd: src/main.c src/keypress_thread.c src/light_thread.c src/backlight.c
${CC} -static $^ -o $@
run_prep:
diff --git a/README b/README
index 26bf680..bb47bce 100644
--- a/README
+++ b/README
@@ -6,9 +6,9 @@ jank wip keyboard backlight dsemon
roadmap
-------
-[ ] finish refactor
- ( ) separate code into multiple files
- ( ) config.h with consts
+[x] finish refactor
+ (x) separate code into multiple files
+ (x) config.h with consts
[ ] magisk module
diff --git a/src/backlight.c b/src/backlight.c
new file mode 100644
index 0000000..0824f0b
--- /dev/null
+++ b/src/backlight.c
@@ -0,0 +1,28 @@
+#include "common.h"
+#include "config.h"
+
+static uint32_t curr_brightness;
+static FILE* backlight_fp;
+
+void write_to_backlight(int brightness) {
+ if (!backlight_fp) {
+ backlight_fp = fopen(BACKLIGHT_FILE, "w");
+ }
+
+ fprintf(backlight_fp, "%d", brightness);
+ fflush(backlight_fp);
+
+ curr_brightness = brightness;
+}
+
+void backlight_on(void) {
+ write_to_backlight(MAX_BRIGHTNESS);
+
+ usleep(BACKLIGHT_TIME);
+}
+
+void backlight_off(void) {
+ if (curr_brightness == 0) return;
+
+ write_to_backlight(0);
+}
diff --git a/src/backlight.h b/src/backlight.h
new file mode 100644
index 0000000..45c9261
--- /dev/null
+++ b/src/backlight.h
@@ -0,0 +1,7 @@
+#ifndef BACKLIGHT_H
+#define BACKLIGHT_H
+
+void backlight_on(void);
+void backlight_off(void);
+
+#endif
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..4c01c24
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,14 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <sys/time.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+
+#include "consts.h"
+
+extern struct timeval* last_press;
+
+#endif
diff --git a/src/config.h b/src/config.h
new file mode 100644
index 0000000..c1bb692
--- /dev/null
+++ b/src/config.h
@@ -0,0 +1,22 @@
+#ifndef CONFIG_H
+#define CONFIG_H
+
+/* time to wait in /dev/input fred loop */
+#define DEBOUNCE (10 MS)
+
+/* time to wait in backlight setter loop */
+#define THREAD_WAIT (100 MS)
+
+/* how long to keep the light on */
+#define BACKLIGHT_TIME (10 SEC)
+
+/* path to keypad evdev */
+#define KEYBOARD_FILE "/dev/input/event1"
+
+/* path to keyboard's backlight brightness */
+#define BACKLIGHT_FILE "/sys/class/leds/keyboard_light/brightness"
+
+/* cat `echo $BACKLIGHT_FILE | sed 's/brightness/max_brightness/g'` */
+#define MAX_BRIGHTNESS 255
+
+#endif
diff --git a/src/consts.h b/src/consts.h
new file mode 100644
index 0000000..a1b2af4
--- /dev/null
+++ b/src/consts.h
@@ -0,0 +1,9 @@
+#ifndef CONSTS_H
+#define CONSTS_H
+
+#define MS * 1000
+#define SEC * 1000 MS
+
+#define KEYPRESS_SIZE 72 /* XXX sys/???.h */
+
+#endif
diff --git a/src/keypress_thread.c b/src/keypress_thread.c
new file mode 100644
index 0000000..47fb1de
--- /dev/null
+++ b/src/keypress_thread.c
@@ -0,0 +1,32 @@
+// XXX includes
+
+#include "consts.h"
+#include "config.h"
+
+#include "common.h"
+
+void on_press(void) {
+ gettimeofday(last_press, NULL);
+
+ // XXX signal light thread
+}
+
+int keypress_thread(void) {
+ FILE* fp = fopen(KEYBOARD_FILE, "r");
+ fseek(fp, 0, SEEK_END);
+
+ void* discard = malloc(KEYPRESS_SIZE);
+
+ while (1) {
+ fseek(fp, 0, SEEK_END);
+ fread(discard, 1, KEYPRESS_SIZE, fp);
+
+ on_press();
+
+ usleep(DEBOUNCE);
+ }
+
+ // XXX handle SIGINT or whatever
+
+ return 0;
+}
diff --git a/src/light_thread.c b/src/light_thread.c
new file mode 100644
index 0000000..1d273a0
--- /dev/null
+++ b/src/light_thread.c
@@ -0,0 +1,42 @@
+// XXX signals, includes, open, write instead of fopen, fwrite
+
+#include <stdio.h>
+
+#include <stdint.h>
+#include <sys/time.h>
+
+#include "common.h"
+#include "consts.h"
+#include "config.h"
+#include "backlight.h"
+
+uint64_t time_since_press(void) {
+ struct timeval now;
+
+ uint64_t now_usec;
+ uint64_t press_usec;
+ uint64_t diff;
+
+ 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;
+
+ return diff;
+}
+
+int light_thread(void) {
+ while (1) {
+ if (time_since_press() > BACKLIGHT_TIME) backlight_off();
+ else backlight_on();
+
+ usleep(THREAD_WAIT);
+ }
+
+ return 0;
+}
diff --git a/src/main.c b/src/main.c
index 70424d3..4c09563 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,131 +3,15 @@
#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
+#include "consts.h"
+#include "config.h"
-#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)
+extern int light_thread(void);
+extern int keypress_thread(void);
-#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);
-}
-
-uint64_t time_since_press(void) {
- struct timeval now;
-
- uint64_t now_usec;
- uint64_t press_usec;
- uint64_t diff;
-
- 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;
-
- return diff;
-}
-
-void on_press(void) {
- gettimeofday(last_press, NULL);
-}
-
-int light_thread(void) {
- // check last_press vs time, do the thing
- backlight_fp = fopen(BACKLIGHT_FILE, "w");
-
-// FILE* backlight_fp = BACKLIGHT_FILE;
-
- while (1) {
- if (time_since_press() > BACKLIGHT_TIME) backlight_off();
- else backlight_on();
-
- usleep(THREAD_WAIT);
- }
-
- return 0;
-}
-
-int keypress_thread(void) {
- FILE* fp = fopen(KEYBOARD_FILE, "r");
- fseek(fp, 0, SEEK_END);
-
- void* discard = malloc(KEYPRESS_SIZE);
-
- while (1) {
- fseek(fp, 0, SEEK_END);
- fread(discard, 1, KEYPRESS_SIZE, fp);
-
- on_press();
-
- usleep(DEBOUNCE);
- }
-
- return 0;
-}
+struct timeval* last_press;
void daemonize(void) {
daemon(0, 0);