-
Notifications
You must be signed in to change notification settings - Fork 34
/
replay.c
270 lines (217 loc) · 7.72 KB
/
replay.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
/*
Copyright (c) 2011-2013, Lorenzo Gomez ([email protected])
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
struct input_event {
struct timeval time;
unsigned short type;
unsigned short code;
unsigned long value;
};
void goSleep(uint64_t nsec)
{
struct timespec timeout0;
struct timespec timeout1;
struct timespec* tmp;
struct timespec* t0 = &timeout0;
struct timespec* t1 = &timeout1;
t0->tv_sec = (long)(nsec / 1000000000);
t0->tv_nsec = (long)(nsec % 1000000000);
printf("Sleeping for %lu sec, %lu nsec\n", t0->tv_sec, t0->tv_nsec);
while ((nanosleep(t0, t1) == (-1)) && (errno == EINTR))
{
tmp = t0;
t0 = t1;
t1 = tmp;
}
}
// from <linux/input.h>
#define EVIOCGVERSION _IOR('E', 0x01, int) /* get driver version */
#define EVIOCGID _IOR('E', 0x02, struct input_id) /* get device ID */
#define EVIOCGKEYCODE _IOR('E', 0x04, int[2]) /* get keycode */
#define EVIOCSKEYCODE _IOW('E', 0x04, int[2]) /* set keycode */
#define EVIOCGNAME(len) _IOC(_IOC_READ, 'E', 0x06, len) /* get device name */
#define EVIOCGPHYS(len) _IOC(_IOC_READ, 'E', 0x07, len) /* get physical location */
#define EVIOCGUNIQ(len) _IOC(_IOC_READ, 'E', 0x08, len) /* get unique identifier */
#define EVIOCGKEY(len) _IOC(_IOC_READ, 'E', 0x18, len) /* get global keystate */
#define EVIOCGLED(len) _IOC(_IOC_READ, 'E', 0x19, len) /* get all LEDs */
#define EVIOCGSND(len) _IOC(_IOC_READ, 'E', 0x1a, len) /* get all sounds status */
#define EVIOCGSW(len) _IOC(_IOC_READ, 'E', 0x1b, len) /* get all switch states */
#define EVIOCGBIT(ev,len) _IOC(_IOC_READ, 'E', 0x20 + ev, len) /* get event bits */
#define EVIOCGABS(abs) _IOR('E', 0x40 + abs, struct input_absinfo) /* get abs value/limits */
#define EVIOCSABS(abs) _IOW('E', 0xc0 + abs, struct input_absinfo) /* set abs value/limits */
#define EVIOCSFF _IOC(_IOC_WRITE, 'E', 0x80, sizeof(struct ff_effect)) /* send a force effect to a force feedback device */
#define EVIOCRMFF _IOW('E', 0x81, int) /* Erase a force effect */
#define EVIOCGEFFECTS _IOR('E', 0x84, int) /* Report number of effects playable at the same time */
#define EVIOCGRAB _IOW('E', 0x90, int) /* Grab/Release device */
// end <linux/input.h>
#define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x)))
int main(int argc, char *argv[])
{
if(argc == 1)
{
printf("ERROR: Please specify the location of the event file\n\n");
exit(1);
}
int returned = 0;
long lineNumbers = 0;
FILE *file = fopen(argv[1], "r");
if(file)
{
size_t i, j, k, l, m, x;
char buffer[BUFSIZ], *ptr;
fgets(buffer, sizeof buffer, file);
ptr = buffer;
lineNumbers = (long)strtol(ptr, &ptr, 10);
printf("\n\nLine Numbers = %lu\n\n", lineNumbers);
// eventType will then match based on whatever is in the sendEvents.txt file
unsigned short * eventType;
unsigned short * codeData;
unsigned short * typeData;
unsigned long * valueData;
uint64_t * timeArray;
eventType = (unsigned short *) calloc((lineNumbers*1), sizeof(unsigned short));
codeData = (unsigned short *) calloc((lineNumbers*1), sizeof(unsigned short));
typeData = (unsigned short *) calloc((lineNumbers*1), sizeof(unsigned short));
valueData = (unsigned long *) calloc((lineNumbers*1), sizeof(unsigned long));
timeArray = (uint64_t *) calloc((lineNumbers*1), sizeof(uint64_t));
if(eventType == NULL)
printf("eventType failed malloc\n");
if(codeData == NULL)
printf("codeData failed malloc\n");
if(typeData == NULL)
printf("typeData failed malloc\n");
if(valueData == NULL)
printf("valueData failed malloc\n");
if(timeArray == NULL)
printf("timeArray failed malloc\n");
int everyOther = 0;
for(i = 0, l = 0, m = 0; fgets(buffer, sizeof buffer, file); ++i)
{
if(everyOther == 1)
{
for(j = 0, ptr = buffer; j < 4; ++j, ++ptr)
{
if(j == 0)
eventType[m] = (unsigned short)strtoul(ptr, &ptr, 10);
else if(j == 1)
codeData[m] = (unsigned short)strtoul(ptr, &ptr, 10);
else if(j == 2)
typeData[m] = (unsigned short)strtoul(ptr, &ptr, 10);
else if(j == 3)
valueData[m] = (unsigned long)strtoul(ptr, &ptr, 10);
}
m++;
everyOther = 0;
}
else
{
ptr = buffer;
timeArray[l] = (uint64_t)strtoull(ptr, &ptr, 10);
everyOther = 1;
l++;
}
}
fclose(file);
//======== Start Sending Events ============
char device[] = "/dev/input/event ";
//[16] is for the event input number
char* deviceP = device;
int fd;
j=0,k=0;
// For each of the line numbers get the event, validate it, and then write it
while(k < lineNumbers)
{
deviceP[16] = eventType[k]+48; //add 48 to get to the ascii char
fd = open(deviceP, O_RDWR);
int ret;
int version;
// Make sure opening the device opens properly
if(fd <= 0)
{
fprintf(stderr, "could not open %s, %s\n", *(&deviceP), strerror(errno));
return 1;
}
if (ioctl(fd, EVIOCGVERSION, &version))
{
fprintf(stderr, "could not get driver version for %s, %s\n", *(&deviceP), strerror(errno));
return 1;
}
struct input_event checkEvent[5];
int valid = 0;
if(timeArray[j] == 0)
{
// Prep the event for checking, store the type, code, value in checkEvent
l = 0;
while((timeArray[j] == 0) && (j < lineNumbers)) //check the next one, but not if at end of array
{
checkEvent[l].type = codeData[k];
checkEvent[l].code = typeData[k];
checkEvent[l].value = valueData[k];
j++;
k++;
l++;
valid++;
}
}
else
{
// Sleep for time interval calculated in Translate
printf("%d. ", k);
goSleep(timeArray[j]);
checkEvent[0].type = codeData[k];
checkEvent[0].code = typeData[k];
checkEvent[0].value = valueData[k];
j++;
k++;
valid = 1;
}
struct input_event event[valid];
memset(&event, 0, sizeof(event));
for(x = 0; x < valid; x++)
{
event[x].type = checkEvent[x].type;
event[x].code = checkEvent[x].code;
event[x].value = checkEvent[x].value;
}
// ** Write the event that we just got from checkEvent **
ret = write(fd, &event, sizeof(event));
if(ret < sizeof(event))
{
fprintf(stderr, "write event failed, %s\n", strerror(errno));
//should exit...
}
}
}
else // fopen() returned NULL
{
//perror(filename);
perror(argv[1]);
}
return 0;
}