xref: /openbmc/linux/samples/uhid/uhid-example.c (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25148fa52SDavid Herrmann /*
35148fa52SDavid Herrmann  * UHID Example
45148fa52SDavid Herrmann  *
5f5e4e7fdSDavid Herrmann  * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
65148fa52SDavid Herrmann  *
75148fa52SDavid Herrmann  * The code may be used by anyone for any purpose,
85148fa52SDavid Herrmann  * and can serve as a starting point for developing
95148fa52SDavid Herrmann  * applications using uhid.
105148fa52SDavid Herrmann  */
115148fa52SDavid Herrmann 
12f5e4e7fdSDavid Herrmann /*
13f5e4e7fdSDavid Herrmann  * UHID Example
145148fa52SDavid Herrmann  * This example emulates a basic 3 buttons mouse with wheel over UHID. Run this
155148fa52SDavid Herrmann  * program as root and then use the following keys to control the mouse:
165148fa52SDavid Herrmann  *   q: Quit the application
175148fa52SDavid Herrmann  *   1: Toggle left button (down, up, ...)
185148fa52SDavid Herrmann  *   2: Toggle right button
195148fa52SDavid Herrmann  *   3: Toggle middle button
205148fa52SDavid Herrmann  *   a: Move mouse left
215148fa52SDavid Herrmann  *   d: Move mouse right
225148fa52SDavid Herrmann  *   w: Move mouse up
235148fa52SDavid Herrmann  *   s: Move mouse down
245148fa52SDavid Herrmann  *   r: Move wheel up
255148fa52SDavid Herrmann  *   f: Move wheel down
265148fa52SDavid Herrmann  *
27f5e4e7fdSDavid Herrmann  * Additionally to 3 button mouse, 3 keyboard LEDs are also supported (LED_NUML,
28f5e4e7fdSDavid Herrmann  * LED_CAPSL and LED_SCROLLL). The device doesn't generate any related keyboard
29f5e4e7fdSDavid Herrmann  * events, though. You need to manually write the EV_LED/LED_XY/1 activation
30f5e4e7fdSDavid Herrmann  * input event to the evdev device to see it being sent to this device.
31f5e4e7fdSDavid Herrmann  *
325148fa52SDavid Herrmann  * If uhid is not available as /dev/uhid, then you can pass a different path as
335148fa52SDavid Herrmann  * first argument.
345148fa52SDavid Herrmann  * If <linux/uhid.h> is not installed in /usr, then compile this with:
355148fa52SDavid Herrmann  *   gcc -o ./uhid_test -Wall -I./include ./samples/uhid/uhid-example.c
365148fa52SDavid Herrmann  * And ignore the warning about kernel headers. However, it is recommended to
375148fa52SDavid Herrmann  * use the installed uhid.h if available.
385148fa52SDavid Herrmann  */
395148fa52SDavid Herrmann 
405148fa52SDavid Herrmann #include <errno.h>
415148fa52SDavid Herrmann #include <fcntl.h>
425148fa52SDavid Herrmann #include <poll.h>
435148fa52SDavid Herrmann #include <stdbool.h>
445148fa52SDavid Herrmann #include <stdio.h>
455148fa52SDavid Herrmann #include <stdlib.h>
465148fa52SDavid Herrmann #include <string.h>
475148fa52SDavid Herrmann #include <termios.h>
485148fa52SDavid Herrmann #include <unistd.h>
495148fa52SDavid Herrmann #include <linux/uhid.h>
505148fa52SDavid Herrmann 
51f5e4e7fdSDavid Herrmann /*
52f5e4e7fdSDavid Herrmann  * HID Report Desciptor
53f5e4e7fdSDavid Herrmann  * We emulate a basic 3 button mouse with wheel and 3 keyboard LEDs. This is
54f5e4e7fdSDavid Herrmann  * the report-descriptor as the kernel will parse it:
555148fa52SDavid Herrmann  *
56f5e4e7fdSDavid Herrmann  * INPUT(1)[INPUT]
575148fa52SDavid Herrmann  *   Field(0)
585148fa52SDavid Herrmann  *     Physical(GenericDesktop.Pointer)
595148fa52SDavid Herrmann  *     Application(GenericDesktop.Mouse)
605148fa52SDavid Herrmann  *     Usage(3)
615148fa52SDavid Herrmann  *       Button.0001
625148fa52SDavid Herrmann  *       Button.0002
635148fa52SDavid Herrmann  *       Button.0003
645148fa52SDavid Herrmann  *     Logical Minimum(0)
655148fa52SDavid Herrmann  *     Logical Maximum(1)
665148fa52SDavid Herrmann  *     Report Size(1)
675148fa52SDavid Herrmann  *     Report Count(3)
685148fa52SDavid Herrmann  *     Report Offset(0)
695148fa52SDavid Herrmann  *     Flags( Variable Absolute )
705148fa52SDavid Herrmann  *   Field(1)
715148fa52SDavid Herrmann  *     Physical(GenericDesktop.Pointer)
725148fa52SDavid Herrmann  *     Application(GenericDesktop.Mouse)
735148fa52SDavid Herrmann  *     Usage(3)
745148fa52SDavid Herrmann  *       GenericDesktop.X
755148fa52SDavid Herrmann  *       GenericDesktop.Y
765148fa52SDavid Herrmann  *       GenericDesktop.Wheel
775148fa52SDavid Herrmann  *     Logical Minimum(-128)
785148fa52SDavid Herrmann  *     Logical Maximum(127)
795148fa52SDavid Herrmann  *     Report Size(8)
805148fa52SDavid Herrmann  *     Report Count(3)
815148fa52SDavid Herrmann  *     Report Offset(8)
825148fa52SDavid Herrmann  *     Flags( Variable Relative )
83f5e4e7fdSDavid Herrmann  * OUTPUT(2)[OUTPUT]
84f5e4e7fdSDavid Herrmann  *   Field(0)
85f5e4e7fdSDavid Herrmann  *     Application(GenericDesktop.Keyboard)
86f5e4e7fdSDavid Herrmann  *     Usage(3)
87f5e4e7fdSDavid Herrmann  *       LED.NumLock
88f5e4e7fdSDavid Herrmann  *       LED.CapsLock
89f5e4e7fdSDavid Herrmann  *       LED.ScrollLock
90f5e4e7fdSDavid Herrmann  *     Logical Minimum(0)
91f5e4e7fdSDavid Herrmann  *     Logical Maximum(1)
92f5e4e7fdSDavid Herrmann  *     Report Size(1)
93f5e4e7fdSDavid Herrmann  *     Report Count(3)
94f5e4e7fdSDavid Herrmann  *     Report Offset(0)
95f5e4e7fdSDavid Herrmann  *     Flags( Variable Absolute )
965148fa52SDavid Herrmann  *
975148fa52SDavid Herrmann  * This is the mapping that we expect:
985148fa52SDavid Herrmann  *   Button.0001 ---> Key.LeftBtn
995148fa52SDavid Herrmann  *   Button.0002 ---> Key.RightBtn
1005148fa52SDavid Herrmann  *   Button.0003 ---> Key.MiddleBtn
1015148fa52SDavid Herrmann  *   GenericDesktop.X ---> Relative.X
1025148fa52SDavid Herrmann  *   GenericDesktop.Y ---> Relative.Y
1035148fa52SDavid Herrmann  *   GenericDesktop.Wheel ---> Relative.Wheel
104f5e4e7fdSDavid Herrmann  *   LED.NumLock ---> LED.NumLock
105f5e4e7fdSDavid Herrmann  *   LED.CapsLock ---> LED.CapsLock
106f5e4e7fdSDavid Herrmann  *   LED.ScrollLock ---> LED.ScrollLock
1075148fa52SDavid Herrmann  *
1085148fa52SDavid Herrmann  * This information can be verified by reading /sys/kernel/debug/hid/<dev>/rdesc
1095148fa52SDavid Herrmann  * This file should print the same information as showed above.
1105148fa52SDavid Herrmann  */
1115148fa52SDavid Herrmann 
1125148fa52SDavid Herrmann static unsigned char rdesc[] = {
113f5e4e7fdSDavid Herrmann 	0x05, 0x01,	/* USAGE_PAGE (Generic Desktop) */
114f5e4e7fdSDavid Herrmann 	0x09, 0x02,	/* USAGE (Mouse) */
115f5e4e7fdSDavid Herrmann 	0xa1, 0x01,	/* COLLECTION (Application) */
116f5e4e7fdSDavid Herrmann 	0x09, 0x01,		/* USAGE (Pointer) */
117f5e4e7fdSDavid Herrmann 	0xa1, 0x00,		/* COLLECTION (Physical) */
118f5e4e7fdSDavid Herrmann 	0x85, 0x01,			/* REPORT_ID (1) */
119f5e4e7fdSDavid Herrmann 	0x05, 0x09,			/* USAGE_PAGE (Button) */
120f5e4e7fdSDavid Herrmann 	0x19, 0x01,			/* USAGE_MINIMUM (Button 1) */
121f5e4e7fdSDavid Herrmann 	0x29, 0x03,			/* USAGE_MAXIMUM (Button 3) */
122f5e4e7fdSDavid Herrmann 	0x15, 0x00,			/* LOGICAL_MINIMUM (0) */
123f5e4e7fdSDavid Herrmann 	0x25, 0x01,			/* LOGICAL_MAXIMUM (1) */
124f5e4e7fdSDavid Herrmann 	0x95, 0x03,			/* REPORT_COUNT (3) */
125f5e4e7fdSDavid Herrmann 	0x75, 0x01,			/* REPORT_SIZE (1) */
126f5e4e7fdSDavid Herrmann 	0x81, 0x02,			/* INPUT (Data,Var,Abs) */
127f5e4e7fdSDavid Herrmann 	0x95, 0x01,			/* REPORT_COUNT (1) */
128f5e4e7fdSDavid Herrmann 	0x75, 0x05,			/* REPORT_SIZE (5) */
129f5e4e7fdSDavid Herrmann 	0x81, 0x01,			/* INPUT (Cnst,Var,Abs) */
130f5e4e7fdSDavid Herrmann 	0x05, 0x01,			/* USAGE_PAGE (Generic Desktop) */
131f5e4e7fdSDavid Herrmann 	0x09, 0x30,			/* USAGE (X) */
132f5e4e7fdSDavid Herrmann 	0x09, 0x31,			/* USAGE (Y) */
133f5e4e7fdSDavid Herrmann 	0x09, 0x38,			/* USAGE (WHEEL) */
134f5e4e7fdSDavid Herrmann 	0x15, 0x81,			/* LOGICAL_MINIMUM (-127) */
135f5e4e7fdSDavid Herrmann 	0x25, 0x7f,			/* LOGICAL_MAXIMUM (127) */
136f5e4e7fdSDavid Herrmann 	0x75, 0x08,			/* REPORT_SIZE (8) */
137f5e4e7fdSDavid Herrmann 	0x95, 0x03,			/* REPORT_COUNT (3) */
138f5e4e7fdSDavid Herrmann 	0x81, 0x06,			/* INPUT (Data,Var,Rel) */
139f5e4e7fdSDavid Herrmann 	0xc0,			/* END_COLLECTION */
140f5e4e7fdSDavid Herrmann 	0xc0,		/* END_COLLECTION */
141f5e4e7fdSDavid Herrmann 	0x05, 0x01,	/* USAGE_PAGE (Generic Desktop) */
142f5e4e7fdSDavid Herrmann 	0x09, 0x06,	/* USAGE (Keyboard) */
143f5e4e7fdSDavid Herrmann 	0xa1, 0x01,	/* COLLECTION (Application) */
144f5e4e7fdSDavid Herrmann 	0x85, 0x02,		/* REPORT_ID (2) */
145f5e4e7fdSDavid Herrmann 	0x05, 0x08,		/* USAGE_PAGE (Led) */
146f5e4e7fdSDavid Herrmann 	0x19, 0x01,		/* USAGE_MINIMUM (1) */
147f5e4e7fdSDavid Herrmann 	0x29, 0x03,		/* USAGE_MAXIMUM (3) */
148f5e4e7fdSDavid Herrmann 	0x15, 0x00,		/* LOGICAL_MINIMUM (0) */
149f5e4e7fdSDavid Herrmann 	0x25, 0x01,		/* LOGICAL_MAXIMUM (1) */
150f5e4e7fdSDavid Herrmann 	0x95, 0x03,		/* REPORT_COUNT (3) */
151f5e4e7fdSDavid Herrmann 	0x75, 0x01,		/* REPORT_SIZE (1) */
152f5e4e7fdSDavid Herrmann 	0x91, 0x02,		/* Output (Data,Var,Abs) */
153f5e4e7fdSDavid Herrmann 	0x95, 0x01,		/* REPORT_COUNT (1) */
154f5e4e7fdSDavid Herrmann 	0x75, 0x05,		/* REPORT_SIZE (5) */
155f5e4e7fdSDavid Herrmann 	0x91, 0x01,		/* Output (Cnst,Var,Abs) */
156f5e4e7fdSDavid Herrmann 	0xc0,		/* END_COLLECTION */
1575148fa52SDavid Herrmann };
1585148fa52SDavid Herrmann 
1595148fa52SDavid Herrmann static int uhid_write(int fd, const struct uhid_event *ev)
1605148fa52SDavid Herrmann {
1615148fa52SDavid Herrmann 	ssize_t ret;
1625148fa52SDavid Herrmann 
1635148fa52SDavid Herrmann 	ret = write(fd, ev, sizeof(*ev));
1645148fa52SDavid Herrmann 	if (ret < 0) {
1655148fa52SDavid Herrmann 		fprintf(stderr, "Cannot write to uhid: %m\n");
1665148fa52SDavid Herrmann 		return -errno;
1675148fa52SDavid Herrmann 	} else if (ret != sizeof(*ev)) {
1685148fa52SDavid Herrmann 		fprintf(stderr, "Wrong size written to uhid: %ld != %lu\n",
1695148fa52SDavid Herrmann 			ret, sizeof(ev));
1705148fa52SDavid Herrmann 		return -EFAULT;
1715148fa52SDavid Herrmann 	} else {
1725148fa52SDavid Herrmann 		return 0;
1735148fa52SDavid Herrmann 	}
1745148fa52SDavid Herrmann }
1755148fa52SDavid Herrmann 
1765148fa52SDavid Herrmann static int create(int fd)
1775148fa52SDavid Herrmann {
1785148fa52SDavid Herrmann 	struct uhid_event ev;
1795148fa52SDavid Herrmann 
1805148fa52SDavid Herrmann 	memset(&ev, 0, sizeof(ev));
1815148fa52SDavid Herrmann 	ev.type = UHID_CREATE;
1825148fa52SDavid Herrmann 	strcpy((char*)ev.u.create.name, "test-uhid-device");
1835148fa52SDavid Herrmann 	ev.u.create.rd_data = rdesc;
1845148fa52SDavid Herrmann 	ev.u.create.rd_size = sizeof(rdesc);
1855148fa52SDavid Herrmann 	ev.u.create.bus = BUS_USB;
1865148fa52SDavid Herrmann 	ev.u.create.vendor = 0x15d9;
1875148fa52SDavid Herrmann 	ev.u.create.product = 0x0a37;
1885148fa52SDavid Herrmann 	ev.u.create.version = 0;
1895148fa52SDavid Herrmann 	ev.u.create.country = 0;
1905148fa52SDavid Herrmann 
1915148fa52SDavid Herrmann 	return uhid_write(fd, &ev);
1925148fa52SDavid Herrmann }
1935148fa52SDavid Herrmann 
1945148fa52SDavid Herrmann static void destroy(int fd)
1955148fa52SDavid Herrmann {
1965148fa52SDavid Herrmann 	struct uhid_event ev;
1975148fa52SDavid Herrmann 
1985148fa52SDavid Herrmann 	memset(&ev, 0, sizeof(ev));
1995148fa52SDavid Herrmann 	ev.type = UHID_DESTROY;
2005148fa52SDavid Herrmann 
2015148fa52SDavid Herrmann 	uhid_write(fd, &ev);
2025148fa52SDavid Herrmann }
2035148fa52SDavid Herrmann 
204f5e4e7fdSDavid Herrmann /* This parses raw output reports sent by the kernel to the device. A normal
205f5e4e7fdSDavid Herrmann  * uhid program shouldn't do this but instead just forward the raw report.
206f5e4e7fdSDavid Herrmann  * However, for ducomentational purposes, we try to detect LED events here and
207f5e4e7fdSDavid Herrmann  * print debug messages for it. */
208f5e4e7fdSDavid Herrmann static void handle_output(struct uhid_event *ev)
209f5e4e7fdSDavid Herrmann {
210f5e4e7fdSDavid Herrmann 	/* LED messages are adverised via OUTPUT reports; ignore the rest */
211f5e4e7fdSDavid Herrmann 	if (ev->u.output.rtype != UHID_OUTPUT_REPORT)
212f5e4e7fdSDavid Herrmann 		return;
213f5e4e7fdSDavid Herrmann 	/* LED reports have length 2 bytes */
214f5e4e7fdSDavid Herrmann 	if (ev->u.output.size != 2)
215f5e4e7fdSDavid Herrmann 		return;
216f5e4e7fdSDavid Herrmann 	/* first byte is report-id which is 0x02 for LEDs in our rdesc */
217f5e4e7fdSDavid Herrmann 	if (ev->u.output.data[0] != 0x2)
218f5e4e7fdSDavid Herrmann 		return;
219f5e4e7fdSDavid Herrmann 
220f5e4e7fdSDavid Herrmann 	/* print flags payload */
221f5e4e7fdSDavid Herrmann 	fprintf(stderr, "LED output report received with flags %x\n",
222f5e4e7fdSDavid Herrmann 		ev->u.output.data[1]);
223f5e4e7fdSDavid Herrmann }
224f5e4e7fdSDavid Herrmann 
2255148fa52SDavid Herrmann static int event(int fd)
2265148fa52SDavid Herrmann {
2275148fa52SDavid Herrmann 	struct uhid_event ev;
2285148fa52SDavid Herrmann 	ssize_t ret;
2295148fa52SDavid Herrmann 
2305148fa52SDavid Herrmann 	memset(&ev, 0, sizeof(ev));
2315148fa52SDavid Herrmann 	ret = read(fd, &ev, sizeof(ev));
2325148fa52SDavid Herrmann 	if (ret == 0) {
2335148fa52SDavid Herrmann 		fprintf(stderr, "Read HUP on uhid-cdev\n");
2345148fa52SDavid Herrmann 		return -EFAULT;
2355148fa52SDavid Herrmann 	} else if (ret < 0) {
2365148fa52SDavid Herrmann 		fprintf(stderr, "Cannot read uhid-cdev: %m\n");
2375148fa52SDavid Herrmann 		return -errno;
2385148fa52SDavid Herrmann 	} else if (ret != sizeof(ev)) {
2395148fa52SDavid Herrmann 		fprintf(stderr, "Invalid size read from uhid-dev: %ld != %lu\n",
2405148fa52SDavid Herrmann 			ret, sizeof(ev));
2415148fa52SDavid Herrmann 		return -EFAULT;
2425148fa52SDavid Herrmann 	}
2435148fa52SDavid Herrmann 
2445148fa52SDavid Herrmann 	switch (ev.type) {
2455148fa52SDavid Herrmann 	case UHID_START:
2465148fa52SDavid Herrmann 		fprintf(stderr, "UHID_START from uhid-dev\n");
2475148fa52SDavid Herrmann 		break;
2485148fa52SDavid Herrmann 	case UHID_STOP:
2495148fa52SDavid Herrmann 		fprintf(stderr, "UHID_STOP from uhid-dev\n");
2505148fa52SDavid Herrmann 		break;
2515148fa52SDavid Herrmann 	case UHID_OPEN:
2525148fa52SDavid Herrmann 		fprintf(stderr, "UHID_OPEN from uhid-dev\n");
2535148fa52SDavid Herrmann 		break;
2545148fa52SDavid Herrmann 	case UHID_CLOSE:
2555148fa52SDavid Herrmann 		fprintf(stderr, "UHID_CLOSE from uhid-dev\n");
2565148fa52SDavid Herrmann 		break;
2575148fa52SDavid Herrmann 	case UHID_OUTPUT:
2585148fa52SDavid Herrmann 		fprintf(stderr, "UHID_OUTPUT from uhid-dev\n");
259f5e4e7fdSDavid Herrmann 		handle_output(&ev);
2605148fa52SDavid Herrmann 		break;
2615148fa52SDavid Herrmann 	case UHID_OUTPUT_EV:
2625148fa52SDavid Herrmann 		fprintf(stderr, "UHID_OUTPUT_EV from uhid-dev\n");
2635148fa52SDavid Herrmann 		break;
2645148fa52SDavid Herrmann 	default:
2655148fa52SDavid Herrmann 		fprintf(stderr, "Invalid event from uhid-dev: %u\n", ev.type);
2665148fa52SDavid Herrmann 	}
2675148fa52SDavid Herrmann 
2685148fa52SDavid Herrmann 	return 0;
2695148fa52SDavid Herrmann }
2705148fa52SDavid Herrmann 
2715148fa52SDavid Herrmann static bool btn1_down;
2725148fa52SDavid Herrmann static bool btn2_down;
2735148fa52SDavid Herrmann static bool btn3_down;
2745148fa52SDavid Herrmann static signed char abs_hor;
2755148fa52SDavid Herrmann static signed char abs_ver;
2765148fa52SDavid Herrmann static signed char wheel;
2775148fa52SDavid Herrmann 
2785148fa52SDavid Herrmann static int send_event(int fd)
2795148fa52SDavid Herrmann {
2805148fa52SDavid Herrmann 	struct uhid_event ev;
2815148fa52SDavid Herrmann 
2825148fa52SDavid Herrmann 	memset(&ev, 0, sizeof(ev));
2835148fa52SDavid Herrmann 	ev.type = UHID_INPUT;
284f5e4e7fdSDavid Herrmann 	ev.u.input.size = 5;
2855148fa52SDavid Herrmann 
286f5e4e7fdSDavid Herrmann 	ev.u.input.data[0] = 0x1;
2875148fa52SDavid Herrmann 	if (btn1_down)
288f5e4e7fdSDavid Herrmann 		ev.u.input.data[1] |= 0x1;
2895148fa52SDavid Herrmann 	if (btn2_down)
290f5e4e7fdSDavid Herrmann 		ev.u.input.data[1] |= 0x2;
2915148fa52SDavid Herrmann 	if (btn3_down)
292f5e4e7fdSDavid Herrmann 		ev.u.input.data[1] |= 0x4;
2935148fa52SDavid Herrmann 
294f5e4e7fdSDavid Herrmann 	ev.u.input.data[2] = abs_hor;
295f5e4e7fdSDavid Herrmann 	ev.u.input.data[3] = abs_ver;
296f5e4e7fdSDavid Herrmann 	ev.u.input.data[4] = wheel;
2975148fa52SDavid Herrmann 
2985148fa52SDavid Herrmann 	return uhid_write(fd, &ev);
2995148fa52SDavid Herrmann }
3005148fa52SDavid Herrmann 
3015148fa52SDavid Herrmann static int keyboard(int fd)
3025148fa52SDavid Herrmann {
3035148fa52SDavid Herrmann 	char buf[128];
3045148fa52SDavid Herrmann 	ssize_t ret, i;
3055148fa52SDavid Herrmann 
3065148fa52SDavid Herrmann 	ret = read(STDIN_FILENO, buf, sizeof(buf));
3075148fa52SDavid Herrmann 	if (ret == 0) {
3085148fa52SDavid Herrmann 		fprintf(stderr, "Read HUP on stdin\n");
3095148fa52SDavid Herrmann 		return -EFAULT;
3105148fa52SDavid Herrmann 	} else if (ret < 0) {
3115148fa52SDavid Herrmann 		fprintf(stderr, "Cannot read stdin: %m\n");
3125148fa52SDavid Herrmann 		return -errno;
3135148fa52SDavid Herrmann 	}
3145148fa52SDavid Herrmann 
3155148fa52SDavid Herrmann 	for (i = 0; i < ret; ++i) {
3165148fa52SDavid Herrmann 		switch (buf[i]) {
3175148fa52SDavid Herrmann 		case '1':
3185148fa52SDavid Herrmann 			btn1_down = !btn1_down;
3195148fa52SDavid Herrmann 			ret = send_event(fd);
3205148fa52SDavid Herrmann 			if (ret)
3215148fa52SDavid Herrmann 				return ret;
3225148fa52SDavid Herrmann 			break;
3235148fa52SDavid Herrmann 		case '2':
3245148fa52SDavid Herrmann 			btn2_down = !btn2_down;
3255148fa52SDavid Herrmann 			ret = send_event(fd);
3265148fa52SDavid Herrmann 			if (ret)
3275148fa52SDavid Herrmann 				return ret;
3285148fa52SDavid Herrmann 			break;
3295148fa52SDavid Herrmann 		case '3':
3305148fa52SDavid Herrmann 			btn3_down = !btn3_down;
3315148fa52SDavid Herrmann 			ret = send_event(fd);
3325148fa52SDavid Herrmann 			if (ret)
3335148fa52SDavid Herrmann 				return ret;
3345148fa52SDavid Herrmann 			break;
3355148fa52SDavid Herrmann 		case 'a':
3365148fa52SDavid Herrmann 			abs_hor = -20;
3375148fa52SDavid Herrmann 			ret = send_event(fd);
3385148fa52SDavid Herrmann 			abs_hor = 0;
3395148fa52SDavid Herrmann 			if (ret)
3405148fa52SDavid Herrmann 				return ret;
3415148fa52SDavid Herrmann 			break;
3425148fa52SDavid Herrmann 		case 'd':
3435148fa52SDavid Herrmann 			abs_hor = 20;
3445148fa52SDavid Herrmann 			ret = send_event(fd);
3455148fa52SDavid Herrmann 			abs_hor = 0;
3465148fa52SDavid Herrmann 			if (ret)
3475148fa52SDavid Herrmann 				return ret;
3485148fa52SDavid Herrmann 			break;
3495148fa52SDavid Herrmann 		case 'w':
3505148fa52SDavid Herrmann 			abs_ver = -20;
3515148fa52SDavid Herrmann 			ret = send_event(fd);
3525148fa52SDavid Herrmann 			abs_ver = 0;
3535148fa52SDavid Herrmann 			if (ret)
3545148fa52SDavid Herrmann 				return ret;
3555148fa52SDavid Herrmann 			break;
3565148fa52SDavid Herrmann 		case 's':
3575148fa52SDavid Herrmann 			abs_ver = 20;
3585148fa52SDavid Herrmann 			ret = send_event(fd);
3595148fa52SDavid Herrmann 			abs_ver = 0;
3605148fa52SDavid Herrmann 			if (ret)
3615148fa52SDavid Herrmann 				return ret;
3625148fa52SDavid Herrmann 			break;
3635148fa52SDavid Herrmann 		case 'r':
3645148fa52SDavid Herrmann 			wheel = 1;
3655148fa52SDavid Herrmann 			ret = send_event(fd);
3665148fa52SDavid Herrmann 			wheel = 0;
3675148fa52SDavid Herrmann 			if (ret)
3685148fa52SDavid Herrmann 				return ret;
3695148fa52SDavid Herrmann 			break;
3705148fa52SDavid Herrmann 		case 'f':
3715148fa52SDavid Herrmann 			wheel = -1;
3725148fa52SDavid Herrmann 			ret = send_event(fd);
3735148fa52SDavid Herrmann 			wheel = 0;
3745148fa52SDavid Herrmann 			if (ret)
3755148fa52SDavid Herrmann 				return ret;
3765148fa52SDavid Herrmann 			break;
3775148fa52SDavid Herrmann 		case 'q':
3785148fa52SDavid Herrmann 			return -ECANCELED;
3795148fa52SDavid Herrmann 		default:
3805148fa52SDavid Herrmann 			fprintf(stderr, "Invalid input: %c\n", buf[i]);
3815148fa52SDavid Herrmann 		}
3825148fa52SDavid Herrmann 	}
3835148fa52SDavid Herrmann 
3845148fa52SDavid Herrmann 	return 0;
3855148fa52SDavid Herrmann }
3865148fa52SDavid Herrmann 
3875148fa52SDavid Herrmann int main(int argc, char **argv)
3885148fa52SDavid Herrmann {
3895148fa52SDavid Herrmann 	int fd;
3905148fa52SDavid Herrmann 	const char *path = "/dev/uhid";
3915148fa52SDavid Herrmann 	struct pollfd pfds[2];
3925148fa52SDavid Herrmann 	int ret;
3935148fa52SDavid Herrmann 	struct termios state;
3945148fa52SDavid Herrmann 
3955148fa52SDavid Herrmann 	ret = tcgetattr(STDIN_FILENO, &state);
3965148fa52SDavid Herrmann 	if (ret) {
3975148fa52SDavid Herrmann 		fprintf(stderr, "Cannot get tty state\n");
3985148fa52SDavid Herrmann 	} else {
3995148fa52SDavid Herrmann 		state.c_lflag &= ~ICANON;
4005148fa52SDavid Herrmann 		state.c_cc[VMIN] = 1;
4015148fa52SDavid Herrmann 		ret = tcsetattr(STDIN_FILENO, TCSANOW, &state);
4025148fa52SDavid Herrmann 		if (ret)
4035148fa52SDavid Herrmann 			fprintf(stderr, "Cannot set tty state\n");
4045148fa52SDavid Herrmann 	}
4055148fa52SDavid Herrmann 
4065148fa52SDavid Herrmann 	if (argc >= 2) {
4075148fa52SDavid Herrmann 		if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
4085148fa52SDavid Herrmann 			fprintf(stderr, "Usage: %s [%s]\n", argv[0], path);
4095148fa52SDavid Herrmann 			return EXIT_SUCCESS;
4105148fa52SDavid Herrmann 		} else {
4115148fa52SDavid Herrmann 			path = argv[1];
4125148fa52SDavid Herrmann 		}
4135148fa52SDavid Herrmann 	}
4145148fa52SDavid Herrmann 
4155148fa52SDavid Herrmann 	fprintf(stderr, "Open uhid-cdev %s\n", path);
4165148fa52SDavid Herrmann 	fd = open(path, O_RDWR | O_CLOEXEC);
4175148fa52SDavid Herrmann 	if (fd < 0) {
4185148fa52SDavid Herrmann 		fprintf(stderr, "Cannot open uhid-cdev %s: %m\n", path);
4195148fa52SDavid Herrmann 		return EXIT_FAILURE;
4205148fa52SDavid Herrmann 	}
4215148fa52SDavid Herrmann 
4225148fa52SDavid Herrmann 	fprintf(stderr, "Create uhid device\n");
4235148fa52SDavid Herrmann 	ret = create(fd);
4245148fa52SDavid Herrmann 	if (ret) {
4255148fa52SDavid Herrmann 		close(fd);
4265148fa52SDavid Herrmann 		return EXIT_FAILURE;
4275148fa52SDavid Herrmann 	}
4285148fa52SDavid Herrmann 
4295148fa52SDavid Herrmann 	pfds[0].fd = STDIN_FILENO;
4305148fa52SDavid Herrmann 	pfds[0].events = POLLIN;
4315148fa52SDavid Herrmann 	pfds[1].fd = fd;
4325148fa52SDavid Herrmann 	pfds[1].events = POLLIN;
4335148fa52SDavid Herrmann 
4345148fa52SDavid Herrmann 	fprintf(stderr, "Press 'q' to quit...\n");
4355148fa52SDavid Herrmann 	while (1) {
4365148fa52SDavid Herrmann 		ret = poll(pfds, 2, -1);
4375148fa52SDavid Herrmann 		if (ret < 0) {
4385148fa52SDavid Herrmann 			fprintf(stderr, "Cannot poll for fds: %m\n");
4395148fa52SDavid Herrmann 			break;
4405148fa52SDavid Herrmann 		}
4415148fa52SDavid Herrmann 		if (pfds[0].revents & POLLHUP) {
4425148fa52SDavid Herrmann 			fprintf(stderr, "Received HUP on stdin\n");
4435148fa52SDavid Herrmann 			break;
4445148fa52SDavid Herrmann 		}
4455148fa52SDavid Herrmann 		if (pfds[1].revents & POLLHUP) {
4465148fa52SDavid Herrmann 			fprintf(stderr, "Received HUP on uhid-cdev\n");
4475148fa52SDavid Herrmann 			break;
4485148fa52SDavid Herrmann 		}
4495148fa52SDavid Herrmann 
4505148fa52SDavid Herrmann 		if (pfds[0].revents & POLLIN) {
4515148fa52SDavid Herrmann 			ret = keyboard(fd);
4525148fa52SDavid Herrmann 			if (ret)
4535148fa52SDavid Herrmann 				break;
4545148fa52SDavid Herrmann 		}
4555148fa52SDavid Herrmann 		if (pfds[1].revents & POLLIN) {
4565148fa52SDavid Herrmann 			ret = event(fd);
4575148fa52SDavid Herrmann 			if (ret)
4585148fa52SDavid Herrmann 				break;
4595148fa52SDavid Herrmann 		}
4605148fa52SDavid Herrmann 	}
4615148fa52SDavid Herrmann 
4625148fa52SDavid Herrmann 	fprintf(stderr, "Destroy uhid device\n");
4635148fa52SDavid Herrmann 	destroy(fd);
4645148fa52SDavid Herrmann 	return EXIT_SUCCESS;
4655148fa52SDavid Herrmann }
466