aboutsummaryrefslogtreecommitdiff
path: root/src/key_tracker.c
blob: 205e29a52ca14fb1275150e9883f16bc548cd4bf (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
// key tracker

#include <linux/input.h>
#include <sys/time.h>
#include <stdlib.h>

#include "report.h"
#include "common.h"

keypresses_t keypresses;

extern FILE* report_fp;

void log_key(int key) {
	struct timeval press_time;
	keypress_t keypress;

	gettimeofday(&press_time, NULL);

	keypress.when = press_time.tv_sec + (press_time.tv_usec / 1000000.0);
	keypress.what = key;

	if (keypresses.keys == NULL) {
		keypresses.keys = calloc(KEYPRESSES_START_SIZE, sizeof(keypress_t));
		keypresses.length = 0;
		keypresses.size = KEYPRESSES_START_SIZE;
	}

	if (keypresses.length >= keypresses.size) {
		keypresses.keys = reallocarray(keypresses.keys, keypresses.size + KEYPRESSES_ADDITIONAL_SIZE, sizeof(keypress_t));
		keypresses.size += KEYPRESSES_ADDITIONAL_SIZE;

		gen_report(report_fp);
	}

	keypresses.keys[keypresses.length++] = keypress;

	if (key == KEY_ENTER) {
		gen_report(report_fp);
	}
}