Advertisement
Guest User

Untitled

a guest
Feb 14th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 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.  
  10. #define die(str, args...) do { \
  11.         perror(str); \
  12.         exit(EXIT_FAILURE); \
  13.     } while(0)
  14.  
  15. int
  16. main(void)
  17. {
  18.     int                    fd;
  19.     struct uinput_user_dev uidev;
  20.     struct input_event     ev;
  21.     int                    dx, dy;
  22.     int                    i;
  23.  
  24.     fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
  25.     if(fd < 0)
  26.         die("error: open");
  27.  
  28.     if(ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
  29.         die("error: ioctl");
  30.     if(ioctl(fd, UI_SET_KEYBIT, KEY_0) < 0)
  31.         die("error: ioctl");
  32.     if(ioctl(fd, UI_SET_KEYBIT, BTN_A) < 0)
  33.         die("error: ioctl");
  34.  
  35.     if(ioctl(fd, UI_SET_EVBIT, EV_SYN) < 0)
  36.         die("error: ioctl");
  37.  
  38.     if(ioctl(fd, UI_SET_EVBIT, EV_REL) < 0)
  39.         die("error: ioctl");
  40.     if(ioctl(fd, UI_SET_RELBIT, REL_X) < 0)
  41.         die("error: ioctl");
  42.     if(ioctl(fd, UI_SET_RELBIT, REL_Y) < 0)
  43.         die("error: ioctl");
  44.  
  45.     if(ioctl(fd, UI_SET_EVBIT, EV_ABS) < 0)
  46.         die("error: ioctl");
  47.     if(ioctl(fd, UI_SET_RELBIT, ABS_X) < 0)
  48.         die("error: ioctl");
  49.     if(ioctl(fd, UI_SET_RELBIT, ABS_Y) < 0)
  50.         die("error: ioctl");
  51.  
  52.     memset(&uidev, 0, sizeof(uidev));
  53.     snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
  54.     uidev.id.bustype = BUS_USB;
  55.     uidev.id.vendor  = 0x1;
  56.     uidev.id.product = 0x1;
  57.     uidev.id.version = 1;
  58.  
  59.     if(write(fd, &uidev, sizeof(uidev)) < 0)
  60.         die("error: write");
  61.  
  62.     if(ioctl(fd, UI_DEV_CREATE) < 0)
  63.         die("error: ioctl");
  64.  
  65.     sleep(2);
  66.  
  67.     srand(time(NULL));
  68.  
  69.     while(1) {
  70.             memset(&ev, 0, sizeof(struct input_event));
  71.             ev.type = EV_KEY;
  72.             ev.code = KEY_0;
  73.             ev.value = 1;
  74.             if(write(fd, &ev, sizeof(struct input_event)) < 0)
  75.                 die("error: write");
  76.  
  77.             memset(&ev, 0, sizeof(struct input_event));
  78.             ev.type = EV_KEY;
  79.             ev.code = KEY_0;
  80.             ev.value = 0;
  81.             if(write(fd, &ev, sizeof(struct input_event)) < 0)
  82.                 die("error: write");
  83.  
  84.         memset(&ev, 0, sizeof(struct input_event));
  85.             ev.type = EV_KEY;
  86.             ev.code = BTN_A;
  87.             ev.value = 1;
  88.             if(write(fd, &ev, sizeof(struct input_event)) < 0)
  89.                 die("error: write");
  90.  
  91.             memset(&ev, 0, sizeof(struct input_event));
  92.             ev.type = EV_SYN;
  93.             ev.code = 0;
  94.             ev.value = 0;
  95.             if(write(fd, &ev, sizeof(struct input_event)) < 0)
  96.                 die("error: write");
  97.  
  98.  
  99.         sleep(1);
  100.     }
  101.  
  102.     sleep(2);
  103.  
  104.     if(ioctl(fd, UI_DEV_DESTROY) < 0)
  105.         die("error: ioctl");
  106.  
  107.     close(fd);
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement