adb-forward-touch

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

adb-forward-touch.c (2221B)


      1 #include <linux/uinput.h>
      2 #include <stdio.h>
      3 #include <fcntl.h>
      4 #include <string.h>
      5 #include <stdlib.h>
      6 #include <unistd.h>
      7 #include <stdbool.h>
      8 
      9 void set_axis(int fd, unsigned short code, int max)
     10 {
     11   struct uinput_abs_setup abs_setup;
     12   memset(&abs_setup, 0, sizeof abs_setup);
     13   abs_setup.code = code;
     14   abs_setup.absinfo.maximum = max;
     15   ioctl(fd, UI_SET_ABSBIT, code);
     16   ioctl(fd, UI_ABS_SETUP, &abs_setup);
     17 }
     18 
     19 static char adb_prefix[] = "adb exec-out getevent ";
     20 
     21 int main(int argc, char *argv[])
     22 {
     23    struct uinput_setup usetup;
     24    int i = 50;
     25 
     26    if(argc < 2) {
     27        system("adb shell getevent -p");
     28        fprintf(stderr, "\nUsage: %s /dev/input/eventX\n\n  where /dev/input/eventX matches the touchscreen device of your phone\n" , argv[0]);
     29        exit(1);
     30    }
     31    char * adb_command = malloc(strlen(argv[1]) + strlen(adb_prefix));
     32    strcpy(adb_command, adb_prefix);
     33    strcat(adb_command, argv[1]);
     34 
     35    FILE *from_android = popen(adb_command, "r");
     36 
     37    int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
     38    if(fd < 0) {
     39        perror("Can't open /dev/uinput");
     40        exit(1);
     41    }
     42 
     43 
     44    ioctl(fd, UI_SET_EVBIT, EV_KEY);
     45    ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH);
     46 
     47    ioctl(fd, UI_SET_EVBIT, EV_ABS);
     48 
     49    set_axis(fd, ABS_X, 1079);
     50    set_axis(fd, ABS_Y, 1919);
     51    set_axis(fd, ABS_PRESSURE, 255);
     52    set_axis(fd, ABS_MT_SLOT, 9);
     53    set_axis(fd, ABS_MT_TOUCH_MAJOR, 15);
     54    set_axis(fd, ABS_MT_TOUCH_MINOR, 15);
     55    set_axis(fd, ABS_MT_ORIENTATION, 1);
     56    set_axis(fd, ABS_MT_POSITION_X, 1079);
     57    set_axis(fd, ABS_MT_POSITION_Y, 1919);
     58    set_axis(fd, ABS_MT_TRACKING_ID, 65535);
     59    set_axis(fd, ABS_MT_TOOL_TYPE, 15);
     60    set_axis(fd, ABS_MT_PRESSURE, 255);
     61 
     62    ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
     63 
     64    memset(&usetup, 0, sizeof(usetup));
     65    usetup.id.bustype = BUS_USB;
     66    usetup.id.vendor = 0x06cb;
     67    usetup.id.product = 0x0af9;
     68    strcpy(usetup.name, "Synaptics s3203b");
     69    ioctl(fd, UI_DEV_SETUP, &usetup);
     70 
     71    ioctl(fd, UI_DEV_CREATE);
     72 
     73    while (true) {
     74       struct input_event ie;
     75       if(fscanf(from_android, "%hx %hx %x",
     76 		&ie.type, &ie.code, &ie.value) == EOF)
     77 	break;
     78       if(write(fd, &ie, sizeof(ie)) < 0)
     79 	break;
     80    }
     81 
     82    ioctl(fd, UI_DEV_DESTROY);
     83    close(fd);
     84 
     85    return 0;
     86 }