Advertisement
xerpi

uinput utility v1

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