|
- #include "IZ_joystick.h"
-
- static IZ_ConfigItem joystick_config_items[IZ_PLAYERS * (IZ_CONTROLS - 4 + 2) + 1];
-
- bool IZ_JoystickIsValidAxisThreshold(u16 value) {
- return (4000 <= value && value <= 12000);
- }
-
- void IZ_JoystickHandleDeviceEvents(IZ_JoystickState* state, SDL_Event e) {
- if (e.type == SDL_JOYDEVICEADDED) {
- if (SDL_NumJoysticks() <= IZ_PLAYERS && !state->device) {
- state->device = SDL_JoystickOpen(e.jdevice.which);
- }
- return;
- }
-
- if (e.type == SDL_JOYDEVICEREMOVED) {
- if (
- state->device
- && SDL_JoystickInstanceID(state->device) == e.jdevice.which
- ) {
- state->device = NULL;
- }
- }
- }
-
- void IZ_JoystickHandleAxisEvents(IZ_JoystickState* state, IZ_Action* action, SDL_Event e) {
- if (e.type == SDL_JOYAXISMOTION) {
- // XInput handling
- u8 control_index;
- for (control_index = 4; control_index < IZ_CONTROLS; control_index += 1) {
- if (e.jaxis.axis == IZ_JOY_AXIS_DIRECTION_LEFT_SHOULDER && state->config.control_mapping[control_index] == 10) {
- const u16 bitflag = (0x1 << control_index);
-
- if (e.jaxis.value > state->config.axis_threshold) {
- *action |= bitflag;
- return;
- }
-
- if (e.jaxis.value <= state->config.axis_threshold) {
- *action &= ~bitflag;
- return;
- }
- }
-
- if (e.jaxis.axis == IZ_JOY_AXIS_DIRECTION_RIGHT_SHOULDER && state->config.control_mapping[control_index] == 11) {
- const u16 bitflag = (0x1 << control_index);
-
- if (e.jaxis.value > state->config.axis_threshold) {
- *action |= bitflag;
- return;
- }
-
- if (e.jaxis.value <= state->config.axis_threshold) {
- *action &= ~bitflag;
- return;
- }
- }
- }
-
- if (
- e.jaxis.axis == IZ_JOY_AXIS_DIRECTION_HORIZONTAL1
- || e.jaxis.axis == IZ_JOY_AXIS_DIRECTION_HORIZONTAL2
- ) {
- *action &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
- *action &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
- if (e.jaxis.value > state->config.axis_threshold) {
- *action |= (0x1 << IZ_ACTION_INDEX_RIGHT);
- return;
- }
- if (e.jaxis.value <= -state->config.axis_threshold) {
- *action |= (0x1 << IZ_ACTION_INDEX_LEFT);
- }
- return;
- }
- if (
- e.jaxis.axis == IZ_JOY_AXIS_DIRECTION_VERTICAL1
- || e.jaxis.axis == IZ_JOY_AXIS_DIRECTION_VERTICAL2
- ) {
- *action &= ~(0x1 << IZ_ACTION_INDEX_UP);
- *action &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
- if (e.jaxis.value > state->config.axis_threshold) {
- *action |= (0x1 << IZ_ACTION_INDEX_DOWN);
- return;
- }
- if (e.jaxis.value <= -state->config.axis_threshold) {
- *action |= (0x1 << IZ_ACTION_INDEX_UP);
- }
- }
- }
- }
-
- void IZ_JoystickHandleHatEvents(IZ_Action* action, SDL_Event e) {
- if (e.type == SDL_JOYHATMOTION) {
- *action &= ~(0x1 << IZ_ACTION_INDEX_UP);
- *action &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
- *action &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
- *action &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
- if (e.jhat.value != 0) {
- *action |= e.jhat.value;
- }
- }
- }
-
- void IZ_JoystickHandleButtonEvents(IZ_JoystickState* state, IZ_Action* action, SDL_Event e) {
- u8 control_index;
-
- for (control_index = 4; control_index < IZ_CONTROLS; control_index += 1) {
- if (e.jbutton.button == state->config.control_mapping[control_index]) {
- const u16 bitflag = (0x1 << control_index);
-
- if (e.type == SDL_JOYBUTTONDOWN) {
- *action |= bitflag;
- return;
- }
-
- if (e.type == SDL_JOYBUTTONUP) {
- *action &= ~bitflag;
- return;
- }
- }
- }
- }
-
- void IZ_JoystickHandleEvents(IZ_JoystickState(* state)[IZ_PLAYERS], IZ_Action(* action)[IZ_PLAYERS], SDL_Event e) {
- u8 player_index;
- for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
- IZ_JoystickHandleDeviceEvents(&(*state)[player_index], e);
- IZ_JoystickHandleAxisEvents(&(*state)[player_index], &(*action)[player_index], e);
- IZ_JoystickHandleHatEvents(&(*action)[player_index], e);
- IZ_JoystickHandleButtonEvents(&(*state)[player_index], &(*action)[player_index], e);
- }
- }
-
- void IZ_JoystickBindStateToConfig(IZ_JoystickState(* state)[IZ_PLAYERS], IZ_ConfigItem config_items[]) {
- u8 player_index;
- u8 control_index;
- for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
- u8 base_index = (player_index * (IZ_CONTROLS - 4 + 2));
- config_items[base_index].dest = &((*state)[player_index].config.device_id);
- config_items[base_index + 1].dest = &((*state)[player_index].config.axis_threshold);
- for (control_index = 4; control_index < IZ_CONTROLS; control_index += 1) {
- config_items[base_index + 2 + (control_index - 4)].dest = &((*state)[player_index].config.control_mapping[control_index]);
- }
- }
- }
-
- IZ_ProcedureResult IZ_JoystickSaveConfig(IZ_JoystickState(* state)[IZ_PLAYERS], const char* config_path) {
- IZ_JoystickBindStateToConfig(state, joystick_config_items);
- return IZ_ConfigSave(joystick_config_items, config_path);
- }
-
- void IZ_JoystickInitializeConfigItems(IZ_ConfigItem config_items[]) {
- u8 player_index;
- u8 control_index;
- char* main_section_name;
- char* control_mapping_section_name;
- for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
- main_section_name = SDL_malloc(sizeof(char) * 64);
- sprintf_s(main_section_name, 64, "Joystick.%d", player_index);
- u8 base_index = (player_index * (IZ_CONTROLS - 4 + 2));
- config_items[base_index] = (IZ_ConfigItem) {
- IZ_CONFIG_TYPE_I32,
- sizeof(i32),
- main_section_name,
- "DeviceID",
- NULL,
- &IZ_JOYSTICK_DEFAULT_STATE[player_index].config.device_id,
- NULL,
- NULL,
- };
-
- config_items[base_index + 1] = (IZ_ConfigItem) {
- IZ_CONFIG_TYPE_U16,
- sizeof(u16),
- main_section_name,
- "AxisThreshold",
- NULL,
- &IZ_JOYSTICK_DEFAULT_STATE[player_index].config.axis_threshold,
- IZ_JoystickIsValidAxisThreshold,
- NULL,
- };
-
- control_mapping_section_name = SDL_malloc(sizeof(char) * 64);
- sprintf_s(control_mapping_section_name, 64, "Joystick.%d.ControlMapping", player_index);
- for (control_index = 4; control_index < IZ_CONTROLS; control_index += 1) {
- config_items[base_index + 2 + (control_index - 4)] = (IZ_ConfigItem) {
- IZ_CONFIG_TYPE_U8,
- sizeof(u8),
- control_mapping_section_name,
- IZ_ACTION_NAMES[control_index],
- NULL,
- &IZ_JOYSTICK_DEFAULT_STATE[player_index].config.control_mapping[control_index],
- NULL,
- NULL,
- };
- }
- }
-
- config_items[IZ_PLAYERS * (IZ_CONTROLS - 4 + 2)] = (IZ_ConfigItem) {
- IZ_CONFIG_TYPE_VOID,
- 0,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- };
- }
-
- IZ_ProcedureResult IZ_JoystickInitializeConfig(IZ_JoystickState(* state)[IZ_PLAYERS], const char* config_path, u8 argc, const char* argv[]) {
- IZ_JoystickInitializeConfigItems(joystick_config_items);
- IZ_JoystickBindStateToConfig(state, joystick_config_items);
- if (IZ_ConfigInitialize(joystick_config_items, config_path, argc, argv) < 0) {
- return -1;
- }
- return 0;
- }
-
- IZ_ProcedureResult IZ_JoystickInitialize(IZ_JoystickState(* state)[IZ_PLAYERS], const char* config_path, u8 argc, const char* argv[]) {
- SDL_memcpy(state, &IZ_JOYSTICK_DEFAULT_STATE, sizeof(IZ_JoystickState) * IZ_PLAYERS);
- if (IZ_JoystickInitializeConfig(state, config_path, argc, argv) < 0) {
- return -2;
- }
-
- u8 player_index;
- u8 joysticks_count = SDL_NumJoysticks();
- for (player_index = 0; player_index < joysticks_count; player_index += 1) {
- if (player_index >= IZ_PLAYERS) {
- break;
- }
- (*state)[player_index].device = SDL_JoystickOpen(state[player_index]->config.device_id);
- }
-
- return 0;
- }
-
- void IZ_JoystickTeardown(IZ_JoystickState(* state)[IZ_PLAYERS]) {
- u8 player_index;
- for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
- if (!(*state)[player_index].device) {
- continue;
- }
- SDL_JoystickClose((*state)[player_index].device);
- }
- }
|