diff options
| author | spv <spv@spv.sh> | 2025-09-21 00:07:50 -0400 |
|---|---|---|
| committer | spv <spv@spv.sh> | 2025-09-21 00:08:43 -0400 |
| commit | 38d25483657e82eb546ecb8566214f830efba461 (patch) | |
| tree | b9568debc69bffc2595df42266c5550cdf05f3d2 /src | |
| parent | 9ddf37ad959faaca72f1634dc61439cdd3c585f1 (diff) | |
refactoring ftw (now w/ README updates!)
Diffstat (limited to 'src')
| -rw-r--r-- | src/backlight.c | 28 | ||||
| -rw-r--r-- | src/backlight.h | 7 | ||||
| -rw-r--r-- | src/common.h | 14 | ||||
| -rw-r--r-- | src/config.h | 22 | ||||
| -rw-r--r-- | src/consts.h | 9 | ||||
| -rw-r--r-- | src/keypress_thread.c | 32 | ||||
| -rw-r--r-- | src/light_thread.c | 42 | ||||
| -rw-r--r-- | src/main.c | 126 |
8 files changed, 159 insertions, 121 deletions
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; +} @@ -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); |
