Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * UHID Example
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * The code may be used by anyone for any purpose,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * and can serve as a starting point for developing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * applications using uhid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * UHID Example
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * This example emulates a basic 3 buttons mouse with wheel over UHID. Run this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * program as root and then use the following keys to control the mouse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *   q: Quit the application
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *   1: Toggle left button (down, up, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *   2: Toggle right button
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *   3: Toggle middle button
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *   a: Move mouse left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *   d: Move mouse right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *   w: Move mouse up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *   s: Move mouse down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *   r: Move wheel up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *   f: Move wheel down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * Additionally to 3 button mouse, 3 keyboard LEDs are also supported (LED_NUML,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * LED_CAPSL and LED_SCROLLL). The device doesn't generate any related keyboard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * events, though. You need to manually write the EV_LED/LED_XY/1 activation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * input event to the evdev device to see it being sent to this device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * If uhid is not available as /dev/uhid, then you can pass a different path as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * first argument.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * If <linux/uhid.h> is not installed in /usr, then compile this with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *   gcc -o ./uhid_test -Wall -I./include ./samples/uhid/uhid-example.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * And ignore the warning about kernel headers. However, it is recommended to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * use the installed uhid.h if available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include <termios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #include <linux/uhid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * HID Report Desciptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * We emulate a basic 3 button mouse with wheel and 3 keyboard LEDs. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * the report-descriptor as the kernel will parse it:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * INPUT(1)[INPUT]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  *   Field(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  *     Physical(GenericDesktop.Pointer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  *     Application(GenericDesktop.Mouse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  *     Usage(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  *       Button.0001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  *       Button.0002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  *       Button.0003
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  *     Logical Minimum(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  *     Logical Maximum(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  *     Report Size(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  *     Report Count(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  *     Report Offset(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  *     Flags( Variable Absolute )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  *   Field(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  *     Physical(GenericDesktop.Pointer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  *     Application(GenericDesktop.Mouse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  *     Usage(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  *       GenericDesktop.X
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  *       GenericDesktop.Y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  *       GenericDesktop.Wheel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  *     Logical Minimum(-128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  *     Logical Maximum(127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  *     Report Size(8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  *     Report Count(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  *     Report Offset(8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  *     Flags( Variable Relative )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * OUTPUT(2)[OUTPUT]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  *   Field(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  *     Application(GenericDesktop.Keyboard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  *     Usage(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  *       LED.NumLock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  *       LED.CapsLock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  *       LED.ScrollLock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  *     Logical Minimum(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  *     Logical Maximum(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  *     Report Size(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  *     Report Count(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  *     Report Offset(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  *     Flags( Variable Absolute )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * This is the mapping that we expect:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  *   Button.0001 ---> Key.LeftBtn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  *   Button.0002 ---> Key.RightBtn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  *   Button.0003 ---> Key.MiddleBtn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  *   GenericDesktop.X ---> Relative.X
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  *   GenericDesktop.Y ---> Relative.Y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  *   GenericDesktop.Wheel ---> Relative.Wheel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  *   LED.NumLock ---> LED.NumLock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  *   LED.CapsLock ---> LED.CapsLock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  *   LED.ScrollLock ---> LED.ScrollLock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  * This information can be verified by reading /sys/kernel/debug/hid/<dev>/rdesc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  * This file should print the same information as showed above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static unsigned char rdesc[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	0x05, 0x01,	/* USAGE_PAGE (Generic Desktop) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	0x09, 0x02,	/* USAGE (Mouse) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	0xa1, 0x01,	/* COLLECTION (Application) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	0x09, 0x01,		/* USAGE (Pointer) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	0xa1, 0x00,		/* COLLECTION (Physical) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	0x85, 0x01,			/* REPORT_ID (1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	0x05, 0x09,			/* USAGE_PAGE (Button) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	0x19, 0x01,			/* USAGE_MINIMUM (Button 1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	0x29, 0x03,			/* USAGE_MAXIMUM (Button 3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	0x15, 0x00,			/* LOGICAL_MINIMUM (0) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	0x25, 0x01,			/* LOGICAL_MAXIMUM (1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	0x95, 0x03,			/* REPORT_COUNT (3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	0x75, 0x01,			/* REPORT_SIZE (1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	0x81, 0x02,			/* INPUT (Data,Var,Abs) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	0x95, 0x01,			/* REPORT_COUNT (1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	0x75, 0x05,			/* REPORT_SIZE (5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	0x81, 0x01,			/* INPUT (Cnst,Var,Abs) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	0x05, 0x01,			/* USAGE_PAGE (Generic Desktop) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	0x09, 0x30,			/* USAGE (X) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	0x09, 0x31,			/* USAGE (Y) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	0x09, 0x38,			/* USAGE (WHEEL) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	0x15, 0x81,			/* LOGICAL_MINIMUM (-127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	0x25, 0x7f,			/* LOGICAL_MAXIMUM (127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	0x75, 0x08,			/* REPORT_SIZE (8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	0x95, 0x03,			/* REPORT_COUNT (3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	0x81, 0x06,			/* INPUT (Data,Var,Rel) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	0xc0,			/* END_COLLECTION */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	0xc0,		/* END_COLLECTION */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	0x05, 0x01,	/* USAGE_PAGE (Generic Desktop) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	0x09, 0x06,	/* USAGE (Keyboard) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	0xa1, 0x01,	/* COLLECTION (Application) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	0x85, 0x02,		/* REPORT_ID (2) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	0x05, 0x08,		/* USAGE_PAGE (Led) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	0x19, 0x01,		/* USAGE_MINIMUM (1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	0x29, 0x03,		/* USAGE_MAXIMUM (3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	0x15, 0x00,		/* LOGICAL_MINIMUM (0) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	0x25, 0x01,		/* LOGICAL_MAXIMUM (1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	0x95, 0x03,		/* REPORT_COUNT (3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	0x75, 0x01,		/* REPORT_SIZE (1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	0x91, 0x02,		/* Output (Data,Var,Abs) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	0x95, 0x01,		/* REPORT_COUNT (1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	0x75, 0x05,		/* REPORT_SIZE (5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	0x91, 0x01,		/* Output (Cnst,Var,Abs) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	0xc0,		/* END_COLLECTION */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int uhid_write(int fd, const struct uhid_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	ret = write(fd, ev, sizeof(*ev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		fprintf(stderr, "Cannot write to uhid: %m\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	} else if (ret != sizeof(*ev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		fprintf(stderr, "Wrong size written to uhid: %zd != %zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			ret, sizeof(ev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static int create(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	struct uhid_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	memset(&ev, 0, sizeof(ev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	ev.type = UHID_CREATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	strcpy((char*)ev.u.create.name, "test-uhid-device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	ev.u.create.rd_data = rdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	ev.u.create.rd_size = sizeof(rdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	ev.u.create.bus = BUS_USB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	ev.u.create.vendor = 0x15d9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	ev.u.create.product = 0x0a37;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	ev.u.create.version = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	ev.u.create.country = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return uhid_write(fd, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static void destroy(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct uhid_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	memset(&ev, 0, sizeof(ev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	ev.type = UHID_DESTROY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	uhid_write(fd, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /* This parses raw output reports sent by the kernel to the device. A normal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * uhid program shouldn't do this but instead just forward the raw report.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  * However, for ducomentational purposes, we try to detect LED events here and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  * print debug messages for it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static void handle_output(struct uhid_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	/* LED messages are adverised via OUTPUT reports; ignore the rest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (ev->u.output.rtype != UHID_OUTPUT_REPORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	/* LED reports have length 2 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (ev->u.output.size != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	/* first byte is report-id which is 0x02 for LEDs in our rdesc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (ev->u.output.data[0] != 0x2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	/* print flags payload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	fprintf(stderr, "LED output report received with flags %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		ev->u.output.data[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static int event(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	struct uhid_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	memset(&ev, 0, sizeof(ev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	ret = read(fd, &ev, sizeof(ev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		fprintf(stderr, "Read HUP on uhid-cdev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	} else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		fprintf(stderr, "Cannot read uhid-cdev: %m\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	} else if (ret != sizeof(ev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		fprintf(stderr, "Invalid size read from uhid-dev: %zd != %zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			ret, sizeof(ev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	switch (ev.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	case UHID_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		fprintf(stderr, "UHID_START from uhid-dev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	case UHID_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		fprintf(stderr, "UHID_STOP from uhid-dev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	case UHID_OPEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		fprintf(stderr, "UHID_OPEN from uhid-dev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	case UHID_CLOSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		fprintf(stderr, "UHID_CLOSE from uhid-dev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	case UHID_OUTPUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		fprintf(stderr, "UHID_OUTPUT from uhid-dev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		handle_output(&ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	case UHID_OUTPUT_EV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		fprintf(stderr, "UHID_OUTPUT_EV from uhid-dev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		fprintf(stderr, "Invalid event from uhid-dev: %u\n", ev.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static bool btn1_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static bool btn2_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static bool btn3_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static signed char abs_hor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static signed char abs_ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static signed char wheel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static int send_event(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	struct uhid_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	memset(&ev, 0, sizeof(ev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	ev.type = UHID_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	ev.u.input.size = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	ev.u.input.data[0] = 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (btn1_down)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		ev.u.input.data[1] |= 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (btn2_down)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		ev.u.input.data[1] |= 0x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (btn3_down)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		ev.u.input.data[1] |= 0x4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	ev.u.input.data[2] = abs_hor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	ev.u.input.data[3] = abs_ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	ev.u.input.data[4] = wheel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	return uhid_write(fd, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static int keyboard(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	char buf[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	ssize_t ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	ret = read(STDIN_FILENO, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		fprintf(stderr, "Read HUP on stdin\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	} else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		fprintf(stderr, "Cannot read stdin: %m\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	for (i = 0; i < ret; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		switch (buf[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		case '1':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			btn1_down = !btn1_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			ret = send_event(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		case '2':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			btn2_down = !btn2_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			ret = send_event(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		case '3':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			btn3_down = !btn3_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			ret = send_event(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		case 'a':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			abs_hor = -20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			ret = send_event(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			abs_hor = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		case 'd':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			abs_hor = 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			ret = send_event(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			abs_hor = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		case 'w':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			abs_ver = -20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			ret = send_event(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			abs_ver = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		case 's':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			abs_ver = 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			ret = send_event(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			abs_ver = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		case 'r':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			wheel = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			ret = send_event(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			wheel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		case 'f':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			wheel = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			ret = send_event(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			wheel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		case 'q':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			return -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			fprintf(stderr, "Invalid input: %c\n", buf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	const char *path = "/dev/uhid";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	struct pollfd pfds[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	struct termios state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	ret = tcgetattr(STDIN_FILENO, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		fprintf(stderr, "Cannot get tty state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		state.c_lflag &= ~ICANON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		state.c_cc[VMIN] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		ret = tcsetattr(STDIN_FILENO, TCSANOW, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			fprintf(stderr, "Cannot set tty state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	if (argc >= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			fprintf(stderr, "Usage: %s [%s]\n", argv[0], path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			return EXIT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			path = argv[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	fprintf(stderr, "Open uhid-cdev %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	fd = open(path, O_RDWR | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		fprintf(stderr, "Cannot open uhid-cdev %s: %m\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	fprintf(stderr, "Create uhid device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	ret = create(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	pfds[0].fd = STDIN_FILENO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	pfds[0].events = POLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	pfds[1].fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	pfds[1].events = POLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	fprintf(stderr, "Press 'q' to quit...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		ret = poll(pfds, 2, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 			fprintf(stderr, "Cannot poll for fds: %m\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		if (pfds[0].revents & POLLHUP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			fprintf(stderr, "Received HUP on stdin\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		if (pfds[1].revents & POLLHUP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			fprintf(stderr, "Received HUP on uhid-cdev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		if (pfds[0].revents & POLLIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			ret = keyboard(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		if (pfds[1].revents & POLLIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			ret = event(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	fprintf(stderr, "Destroy uhid device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	destroy(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	return EXIT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }