diff --git a/input.h b/input.h index 4242dd6..f7d80de 100644 --- a/input.h +++ b/input.h @@ -44,6 +44,27 @@ typedef enum { MOUSE_KEY_TOTAL = 18 } mouse_keysym_t; +typedef enum { + CONT_NORTH = 0, + CONT_EAST, + CONT_SOUTH, + CONT_WEST, + CONT_UP, + CONT_RIGHT, + CONT_DOWN, + CONT_LEFT, + CONT_L, + CONT_L2, + CONT_R, + CONT_R2, + CONT_RTHUMB, + CONT_LTHUMB, + CONT_SELECT, + CONT_START, + CONT_MODE, + CONT_TOTAL +} controller_keysym_t; + #define IME_STR_MAX 200 // TODO: check if this limit is sensible #define KEYBOARD_KEY_TOTAL 256 @@ -51,11 +72,18 @@ typedef struct { uint8_t keyboard_state; // mostly used to check if we are using the IME uint8_t keys[KEYBOARD_KEY_TOTAL]; // the state of the full keyboard uint8_t mouse_state; - int32_t mouse_x; + int32_t mouse_x; int32_t mouse_y; - uint8_t mouse_keys[MOUSE_KEY_TOTAL]; + uint8_t mouse_keys[MOUSE_KEY_TOTAL]; uint32_t modifiers; // TODO: Decide on a format for keyboard mod keys uint8_t ime_str[IME_STR_MAX]; // the final string coming from the IME for text input fields as utf8 + uint8_t controller_buttons[CONT_TOTAL]; + float lx_axis; + float ly_axis; + float lz_axis; + float rx_axis; + float ry_axis; + float rz_axis; } input_t; int32_t is_key_down(input_t *i, uint8_t key) diff --git a/wayland.c b/wayland.c index 1bc92ad..11cba0a 100644 --- a/wayland.c +++ b/wayland.c @@ -747,6 +747,63 @@ int main() // TODO: Debug printing if (iev.type == EV_ABS || iev.type == EV_KEY) console_debug_ss(MODULE_CONTROLLER, "Controller event\n\tTime: %ld.%06ld\n\tType: %s\n\tCode: %s\n\tValue: %d\n", iev.input_event_sec, iev.input_event_usec, libevdev_event_type_get_name(iev.type), libevdev_event_code_get_name(iev.type, iev.code), iev.value); + + if (iev.type == EV_ABS) + { + int max_val = libevdev_event_type_get_max(EV_ABS); + float normalized_val = (2 * iev.value / (float)max_val) - 1.0f; + + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "ABS_X")) + user_input.lx_axis = normalized_val; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "ABS_Y")) + user_input.ly_axis = normalized_val; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "ABS_Z")) + user_input.lz_axis = normalized_val; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "ABS_RX")) + user_input.rx_axis = normalized_val; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "ABS_RY")) + user_input.ry_axis = normalized_val; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "ABS_RZ")) + user_input.rz_axis = normalized_val; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "ABS_HAT0X")) + { + user_input.controller_buttons[CONT_LEFT] = iev.value == -1 ? 1 : 0; + user_input.controller_buttons[CONT_RIGHT] = iev.value == 1 ? 1 : 0; + } + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "ABS_HAT0Y")) + { + user_input.controller_buttons[CONT_UP] = iev.value == -1 ? 1 : 0; + user_input.controller_buttons[CONT_DOWN] = iev.value == 1 ? 1 : 0; + } + + } + if (iev.type == EV_KEY) + { + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_TL")) + user_input.controller_buttons[CONT_L] = iev.value; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_TR")) + user_input.controller_buttons[CONT_R] = iev.value; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_SELECT")) + user_input.controller_buttons[CONT_SELECT] = iev.value; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_START")) + user_input.controller_buttons[CONT_START] = iev.value; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_MODE")) + user_input.controller_buttons[CONT_MODE] = iev.value; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_THUMBR")) + user_input.controller_buttons[CONT_RTHUMB] = iev.value; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_THUMBL")) + user_input.controller_buttons[CONT_RTHUMB] = iev.value; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_NORTH")) + user_input.controller_buttons[CONT_NORTH] = iev.value; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_EAST")) + user_input.controller_buttons[CONT_EAST] = iev.value; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_SOUTH")) + user_input.controller_buttons[CONT_SOUTH] = iev.value; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_WEST")) + user_input.controller_buttons[CONT_WEST] = iev.value; + if (!strcmp(libevdev_event_code_get_name(iev.type, iev.code), "BTN_MODE")) + user_input.controller_buttons[CONT_MODE] = iev.value; + } } } while (errcode != LIBEVDEV_READ_STATUS_SYNC && errcode != LIBEVDEV_READ_STATUS_SUCCESS && errcode != -EAGAIN);