-
Notifications
You must be signed in to change notification settings - Fork 0
/
ba-auto.c
346 lines (318 loc) · 13.4 KB
/
ba-auto.c
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* Copyright (c) 2017, William Breathitt Gray
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
struct event_input{
INPUT ei;
struct event_input *next;
unsigned long delay_time;
};
unsigned first_event = 1;
unsigned long first_event_delay;
unsigned long prev_event_time;
unsigned loop = 1;
FILE *record_fp = NULL;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam){
if(nCode == HC_ACTION){
KBDLLHOOKSTRUCT *kbinfo = lParam;
unsigned long flags = 0;
if(LLKHF_EXTENDED & kbinfo->flags){
flags |= KEYEVENTF_EXTENDEDKEY;
}
if(LLKHF_UP & kbinfo->flags){
flags |= KEYEVENTF_KEYUP;
}
unsigned long event_time;
if(first_event){
event_time = first_event_delay;
first_event = 0;
}else{
event_time = kbinfo->time - prev_event_time;
}
fprintf(record_fp, "1 0x%lX 0x%lX 0x%lX 0x%lX 0x%lX\n", (unsigned long)kbinfo->vkCode, (unsigned long)kbinfo->scanCode, flags, event_time, (unsigned long)kbinfo->dwExtraInfo);
prev_event_time = kbinfo->time;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam){
if(nCode == HC_ACTION){
MSLLHOOKSTRUCT *mouseinfo = lParam;
unsigned x = mouseinfo->pt.x;
unsigned y = mouseinfo->pt.y;
RECT desktop;
if(GetWindowRect(GetDesktopWindow(), &desktop)){
x = 65535 * (double)x/desktop.right;
y = 65535 * (double)y/desktop.bottom;
}
unsigned long flags = MOUSEEVENTF_ABSOLUTE;
switch(wParam){
case WM_LBUTTONDOWN:
flags |= MOUSEEVENTF_LEFTDOWN;
break;
case WM_LBUTTONUP:
flags |= MOUSEEVENTF_LEFTUP;
break;
case WM_MOUSEMOVE:
flags |= MOUSEEVENTF_MOVE;
break;
case WM_RBUTTONDOWN:
flags |= MOUSEEVENTF_RIGHTDOWN;
break;
case WM_RBUTTONUP:
flags |= MOUSEEVENTF_RIGHTUP;
break;
}
unsigned long event_time;
if(first_event){
event_time = first_event_delay;
first_event = 0;
}else{
event_time = mouseinfo->time - prev_event_time;
}
fprintf(record_fp, "0 %u %u 0x%lX 0x%lX 0x%lX 0x%lX\n", x, y, (unsigned long)mouseinfo->mouseData, flags, event_time, (unsigned long)mouseinfo->dwExtraInfo);
prev_event_time = mouseinfo->time;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
void freeEvents(struct event_input *head){
while(head){
struct event_input *curr = head;
head = head->next;
free(curr);
}
}
unsigned playEvents(FILE *fp){
char buffer[256];
struct event_input *head = NULL;
struct event_input *end = NULL;
while(fgets(buffer, sizeof(buffer), fp)){
struct event_input *curr = calloc(1, sizeof(*curr));
if(!curr){
fprintf(stderr, "Unable to allocate memory for event\n");
goto playEvents_err;
}
if(!end){
head = curr;
}else{
end->next = curr;
}
end = curr;
char *event_info;
const unsigned event_type = strtoul(buffer, &event_info, 0);
if(event_type){
unsigned long wVk;
unsigned long wScan;
unsigned long dwFlags;
unsigned long dwExtraInfo;
const int retval = sscanf(event_info, "%lX %lX %lX %lX %lX", &wVk, &wScan, &dwFlags, &curr->delay_time, &dwExtraInfo);
if(retval < 5){
fprintf(stderr, "Recording file syntax error\n");
goto playEvents_err;
}
curr->ei.type = INPUT_KEYBOARD;
curr->ei.ki.wVk = wVk;
curr->ei.ki.wScan = wScan;
curr->ei.ki.dwFlags = dwFlags;
curr->ei.ki.dwExtraInfo = dwExtraInfo;
}else{
unsigned dx;
unsigned dy;
unsigned long mouseData;
unsigned long dwFlags;
unsigned long dwExtraInfo;
const int retval = sscanf(event_info, "%u %u %lX %lX %lX %lX", &dx, &dy, &mouseData, &dwFlags, &curr->delay_time, &dwExtraInfo);
if(retval < 6){
fprintf(stderr, "Recording file syntax error\n");
goto playEvents_err;
}
curr->ei.type = INPUT_MOUSE;
curr->ei.mi.dx = dx;
curr->ei.mi.dy = dy;
curr->ei.mi.mouseData = mouseData;
curr->ei.mi.dwFlags = dwFlags;
curr->ei.mi.dwExtraInfo = dwExtraInfo;
}
}
if(ferror(fp)){
fprintf(stderr, "Recording file read error\n");
goto playEvents_err;
}
while(loop--){
struct event_input *curr = head;
while(curr){
Sleep(curr->delay_time);
if(!SendInput(1, &curr->ei, sizeof(curr->ei))){
fprintf(stderr, "Event input insertion failure\n");
goto playEvents_err;
}
curr = curr->next;
}
}
freeEvents(head);
return 0;
playEvents_err:
freeEvents(head);
return 1;
}
unsigned recordEvents(const unsigned long delay_record_time, const unsigned long record_time){
HHOOK hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, 0);
if(!hhkLowLevelKybd){
fprintf(stderr, "ERROR: Unable to attach low-level keyboard hook\n");
return 1;
}
HHOOK hhkLowLevelMouse = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, NULL, 0);
if(!hhkLowLevelMouse){
fprintf(stderr, "ERROR: Unable to attach low-level mouse hook\n");
goto err_hhkLowLevelMouse;
}
Sleep(delay_record_time);
const DWORD start_tick_count = GetTickCount();
do{
MSG msg;
if(PeekMessage(&msg, NULL, 0, 0, 0)){
fprintf(stderr, "ERROR: Unable to peek message\n");
goto err_peekmessage;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}while(GetTickCount() - start_tick_count < record_time);
UnhookWindowsHookEx(hhkLowLevelMouse);
UnhookWindowsHookEx(hhkLowLevelKybd);
return 0;
err_peekmessage:
UnhookWindowsHookEx(hhkLowLevelMouse);
err_hhkLowLevelMouse:
UnhookWindowsHookEx(hhkLowLevelKybd);
return 1;
}
int main(void){
unsigned choice;
do{
printf("Enter 0 to record or 1 to play:\n");
char buffer[32] = "";
if(!fgets(buffer, sizeof(buffer), stdin)){
continue;
}
choice = strtoul(buffer, NULL, 0);
}while(choice > 1);
if(choice){
FILE *fp = NULL;
do{
printf("Enter path to recording file:\n");
char path[256] = "";
if(!fgets(path, sizeof(path), stdin)){
continue;
}
path[255] = '\0';
size_t cindex = 254;
while(cindex--){
if(isalnum(path[cindex])){
path[cindex+1] = '\0';
break;
}
};
fp = fopen(path, "r");
if(!fp){
fprintf(stderr, "Error opening %s\n", path);
}else{
break;
}
}while(1);
do{
printf("Enter number of times to loop:\n");
char buffer[32] = "";
if(!fgets(buffer, sizeof(buffer), stdin)){
continue;
}
loop = strtoul(buffer, NULL, 0);
}while(0);
if(playEvents(fp)){
fclose(fp);
return 1;
}
fclose(fp);
printf("Playback complete!\n");
}else{
do{
printf("Enter path to recording file:\n");
char path[256] = "";
if(!fgets(path, sizeof(path), stdin)){
continue;
}
path[255] = '\0';
size_t cindex = 254;
while(cindex--){
if(isalnum(path[cindex])){
path[cindex+1] = '\0';
break;
}
};
record_fp = fopen(path, "w");
if(!record_fp){
fprintf(stderr, "Error opening %s\n", path);
}else{
break;
}
}while(1);
unsigned long delay_record_time;
do{
printf("Enter the time in milliseconds to wait before recording:\n");
char buffer[32] = "";
if(!fgets(buffer, sizeof(buffer), stdin)){
continue;
}
delay_record_time = strtoul(buffer, NULL, 0);
}while(0);
unsigned long record_time;
do{
printf("Enter the time in milliseconds for the recording length:\n");
char buffer[32] = "";
if(!fgets(buffer, sizeof(buffer), stdin)){
continue;
}
record_time = strtoul(buffer, NULL, 0);
}while(0);
do{
printf("Enter the time in milliseconds for the first event's delay value:\n");
char buffer[32] = "";
if(!fgets(buffer, sizeof(buffer), stdin)){
continue;
}
first_event_delay = strtoul(buffer, NULL, 0);
}while(0);
if(recordEvents(delay_record_time, record_time)){
fclose(record_fp);
return 1;
}
fclose(record_fp);
printf("Recording complete!\n");
}
return 0;
}