8 uinput is a kernel module that makes it possible to emulate input devices
9 from userspace. By writing to /dev/uinput (or /dev/input/uinput) device, a
10 process can create a virtual input device with specific capabilities. Once
11 this virtual device is created, the process can send events through it,
12 that will be delivered to userspace and in-kernel consumers.
21 The uinput header defines ioctls to create, set up, and destroy virtual
27 libevdev is a wrapper library for evdev devices that provides interfaces to
28 create uinput devices and send events. libevdev is less error-prone than
29 accessing uinput directly, and should be considered for new software.
31 For examples and more information about libevdev:
32 https://www.freedesktop.org/software/libevdev/doc/latest/
40 This first example shows how to create a new virtual device, and how to
41 send a key event. All default imports and error handlers were removed for
42 the sake of simplicity.
46 #include <linux/uinput.h>
48 void emit(int fd, int type, int code, int val)
50 struct input_event ie;
55 /* timestamp values below are ignored */
59 write(fd, &ie, sizeof(ie));
64 struct uinput_setup usetup;
66 int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
70 * The ioctls below will enable the device that is about to be
71 * created, to pass key events, in this case the space key.
73 ioctl(fd, UI_SET_EVBIT, EV_KEY);
74 ioctl(fd, UI_SET_KEYBIT, KEY_SPACE);
76 memset(&usetup, 0, sizeof(usetup));
77 usetup.id.bustype = BUS_USB;
78 usetup.id.vendor = 0x1234; /* sample vendor */
79 usetup.id.product = 0x5678; /* sample product */
80 strcpy(usetup.name, "Example device");
82 ioctl(fd, UI_DEV_SETUP, &usetup);
83 ioctl(fd, UI_DEV_CREATE);
86 * On UI_DEV_CREATE the kernel will create the device node for this
87 * device. We are inserting a pause here so that userspace has time
88 * to detect, initialize the new device, and can start listening to
89 * the event, otherwise it will not notice the event we are about
90 * to send. This pause is only needed in our example code!
94 /* Key press, report the event, send key release, and report again */
95 emit(fd, EV_KEY, KEY_SPACE, 1);
96 emit(fd, EV_SYN, SYN_REPORT, 0);
97 emit(fd, EV_KEY, KEY_SPACE, 0);
98 emit(fd, EV_SYN, SYN_REPORT, 0);
101 * Give userspace some time to read the events before we destroy the
102 * device with UI_DEV_DESTROY.
106 ioctl(fd, UI_DEV_DESTROY);
115 This example shows how to create a virtual device that behaves like a physical
120 #include <linux/uinput.h>
122 /* emit function is identical to of the first example */
126 struct uinput_setup usetup;
129 int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
131 /* enable mouse button left and relative events */
132 ioctl(fd, UI_SET_EVBIT, EV_KEY);
133 ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
135 ioctl(fd, UI_SET_EVBIT, EV_REL);
136 ioctl(fd, UI_SET_RELBIT, REL_X);
137 ioctl(fd, UI_SET_RELBIT, REL_Y);
139 memset(&usetup, 0, sizeof(usetup));
140 usetup.id.bustype = BUS_USB;
141 usetup.id.vendor = 0x1234; /* sample vendor */
142 usetup.id.product = 0x5678; /* sample product */
143 strcpy(usetup.name, "Example device");
145 ioctl(fd, UI_DEV_SETUP, &usetup);
146 ioctl(fd, UI_DEV_CREATE);
149 * On UI_DEV_CREATE the kernel will create the device node for this
150 * device. We are inserting a pause here so that userspace has time
151 * to detect, initialize the new device, and can start listening to
152 * the event, otherwise it will not notice the event we are about
153 * to send. This pause is only needed in our example code!
157 /* Move the mouse diagonally, 5 units per axis */
159 emit(fd, EV_REL, REL_X, 5);
160 emit(fd, EV_REL, REL_Y, 5);
161 emit(fd, EV_SYN, SYN_REPORT, 0);
166 * Give userspace some time to read the events before we destroy the
167 * device with UI_DEV_DESTROY.
171 ioctl(fd, UI_DEV_DESTROY);
181 Before uinput version 5, there wasn't a dedicated ioctl to set up a virtual
182 device. Programs supporting older versions of uinput interface need to fill
183 a uinput_user_dev structure and write it to the uinput file descriptor to
184 configure the new uinput device. New code should not use the old interface
185 but interact with uinput via ioctl calls, or use libevdev.
189 #include <linux/uinput.h>
191 /* emit function is identical to of the first example */
195 struct uinput_user_dev uud;
198 fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
199 rc = ioctl(fd, UI_GET_VERSION, &version);
201 if (rc == 0 && version >= 5) {
202 /* use UI_DEV_SETUP */
207 * The ioctls below will enable the device that is about to be
208 * created, to pass key events, in this case the space key.
210 ioctl(fd, UI_SET_EVBIT, EV_KEY);
211 ioctl(fd, UI_SET_KEYBIT, KEY_SPACE);
213 memset(&uud, 0, sizeof(uud));
214 snprintf(uud.name, UINPUT_MAX_NAME_SIZE, "uinput old interface");
215 write(fd, &uud, sizeof(uud));
217 ioctl(fd, UI_DEV_CREATE);
220 * On UI_DEV_CREATE the kernel will create the device node for this
221 * device. We are inserting a pause here so that userspace has time
222 * to detect, initialize the new device, and can start listening to
223 * the event, otherwise it will not notice the event we are about
224 * to send. This pause is only needed in our example code!
228 /* Key press, report the event, send key release, and report again */
229 emit(fd, EV_KEY, KEY_SPACE, 1);
230 emit(fd, EV_SYN, SYN_REPORT, 0);
231 emit(fd, EV_KEY, KEY_SPACE, 0);
232 emit(fd, EV_SYN, SYN_REPORT, 0);
235 * Give userspace some time to read the events before we destroy the
236 * device with UI_DEV_DESTROY.
240 ioctl(fd, UI_DEV_DESTROY);