uinput.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. =============
  2. uinput module
  3. =============
  4. Introduction
  5. ============
  6. uinput is a kernel module that makes it possible to emulate input devices
  7. from userspace. By writing to /dev/uinput (or /dev/input/uinput) device, a
  8. process can create a virtual input device with specific capabilities. Once
  9. this virtual device is created, the process can send events through it,
  10. that will be delivered to userspace and in-kernel consumers.
  11. Interface
  12. =========
  13. ::
  14. linux/uinput.h
  15. The uinput header defines ioctls to create, set up, and destroy virtual
  16. devices.
  17. libevdev
  18. ========
  19. libevdev is a wrapper library for evdev devices that provides interfaces to
  20. create uinput devices and send events. libevdev is less error-prone than
  21. accessing uinput directly, and should be considered for new software.
  22. For examples and more information about libevdev:
  23. https://www.freedesktop.org/software/libevdev/doc/latest/
  24. Examples
  25. ========
  26. Keyboard events
  27. ---------------
  28. This first example shows how to create a new virtual device, and how to
  29. send a key event. All default imports and error handlers were removed for
  30. the sake of simplicity.
  31. .. code-block:: c
  32. #include <linux/uinput.h>
  33. void emit(int fd, int type, int code, int val)
  34. {
  35. struct input_event ie;
  36. ie.type = type;
  37. ie.code = code;
  38. ie.value = val;
  39. /* timestamp values below are ignored */
  40. ie.time.tv_sec = 0;
  41. ie.time.tv_usec = 0;
  42. write(fd, &ie, sizeof(ie));
  43. }
  44. int main(void)
  45. {
  46. struct uinput_setup usetup;
  47. int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
  48. /*
  49. * The ioctls below will enable the device that is about to be
  50. * created, to pass key events, in this case the space key.
  51. */
  52. ioctl(fd, UI_SET_EVBIT, EV_KEY);
  53. ioctl(fd, UI_SET_KEYBIT, KEY_SPACE);
  54. memset(&usetup, 0, sizeof(usetup));
  55. usetup.id.bustype = BUS_USB;
  56. usetup.id.vendor = 0x1234; /* sample vendor */
  57. usetup.id.product = 0x5678; /* sample product */
  58. strcpy(usetup.name, "Example device");
  59. ioctl(fd, UI_DEV_SETUP, &usetup);
  60. ioctl(fd, UI_DEV_CREATE);
  61. /*
  62. * On UI_DEV_CREATE the kernel will create the device node for this
  63. * device. We are inserting a pause here so that userspace has time
  64. * to detect, initialize the new device, and can start listening to
  65. * the event, otherwise it will not notice the event we are about
  66. * to send. This pause is only needed in our example code!
  67. */
  68. sleep(1);
  69. /* Key press, report the event, send key release, and report again */
  70. emit(fd, EV_KEY, KEY_SPACE, 1);
  71. emit(fd, EV_SYN, SYN_REPORT, 0);
  72. emit(fd, EV_KEY, KEY_SPACE, 0);
  73. emit(fd, EV_SYN, SYN_REPORT, 0);
  74. /*
  75. * Give userspace some time to read the events before we destroy the
  76. * device with UI_DEV_DESTROY.
  77. */
  78. sleep(1);
  79. ioctl(fd, UI_DEV_DESTROY);
  80. close(fd);
  81. return 0;
  82. }
  83. Mouse movements
  84. ---------------
  85. This example shows how to create a virtual device that behaves like a physical
  86. mouse.
  87. .. code-block:: c
  88. #include <linux/uinput.h>
  89. /* emit function is identical to of the first example */
  90. int main(void)
  91. {
  92. struct uinput_setup usetup;
  93. int i = 50;
  94. int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
  95. /* enable mouse button left and relative events */
  96. ioctl(fd, UI_SET_EVBIT, EV_KEY);
  97. ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
  98. ioctl(fd, UI_SET_EVBIT, EV_REL);
  99. ioctl(fd, UI_SET_RELBIT, REL_X);
  100. ioctl(fd, UI_SET_RELBIT, REL_Y);
  101. memset(&usetup, 0, sizeof(usetup));
  102. usetup.id.bustype = BUS_USB;
  103. usetup.id.vendor = 0x1234; /* sample vendor */
  104. usetup.id.product = 0x5678; /* sample product */
  105. strcpy(usetup.name, "Example device");
  106. ioctl(fd, UI_DEV_SETUP, &usetup);
  107. ioctl(fd, UI_DEV_CREATE);
  108. /*
  109. * On UI_DEV_CREATE the kernel will create the device node for this
  110. * device. We are inserting a pause here so that userspace has time
  111. * to detect, initialize the new device, and can start listening to
  112. * the event, otherwise it will not notice the event we are about
  113. * to send. This pause is only needed in our example code!
  114. */
  115. sleep(1);
  116. /* Move the mouse diagonally, 5 units per axis */
  117. while (i--) {
  118. emit(fd, EV_REL, REL_X, 5);
  119. emit(fd, EV_REL, REL_Y, 5);
  120. emit(fd, EV_SYN, SYN_REPORT, 0);
  121. usleep(15000);
  122. }
  123. /*
  124. * Give userspace some time to read the events before we destroy the
  125. * device with UI_DEV_DESTROY.
  126. */
  127. sleep(1);
  128. ioctl(fd, UI_DEV_DESTROY);
  129. close(fd);
  130. return 0;
  131. }
  132. uinput old interface
  133. --------------------
  134. Before uinput version 5, there wasn't a dedicated ioctl to set up a virtual
  135. device. Programs supportinf older versions of uinput interface need to fill
  136. a uinput_user_dev structure and write it to the uinput file descriptor to
  137. configure the new uinput device. New code should not use the old interface
  138. but interact with uinput via ioctl calls, or use libevdev.
  139. .. code-block:: c
  140. #include <linux/uinput.h>
  141. /* emit function is identical to of the first example */
  142. int main(void)
  143. {
  144. struct uinput_user_dev uud;
  145. int version, rc, fd;
  146. fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
  147. rc = ioctl(fd, UI_GET_VERSION, &version);
  148. if (rc == 0 && version >= 5) {
  149. /* use UI_DEV_SETUP */
  150. return 0;
  151. }
  152. /*
  153. * The ioctls below will enable the device that is about to be
  154. * created, to pass key events, in this case the space key.
  155. */
  156. ioctl(fd, UI_SET_EVBIT, EV_KEY);
  157. ioctl(fd, UI_SET_KEYBIT, KEY_SPACE);
  158. memset(&uud, 0, sizeof(uud));
  159. snprintf(uud.name, UINPUT_MAX_NAME_SIZE, "uinput old interface");
  160. write(fd, &uud, sizeof(uud));
  161. ioctl(fd, UI_DEV_CREATE);
  162. /*
  163. * On UI_DEV_CREATE the kernel will create the device node for this
  164. * device. We are inserting a pause here so that userspace has time
  165. * to detect, initialize the new device, and can start listening to
  166. * the event, otherwise it will not notice the event we are about
  167. * to send. This pause is only needed in our example code!
  168. */
  169. sleep(1);
  170. /* Key press, report the event, send key release, and report again */
  171. emit(fd, EV_KEY, KEY_SPACE, 1);
  172. emit(fd, EV_SYN, SYN_REPORT, 0);
  173. emit(fd, EV_KEY, KEY_SPACE, 0);
  174. emit(fd, EV_SYN, SYN_REPORT, 0);
  175. /*
  176. * Give userspace some time to read the events before we destroy the
  177. * device with UI_DEV_DESTROY.
  178. */
  179. sleep(1);
  180. ioctl(fd, UI_DEV_DESTROY);
  181. close(fd);
  182. return 0;
  183. }