aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c78
1 files changed, 47 insertions, 31 deletions
diff --git a/src/main.c b/src/main.c
index d66f483..70424d3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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();
+}