-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrecorder.c
251 lines (212 loc) · 5 KB
/
recorder.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
#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>
#include <stdlib.h>
#include <poll.h>
#define EV_RELEASED 0
struct input_event_and_dev
{
int dev;
struct input_event event;
};
unsigned int storeInput(char *device, char *device2, struct input_event_and_dev *eventList, int maxSize)
{
printf("Starting recording, press F4 to stop recording \n");
// Read the key back from the keyboard buffer
int fd1 = open(device, O_RDONLY);
int fd2 = open(device2, O_RDONLY);
if( fd1 <= 0 || fd2 <= 0 ){
perror("Error: ");
if( fd1 > 0 ){
close(fd1);
}
if( fd2 > 0 ){
close(fd2);
}
return 0;
}
unsigned int index = 0;
struct pollfd pfds[2];
pfds[0].fd = fd1;
pfds[0].events = POLLIN;
pfds[1].fd = fd2;
pfds[1].events = POLLIN;
int exit = 0;
for(;;)
{
poll(pfds, 2, -1);
for( int i = 0; i < 2; i++ ){
if (pfds[i].revents & POLLIN) {
if( read(pfds[i].fd, &eventList[index].event, sizeof(struct input_event)) != sizeof(struct input_event))
{
printf("Error: \n");
exit = 1;
break;
}
struct input_event *event = &eventList[index].event;
if( index+1 >= maxSize || (i == 0 && event->code == KEY_F4))
{
exit = 1;
break;
}
eventList[index].dev = i;
index++;
// printf("%ld,%ld: read back scan_code is: %d %c \n", event->time.tv_sec, event->time.tv_usec, event->value, event->code);
}
}
if( exit == 1 ){
break;
}
}
close(fd1);
close(fd2);
return index;
}
void replay(char *device, char *device2, struct input_event_and_dev *eventList, unsigned int eventAmount )
{
printf("Replay starts.\n");
int fd[2];
fd[0] = open(device, O_RDWR|O_SYNC);
fd[1] = open(device2, O_RDWR|O_SYNC);
if( fd[0] <= 0 || fd[1] <= 0 ){
perror("Error: ");
if( fd[0] > 0 ){
close(fd[0]);
}
if( fd[1] > 0 ){
close(fd[1]);
}
return;
}
struct pollfd pfds[1];
pfds[0].fd = fd[0];
pfds[0].events = POLLIN;
write(fd[eventList[0].dev], &eventList[0].event, sizeof(struct input_event));
for( unsigned int i = 1; i < eventAmount; i++ )
{
suseconds_t usec = eventList[i].event.time.tv_usec - eventList[i-1].event.time.tv_usec;
time_t sec = eventList[i].event.time.tv_sec - eventList[i-1].event.time.tv_sec;
if( usec > 0 )
{
usleep(usec);
}
else if( usec < 0 )
{
usleep(1000000+usec);
sec -= 1;
}
for( int j = 0; j < sec; j++ )
{
usleep(1000000);
}
poll(pfds, 1, 0);
if (pfds[0].revents & POLLIN) {
// Keyboard action, abort
printf("Aborted by keyboard action\n");
break;
}
struct input_event* event = &eventList[i].event;
// printf("Writing dev: %d, type %d, code %d, value %d\n", eventList[i].dev, event->type, event->code, event->value );
write(fd[eventList[i].dev], event, sizeof(struct input_event));
}
close(fd[0]);
close(fd[1]);
printf("Replay ended.\n");
}
int waitKey(char *device)
{
int fd = 0;
if( (fd = open(device, O_RDONLY)) > 0 )
{
int exit = 0;
struct input_event event;
printf("Press F7 to repeat, F8 to record, F9 to quit.\n");
for(;;)
{
read(fd, &event, sizeof(struct input_event));
if( event.value == EV_RELEASED && event.code == KEY_F7)
{
close(fd);
return 1;
}
if( event.value == EV_RELEASED && event.code == KEY_F9)
{
close(fd);
return 0;
}
if( event.value == EV_RELEASED && event.code == KEY_F8)
{
close(fd);
return 2;
}
}
}else{
perror("Error: ");
}
return -1;
}
/**
* Execute command and return number returned by the output of that command.
*/
int getDev(char *command){
FILE *fp;
char output[3];
fp = popen(command, "r");
if (fp == NULL) {
printf("Failed to run command\n" );
return -1;
}
if (fgets(output, sizeof(output), fp) != NULL) {
pclose(fp);
return atoi(output);
}
pclose(fp);
return -1;
}
int getMouse(){
return getDev("ls /dev/input/by-path/ -la | grep -oh \"event-mouse -> ../event[0-9]*\" | grep -oh \"[0-9]*\"");
}
int getKeyboard(){
return getDev("ls /dev/input/by-path/ -la | grep -oh \"event-kbd -> ../event[0-9]*\" | grep -oh \"[0-9]*\"");
}
int main()
{
int fd = 0;
int mouseDev = getMouse();
int keyboardDev = getKeyboard();
char device[21];
char device2[21];
sprintf(device, "/dev/input/event%d", keyboardDev);
sprintf(device2, "/dev/input/event%d", mouseDev);
printf("keyboard: %s\n", device);
printf("mouse: %s\n", device2);
int fd1 = 0;
int maxInputsToCollect = 1000000;
struct input_event_and_dev *eventList = malloc(sizeof(struct input_event_and_dev)*maxInputsToCollect);
unsigned int eventAmount = 0;
for(;;)
{
int res = waitKey(device);
if( res == -1 ){
printf("Failed to read input device. Only root has read access to input devices.\n");
break;
}
if( res == 0 ){
break;
}
if( res == 2 ){
eventAmount = storeInput( device, device2, eventList, maxInputsToCollect );
continue;
}
if( eventAmount == 0 ){
printf("Record first with F8\n");
}
else{
replay( device, device2, eventList, eventAmount );
}
}
free(eventList);
eventList = 0;
return 0;
}