From 78f545c1ae77b98f73c2ef475a28b37dcbc2cd10 Mon Sep 17 00:00:00 2001 From: Merlijn Wajer Date: Mon, 3 Dec 2012 20:16:19 +0100 Subject: [PATCH] Initial working commit. --- KEYS | 21 +++++++++++ map.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 KEYS create mode 100644 map.c diff --git a/KEYS b/KEYS new file mode 100644 index 0000000..f6cb6f9 --- /dev/null +++ b/KEYS @@ -0,0 +1,21 @@ +[Left] + KEY_LEFT, KEY_UP, KEY_RIGHT, KEY_DOWN (type=EV_KEY, value={0,1}) + +Red: + 0: KEY_LEFTCTRL + 1: KEY_LEFTALT + 2: KEY_SPACE + +Yellow: + 3: KEY_1 + +[Right] + KEY_D, KEY_R, KEY_G, KEY_F (type=EV_KEY, value={0,1}) + +Red: + 0: KEY_A + 1: KEY_S + 2: KEY_Q + +Yellow: + 3: KEY_2 diff --git a/map.c b/map.c new file mode 100644 index 0000000..2a4a129 --- /dev/null +++ b/map.c @@ -0,0 +1,110 @@ +#include +#include +#include + +#include +#include + +#include + +#include +#include + +#define INPUT_PATH "/dev/input/by-path/platform-i8042-serio-0-event-kbd" +/*#define INPUT_PATH "/dev/input/by-path/platform-i8042-serio-0-event-kbd"*/ +#define UINPUT_PATH "/dev/uinput" + +/* TODO: + * - Use atexit() to free joysticks + * - Make joysticks static + * + * + */ + +int main(int argc, char** argv) { + int in, uin; + struct input_event e, je; + struct uinput_user_dev uidev; + + in = open(INPUT_PATH, O_RDONLY); + if(in < 0) { + perror("open in"); + return 1; + } + + uin = open(UINPUT_PATH, O_WRONLY | O_NONBLOCK); + if (uin < 0) { + perror("open uin"); + return 2; + } + + /* Create devices */ + if (ioctl(uin, UI_SET_EVBIT, EV_ABS) < 0) { + perror("ioctl EV_ABS"); + return 1; + } + + if (ioctl(uin, UI_SET_EVBIT, EV_KEY) < 0) { + perror("ioctl EV_KEY"); + return 1; + } + + /* Every ``button'' needs to be registered */ + if (ioctl(uin, UI_SET_ABSBIT, ABS_HAT0Y) < 0) { + perror("ioctl ABS_"); + return 1; + } + + memset(&uidev, '\0', sizeof(struct uinput_user_dev)); + snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "key2joy:1"); + + uidev.id.bustype = BUS_USB; + uidev.id.vendor = 0x42; + uidev.id.product = 0xbebe; + uidev.id.version = 1; + + if (write(uin, &uidev, sizeof(uidev)) < 0) { + perror("write"); + return -1; + } + + if (ioctl(uin, UI_DEV_CREATE)) { + perror("ioctl create"); + return 1; + } + + while (1) { + printf("in: %d, uin: %d\n", in, uin); + if (read(in, &e, sizeof(struct input_event))) { + printf("Event: (Type: %d, Code: %d, Value %d)\n", e.type, e.code, e.value); + } + + + if (e.type == EV_KEY && e.code == KEY_UP) { + memset(&je, '\0', sizeof(struct input_event)); + je.type = EV_ABS; + je.code = ABS_HAT0Y; + je.value = -e.value; + if(write(uin, &je, sizeof(struct input_event)) < 0) { + perror("EV_ABS Write event"); + return -1; + } + } + if (e.type == EV_SYN) { + memset(&je, '\0', sizeof(struct input_event)); + printf("SYN event\n"); + + je.type = EV_SYN; + je.code = 0; + je.value = 0; + + if (write(uin, &je, sizeof(struct input_event)) < 0) { + perror("SYN write event"); + return -1; + } + } + printf("---\n"); + } + + +}