From f72e7244ca28027581853dc97c81b1f51b22b5f3 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Mon, 23 Jan 2023 16:38:35 +0800 Subject: [PATCH] Fix joystick GUID I/O Properly parse joystick GUID on initialization. --- src/packages/config/IZ_config.c | 7 ++-- src/packages/game/input/IZ_joystick.c | 47 +++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/packages/config/IZ_config.c b/src/packages/config/IZ_config.c index a4558c1..a7b8d5b 100644 --- a/src/packages/config/IZ_config.c +++ b/src/packages/config/IZ_config.c @@ -209,7 +209,7 @@ void IZ_ConfigLoadGuid(IZ_ConfigItem* item, const char* config_path) { ini_gets(item->section, item->key, serialized_default_value, buffer, 128, config_path); raw_value = deserialize(buffer); } else { - ini_gets(item->section, item->key, "00000000-0000-0000-0000-000000000000", buffer, 128, config_path); + ini_gets(item->section, item->key, "00000000000000000000000000000000", buffer, 128, config_path); raw_value = SDL_GUIDFromString(buffer); } IZ_ConfigEnsureValidGuid(item, raw_value, default_value); @@ -259,8 +259,9 @@ IZ_ConfigSaveItemResult IZ_ConfigSaveGuid(IZ_ConfigItem* item, const char* confi return 0; } - char guid_str[128]; - SDL_GUIDToString(dest, guid_str, 128); + char guid_str[33]; + memset(guid_str, 0, 33); + SDL_GUIDToString(dest, guid_str, 33); if (!ini_puts(item->section, item->key, guid_str, config_path)) { return -1; } diff --git a/src/packages/game/input/IZ_joystick.c b/src/packages/game/input/IZ_joystick.c index c4c0239..c6244fc 100644 --- a/src/packages/game/input/IZ_joystick.c +++ b/src/packages/game/input/IZ_joystick.c @@ -106,12 +106,20 @@ void IZ_JoystickHandleHatEvents(IZ_Action* action, SDL_Event e) { void IZ_JoystickHandleButtonEvents(IZ_JoystickState* state, IZ_Action* action, SDL_Event e) { u8 control_index; - SDL_JoystickGUID joystick_guid = SDL_JoystickGetGUID(SDL_JoystickFromInstanceID(e.jbutton.which)); + bool is_valid_event = false; for (u8 zz = 0; zz < 16; zz += 1) { - printf("%02x", joystick_guid.data[zz]); + is_valid_event |= joystick_guid.data[zz] != 0; + } + if (!is_valid_event) { + return; } - printf("\n"); + +// printf("button event from guid: "); +// for (u8 zz = 0; zz < 16; zz += 1) { +// printf("%02x", joystick_guid.data[zz]); +// } +// printf("\n"); for (control_index = 4; control_index < IZ_CONTROLS; control_index += 1) { u8 normalized_button = e.jbutton.button; @@ -218,8 +226,8 @@ void IZ_JoystickInitializeConfigItems(IZ_ConfigItem config_items[]) { }; config_items[base_index + 2] = (IZ_ConfigItem) { - IZ_CONFIG_TYPE_STRING, - sizeof(u16), + IZ_CONFIG_TYPE_GUID, + sizeof(SDL_GUID), main_section_name, "GUID", NULL, @@ -269,18 +277,43 @@ IZ_ProcedureResult IZ_JoystickInitializeConfig(IZ_JoystickState(* state)[IZ_PLAY 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); + + // TODO query this file from the internet? + SDL_GameControllerAddMappingsFromFile("assets/gamecontrollerdb.txt"); + u8 player_index; + if (IZ_JoystickInitializeConfig(state, config_path, argc, argv) < 0) { return -2; } - SDL_GameControllerAddMappingsFromFile("assets/gamecontrollerdb.txt"); - 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); + if (!(*state)[player_index].device) { + break; + } + + (*state)[player_index].config.device_id = SDL_JoystickInstanceID((*state)[player_index].device); + SDL_GUID joystick_guid = SDL_JoystickGetGUID((*state)[player_index].device); + memcpy(&(*state)[player_index].config.guid, &joystick_guid, sizeof(SDL_GUID)); + //(*state)[player_index].config.guid = joystick_guid; + + printf("initialize event from guid: "); + for (u8 zz = 0; zz < 16; zz += 1) { + printf("%02x", (*state)[player_index].config.guid.data[zz]); + } + printf("\n"); + } + + // Post config (after joystick GUIDs have been queried), this is unique to joysticks since they can be plugged in any + // time. + IZ_ConfigSaveResult post_config_save_result = IZ_ConfigSave(joystick_config_items, config_path); + if (post_config_save_result < 0) { + return -3; } return 0;