Advertisement
xerpi

uinput test

Jun 23rd, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 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 <time.h>
  10.  
  11.  
  12. #define RELEASE    0
  13. #define PRESS      1
  14. #define AUTOREPEAT 2
  15.  
  16. #define total_time(x) (x.tv_sec * 100000 + x.tv_usec)
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.     struct uinput_user_dev  ui_dev;
  21.     struct input_event      ev;
  22.     int                     fd;
  23.  
  24.     fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
  25.     if(fd <= 0)
  26.     {
  27.         printf("Unable to open /dev/uinput\n");
  28.         return -1;
  29.     }
  30.  
  31.     usleep(100);
  32.  
  33.     memset(&ev, 0, sizeof(ev));
  34.     memset(&ui_dev, 0, sizeof(ui_dev));
  35.  
  36.     strncpy(ui_dev.name, "uinput-test", UINPUT_MAX_NAME_SIZE);
  37.     ui_dev.id.bustype = BUS_USB;
  38.     ui_dev.id.vendor  = 0x1234;
  39.     ui_dev.id.product = 0xABCD;
  40.     ui_dev.id.version = 1;
  41.  
  42.     ioctl(fd, UI_SET_EVBIT,  EV_KEY);
  43.     ioctl(fd, UI_SET_EVBIT,  EV_REP);
  44.     ioctl(fd, UI_SET_KEYBIT, KEY_D);
  45.  
  46.     write(fd, &ui_dev, sizeof(ui_dev));
  47.     ioctl(fd, UI_DEV_CREATE);
  48.  
  49.     usleep(100);
  50.  
  51.     while(1)
  52.     {
  53.         gettimeofday(&ev.time, NULL);
  54.         ev.type = EV_KEY;
  55.         ev.code = KEY_D;
  56.         ev.value = PRESS;
  57.         write(fd, &ev, sizeof(ev));
  58.  
  59.         gettimeofday(&ev.time, NULL);
  60.         ev.type = EV_KEY;
  61.         ev.code = KEY_D;
  62.         ev.value = RELEASE;
  63.         write(fd, &ev, sizeof(ev));
  64.  
  65.         gettimeofday(&ev.time, NULL);
  66.         ev.type = EV_SYN;
  67.         ev.code = SYN_REPORT;
  68.         ev.value = 0;
  69.         write(fd, &ev, sizeof(ev));
  70.  
  71.         usleep(500 * 1000);
  72.     }
  73.  
  74.     ioctl(fd, UI_DEV_DESTROY);
  75.     close(fd);
  76.     printf("\nExit.\n\n");
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement