/* one-shot uinput click at absolute screen coords (touch-like), then exits */ #include #include #include #include #include #include static int fd; static void ev(unsigned short t, unsigned short c, int v) { struct input_event e; memset(&e, 0, sizeof e); e.type = t; e.code = c; e.value = v; if (write(fd, &e, sizeof e) < 0) perror("write"); } static void tapkey(int code) { ev(EV_KEY, code, 1); ev(EV_SYN, SYN_REPORT, 0); usleep(15000); ev(EV_KEY, code, 0); ev(EV_SYN, SYN_REPORT, 0); usleep(15000); } static int keyfor(char c, int *shift) { *shift = 0; if (c >= 'a' && c <= 'z') return KEY_A + (c - 'a'); if (c >= 'A' && c <= 'Z') { *shift = 1; return KEY_A + (c - 'A'); } if (c >= '1' && c <= '9') return KEY_1 + (c - '1'); if (c == '0') return KEY_0; switch (c) { case '-': return KEY_MINUS; case '_': *shift = 1; return KEY_MINUS; case '.': return KEY_DOT; case ',': return KEY_COMMA; case '/': return KEY_SLASH; case ' ': return KEY_SPACE; case '=': return KEY_EQUAL; case ';': return KEY_SEMICOLON; case '~': return KEY_BACKSPACE; } return -1; } int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "usage: inject2 x y | move x y | key KEYCODE\n"); return 1; } fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); if (fd < 0) { perror("uinput"); return 1; } if (!strcmp(argv[1], "key")) { if (argc < 3) return 1; ioctl(fd, UI_DEV_SETUP, &(struct uinput_setup){ .id.bustype = 0x11, .id.vendor = 2, .id.product = 3, .id.version = 4, .name = "vkbd" }); for (int k = KEY_A; k <= KEY_Z; k++) ioctl(fd, UI_SET_EVBIT, EV_KEY), ioctl(fd, UI_SET_KEYBIT, k); for (int k = KEY_1; k <= KEY_9; k++) ioctl(fd, UI_SET_KEYBIT, k); ioctl(fd, UI_SET_KEYBIT, KEY_0); ioctl(fd, UI_SET_KEYBIT, KEY_TAB); ioctl(fd, UI_SET_KEYBIT, KEY_ENTER); ioctl(fd, UI_DEV_CREATE, NULL); usleep(300000); int code = atoi(argv[2]); ev(EV_KEY, code, 1); ev(EV_SYN, SYN_REPORT, 0); usleep(50000); ev(EV_KEY, code, 0); ev(EV_SYN, SYN_REPORT, 0); usleep(100000); ioctl(fd, UI_DEV_DESTROY, NULL); close(fd); return 0; } if (!strcmp(argv[1], "paste")) { ioctl(fd, UI_DEV_SETUP, &(struct uinput_setup){ .id.bustype = 0x11, .id.vendor = 2, .id.product = 3, .id.version = 4, .name = "vkbd" }); ioctl(fd, UI_SET_EVBIT, EV_KEY); for (int k = KEY_A; k <= KEY_Z; k++) ioctl(fd, UI_SET_KEYBIT, k); ioctl(fd, UI_SET_KEYBIT, KEY_LEFTCTRL); ioctl(fd, UI_SET_KEYBIT, KEY_ENTER); ioctl(fd, UI_DEV_CREATE, NULL); usleep(300000); ev(EV_KEY, KEY_LEFTCTRL, 1); ev(EV_SYN, SYN_REPORT, 0); usleep(20000); tapkey(KEY_V); ev(EV_KEY, KEY_LEFTCTRL, 0); ev(EV_SYN, SYN_REPORT, 0); usleep(100000); ioctl(fd, UI_DEV_DESTROY, NULL); close(fd); return 0; } if (!strcmp(argv[1], "text")) { if (argc < 3) return 1; ioctl(fd, UI_DEV_SETUP, &(struct uinput_setup){ .id.bustype = 0x11, .id.vendor = 2, .id.product = 3, .id.version = 4, .name = "vkbd" }); ioctl(fd, UI_SET_EVBIT, EV_KEY); for (int k = KEY_A; k <= KEY_Z; k++) ioctl(fd, UI_SET_KEYBIT, k); for (int k = KEY_1; k <= KEY_9; k++) ioctl(fd, UI_SET_KEYBIT, k); ioctl(fd, UI_SET_KEYBIT, KEY_0); ioctl(fd, UI_SET_KEYBIT, KEY_TAB); ioctl(fd, UI_SET_KEYBIT, KEY_ENTER); ioctl(fd, UI_SET_KEYBIT, KEY_LEFTSHIFT); ioctl(fd, UI_SET_KEYBIT, KEY_RIGHTSHIFT); ioctl(fd, UI_SET_KEYBIT, KEY_MINUS); ioctl(fd, UI_SET_KEYBIT, KEY_DOT); ioctl(fd, UI_SET_KEYBIT, KEY_COMMA); ioctl(fd, UI_SET_KEYBIT, KEY_SLASH); ioctl(fd, UI_SET_KEYBIT, KEY_SPACE); ioctl(fd, UI_SET_KEYBIT, KEY_EQUAL); ioctl(fd, UI_SET_KEYBIT, KEY_SEMICOLON); ioctl(fd, UI_SET_KEYBIT, KEY_BACKSPACE); ioctl(fd, UI_DEV_CREATE, NULL); usleep(300000); for (char *p = argv[2]; *p; p++) { int sh, code = keyfor(*p, &sh); if (code < 0) continue; if (sh) ev(EV_KEY, KEY_LEFTSHIFT, 1); tapkey(code); if (sh) { ev(EV_KEY, KEY_LEFTSHIFT, 0); ev(EV_SYN, SYN_REPORT, 0); usleep(15000); } usleep(45000); } usleep(100000); ioctl(fd, UI_DEV_DESTROY, NULL); close(fd); return 0; } if (!strcmp(argv[1], "swipe") && argc >= 6) { int x1 = atoi(argv[2]), y1 = atoi(argv[3]), x2 = atoi(argv[4]), y2 = atoi(argv[5]); ioctl(fd, UI_DEV_SETUP, &(struct uinput_setup){ .id.bustype = 0x11, .id.vendor = 2, .id.product = 3, .id.version = 4, .name = "vt" }); ioctl(fd, UI_SET_EVBIT, EV_KEY); ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH); ioctl(fd, UI_SET_EVBIT, EV_ABS); int codes[4] = { ABS_X, ABS_Y, ABS_MT_POSITION_X, ABS_MT_POSITION_Y }; for (int i = 0; i < 4; i++) { ioctl(fd, UI_SET_ABSBIT, codes[i]); struct uinput_abs_setup s = { .code = codes[i], .absinfo = { .minimum = 0, .maximum = 1079, .resolution = 10 } }; ioctl(fd, UI_ABS_SETUP, &s); } ioctl(fd, UI_SET_ABSBIT, ABS_MT_SLOT); ioctl(fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID); ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT); ioctl(fd, UI_DEV_CREATE, NULL); usleep(300000); ev(EV_ABS, ABS_MT_TRACKING_ID, 0); ev(EV_ABS, ABS_MT_POSITION_X, x1); ev(EV_ABS, ABS_MT_POSITION_Y, y1); ev(EV_KEY, BTN_TOUCH, 1); ev(EV_SYN, SYN_REPORT, 0); usleep(40000); for (int t = 1; t <= 10; t++) { ev(EV_ABS, ABS_MT_POSITION_X, x1 + (x2 - x1) * t / 10); ev(EV_ABS, ABS_MT_POSITION_Y, y1 + (y2 - y1) * t / 10); ev(EV_SYN, SYN_REPORT, 0); usleep(25000); } ev(EV_KEY, BTN_TOUCH, 0); ev(EV_ABS, ABS_MT_TRACKING_ID, -1); ev(EV_SYN, SYN_REPORT, 0); usleep(50000); ioctl(fd, UI_DEV_DESTROY, NULL); close(fd); return 0; } if (argc < 3) return 1; int x = atoi(argv[2]), y = atoi(argv[3]); ioctl(fd, UI_DEV_SETUP, &(struct uinput_setup){ .id.bustype = 0x11, .id.vendor = 2, .id.product = 3, .id.version = 4, .name = "vt" }); ioctl(fd, UI_SET_EVBIT, EV_KEY); ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH); ioctl(fd, UI_SET_EVBIT, EV_ABS); int codes[4] = { ABS_X, ABS_Y, ABS_MT_POSITION_X, ABS_MT_POSITION_Y }; int maxs[4] = { 1919, 1079, 1919, 1079 }; for (int i = 0; i < 4; i++) { ioctl(fd, UI_SET_ABSBIT, codes[i]); struct uinput_abs_setup s = { .code = codes[i], .absinfo = { .minimum = 0, .maximum = maxs[i], .resolution = 10 } }; ioctl(fd, UI_ABS_SETUP, &s); } ioctl(fd, UI_SET_ABSBIT, ABS_MT_SLOT); ioctl(fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID); ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT); ioctl(fd, UI_DEV_CREATE, NULL); usleep(300000); /* let InputReader settle */ if (!strcmp(argv[1], "move")) { ev(EV_ABS, ABS_MT_POSITION_X, x); ev(EV_ABS, ABS_MT_POSITION_Y, y); ev(EV_SYN, SYN_REPORT, 0); usleep(50000); ev(EV_KEY, BTN_TOUCH, 1); ev(EV_SYN, SYN_REPORT, 0); usleep(50000); ev(EV_ABS, ABS_MT_POSITION_X, x + 6); ev(EV_SYN, SYN_REPORT, 0); usleep(50000); ev(EV_ABS, ABS_MT_POSITION_X, x + 10); ev(EV_SYN, SYN_REPORT, 0); usleep(50000); ev(EV_ABS, ABS_MT_POSITION_X, x); ev(EV_SYN, SYN_REPORT, 0); usleep(50000); ev(EV_KEY, BTN_TOUCH, 0); ev(EV_SYN, SYN_REPORT, 0); usleep(50000); ev(EV_ABS, ABS_MT_TRACKING_ID, -1); ev(EV_SYN, SYN_REPORT, 0); usleep(50000); } else { ev(EV_ABS, ABS_MT_TRACKING_ID, 0); ev(EV_ABS, ABS_MT_POSITION_X, x); ev(EV_ABS, ABS_MT_POSITION_Y, y); ev(EV_KEY, BTN_TOUCH, 1); ev(EV_SYN, SYN_REPORT, 0); usleep(120000); ev(EV_KEY, BTN_TOUCH, 0); ev(EV_SYN, SYN_REPORT, 0); usleep(50000); ev(EV_ABS, ABS_MT_TRACKING_ID, -1); ev(EV_SYN, SYN_REPORT, 0); usleep(50000); } ioctl(fd, UI_DEV_DESTROY, NULL); close(fd); return 0; }