Add logic to loop and additional error information on sleep failure.
This commit is contained in:
parent
a3cb632170
commit
0d0d14cc39
52
playback.c
52
playback.c
|
@ -10,11 +10,14 @@
|
|||
#include <linux/uinput.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
static volatile int is_running = 1;
|
||||
|
||||
int handle_udevice(char* details, int udevice_fd);
|
||||
int handle_line(char* line, int udevice_fd);
|
||||
int handle_mouse(char* details, int udevice_fd);
|
||||
int read_file(char* filename, int udevice_fd);
|
||||
int setup_udevice();
|
||||
void sigint_handler(int dummy);
|
||||
void teardown_udevice(int fd);
|
||||
|
||||
void emit(int fd, int type, int code, int val) {
|
||||
|
@ -81,7 +84,12 @@ int handle_line(char* line, int udevice_fd) {
|
|||
// printf("Read %ld %ld\n", seconds, nanos);
|
||||
// printf("Sleep %ld %ld\n", to_sleep.tv_sec, to_sleep.tv_nsec);
|
||||
while(nanosleep(&to_sleep, &remaining) == -1) {
|
||||
printf("Sleep harder\n");
|
||||
if(!is_running) {
|
||||
return 0;
|
||||
}
|
||||
perror("nanosleep error");
|
||||
printf("Attempted %ld.%ld sleep\n", to_sleep.tv_sec, to_sleep.tv_nsec);
|
||||
printf("Need %ld.%ld more seconds for total sleep\n", remaining.tv_sec, remaining.tv_nsec);
|
||||
to_sleep.tv_sec = remaining.tv_sec;
|
||||
to_sleep.tv_nsec = remaining.tv_nsec;
|
||||
}
|
||||
|
@ -97,8 +105,8 @@ int handle_line(char* line, int udevice_fd) {
|
|||
timer_seconds = seconds;
|
||||
timer_nanos = nanos;
|
||||
|
||||
printf("%ld %ld\tslept %ld %ld\n",
|
||||
total_seconds, total_nanos, to_sleep.tv_sec, to_sleep.tv_nsec);
|
||||
// printf("%ld %ld\tslept %ld %ld\n",
|
||||
// total_seconds, total_nanos, to_sleep.tv_sec, to_sleep.tv_nsec);
|
||||
if(type == 'k') {
|
||||
return handle_keyboard(details, udevice_fd);
|
||||
} else if(type == 'm') {
|
||||
|
@ -151,15 +159,34 @@ int handle_mouse(char* details, int udevice_fd) {
|
|||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int repeat = 1;
|
||||
if(argc < 2) {
|
||||
printf("Please provide a capture file.");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if(argc == 3) {
|
||||
int matched = sscanf(argv[2], "%d", &repeat);
|
||||
if(matched != 1) {
|
||||
fprintf(stderr, "Failed to read repeat value.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Repeating %d times\n", repeat);
|
||||
}
|
||||
|
||||
signal(SIGINT, sigint_handler);
|
||||
|
||||
for(int i = 3; i > 0; i--) {
|
||||
fprintf(stderr, "Playing back in %d seconds\n", i);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
int udevice_fd = setup_udevice();
|
||||
if(read_file(argv[1], udevice_fd)) {
|
||||
result = EXIT_FAILURE;
|
||||
for(int i = 0; is_running && i < repeat; i++) {
|
||||
fprintf(stderr, "Repeat %d/%d\n", i+1, repeat);
|
||||
if(read_file(argv[1], udevice_fd)) {
|
||||
result = EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
teardown_udevice(udevice_fd);
|
||||
return result;
|
||||
|
@ -177,16 +204,13 @@ int read_file(char* filename, int udevice_fd) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
for(int i = 3; i > 0; i--) {
|
||||
fprintf(stderr, "playing back in %d seconds\n", i);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
while((read = getline(&line, &len, fp)) != -1) {
|
||||
while(is_running && (read = getline(&line, &len, fp)) != -1) {
|
||||
if(handle_line(line, udevice_fd)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int setup_udevice() {
|
||||
|
@ -205,7 +229,7 @@ int setup_udevice() {
|
|||
|
||||
// Add mouse buttons
|
||||
ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
|
||||
ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
|
||||
ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT);
|
||||
|
||||
ioctl(fd, UI_SET_EVBIT, EV_REL);
|
||||
ioctl(fd, UI_SET_RELBIT, REL_X);
|
||||
|
@ -231,6 +255,10 @@ int setup_udevice() {
|
|||
return fd;
|
||||
}
|
||||
|
||||
void sigint_handler(int dummy) {
|
||||
is_running = 0;
|
||||
}
|
||||
|
||||
void teardown_udevice(int fd) {
|
||||
ioctl(fd, UI_DEV_DESTROY);
|
||||
close(fd);
|
||||
|
|
Loading…
Reference in New Issue