Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map unknown gamepads and read raw gamepad packets #5967

Merged
merged 8 commits into from
Aug 9, 2021

Conversation

britzl
Copy link
Contributor

@britzl britzl commented Aug 3, 2021

  • Added mapping of unknown gamepads to a default unknown gamepad
  • Added a new GAMEPAD_RAW trigger to read raw gamepad data (buttons, hats and axis)

Fixes #5895

Also map unknown gamepads to a default gamepad config
@@ -379,6 +379,48 @@ namespace dmGameObject
lua_setfield(L, action_table, "gamepad_name");
}

if (params.m_InputAction->m_HasGamepadPacket)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tempted to put this in a action.gamepad table instead:

action.gamepad.axis
action.gamepad.hats
action.gamepad.buttons

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a good idea. Question is, what else should be put there?
Ofc we'll get some backwards compatibility issues if we move things without adding duplicate data. (e.g. device id)

@@ -183,7 +183,8 @@ enum Gamepad
GAMEPAD_GUIDE = 24;
GAMEPAD_CONNECTED = 25;
GAMEPAD_DISCONNECTED = 26;
MAX_GAMEPAD_COUNT = 27;
GAMEPAD_RAW = 27;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trigger to set in the gamepad section of your input bindings to get raw gamepad data from a connected gamepad. With raw data I mean the axis, hats and buttons as they are read from the OS, without applying any deadzone or other operations on the data.

@@ -26,6 +26,8 @@

namespace dmInput
{
const uint32_t UNKNOWN_GAMEPAD_CONFIG_ID = dmHashString32("UNKNOWN_GAMEPAD_CONFIG_ID");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unidentified connected gamepads will be assigned to this id

@@ -98,6 +100,16 @@ namespace dmInput
}
}

static GamepadConfig* GetGamepadConfigFromDeviceName(HBinding binding, const uint32_t device_hash)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get the gamepad config for the device if it exists. If it doesn't exist the device will be assigned to the unknown gamepad config.

Copy link
Contributor

@JCash JCash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, some small changes needed.

engine/hid/src/dmsdk/hid/hid.h Outdated Show resolved Hide resolved
engine/hid/src/dmsdk/hid/hid.h Outdated Show resolved Hide resolved
engine/input/src/input.cpp Outdated Show resolved Hide resolved
unknownGamepadConfig.m_DeadZone = 0.0;
unknownGamepadConfig.m_DeviceId = UNKNOWN_GAMEPAD_CONFIG_ID;
memset(unknownGamepadConfig.m_Inputs, 0, sizeof(unknownGamepadConfig.m_Inputs));
for (uint32_t j = 0; j < (sizeof(unknownGamepadConfig.m_Inputs) / sizeof(struct GamepadInput)); ++j)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In array.hwe now have DM_ARRAY_SIZE, which is easier to write, and also handles some special cases.

memset(unknownGamepadConfig.m_Inputs, 0, sizeof(unknownGamepadConfig.m_Inputs));
for (uint32_t j = 0; j < (sizeof(unknownGamepadConfig.m_Inputs) / sizeof(struct GamepadInput)); ++j)
{
unknownGamepadConfig.m_Inputs[j].m_Index = (uint16_t)~0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. We want the change I did from the Switch-repo where I fixed the memory overwrite bug by using a constant instead (e.g. INVALID_INDEX).

I haven't merged that back to the public Defold repo, did you?
Technically, we want the fixes from the private repos back to the main repo so that we can keep the bug fixes up-to-date.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged back to Defold now

@@ -649,7 +682,7 @@ namespace dmInput
dmHID::GamepadPacket* packet = &gamepad_binding->m_Packet;

dmHID::GamepadPacket* prev_packet = &gamepad_binding->m_PreviousPacket;
GamepadConfig* config = binding->m_Context->m_GamepadMaps.Get(gamepad_binding->m_DeviceId);
GamepadConfig* config = GetGamepadConfigFromDeviceName(binding, gamepad_binding->m_DeviceId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually not entirely sure why we don't store the pointer to the config in the gamepad binding struct.

Possibly it's because if the memory of the configs would move, i.e. if the table is reallocated (e.g. after some new configs are inserted). But we we read all configs at startup, and then don't touch that table. Strange.

engine/input/src/input.h Outdated Show resolved Hide resolved
@@ -379,6 +379,48 @@ namespace dmGameObject
lua_setfield(L, action_table, "gamepad_name");
}

if (params.m_InputAction->m_HasGamepadPacket)
{
dmHID::GamepadPacket gamepadPacket = params.m_InputAction->m_GamepadPacket;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the performance difference here versus using a GamepadPacker*. On one hand we're copying the 144bytes, and have the data in a local cache, versus using a pointer, and it might thrash the caches.
I think this setup is probably better.

I only mention it since I don't think we normally use this pattern of copying a whole struct on the stack, but I'm thinking it's a good use case for itt.

engine/gameobject/src/dmsdk/gameobject/gameobject.h Outdated Show resolved Hide resolved
@@ -379,6 +379,48 @@ namespace dmGameObject
lua_setfield(L, action_table, "gamepad_name");
}

if (params.m_InputAction->m_HasGamepadPacket)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a good idea. Question is, what else should be put there?
Ofc we'll get some backwards compatibility issues if we move things without adding duplicate data. (e.g. device id)

JCash
JCash previously approved these changes Aug 7, 2021
@britzl britzl merged commit 306de42 into dev Aug 9, 2021
@britzl britzl deleted the Issue-5895-Match-gamepad-name-using-wildcard branch August 9, 2021 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default mapping for not-listed gamepads
2 participants