24 #include <linux/uinput.h>
25 #include <linux/input.h>
32 #if defined _SKQ_KEY_MAP_1_
33 #include "IrInputRemoteKeyCodes_SkyQ.h"
38 #define UINPUT_SETUP_ID(uidev) \
40 memset(&uidev, 0, sizeof(uidev));\
41 snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-remote");\
42 uidev.id.bustype = BUS_USB;\
43 uidev.id.vendor = 0x1234;\
44 uidev.id.product = 0xfedc;\
45 uidev.id.version = 1;\
48 #define UINPUT_SETUP_ID_SKY_KEYSIM(uidev) \
50 memset(&uidev, 0, sizeof(uidev));\
51 snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "key-simulator");\
52 uidev.id.bustype = BUS_USB;\
53 uidev.id.vendor = 0xbeef;\
54 uidev.id.product = 0xfedc;\
55 uidev.id.version = 1;\
62 static int devFd = -1;
63 static uint32_t getKeyCode(uint32_t keycode, uint32_t *uCode, uint32_t *uModi)
67 printf(
"%s %d uinput received Key code %d 0x%x \r\n", __FUNCTION__, __LINE__, keycode, keycode);
68 for (i=0; i < (
sizeof(kcodesMap_IARM2Linux)/
sizeof(kcodesMap_IARM2Linux[0])); i++)
70 if (kcodesMap_IARM2Linux[i].iCode == keycode)
72 *uCode = kcodesMap_IARM2Linux[i].uCode;
73 *uModi = kcodesMap_IARM2Linux[i].uModi;
74 return kcodesMap_IARM2Linux[i].uCode;
78 printf(
"UNrecognized Key code %d \r\n", keycode);
79 return KED_UNDEFINEDKEY;
83 static uint32_t getKeyValue(uint32_t
keyType)
94 printf(
"UNrecognized Key type %d \r\n",
keyType);
100 static void udispatcher_internal(
int code,
int value)
103 struct input_event ev;
104 memset(&ev, 0,
sizeof(ev));
105 gettimeofday(&ev.time, NULL);
109 ret = write(devFd, &ev,
sizeof(ev));
110 if (ret ==
sizeof(ev)) {
113 ev.code = SYN_REPORT;
115 ret = write(devFd, &ev,
sizeof(ev));
118 perror(
"uinput write key failed\r\n");
123 #if defined _SKQ_KEY_MAP_1_
124 static void udispatcherScancode_internal(
int scancode,
int code,
int value)
127 struct input_event ev;
128 memset(&ev, 0,
sizeof(ev));
129 gettimeofday(&ev.time, NULL);
133 ret = write(devFd, &ev,
sizeof(ev));
134 if (ret ==
sizeof(ev)) {
138 ret = write(devFd, &ev,
sizeof(ev));
139 if (ret ==
sizeof(ev)) {
142 ev.code = SYN_REPORT;
144 ret = write(devFd, &ev,
sizeof(ev));
147 perror(
"uinput write key failed\r\n");
153 perror(
"uinput write key failed\r\n");
159 static void udispatcher (
int keyCode,
int keyType,
int source)
161 printf(
"%s %d uinput received Key code= %d 0x%x keyType= %d 0x%x \r\n", __FUNCTION__, __LINE__, keyCode, keyCode,
keyType,
keyType);
163 static const char * type2str[] = {
"KEY_UP",
"KEY_DOWN",
"KEY_REPEAT"};
164 uint32_t uCode = _KEY_INVALID;
165 uint32_t uModi = _KEY_INVALID;
168 getKeyCode(keyCode, &uCode, &uModi);
170 printf(
"IR-Keyboard Regular Key: IR=%x key=%x Modifier=%x val=%x [%s]\r\n", keyCode, uCode, uModi, value, type2str[value]);
177 if ((
keyType == KET_KEYDOWN)) {
178 if (uModi != _KEY_INVALID) {
179 udispatcher_internal(uModi, value);
182 udispatcher_internal((
int)uCode, (
int)value);
184 if (uModi != _KEY_INVALID) {
185 udispatcher_internal(uModi, value);
191 #if defined _SKQ_KEY_MAP_1_
192 static void udispatcherScancode (
int scanCode,
int keyCode,
int keyType,
int source)
194 printf(
"%s %d uinput received Key code= %d 0x%x keyType= %d 0x%x \r\n", __FUNCTION__, __LINE__, keyCode, keyCode,
keyType,
keyType);
196 static const char * type2str[] = {
"KEY_UP",
"KEY_DOWN",
"KEY_REPEAT"};
197 uint32_t uCode = _KEY_INVALID;
198 uint32_t uModi = _KEY_INVALID;
201 getKeyCode(keyCode, &uCode, &uModi);
203 printf(
"IR-Keyboard Regular Key: IR=%x key=%x Modifier=%x val=%x [%s]\r\n", keyCode, uCode, uModi, value, type2str[value]);
210 if ((
keyType == KET_KEYDOWN)) {
211 if (uModi != _KEY_INVALID) {
212 udispatcherScancode_internal(scanCode, uModi, value);
215 udispatcherScancode_internal(scanCode, (
int)uCode, (
int)value);
217 if (uModi != _KEY_INVALID) {
218 udispatcherScancode_internal(scanCode, uModi, value);
227 return UINPUT_init_src (IRMGR_UINPUT_SRC_IRMGR);
230 int UINPUT_init_src(IRMg_UINPUT_Src_t eSrc)
232 #ifndef UINPUT_VERSION
233 #define UINPUT_VERSION (0)
238 fd = open(
"/dev/uinput", O_WRONLY|O_SYNC);
240 printf(
"Linux uinput version [%d] is built-in with kernel\r\n", UINPUT_VERSION);
244 ret = ioctl(fd, UI_SET_EVBIT, EV_KEY);
245 ret = ioctl(fd, UI_SET_EVBIT, EV_SYN);
246 #if defined _SKQ_KEY_MAP_1_
247 ret = ioctl(fd, UI_SET_EVBIT, EV_MSC);
248 ret = ioctl(fd, UI_SET_MSCBIT, MSC_SCAN);
255 for (
size_t i = 0; i <
sizeof(kcodesMap_IARM2Linux)/
sizeof(kcodesMap_IARM2Linux[0]); i++) {
256 ret = ioctl(fd, UI_SET_KEYBIT, kcodesMap_IARM2Linux[i].uCode);
257 if (kcodesMap_IARM2Linux[i].uModi != _KEY_INVALID) {
258 ret = ioctl(fd, UI_SET_KEYBIT, kcodesMap_IARM2Linux[i].uModi);
263 #if defined UINPUT_VERSION && (UINPUT_VERSION >= 5)
266 struct uinput_setup usetup;
267 if (IRMGR_UINPUT_SRC_KEYSIM == eSrc) {
268 #if defined _SKQ_KEY_MAP_1_
269 UINPUT_SETUP_ID_SKY_KEYSIM(usetup);
271 UINPUT_SETUP_ID(usetup);
275 UINPUT_SETUP_ID(usetup);
277 ret = ioctl(fd, UI_DEV_SETUP, &usetup);
283 struct uinput_user_dev uidev;
284 if (IRMGR_UINPUT_SRC_KEYSIM == eSrc) {
285 #if defined _SKQ_KEY_MAP_1_
286 UINPUT_SETUP_ID_SKY_KEYSIM(uidev);
288 UINPUT_SETUP_ID(uidev);
292 UINPUT_SETUP_ID(uidev);
294 ret = write(fd, &uidev,
sizeof(uidev));
295 printf(
"write uinput_user_dev return %d vs %d\r\n", ret,
sizeof(uidev));
296 ret = ((ret ==
sizeof(uidev)) ? 0 : -1);
302 ret = ioctl(fd, UI_DEV_CREATE, 0);
307 perror(
"uinput setup failed\r\n");
309 ret = ioctl(fd, UI_DEV_DESTROY, 0);
318 printf(
"Linux uinput is not built-in with kernel\r\n");
327 if (devFd >= 0)
return udispatcher;
331 #if defined _SKQ_KEY_MAP_1_
332 uinput_dispatcherScancode_t UINPUT_GetDispatcherScancode(
void)
334 if (devFd >= 0)
return udispatcherScancode;
343 ioctl(devFd, UI_DEV_DESTROY);