aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorspv <spv@spv.sh>2025-09-21 00:59:56 -0400
committerspv <spv@spv.sh>2025-09-21 00:59:56 -0400
commit02a74cc0b499f6a93c25ea8cd392af977298d60a (patch)
tree71b9008bdb6402e10477c9f80fe43950f7c89c72 /src
parent38d25483657e82eb546ecb8566214f830efba461 (diff)
use signal ipc instead of sleep loop
Diffstat (limited to 'src')
-rw-r--r--src/common.h1
-rw-r--r--src/keypress_thread.c8
-rw-r--r--src/light_thread.c27
-rw-r--r--src/main.c11
4 files changed, 34 insertions, 13 deletions
diff --git a/src/common.h b/src/common.h
index 4c01c24..07cc4ae 100644
--- a/src/common.h
+++ b/src/common.h
@@ -6,6 +6,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
+#include <signal.h>
#include "consts.h"
diff --git a/src/keypress_thread.c b/src/keypress_thread.c
index 47fb1de..a174968 100644
--- a/src/keypress_thread.c
+++ b/src/keypress_thread.c
@@ -5,13 +5,19 @@
#include "common.h"
+static pid_t light_pid;
+
void on_press(void) {
gettimeofday(last_press, NULL);
+ kill(light_pid, SIGCONT);
+
// XXX signal light thread
}
-int keypress_thread(void) {
+int keypress_thread(pid_t _light_pid) {
+ light_pid = _light_pid;
+
FILE* fp = fopen(KEYBOARD_FILE, "r");
fseek(fp, 0, SEEK_END);
diff --git a/src/light_thread.c b/src/light_thread.c
index 1d273a0..20b7bb9 100644
--- a/src/light_thread.c
+++ b/src/light_thread.c
@@ -2,14 +2,13 @@
#include <stdio.h>
-#include <stdint.h>
-#include <sys/time.h>
-
#include "common.h"
#include "consts.h"
#include "config.h"
#include "backlight.h"
+static pid_t keypress_pid;
+
uint64_t time_since_press(void) {
struct timeval now;
@@ -30,12 +29,24 @@ uint64_t time_since_press(void) {
return diff;
}
-int light_thread(void) {
- while (1) {
- if (time_since_press() > BACKLIGHT_TIME) backlight_off();
- else backlight_on();
+void handler(int _) {
+ if (time_since_press() > BACKLIGHT_TIME) {
+ backlight_off();
+ } else {
+ backlight_on();
- usleep(THREAD_WAIT);
+ handler(_);
+ }
+}
+
+int light_thread(pid_t _keypress_pid) {
+ keypress_pid = _keypress_pid;
+
+ signal(SIGCONT, handler);
+
+ while (1) {
+ pause();
+// usleep(THREAD_WAIT);
}
return 0;
diff --git a/src/main.c b/src/main.c
index 4c09563..5c57bbf 100644
--- a/src/main.c
+++ b/src/main.c
@@ -8,8 +8,8 @@
#include "consts.h"
#include "config.h"
-extern int light_thread(void);
-extern int keypress_thread(void);
+extern int light_thread(pid_t _keypress_pid);
+extern int keypress_thread(pid_t _light_pid);
struct timeval* last_press;
@@ -33,6 +33,9 @@ int setup(void) {
int main(int argc, const char* argv[]) {
setup();
- if (fork() == 0) return light_thread();
- else return keypress_thread();
+ pid_t keypress_pid = getpid();
+ pid_t pid = fork();
+
+ if (pid == 0) return light_thread(keypress_pid);
+ else return keypress_thread(/*light_*/pid);
}