diff --git a/capture.c b/capture.c index b68a312..04ed6fc 100644 --- a/capture.c +++ b/capture.c @@ -62,7 +62,7 @@ int dump_event(struct timespec* start, int fd, char* type) { return 1; } - printf("%ld.%ld,%s,%s\n", + printf("%ld %ld,%s,%s\n", diff.tv_sec, diff.tv_nsec, type, diff --git a/playback.c b/playback.c index 3c480fb..1f47a60 100644 --- a/playback.c +++ b/playback.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -22,6 +23,45 @@ void emit(int fd, int type, int code, int val) { 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[]) { if(argc < 2) { @@ -48,7 +88,9 @@ int read_file(char* filename) { } while((read = getline(&line, &len, fp)) != -1) { - printf("%s", line); + if(handle_line(line)) { + return 1; + } } }