Add sleep logic in playback.

This also removes the "." between the seconds and nanos so that we don't mistake it
for a float, which it isn't.
This commit is contained in:
Eli Ribble 2021-08-27 12:44:14 -06:00
parent 6ed5162e8e
commit 61843f23c3
2 changed files with 44 additions and 2 deletions

View File

@ -62,7 +62,7 @@ int dump_event(struct timespec* start, int fd, char* type) {
return 1; return 1;
} }
printf("%ld.%ld,%s,%s\n", printf("%ld %ld,%s,%s\n",
diff.tv_sec, diff.tv_sec,
diff.tv_nsec, diff.tv_nsec,
type, type,

View File

@ -4,6 +4,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include <linux/uinput.h> #include <linux/uinput.h>
@ -22,6 +23,45 @@ void emit(int fd, int type, int code, int val) {
write(fd, &ie, sizeof(ie)); write(fd, &ie, sizeof(ie));
} }
int handle_line(char* line) {
static time_t timer_seconds = 0;
static long timer_nanos = 0;
time_t seconds;
long nanos;
char type;
char details[32];
struct timespec to_sleep;
struct timespec remaining;
int matched = sscanf(line, "%ld %ld,%c,%s\n", &seconds, &nanos, &type, details);
if(matched == 0) {
printf("Line '%s' appears incorrect. Exiting", line);
return 1;
} else if(matched < 4) {
printf("Only matched %d", matched);
return 1;
}
to_sleep.tv_sec = seconds - timer_seconds;
to_sleep.tv_nsec = nanos - timer_nanos;
if(to_sleep.tv_nsec < 0) {
--to_sleep.tv_nsec;
to_sleep.tv_nsec += 1000000000L;
}
printf("%s", line);
// printf("Timer %ld %ld\n", timer_seconds, timer_nanos);
// printf("Read %ld %ld\n", seconds, nanos);
// printf("Sleep %ld %ld\n", to_sleep.tv_sec, to_sleep.tv_nsec);
int result = nanosleep(&to_sleep, &remaining);
while(nanosleep(&to_sleep, &remaining) == -1) {
to_sleep.tv_sec = remaining.tv_sec;
to_sleep.tv_nsec = remaining.tv_nsec;
}
timer_seconds = seconds;
timer_nanos = nanos;
return 0;
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if(argc < 2) { if(argc < 2) {
@ -48,7 +88,9 @@ int read_file(char* filename) {
} }
while((read = getline(&line, &len, fp)) != -1) { while((read = getline(&line, &len, fp)) != -1) {
printf("%s", line); if(handle_line(line)) {
return 1;
}
} }
} }