ganadist

uinput sample

Jun 7th, 2011
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/ioctl.h>
  6. #include <linux/input.h>
  7. #include <linux/uinput.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <stdint.h>
  11.  
  12. #include <cutils/log.h>
  13.  
  14. #ifndef BUS_VIRTUAL
  15. /* copy from linux/input.h */
  16. #define BUS_VIRTUAL 0x06
  17. #endif
  18.  
  19.  
  20. int uinput_create(const char *name, struct keymap *map) {
  21.         struct uinput_user_dev dev;
  22.         int fd, err;
  23.  
  24.         const char *devs[] = {
  25.                 "/dev/uinput",
  26.                 "/dev/input/uinput",
  27.                 "/dev/misc/uinput",
  28.                 NULL,
  29.         };
  30.  
  31.         while (*devs) {
  32.                 fd = open(*devs, O_RDWR);
  33.                 if (fd >= 0)
  34.                         break;
  35.         }
  36.  
  37.         if (fd < 0) {
  38.                 err = errno;
  39.                 fprintf(stderr, "open error: %s\n", strerror(err));
  40.                 return -err;
  41.         }
  42.  
  43.         memset(&dev, 0, sizeof(dev));
  44.         if (name) strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE - 1);
  45.  
  46.         dev.id.bustype = BUS_VIRTUAL;
  47.         dev.id.vendor  = 0x0000;
  48.         dev.id.product = 0x0000;
  49.         dev.id.version = 0x0000;
  50.  
  51.         if (write(fd, &dev, sizeof(dev)) < 0) {
  52.                 err = errno;
  53.                 fprintf(stderr, "write device information error: %s\n", strerror(err));
  54.                 close(fd);
  55.                 return -err;
  56.         }
  57.  
  58.  
  59.         ioctl(fd, UI_SET_EVBIT, EV_KEY);
  60.  
  61.         while (map->value != 0xff) {
  62.                 ioctl(fd, UI_SET_KEYBIT, map->scancode);
  63.                 map++;
  64.         }
  65.  
  66.         if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) {
  67.                 err = errno;
  68.                 fprintf(stderr, "write device information error: %s\n", strerror(err));
  69.                 close(fd);
  70.                 return -err;
  71.         }
  72.  
  73.         return fd;
  74. }
  75.  
  76. static void send_event(int fd, uint16_t type, uint16_t code, int32_t value) {
  77.         struct input_event event;
  78.         int err;
  79.  
  80.         memset(&event, 0, sizeof(event));
  81.  
  82.         event.type = type;
  83.         event.code = code;
  84.         event.value = value;
  85.         gettimeofday(&event.time, NULL);
  86.         if (write(fd, &event, sizeof(event)) < 0) {
  87.                 LOGE("event sent error: %s", strerror(errno));
  88.         }
  89. }
  90.  
  91. void send_key(int fd, uint16_t key, int press)
  92. {
  93.         LOGW("send key fd = %d, 0x%04x press: %s", fd, key, press ? "true" : "false");
  94.         send_event(fd, EV_KEY, key, press ? 1 : 0);
  95.         send_event(fd, EV_SYN, SYN_REPORT, 0);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment