aboutsummaryrefslogtreecommitdiff
path: root/src/light_thread.c
blob: 20b7bb9d966841862e4a2dc8c9e5aa7f0bab7ee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// XXX signals, includes, open, write instead of fopen, fwrite

#include <stdio.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;

	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 handler(int _) {
	if (time_since_press() > BACKLIGHT_TIME) {
		backlight_off();
	} else {
		backlight_on();

		handler(_);
	}
}

int light_thread(pid_t _keypress_pid) {
	keypress_pid = _keypress_pid;

	signal(SIGCONT, handler);

	while (1) {
		pause();
//		usleep(THREAD_WAIT);
	}

	return 0;
}