Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <linux/input.h>
- #include <linux/uinput.h>
- #include <stdint.h>
- #include <time.h>
- #define release 0
- #define press 1
- #define autorepeat 2
- struct uinput_user_dev ui_dev;
- struct input_event ui_ev;
- int ui_fd, i;
- inline int open_uinput();
- inline int send_event(uint16_t type, uint16_t code, int32_t value);
- inline int send_report();
- inline int send_rel_mouse(int32_t x, int32_t y);
- inline int send_abs_mouse(int32_t x, int32_t y);
- #define send_key_macro(x) \
- inline int send_key_##x(uint16_t key) { \
- return send_event(EV_KEY, key, x); \
- }
- send_key_macro(press);
- send_key_macro(release);
- send_key_macro(autorepeat);
- int main(int argc, char *argv[])
- {
- if(open_uinput() < 0)
- {
- printf("Unable to open uinput.\n");
- return -1;
- }
- usleep(100);
- memset(&ui_ev, 0, sizeof(ui_ev));
- memset(&ui_dev, 0, sizeof(ui_dev));
- strncpy(ui_dev.name, "uinput-test", UINPUT_MAX_NAME_SIZE);
- ui_dev.id.bustype = BUS_VIRTUAL;
- ui_dev.id.vendor = 0x1234;
- ui_dev.id.product = 0xABCD;
- ui_dev.id.version = 1;
- ui_dev.absmin[ABS_X] = 0;
- ui_dev.absmax[ABS_X] = 1023;
- ui_dev.absmin[ABS_Y] = 0;
- ui_dev.absmax[ABS_Y] = 1023;
- ioctl(ui_fd, UI_SET_EVBIT, EV_KEY);
- ioctl(ui_fd, UI_SET_EVBIT, EV_REP);
- for(i = 0; i < KEY_MAX; i++)
- {
- ioctl(ui_fd, UI_SET_KEYBIT, i);
- }
- ioctl(ui_fd, UI_SET_EVBIT, EV_REL);
- ioctl(ui_fd, UI_SET_EVBIT, EV_ABS);
- ioctl(ui_fd, UI_SET_RELBIT, REL_X);
- ioctl(ui_fd, UI_SET_RELBIT, REL_Y);
- write(ui_fd, &ui_dev, sizeof(ui_dev));
- ioctl(ui_fd, UI_DEV_CREATE);
- usleep(100);
- while(1)
- {
- send_key_press(KEY_A);
- send_key_release(KEY_A);
- send_rel_mouse(110, 110);
- send_report();
- usleep(500 * 1000);
- }
- ioctl(ui_fd, UI_DEV_DESTROY);
- close(ui_fd);
- printf("\nExit.\n\n");
- return 0;
- }
- int open_uinput()
- {
- if((ui_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK)) < 0)
- {
- if((ui_fd = open("/dev/input/uinput", O_WRONLY | O_NONBLOCK)) >= 0)
- {
- return 1;
- }
- return -1;
- }
- return 1;
- }
- int send_event(uint16_t type, uint16_t code, int32_t value)
- {
- gettimeofday(&ui_ev.time, NULL);
- ui_ev.type = type;
- ui_ev.code = code;
- ui_ev.value = value;
- return write(ui_fd, &ui_ev, sizeof(ui_ev));
- }
- int send_report()
- {
- return send_event(EV_SYN, SYN_REPORT, 0);
- }
- int send_rel_mouse(int32_t x, int32_t y)
- {
- int ret;
- ret = send_event(EV_REL, REL_X, x);
- ret += send_event(EV_REL, REL_Y, y);
- return ret;
- }
- int send_abs_mouse(int32_t x, int32_t y)
- {
- int ret;
- ret = send_event(EV_ABS, ABS_X, x);
- ret += send_event(EV_ABS, ABS_Y, y);
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement