diff options
| author | spv <spv@spv.sh> | 2025-09-20 23:23:27 -0400 |
|---|---|---|
| committer | spv <spv@spv.sh> | 2025-09-20 23:23:27 -0400 |
| commit | 9ddf37ad959faaca72f1634dc61439cdd3c585f1 (patch) | |
| tree | d4c7d7772a74e2ee39dfb964d3dc15d0e9b9f1fa /src | |
| parent | 07cb5035b09f34e2f7927d4006261f5802517e27 (diff) | |
functional programming ftw
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 78 |
1 files changed, 47 insertions, 31 deletions
@@ -71,60 +71,52 @@ void backlight_on(void) { 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; - +uint64_t time_since_press(void) { struct timeval now; uint64_t now_usec; uint64_t press_usec; uint64_t diff; - while (1) { - gettimeofday(&now, NULL); + gettimeofday(&now, NULL); - now_usec = now.tv_sec * 1000000L; - now_usec += now.tv_usec; + now_usec = now.tv_sec * 1000000L; + now_usec += now.tv_usec; - press_usec = last_press->tv_sec * 1000000L; - press_usec += last_press->tv_usec; + press_usec = last_press->tv_sec * 1000000L; + press_usec += last_press->tv_usec; - diff = now_usec - press_usec; + diff = now_usec - press_usec; - if (diff > BACKLIGHT_TIME) backlight_off(); - else backlight_on(); - - usleep(THREAD_WAIT); - - fflush(stdout); - } + return diff; } void on_press(void) { gettimeofday(last_press, NULL); } -void daemonize(void) { - daemon(0, 0); +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 main(int argc, const char* argv[]) { +int keypress_thread(void) { 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); @@ -136,3 +128,27 @@ int main(int argc, const char* argv[]) { return 0; } + +void daemonize(void) { + daemon(0, 0); +} + +int shared_mem_setup(void) { + last_press = mmap(NULL, sizeof(last_press), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + + return 0; // XXX error handling +} + +int setup(void) { + daemonize(); + shared_mem_setup(); + + return 0; +} + +int main(int argc, const char* argv[]) { + setup(); + + if (fork() == 0) return light_thread(); + else return keypress_thread(); +} |
