smirea

Custom android send-touch script

Apr 17th, 2014
1,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <fcntl.h>
  6. #include <sys/ioctl.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9.  
  10. #define DEFAULT_SLEEP_TIME (50 * 1000)
  11.  
  12. typedef uint32_t        __u32;
  13. typedef uint16_t        __u16;
  14. typedef __signed__ int  __s32;
  15.  
  16. __attribute__((aligned(1),packed)) struct input_event {
  17.     __u32 time_dummy_1;
  18.     __u32 time_dummy_2;
  19.     __u16 type;
  20.     __u16 code;
  21.     __s32 value;
  22. };
  23.  
  24. typedef struct input_event event_t;
  25.  
  26. int convert (char * str) {
  27.     return (int) strtoul(str, NULL, 16);
  28. }
  29.  
  30. #define S_ALL (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)
  31.  
  32. int main (int argc, char *argv[]) {
  33.     int i;
  34.     int ret;
  35.  
  36.     if(argc < 3) {
  37.         fprintf(stderr, "use: %s in-file out-file [sleep_time=%f]\n", argv[0], DEFAULT_SLEEP_TIME);
  38.         return 1;
  39.     }
  40.  
  41.     FILE * fd_in = fopen(argv[1], "r");
  42.     if (fd_in == NULL) {
  43.         fprintf(stderr, "Can't open input file: %s\n", argv[1]);
  44.         return 1;
  45.     }
  46.  
  47.     int fd = open(argv[2], O_RDWR);
  48.     if(fd < 0) {
  49.         fprintf(stderr, "could not open %s, %s\n", argv[optind], strerror(errno));
  50.         return 1;
  51.     }
  52.  
  53.     int sleep_time = DEFAULT_SLEEP_TIME;
  54.     if (argc >= 4) {
  55.         sleep_time = (int) (atof(argv[3]) * 1000 * 1000);
  56.     }
  57.  
  58.     event_t events[16];
  59.     int count = 0;
  60.  
  61.     char line[128];
  62.     char type[32];
  63.     char code[32];
  64.     char value[32];
  65.  
  66.     int num_writes = 0;
  67.     printf("sleep_time = %d\n", sleep_time);
  68.     printf("Writing: ");
  69.     fflush(stdout);
  70.     while (fgets(line, sizeof(line), fd_in) != NULL) {
  71.         // Every newline, flush buffer in the output file.
  72.         if (strlen(line) < 5) {
  73.             for (int i=0; i<count; ++i) {
  74.                 ret = write(fd, &events[i], sizeof(events[i]));
  75.                 if(ret < sizeof(events[i])) {
  76.                     fprintf(stderr, "write events[%d] failed, %s\n", i, strerror(errno));
  77.                     return -1;
  78.                 }
  79.             }
  80.             count = 0;
  81.             ++num_writes;
  82.             // printf(".");
  83.             // fflush(stdout);
  84.             continue;
  85.         }
  86.  
  87.         // cache event
  88.         sscanf(line, "%s %s %s", type, code, value);
  89.         memset(&events[count], 0, sizeof(events[count]));
  90.         events[count].type = (__u16) convert(type);
  91.         events[count].code = (__u16) convert(code);
  92.         events[count].value = (__s32) convert(value);
  93.         ++count;
  94.  
  95.         // clear tmp buffers
  96.         memset(line, 0, sizeof(line));
  97.         memset(type, 0, sizeof(type));
  98.         memset(code, 0, sizeof(code));
  99.         memset(value, 0, sizeof(value));
  100.  
  101.         usleep(sleep_time);
  102.     }
  103.  
  104.     printf("\nNum writes: %d\n", num_writes);
  105.  
  106.     fclose(fd_in);
  107.     close(fd);
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment