Advertisement
xerpi

Untitled

Jun 23rd, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <linux/input.h>
  8. #include <linux/uinput.h>
  9. #include <stdint.h>
  10. #include <time.h>
  11.  
  12. #define release    0
  13. #define press      1
  14. #define autorepeat 2
  15.  
  16.  
  17. struct uinput_user_dev  ui_dev;
  18. struct input_event      ui_ev;
  19. int                     ui_fd, i;
  20.  
  21. inline int open_uinput();
  22. inline int send_event(uint16_t type, uint16_t code, int32_t value);
  23. inline int send_report();
  24. inline int send_rel_mouse(int32_t x, int32_t y);
  25. inline int send_abs_mouse(int32_t x, int32_t y);
  26.  
  27. #define send_key_macro(x) \
  28.     inline int send_key_##x(uint16_t key) {  \
  29.         return send_event(EV_KEY, key, x);   \
  30.     }
  31.  
  32.  
  33. send_key_macro(press);
  34. send_key_macro(release);
  35. send_key_macro(autorepeat);
  36.  
  37.  
  38. int main(int argc, char *argv[])
  39. {
  40.  
  41.     if(open_uinput() < 0)
  42.     {
  43.         printf("Unable to open uinput.\n");
  44.         return -1;
  45.     }
  46.  
  47.     usleep(100);
  48.  
  49.     memset(&ui_ev,  0, sizeof(ui_ev));
  50.     memset(&ui_dev, 0, sizeof(ui_dev));
  51.  
  52.     strncpy(ui_dev.name, "uinput-test", UINPUT_MAX_NAME_SIZE);
  53.     ui_dev.id.bustype = BUS_VIRTUAL;
  54.     ui_dev.id.vendor  = 0x1234;
  55.     ui_dev.id.product = 0xABCD;
  56.     ui_dev.id.version = 1;
  57.  
  58.     ui_dev.absmin[ABS_X] = 0;
  59.     ui_dev.absmax[ABS_X] = 1023;
  60.     ui_dev.absmin[ABS_Y] = 0;
  61.     ui_dev.absmax[ABS_Y] = 1023;
  62.  
  63.     ioctl(ui_fd, UI_SET_EVBIT,  EV_KEY);
  64.     ioctl(ui_fd, UI_SET_EVBIT,  EV_REP);
  65.  
  66.     for(i = 0; i < KEY_MAX; i++)
  67.     {
  68.         ioctl(ui_fd, UI_SET_KEYBIT, i);
  69.     }
  70.  
  71.     ioctl(ui_fd, UI_SET_EVBIT, EV_REL);
  72.     ioctl(ui_fd, UI_SET_EVBIT, EV_ABS);
  73.     ioctl(ui_fd, UI_SET_RELBIT, REL_X);
  74.     ioctl(ui_fd, UI_SET_RELBIT, REL_Y);
  75.  
  76.     write(ui_fd, &ui_dev, sizeof(ui_dev));
  77.     ioctl(ui_fd, UI_DEV_CREATE);
  78.  
  79.     usleep(100);
  80.  
  81.     while(1)
  82.     {
  83.         send_key_press(KEY_A);
  84.         send_key_release(KEY_A);
  85.         send_rel_mouse(110, 110);
  86.         send_report();
  87.         usleep(500 * 1000);
  88.     }
  89.  
  90.     ioctl(ui_fd, UI_DEV_DESTROY);
  91.     close(ui_fd);
  92.     printf("\nExit.\n\n");
  93.     return 0;
  94. }
  95.  
  96. int open_uinput()
  97. {
  98.     if((ui_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK)) < 0)
  99.     {
  100.         if((ui_fd = open("/dev/input/uinput", O_WRONLY | O_NONBLOCK)) >= 0)
  101.         {
  102.             return 1;
  103.         }
  104.         return -1;
  105.     }
  106.     return 1;
  107. }
  108.  
  109. int send_event(uint16_t type, uint16_t code, int32_t value)
  110. {
  111.     gettimeofday(&ui_ev.time, NULL);
  112.     ui_ev.type = type;
  113.     ui_ev.code = code;
  114.     ui_ev.value = value;
  115.     return write(ui_fd, &ui_ev, sizeof(ui_ev));
  116. }
  117.  
  118. int send_report()
  119. {
  120.     return send_event(EV_SYN, SYN_REPORT, 0);
  121. }
  122.  
  123. int send_rel_mouse(int32_t x, int32_t y)
  124. {
  125.     int ret;
  126.     ret =  send_event(EV_REL, REL_X, x);
  127.     ret += send_event(EV_REL, REL_Y, y);
  128.     return ret;
  129. }
  130.  
  131. int send_abs_mouse(int32_t x, int32_t y)
  132. {
  133.     int ret;
  134.     ret =  send_event(EV_ABS, ABS_X, x);
  135.     ret += send_event(EV_ABS, ABS_Y, y);
  136.     return ret;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement