Record controller input

This commit is contained in:
Phireh 2024-12-13 02:44:08 +01:00
commit e84a413c9e
2 changed files with 87 additions and 2 deletions

28
input.h
View file

@ -44,6 +44,27 @@ typedef enum {
MOUSE_KEY_TOTAL = 18 MOUSE_KEY_TOTAL = 18
} mouse_keysym_t; } 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 IME_STR_MAX 200 // TODO: check if this limit is sensible
#define KEYBOARD_KEY_TOTAL 256 #define KEYBOARD_KEY_TOTAL 256
@ -56,6 +77,13 @@ typedef struct {
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 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 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; } input_t;
int32_t is_key_down(input_t *i, uint8_t key) int32_t is_key_down(input_t *i, uint8_t key)

View file

@ -747,6 +747,63 @@ int main()
// TODO: Debug printing // TODO: Debug printing
if (iev.type == EV_ABS || iev.type == EV_KEY) 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); 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); } while (errcode != LIBEVDEV_READ_STATUS_SYNC && errcode != LIBEVDEV_READ_STATUS_SUCCESS && errcode != -EAGAIN);