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
30 #include <linux/uinput.h>
33 #include <libevdev/libevdev.h>
34 #include <libevdev/libevdev-int.h>
35 #include <libevdev/libevdev-util.h>
36 #include <libevdev/libevdev-uinput.h>
38 #include "test-common-uinput.h"
40 #define SYS_INPUT_DIR "/sys/class/input"
41 #define DEV_INPUT_DIR "/dev/input/"
45 struct libevdev *d; /* lazy, it has all the accessors */
46 struct libevdev_uinput *uidev;
47 int dev_fd; /* open fd to the devnode */
52 uinput_device_new(const char *name)
54 struct uinput_device *dev;
56 dev = calloc(1, sizeof(*dev));
60 dev->d = libevdev_new();
65 libevdev_set_name(dev->d, name);
71 uinput_device_new_with_events_v(struct uinput_device **d, const char *name, const struct input_id *id, va_list args)
74 struct uinput_device *dev;
76 dev = uinput_device_new(name);
79 if (id != DEFAULT_IDS)
80 uinput_device_set_ids(dev, id);
82 rc = uinput_device_set_event_bits_v(dev, args);
85 rc = uinput_device_create(dev);
88 uinput_device_free(dev);
97 uinput_device_new_with_events(struct uinput_device **d, const char *name, const struct input_id *id, ...)
103 rc = uinput_device_new_with_events_v(d, name, id, args);
110 uinput_device_free(struct uinput_device *dev)
115 if (dev->uinput_fd != -1) {
116 ioctl(dev->uinput_fd, UI_DEV_DESTROY, NULL);
117 close(dev->uinput_fd);
119 if (dev->dev_fd != -1)
121 libevdev_free(dev->d);
122 libevdev_uinput_destroy(dev->uidev);
127 uinput_device_get_fd(const struct uinput_device *dev)
133 uinput_device_get_devnode(const struct uinput_device *dev)
135 return libevdev_uinput_get_devnode(dev->uidev);
139 uinput_device_create(struct uinput_device* d)
145 fd = open("/dev/uinput", O_RDWR);
151 rc = libevdev_uinput_create_from_device(d->d, fd, &d->uidev);
155 devnode = libevdev_uinput_get_devnode(d->uidev);
159 d->dev_fd = open(devnode, O_RDWR);
163 /* write abs resolution now */
164 if (libevdev_has_event_type(d->d, EV_ABS)) {
166 for (code = 0; code < ABS_CNT; code++) {
167 const struct input_absinfo *abs;
169 /* can't change slots */
170 if (code == ABS_MT_SLOT)
173 abs = libevdev_get_abs_info(d->d, code);
177 rc = ioctl(d->dev_fd, EVIOCSABS(code), abs);
179 printf("error %s for code %d\n", strerror(-rc), code);
190 if (d->uinput_fd != -1)
196 int uinput_device_set_name(struct uinput_device *dev, const char *name)
198 libevdev_set_name(dev->d, name);
202 int uinput_device_set_ids(struct uinput_device *dev, const struct input_id *ids)
204 libevdev_set_id_product(dev->d, ids->product);
205 libevdev_set_id_vendor(dev->d, ids->vendor);
206 libevdev_set_id_bustype(dev->d, ids->bustype);
207 libevdev_set_id_version(dev->d, ids->version);
212 uinput_device_set_bit(struct uinput_device* dev, unsigned int bit)
214 return libevdev_enable_event_type(dev->d, bit);
218 uinput_device_set_prop(struct uinput_device *dev, unsigned int prop)
220 return libevdev_enable_property(dev->d, prop);
224 uinput_device_set_event_bit(struct uinput_device* dev, unsigned int type, unsigned int code)
226 return libevdev_enable_event_code(dev->d, type, code, NULL);
230 uinput_device_set_event_bits_v(struct uinput_device *dev, va_list args)
236 type = va_arg(args, int);
239 code = va_arg(args, int);
242 rc = libevdev_enable_event_code(dev->d, type, code, NULL);
249 uinput_device_set_event_bits(struct uinput_device *dev, ...)
254 rc = uinput_device_set_event_bits_v(dev, args);
261 uinput_device_set_abs_bit(struct uinput_device* dev, unsigned int code, const struct input_absinfo *absinfo)
263 return libevdev_enable_event_code(dev->d, EV_ABS, code, absinfo);
267 uinput_device_event(const struct uinput_device *dev, unsigned int type, unsigned int code, int value)
269 return libevdev_uinput_write_event(dev->uidev, type, code, value);
272 int uinput_device_event_multiple_v(const struct uinput_device* dev, va_list args)
274 int type, code, value;
278 type = va_arg(args, int);
281 code = va_arg(args, int);
284 value = va_arg(args, int);
285 rc = uinput_device_event(dev, type, code, value);
291 int uinput_device_event_multiple(const struct uinput_device* dev, ...)
296 rc = uinput_device_event_multiple_v(dev, args);