aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: dab70afb60aa0d65d5ee8dfe425966d81c8cae4a (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <linux/input.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#include <time.h>

#include "common.h"

#include "key_tracker.h"
#include "report.h"

FILE* report_fp;
FILE* evdev_fp;

void handler(int _) {
	gen_report(report_fp);

	fclose(report_fp);
	fclose(evdev_fp);

	exit(0);
}

void wait_thread(pid_t keypress_pid) {
	time_t start_time = time(NULL);
	struct tm* start_tm = localtime(&start_time);
	int start_hour = start_tm->tm_hour;

	while (1) {
		time_t now = time(NULL);
		struct tm* now_tm = localtime(&now);

		if (now_tm->tm_hour != start_hour) {
			kill(keypress_pid, SIGQUIT);

			exit(0);
		}

		sleep(1);
	}
}

int main(int argc, char* argv[]) {
	pid_t keypress_pid = getpid();
	pid_t wait_pid = fork();

	if (wait_pid == 0) wait_thread(keypress_pid);

	evdev_fp = fopen(KEYBOARD_EVDEV, "r");
	report_fp = NULL;

	char* report_path = NULL;

	struct input_event pressed_key;

	int perkey_pressed[PERKEY_LEN + 1];
	int keys_pressed = 0;

	struct timeval start_time;
	struct timeval press_time;

	gettimeofday(&start_time, NULL);

	struct tm* start_tm = localtime(&start_time.tv_sec);

	asprintf(&report_path, REPORT_PATH, start_tm->tm_year + 1900, start_tm->tm_mon + 1, start_tm->tm_mday, start_tm->tm_hour);

	report_fp = fopen(report_path, "w");

	signal(SIGHUP, handler);
	signal(SIGQUIT, handler);
	signal(SIGTERM, handler);

	do {
		fread(&pressed_key, sizeof(pressed_key), 1, evdev_fp);

		if (pressed_key.value == 1) {
			log_key(pressed_key.code);
		}
	} while (1);

	return 0;
}