diff --git a/input_dev.c b/input_dev.c index c5475b1..42e631b 100644 --- a/input_dev.c +++ b/input_dev.c @@ -19,6 +19,10 @@ static const char *input_path = "/dev/input/"; static const char *iio_path = "/sys/bus/iio/devices/"; +uint32_t input_filter_imu_identity(struct input_event* events, size_t* size, uint32_t* count) { + return INPUT_FILTER_FLAGS_PRESERVE_TIME | INPUT_FILTER_FLAGS_IMU; +} + uint32_t input_filter_identity(struct input_event* events, size_t* size, uint32_t* count) { return INPUT_FILTER_FLAGS_NONE; } @@ -26,6 +30,14 @@ uint32_t input_filter_identity(struct input_event* events, size_t* size, uint32_ uint32_t input_filter_asus_kb(struct input_event* events, size_t* size, uint32_t* count) { static int F15_status = 0; + if (events[0].type == EV_REL) { + return INPUT_FILTER_FLAGS_MOUSE; + } else if (events[0].type == EV_KEY) { + if ((events[0].code == BTN_MIDDLE) || (events[0].code == BTN_LEFT) || (events[0].code == BTN_RIGHT)) { + return INPUT_FILTER_FLAGS_MOUSE; + } + } + if ((*count >= 2) && (events[0].type == EV_MSC) && (events[0].code == MSC_SCAN)) { if ((events[0].value == -13565784) && (events[1].type == EV_KEY) && (events[1].code == KEY_F18)) { if (events[1].value == 1) { diff --git a/input_dev.h b/input_dev.h index bbe456b..199cade 100644 --- a/input_dev.h +++ b/input_dev.h @@ -38,6 +38,8 @@ void *input_dev_thread_func(void *ptr); int open_and_hide_input(); +uint32_t input_filter_imu_identity(struct input_event* events, size_t* size, uint32_t* count); + uint32_t input_filter_identity(struct input_event* events, size_t* size, uint32_t* count); uint32_t input_filter_asus_kb(struct input_event*, size_t*, uint32_t*); \ No newline at end of file diff --git a/main.c b/main.c index 0b37860..acd426d 100644 --- a/main.c +++ b/main.c @@ -4,17 +4,12 @@ #include "output_dev.h" #include "platform.h" -queue_t imu_ev; queue_t gamepad_ev; -static output_dev_t out_imu_dev = { - .fd = -1, - .crtl_flags = 0x00000000U, - .queue = &imu_ev, -}; static output_dev_t out_gamepadd_dev = { - .fd = -1, + .gamepad_fd = -1, + .imu_fd = -1, .crtl_flags = 0x00000000U, .queue = &gamepad_ev, }; @@ -27,8 +22,8 @@ static input_dev_t in_iio_dev = { .dev_type = input_dev_type_iio, .crtl_flags = 0x00000000U, .iio_filters = &in_iio_filters, - .queue = &imu_ev, - .input_filter_fn = input_filter_identity, + .queue = &gamepad_ev, + .input_filter_fn = input_filter_imu_identity, }; static uinput_filters_t in_asus_kb_1_filters = { @@ -80,7 +75,6 @@ static input_dev_t in_xbox_dev = { }; void request_termination() { - out_imu_dev.crtl_flags |= OUTPUT_DEV_CTRL_FLAG_EXIT; out_gamepadd_dev.crtl_flags |= OUTPUT_DEV_CTRL_FLAG_EXIT; in_xbox_dev.crtl_flags |= INPUT_DEV_CTRL_FLAG_EXIT; @@ -101,25 +95,32 @@ int main(int argc, char ** argv) { init_global_mode(); queue_init(&gamepad_ev, 32); - queue_init(&imu_ev, 32); - out_imu_dev.fd = create_output_dev("/dev/uinput", OUTPUT_DEV_NAME /*" - IMU"*/, output_dev_imu); - if (out_imu_dev.fd < 0) { - // TODO: free(imu_dev.events_list); - // TODO: free(gamepadd_dev.events_list); + int imu_fd = create_output_dev("/dev/uinput", output_dev_imu); + if (imu_fd < 0) { fprintf(stderr, "Unable to create IMU virtual device\n"); return EXIT_FAILURE; } - out_gamepadd_dev.fd = create_output_dev("/dev/uinput", OUTPUT_DEV_NAME /*" - GamePad"*/, output_dev_gamepad); - if (out_gamepadd_dev.fd < 0) { - close(out_imu_dev.fd); - // TODO: free(imu_dev.events_list); - // TODO: free(gamepadd_dev.events_list); + int gamepad_fd = create_output_dev("/dev/uinput", output_dev_gamepad); + if (gamepad_fd < 0) { + close(imu_fd); fprintf(stderr, "Unable to create gamepad virtual device\n"); return EXIT_FAILURE; } + int mouse_fd = create_output_dev("/dev/uinput", output_dev_mouse); + if (mouse_fd < 0) { + close(gamepad_fd); + close(imu_fd); + fprintf(stderr, "Unable to create mouse virtual device\n"); + return EXIT_FAILURE; + } + + out_gamepadd_dev.gamepad_fd = gamepad_fd; + out_gamepadd_dev.imu_fd = imu_fd; + out_gamepadd_dev.mouse_fd = mouse_fd; + /* __sighandler_t sigint_hndl = signal(SIGINT, sig_handler); if (sigint_hndl == SIG_ERR) { @@ -130,16 +131,8 @@ int main(int argc, char ** argv) { int ret = 0; - pthread_t imu_thread, gamepad_thread; + pthread_t gamepad_thread; pthread_t xbox_thread, asus_kb_1_thread, asus_kb_2_thread, asus_kb_3_thread, iio_thread; - - const int imu_thread_creation = pthread_create(&imu_thread, NULL, output_dev_thread_func, (void*)(&out_imu_dev)); - if (imu_thread_creation != 0) { - fprintf(stderr, "Error creating IMU output thread: %d\n", imu_thread_creation); - ret = -1; - request_termination(); - goto imu_thread_err; - } const int gamepad_thread_creation = pthread_create(&gamepad_thread, NULL, output_dev_thread_func, (void*)(&out_gamepadd_dev)); if (gamepad_thread_creation != 0) { @@ -207,14 +200,8 @@ xbox_drv_thread_err: pthread_join(gamepad_thread, NULL); gamepad_thread_err: - pthread_join(imu_thread, NULL); - -imu_thread_err: - if (!(out_gamepadd_dev.fd < 0)) - close(out_gamepadd_dev.fd); - - if (!(out_imu_dev.fd < 0)) - close(out_imu_dev.fd); + ioctl(gamepad_fd, UI_DEV_DESTROY); + close(gamepad_fd); // TODO: free(imu_dev.events_list); // TODO: free(gamepadd_dev.events_list); diff --git a/message.h b/message.h index 78844b9..fbfa715 100644 --- a/message.h +++ b/message.h @@ -17,5 +17,7 @@ typedef struct message { #define INPUT_FILTER_FLAGS_NONE 0x00000000U #define INPUT_FILTER_FLAGS_DO_NOT_EMIT 0x00000001U #define INPUT_FILTER_FLAGS_PRESERVE_TIME 0x00000002U +#define INPUT_FILTER_FLAGS_IMU 0x00000004U +#define INPUT_FILTER_FLAGS_MOUSE 0x00000008U typedef uint32_t (*input_filter_t)(struct input_event*, size_t*, uint32_t*); \ No newline at end of file diff --git a/output_dev.c b/output_dev.c index f8945c5..0a0d662 100644 --- a/output_dev.c +++ b/output_dev.c @@ -4,7 +4,7 @@ #include #include -int create_output_dev(const char* uinput_path, const char* name, output_dev_type_t type) { +int create_output_dev(const char* uinput_path, output_dev_type_t type) { int fd = open(uinput_path, O_WRONLY | O_NONBLOCK); if(fd < 0) { fd = -1; @@ -13,11 +13,8 @@ int create_output_dev(const char* uinput_path, const char* name, output_dev_type } struct uinput_setup dev = {0}; - if (strlen(name) < UINPUT_MAX_NAME_SIZE) { - strcpy(dev.name, name); - } else { - strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE-1); - } + strncpy(dev.name, OUTPUT_DEV_NAME, UINPUT_MAX_NAME_SIZE-1); + #if defined(OUTPUT_DEV_BUS_TYPE) dev.id.bustype = OUTPUT_DEV_BUS_TYPE; @@ -39,9 +36,10 @@ int create_output_dev(const char* uinput_path, const char* name, output_dev_type case output_dev_imu: { ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_ACCELEROMETER); ioctl(fd, UI_SET_EVBIT, EV_ABS); - //ioctl(fd, UI_SET_EVBIT, EV_KEY); +#if defined(INCLUDE_TIMESTAMP) ioctl(fd, UI_SET_EVBIT, EV_MSC); ioctl(fd, UI_SET_MSCBIT, MSC_TIMESTAMP); +#endif ioctl(fd, UI_SET_ABSBIT, ABS_X); ioctl(fd, UI_SET_ABSBIT, ABS_Y); @@ -149,11 +147,10 @@ int create_output_dev(const char* uinput_path, const char* name, output_dev_type case output_dev_gamepad: { //ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_BUTTONPAD); ioctl(fd, UI_SET_EVBIT, EV_ABS); - ioctl(fd, UI_SET_EVBIT, EV_REL); ioctl(fd, UI_SET_EVBIT, EV_KEY); - ioctl(fd, UI_SET_EVBIT, EV_MSC); ioctl(fd, UI_SET_EVBIT, EV_SYN); #if defined(INCLUDE_TIMESTAMP) + ioctl(fd, UI_SET_EVBIT, EV_MSC); ioctl(fd, UI_SET_MSCBIT, MSC_TIMESTAMP); #endif @@ -181,6 +178,8 @@ int create_output_dev(const char* uinput_path, const char* name, output_dev_type ioctl(fd, UI_SET_KEYBIT, BTN_MODE); ioctl(fd, UI_SET_KEYBIT, BTN_THUMBL); ioctl(fd, UI_SET_KEYBIT, BTN_THUMBR); + ioctl(fd, UI_SET_KEYBIT, BTN_THUMB); + ioctl(fd, UI_SET_KEYBIT, BTN_THUMB2); ioctl(fd, UI_SET_KEYBIT, BTN_GEAR_DOWN); ioctl(fd, UI_SET_KEYBIT, BTN_GEAR_UP); ioctl(fd, UI_SET_KEYBIT, BTN_DPAD_UP); @@ -188,16 +187,7 @@ int create_output_dev(const char* uinput_path, const char* name, output_dev_type ioctl(fd, UI_SET_KEYBIT, BTN_DPAD_LEFT); ioctl(fd, UI_SET_KEYBIT, BTN_DPAD_RIGHT); - // mouse buttons - ioctl(fd, UI_SET_KEYBIT, BTN_LEFT); - ioctl(fd, UI_SET_KEYBIT, BTN_MIDDLE); - ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT); - ioctl(fd, UI_SET_RELBIT, REL_X); - ioctl(fd, UI_SET_RELBIT, REL_Y); - ioctl(fd, UI_SET_RELBIT, REL_WHEEL); - ioctl(fd, UI_SET_RELBIT, REL_WHEEL_HI_RES); - - ioctl(fd, UI_SET_KEYBIT, KEY_F12); + //ioctl(fd, UI_SET_KEYBIT, KEY_F12); //ioctl(fd, UI_SET_KEYBIT, KEY_F15); //ioctl(fd, UI_SET_KEYBIT, KEY_F16); //ioctl(fd, UI_SET_KEYBIT, KEY_F17); @@ -392,6 +382,41 @@ int create_output_dev(const char* uinput_path, const char* name, output_dev_type break; } + case output_dev_mouse: { + ioctl(fd, UI_SET_EVBIT, EV_REL); + ioctl(fd, UI_SET_EVBIT, EV_KEY); + ioctl(fd, UI_SET_EVBIT, EV_MSC); + ioctl(fd, UI_SET_EVBIT, EV_SYN); +#if defined(INCLUDE_TIMESTAMP) + ioctl(fd, UI_SET_MSCBIT, MSC_TIMESTAMP); +#endif + + ioctl(fd, UI_SET_KEYBIT, BTN_LEFT); + ioctl(fd, UI_SET_KEYBIT, BTN_MIDDLE); + ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT); + //ioctl(fd, UI_SET_KEYBIT, BTN_SIDE); + //ioctl(fd, UI_SET_KEYBIT, BTN_EXTRA); + + ioctl(fd, UI_SET_RELBIT, REL_X); + ioctl(fd, UI_SET_RELBIT, REL_Y); + ioctl(fd, UI_SET_RELBIT, REL_WHEEL); + ioctl(fd, UI_SET_RELBIT, REL_WHEEL_HI_RES); + + if(ioctl(fd, UI_DEV_SETUP, &dev) < 0) { + fd = -1; + close(fd); + goto create_output_dev_err; + } + + if(ioctl(fd, UI_DEV_CREATE) < 0) { + fd = -1; + close(fd); + goto create_output_dev_err; + } + + break; + } + default: // error close(fd); @@ -407,8 +432,6 @@ void *output_dev_thread_func(void *ptr) { output_dev_t *out_dev = (output_dev_t*)ptr; struct timeval now = {0}; - const int fd = out_dev->fd; - #if defined(INCLUDE_TIMESTAMP) gettimeofday(&now, NULL); __time_t secAtInit = now.tv_sec; @@ -421,6 +444,13 @@ void *output_dev_thread_func(void *ptr) { if (pop_res == 0) { message_t *const msg = (message_t*)raw_ev; + int fd = out_dev->gamepad_fd; + if ((msg->flags & INPUT_FILTER_FLAGS_IMU) != 0) { + fd = out_dev->imu_fd; + } else if ((msg->flags & INPUT_FILTER_FLAGS_MOUSE) != 0) { + fd = out_dev->mouse_fd; + } + for (uint32_t i = 0; i < msg->ev_count; ++i) { struct input_event ev = { .code = msg->ev[i].code, @@ -505,8 +535,5 @@ void *output_dev_thread_func(void *ptr) { } } - ioctl(fd, UI_DEV_DESTROY); - close(fd); - return NULL; } diff --git a/output_dev.h b/output_dev.h index 866fd20..55372c2 100644 --- a/output_dev.h +++ b/output_dev.h @@ -52,16 +52,19 @@ typedef enum output_dev_type { output_dev_gamepad, output_dev_imu, + output_dev_mouse, } output_dev_type_t; typedef struct output_dev { - int fd; + int gamepad_fd; + int imu_fd; + int mouse_fd; volatile uint32_t crtl_flags; queue_t *queue; } output_dev_t; -int create_output_dev(const char* uinput_path, const char* name, output_dev_type_t type); +int create_output_dev(const char* uinput_path, output_dev_type_t type); void *output_dev_thread_func(void *ptr); \ No newline at end of file