Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <errno.h>
- #define DEFAULT_SLEEP_TIME (50 * 1000)
- typedef uint32_t __u32;
- typedef uint16_t __u16;
- typedef __signed__ int __s32;
- __attribute__((aligned(1),packed)) struct input_event {
- __u32 time_dummy_1;
- __u32 time_dummy_2;
- __u16 type;
- __u16 code;
- __s32 value;
- };
- typedef struct input_event event_t;
- int convert (char * str) {
- return (int) strtoul(str, NULL, 16);
- }
- #define S_ALL (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)
- int main (int argc, char *argv[]) {
- int i;
- int ret;
- if(argc < 3) {
- fprintf(stderr, "use: %s in-file out-file [sleep_time=%f]\n", argv[0], DEFAULT_SLEEP_TIME);
- return 1;
- }
- FILE * fd_in = fopen(argv[1], "r");
- if (fd_in == NULL) {
- fprintf(stderr, "Can't open input file: %s\n", argv[1]);
- return 1;
- }
- int fd = open(argv[2], O_RDWR);
- if(fd < 0) {
- fprintf(stderr, "could not open %s, %s\n", argv[optind], strerror(errno));
- return 1;
- }
- int sleep_time = DEFAULT_SLEEP_TIME;
- if (argc >= 4) {
- sleep_time = (int) (atof(argv[3]) * 1000 * 1000);
- }
- event_t events[16];
- int count = 0;
- char line[128];
- char type[32];
- char code[32];
- char value[32];
- int num_writes = 0;
- printf("sleep_time = %d\n", sleep_time);
- printf("Writing: ");
- fflush(stdout);
- while (fgets(line, sizeof(line), fd_in) != NULL) {
- // Every newline, flush buffer in the output file.
- if (strlen(line) < 5) {
- for (int i=0; i<count; ++i) {
- ret = write(fd, &events[i], sizeof(events[i]));
- if(ret < sizeof(events[i])) {
- fprintf(stderr, "write events[%d] failed, %s\n", i, strerror(errno));
- return -1;
- }
- }
- count = 0;
- ++num_writes;
- // printf(".");
- // fflush(stdout);
- continue;
- }
- // cache event
- sscanf(line, "%s %s %s", type, code, value);
- memset(&events[count], 0, sizeof(events[count]));
- events[count].type = (__u16) convert(type);
- events[count].code = (__u16) convert(code);
- events[count].value = (__s32) convert(value);
- ++count;
- // clear tmp buffers
- memset(line, 0, sizeof(line));
- memset(type, 0, sizeof(type));
- memset(code, 0, sizeof(code));
- memset(value, 0, sizeof(value));
- usleep(sleep_time);
- }
- printf("\nNum writes: %d\n", num_writes);
- fclose(fd_in);
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment