This repository was archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogger.c
More file actions
330 lines (253 loc) · 9.59 KB
/
Copy pathlogger.c
File metadata and controls
330 lines (253 loc) · 9.59 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <stdint.h>
#include <stdlib.h>
#include <Windows.h>
#include "interception/interception.h"
#include "rustedleaf/rl_cdrain_queue.h"
#include "console_handler.h"
#include "logger.h"
#include "utils.h"
#define LOG_BUFFER_SIZE 8192
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
struct logging_options {
uint16_t toggle;
uint16_t line;
uint8_t mode;
uint8_t debug;
};
static const char CSV_HEADER[] = "TYPE,INFO,ID,TIME (ms),ORIGX,ORIGY,ACCELX,ACCELY\n";
static const char MOUSE_MISC_INPUT_FORMAT[] = "MISC INPUT,%s,,%.3e\n";
static const char DEBUG_MESSAGE_FORMAT[] = "DEBUG MSG,%s,,%.3e\n";
static const char MOUSE_INPUT_FORMAT[] = "MOUSE MOVE,,%u,%.3e,%+i,%+i,%+i,%+i\n";
static struct rl_cdrain_queue queue;
static inline const char *mouse_state(const int value) {
switch (value) {
case INTERCEPTION_MOUSE_BUTTON_1_DOWN:
return "LEFT BUTTON DOWN";
case INTERCEPTION_MOUSE_BUTTON_1_UP:
return "LEFT BUTTON UP";
case INTERCEPTION_MOUSE_BUTTON_2_DOWN:
return "RIGHT BUTTON DOWN";
case INTERCEPTION_MOUSE_BUTTON_2_UP:
return "RIGHT BUTTON UP";
case INTERCEPTION_MOUSE_BUTTON_3_DOWN:
return "MIDDLE BUTTON DOWN";
case INTERCEPTION_MOUSE_BUTTON_3_UP:
return "MIDDLE BUTTON UP";
case INTERCEPTION_MOUSE_BUTTON_4_DOWN:
return "BUTTON 4 DOWN";
case INTERCEPTION_MOUSE_BUTTON_4_UP:
return "BUTTON 4 UP";
case INTERCEPTION_MOUSE_BUTTON_5_DOWN:
return "BUTTON 5 DOWN";
case INTERCEPTION_MOUSE_BUTTON_5_UP:
return "BUTTON 5 UP";
case INTERCEPTION_MOUSE_HWHEEL:
return "HORIZONTAL SCROLL";
case INTERCEPTION_MOUSE_WHEEL:
return "VERTICAL SCROLL";
}
return "UNKNOWN INPUT";
}
void push_entry(const struct log_entry *log) {
rl_cdrain_queue_add(&queue, log, 1, sizeof(*log), 0);
}
static DWORD get_time(char *buffer, const size_t len) {
SYSTEMTIME curr_time;
GetLocalTime(&curr_time);
TCHAR time_buffer[32];
int err = GetTimeFormat(MAKELCID(0x0C09, SORT_DEFAULT), 0, NULL, L"HH-mm-ss", time_buffer, (int)(sizeof(time_buffer) / sizeof(*time_buffer)));
if (!err) {
return GetLastError();
}
TCHAR temp[64];
int length = _snwprintf_s(temp, sizeof(temp) / sizeof(*temp), sizeof(temp) / sizeof(*temp), L"%.4i-%.2i-%.2i_%ls",
curr_time.wYear, curr_time.wMonth, curr_time.wDay,
time_buffer);
if (length >= (sizeof(temp) / sizeof(*temp))) {
return ERROR_OUTOFMEMORY;
}
for (size_t i = 0; i < (length + 1); ++i) {
*(buffer + i) = (char)temp[i];
}
return ERROR_SUCCESS;
}
static DWORD get_filename(const char *format, char *buffer, const size_t bufferlen) {
char time[32];
DWORD err = get_time(time, sizeof(time));
if (err) {
return err;
}
int len = snprintf(buffer, bufferlen, format, time);
if (len >= bufferlen) {
return ERROR_OUTOFMEMORY;
}
return ERROR_SUCCESS;
}
void logging_thread(void *param) {
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
protect_thread();
char filename[64];
DWORD terr = get_filename("MAccel-log %s.csv", filename, sizeof(filename));
if (terr) {
printf_to_console("Failed to create timestamp, errno %I32u\n", terr);
return;
}
printf_to_console("Logging to file %s\n", filename);
FILE *logfile;
errno_t err = fopen_s(&logfile, filename, "w");
if (err) {
printf_to_console("Failed to open file \"%s\" for writing! (errno %i)\n", filename, err);
return;
}
/* Init header */
fprintf(logfile, "_%s", CSV_HEADER); /* Prefix with _ because Excel */
static struct log_entry log_entries[4096];
const double perf_freq = get_performance_frequency_ms();
LARGE_INTEGER prev_time;
int do_log = 1;
for (;;) {
const size_t items = rl_cdrain_queue_drain(&queue, log_entries,
sizeof(log_entries) / sizeof(*log_entries), sizeof(*log_entries), 0);
if (!items) {
Sleep(500);
continue;
}
for (size_t i = 0; i < items; ++i) {
struct log_entry *curr = log_entries + i;
if (curr->type == ENTRY_TYPE_START_TIME) {
prev_time = curr->timestamp;
continue;
}
const double time = (curr->timestamp.QuadPart - prev_time.QuadPart) / perf_freq;
if (curr->type == ENTRY_TYPE_CHANGE_LOG_STATUS) {
int new_log = (int) curr->accelx;
if (new_log == do_log) {
continue;
}
do_log ^= 1;
if (new_log) {
fprintf(logfile, DEBUG_MESSAGE_FORMAT, "Logging toggled ON", time);
} else {
fprintf(logfile, DEBUG_MESSAGE_FORMAT, "Logging toggled OFF", time);
}
continue;
}
if (!do_log) {
continue;
}
prev_time = curr->timestamp;
if (curr->type == ENTRY_TYPE_MESSAGE) {
fprintf(logfile, DEBUG_MESSAGE_FORMAT, curr->message, time);
free((void *)curr->message);
continue;
}
if (curr->type == ENTRY_TYPE_MOUSE_MISC) {
const char *state = mouse_state(curr->accelx);
if (!state) {
continue;
}
fprintf(logfile, MOUSE_MISC_INPUT_FORMAT, state, time);
continue;
}
if (curr->type == ENTRY_TYPE_MOUSE_MOVEMENT) {
fprintf(logfile, MOUSE_INPUT_FORMAT, curr->id,
time, curr->unaccelx, curr->unaccely,
curr->accelx, curr->accely);
}
}
if (items != (sizeof(log_entries) / sizeof(*log_entries))) {
fflush(logfile);
Sleep(500); /* Only sleep if the read buffer was not full */
}
}
}
void keylistener_thread(void *param) {
struct logging_options options = *(struct logging_options *)param;
free(param);
COORD debug_line;
debug_line.X = 0;
debug_line.Y = options.line;
int logging_status = options.mode == 2 ? 1 : 0;
InterceptionContext context = interception_create_context();
InterceptionDevice device;
interception_set_filter(context, interception_is_keyboard, INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP |
INTERCEPTION_FILTER_KEY_E0 | INTERCEPTION_FILTER_KEY_E1);
LARGE_INTEGER curr_time;
InterceptionStroke strokes[32];
int read;
while ((read = interception_receive(context, device = interception_wait(context), strokes, sizeof(strokes) / sizeof(*strokes))) > 0) {
interception_send(context, device, strokes, read);
for (size_t i = 0; i < read; ++i) {
const InterceptionKeyStroke *key = (InterceptionKeyStroke *)(strokes + i);
if (key->code != options.toggle) {
continue;
}
if (options.mode == 1 && key->state != INTERCEPTION_KEY_UP) {
continue;
}
if (options.mode == 1) {
logging_status ^= 1;
} else {
int old_state = logging_status;
logging_status = ((key->state & 1) ^ 1);
if (logging_status == old_state) {
continue;
}
}
QueryPerformanceCounter(&curr_time);
const char *message;
if (logging_status) {
message = "Log: ON";
} else {
message = "Log: OFF";
}
if (options.debug) {
printf_to_console_pos(debug_line, message);
}
log_change_status(curr_time, logging_status);
}
}
}
DWORD init_logger(const uint8_t logmode, const uint16_t log_toggle, const uint16_t toggle_line, const uint8_t debug) {
rl_cdrain_queue_init(&queue, 8192*2, sizeof(struct log_entry));
HANDLE thr = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&logging_thread, NULL, 0, NULL);
DWORD err = GetLastError();
if (!thr) {
printf_to_console("Could not start logger thread. Error: %I32u\n", err);
printf_to_console("Logging will not be available.\n");
return err;
}
struct logging_options *options = (struct logging_options *) malloc(sizeof(*options));
if (!options) {
/* TODO: Send debug message and ret 0 */
return ERROR_OUTOFMEMORY;
}
/* Don't create a thread if it's always on... */
if (logmode == 2) {
if (!debug) {
return 0;
}
COORD pos;
pos.X = 0;
pos.Y = toggle_line;
printf_to_console_pos(pos, "Log: ON");
return 0;
}
options->toggle = log_toggle;
options->mode = logmode;
options->line = toggle_line;
options->debug = debug;
thr = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&keylistener_thread, options, 0, NULL);
err = GetLastError();
if (!thr) {
printf_to_console("Could not start keylistener thread. Error: %I32u\n", err);
printf_to_console("Features relying on key listening will not be available.\n");
return err;
}
return 0;
}
#ifdef __cplusplus
}
#endif /* __cplusplus */