2 * Copyright © 2013 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 #ifndef LIBEVDEV_UINPUT_H
24 #define LIBEVDEV_UINPUT_H
30 #include <libevdev/libevdev.h>
32 struct libevdev_uinput;
35 * @defgroup uinput uinput device creation
37 * Creation of uinput devices based on existing libevdev devices. These functions
38 * help to create uinput devices that emulate libevdev devices. In the simplest
39 * form it serves to duplicate an existing device:
45 struct libevdev_uinput *uidev;
46 struct input_event ev[2];
48 err = libevdev_new_from_fd(&dev, fd);
52 uifd = open("/dev/uinput", O_RDWR);
56 err = libevdev_uinput_create_from_device(dev, uifd, &uidev);
61 err = libevdev_uinput_write_event(uidev, EV_REL, REL_X, -1);
64 libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);
68 libevdev_uinput_destroy(uidev);
73 * Alternatively, a device can be constructed from scratch:
78 struct libevdev_uinput *uidev;
81 libevdev_set_name(dev, "test device");
82 libevdev_enable_event_type(dev, EV_REL);
83 libevdev_enable_event_code(dev, EV_REL, REL_X);
84 libevdev_enable_event_code(dev, EV_REL, REL_Y);
85 libevdev_enable_event_type(dev, EV_KEY);
86 libevdev_enable_event_code(dev, EV_KEY, BTN_LEFT);
87 libevdev_enable_event_code(dev, EV_KEY, BTN_MIDDLE);
88 libevdev_enable_event_code(dev, EV_KEY, BTN_RIGHT);
90 err = libevdev_uinput_create_from_device(dev,
91 LIBEVDEV_UINPUT_OPEN_MANAGED,
96 // ... do something ...
98 libevdev_uinput_destroy(uidev);
103 enum libevdev_uinput_open_mode {
104 /* intentionally -2 to avoid to avoid code like the below from accidentally working:
105 fd = open("/dev/uinput", O_RDWR); // fails, fd is -1
106 libevdev_uinput_create_from_device(dev, fd, &uidev); // may hide the error */
107 LIBEVDEV_UINPUT_OPEN_MANAGED = -2 /**< let libevdev open and close @c /dev/uinput */
113 * Create a uinput device based on the given libevdev device. The uinput device
114 * will be an exact copy of the libevdev device, minus the bits that uinput doesn't
117 * If uinput_fd is @ref LIBEVDEV_UINPUT_OPEN_MANAGED, libevdev_uinput_create_from_device()
118 * will open @c /dev/uinput in read/write mode and manage the file descriptor.
119 * Otherwise, uinput_fd must be opened by the caller and opened with the
120 * appropriate permissions.
122 * The device's lifetime is tied to the uinput file descriptor, closing it will
123 * destroy the uinput device. You should call libevdev_uinput_destroy() before
124 * closing the file descriptor to free allocated resources.
125 * A file descriptor can only create one uinput device at a time; the second device
126 * will fail with -EINVAL.
128 * You don't need to keep the file descriptor variable around,
129 * libevdev_uinput_get_fd() will return it when needed.
131 * @note Due to limitations in the uinput kernel module, REP_DELAY and
132 * REP_PERIOD will default to the kernel defaults, not to the ones set in the
135 * @param dev The device to duplicate
136 * @param uinput_fd @ref LIBEVDEV_UINPUT_OPEN_MANAGED or a file descriptor to @c /dev/uinput,
137 * @param[out] uinput_dev The newly created libevdev device.
139 * @return 0 on success or a negative errno on failure. On failure, the value of
140 * uinput_dev is unmodified.
142 * @see libevdev_uinput_destroy
144 int libevdev_uinput_create_from_device(const struct libevdev *dev,
146 struct libevdev_uinput **uinput_dev);
151 * Destroy a previously created uinput device and free associated memory.
153 * If the device was opened with @ref LIBEVDEV_UINPUT_OPEN_MANAGED,
154 * libevdev_uinput_destroy() also closes the file descriptor. Otherwise, the
155 * fd is left as-is and must be closed by the caller.
157 * @param uinput_dev A previously created uinput device.
159 void libevdev_uinput_destroy(struct libevdev_uinput *uinput_dev);
164 * Return the file descriptor used to create this uinput device. This is the
165 * fd pointing to <strong>/dev/uinput</strong>. This file descriptor may be used to write
166 * events that are emitted by the uinput device.
167 * Closing this file descriptor will destroy the uinput device, you should
168 * call libevdev_uinput_destroy() first to free allocated resources.
170 * @param uinput_dev A previously created uinput device.
172 * @return The file descriptor used to create this device
174 int libevdev_uinput_get_fd(const struct libevdev_uinput *uinput_dev);
179 * Return the syspath representing this uinput device.
180 * At the time of writing, the uinput kernel device does not
181 * provide a way to get the syspath directly through uinput so libevdev must guess.
182 * In some cases libevdev is unable to derive the syspath. If the running kernel
183 * supports the UI_GET_SYSNAME ioctl, the syspath is retrieved through that and will
184 * be reliable and not be NULL. The UI_GET_SYSNAME ioctl is currently
185 * scheduled for 3.15.
187 * @note This function may return NULL. libevdev currently uses ctime and
188 * the device name to guess devices. To avoid false positives, wait at least
189 * wait at least 1.5s between creating devices that have the same name.
190 * @param uinput_dev A previously created uinput device.
191 * @return The syspath for this device, including the preceding /sys
193 * @see libevdev_uinput_get_devnode
195 const char*libevdev_uinput_get_syspath(struct libevdev_uinput *uinput_dev);
200 * Return the device node representing this uinput device.
202 * This relies on libevdev_uinput_get_syspath() to provide a valid syspath.
203 * See libevdev_uinput_get_syspath() for more details.
205 * @note This function may return NULL. libevdev currently has to guess the
206 * syspath and the device node. See libevdev_uinput_get_syspath() for details.
207 * @param uinput_dev A previously created uinput device.
208 * @return The device node for this device, in the form of /dev/input/eventN
210 * @see libevdev_uinput_get_syspath
212 const char* libevdev_uinput_get_devnode(struct libevdev_uinput *uinput_dev);
217 * Post an event through the uinput device. It is the caller's responsibility
218 * that any event sequence is terminated with an EV_SYN/SYN_REPORT/0 event.
219 * Otherwise, listeners on the device node will not see the events until the
220 * next EV_SYN event is posted.
222 * @param uinput_dev A previously created uinput device.
223 * @param type Event type (EV_ABS, EV_REL, etc.)
224 * @param code Event code (ABS_X, REL_Y, etc.)
225 * @param value The event value
226 * @return 0 on success or a negative errno on error
228 int libevdev_uinput_write_event(const struct libevdev_uinput *uinput_dev,
236 #endif /* LIBEVDEV_UINPUT_H */