summaryrefslogtreecommitdiff
path: root/test/test-common-uinput.c
blob: 32ecb89220b24ba290cac0df9f7e33398840def3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright © 2013 Red Hat, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include <config.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <linux/uinput.h>
#include <dirent.h>

#include <libevdev/libevdev.h>
#include <libevdev/libevdev-int.h>
#include <libevdev/libevdev-util.h>
#include <libevdev/libevdev-uinput.h>

#include "test-common-uinput.h"

#define SYS_INPUT_DIR "/sys/class/input"
#define DEV_INPUT_DIR "/dev/input/"

struct uinput_device
{
	struct libevdev *d; /* lazy, it has all the accessors */
	struct libevdev_uinput *uidev;
	int dev_fd; /* open fd to the devnode */
	int uinput_fd;
};

struct uinput_device*
uinput_device_new(const char *name)
{
	struct uinput_device *dev;

	dev = calloc(1, sizeof(*dev));
	if (!dev)
		return NULL;

	dev->d = libevdev_new();
	dev->dev_fd = -1;
	dev->uinput_fd = -1;

	if (name)
		libevdev_set_name(dev->d, name);

	return dev;
}

int
uinput_device_new_with_events_v(struct uinput_device **d, const char *name, const struct input_id *id, va_list args)
{
	int rc;
	struct uinput_device *dev;

	dev = uinput_device_new(name);
	if (!dev)
		return -ENOMEM;
	if (id != DEFAULT_IDS)
		uinput_device_set_ids(dev, id);

	rc = uinput_device_set_event_bits_v(dev, args);

	if (rc == 0)
		rc = uinput_device_create(dev);

	if (rc != 0) {
		uinput_device_free(dev);
		dev = NULL;
	} else
		*d = dev;

	return rc;
}

int
uinput_device_new_with_events(struct uinput_device **d, const char *name, const struct input_id *id, ...)
{
	int rc;
	va_list args;

	va_start(args, id);
	rc = uinput_device_new_with_events_v(d, name, id, args);
	va_end(args);

	return rc;
}

void
uinput_device_free(struct uinput_device *dev)
{
	if (!dev)
		return;

	if (dev->uinput_fd != -1) {
		ioctl(dev->uinput_fd, UI_DEV_DESTROY, NULL);
		close(dev->uinput_fd);
	}
	if (dev->dev_fd != -1)
		close(dev->dev_fd);
	libevdev_free(dev->d);
	free(dev);
}

int
uinput_device_get_fd(const struct uinput_device *dev)
{
	return dev->dev_fd;
}

int
uinput_device_create(struct uinput_device* d)
{
	int rc;
	int fd;
	const char *devnode;

	fd = open("/dev/uinput", O_RDWR);
	if (fd < 0)
		goto error;

	d->uinput_fd = fd;

	rc = libevdev_uinput_create_from_device(d->d, fd, &d->uidev);
	if (rc != 0)
		goto error;

	devnode = libevdev_uinput_get_devnode(d->uidev);
	if (devnode == NULL)
		goto error;

	d->dev_fd = open(devnode, O_RDWR);
	if (d->dev_fd == -1)
		goto error;

	/* write abs resolution now */
	if (libevdev_has_event_type(d->d, EV_ABS)) {
		int  code;
		for (code = 0; code < ABS_CNT; code++) {
			const struct input_absinfo *abs;

			/* can't change slots */
			if (code == ABS_MT_SLOT)
				continue;

			abs = libevdev_get_abs_info(d->d, code);
			if (!abs)
				continue;

			rc = ioctl(d->dev_fd, EVIOCSABS(code), abs);
			if (rc < 0) {
				printf("error %s for code %d\n", strerror(-rc), code);
				goto error;
			}
		}
	}

	return 0;

error:
	if (d->dev_fd != -1)
		close(d->dev_fd);
	if (d->uinput_fd != -1)
		close(d->uinput_fd);
	return -errno;

}

int uinput_device_set_name(struct uinput_device *dev, const char *name)
{
	libevdev_set_name(dev->d, name);
	return 0;
}

int uinput_device_set_ids(struct uinput_device *dev, const struct input_id *ids)
{
	libevdev_set_id_product(dev->d, ids->product);
	libevdev_set_id_vendor(dev->d, ids->vendor);
	libevdev_set_id_bustype(dev->d, ids->bustype);
	libevdev_set_id_version(dev->d, ids->version);
	return 0;
}

int
uinput_device_set_bit(struct uinput_device* dev, unsigned int bit)
{
	return libevdev_enable_event_type(dev->d, bit);
}

int
uinput_device_set_prop(struct uinput_device *dev, unsigned int prop)
{
	return libevdev_enable_property(dev->d, prop);
}

int
uinput_device_set_event_bit(struct uinput_device* dev, unsigned int type, unsigned int code)
{
	return libevdev_enable_event_code(dev->d, type, code, NULL);
}

int
uinput_device_set_event_bits_v(struct uinput_device *dev, va_list args)
{
	int type, code;
	int rc = 0;

	do {
		type = va_arg(args, int);
		if (type == -1)
			break;
		code = va_arg(args, int);
		if (code == -1)
			break;
		rc = libevdev_enable_event_code(dev->d, type, code, NULL);
	} while (rc == 0);

	return rc;
}

int
uinput_device_set_event_bits(struct uinput_device *dev, ...)
{
	int rc;
	va_list args;
	va_start(args, dev);
	rc = uinput_device_set_event_bits_v(dev, args);
	va_end(args);

	return rc;
}

int
uinput_device_set_abs_bit(struct uinput_device* dev, unsigned int code, const struct input_absinfo *absinfo)
{
	return libevdev_enable_event_code(dev->d, EV_ABS, code, absinfo);
}

int
uinput_device_event(const struct uinput_device *dev, unsigned int type, unsigned int code, int value)
{
	return libevdev_uinput_write_event(dev->uidev, type, code, value);
}

int uinput_device_event_multiple_v(const struct uinput_device* dev, va_list args)
{
	int type, code, value;
	int rc = 0;

	do {
		type = va_arg(args, int);
		if (type == -1)
			break;
		code = va_arg(args, int);
		if (code == -1)
			break;
		value = va_arg(args, int);
		rc = uinput_device_event(dev, type, code, value);
	} while (rc == 0);

	return rc;
}

int uinput_device_event_multiple(const struct uinput_device* dev, ...)
{
	int rc;
	va_list args;
	va_start(args, dev);
	rc = uinput_device_event_multiple_v(dev, args);
	va_end(args);
	return rc;
}