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

Controller/Joystick input added twice to SDL joystick_polls and causes assert #942

Open
SRSaunders opened this issue Nov 10, 2024 · 2 comments

Comments

@SRSaunders
Copy link

SRSaunders commented Nov 10, 2024

When testing controller/joytick input using SDL, there is something strange that adds controller button input events twice in sysEvent_t Sys_GetEvent() and causes an assert later inside idUsercmdGenLocal::Joystick():

case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
	static int controllerButtonRemap[][2] =
	{
		{K_JOY1, J_ACTION1},
		{K_JOY2, J_ACTION2},
		{K_JOY3, J_ACTION3},
		{K_JOY4, J_ACTION4},
		{K_JOY9, J_ACTION9},
		{K_JOY11, J_ACTION11},
		{K_JOY10, J_ACTION10},
		{K_JOY7, J_ACTION7},
		{K_JOY8, J_ACTION8},
		{K_JOY5, J_ACTION5},
		{K_JOY6, J_ACTION6},
		{K_JOY_DPAD_UP, J_DPAD_UP},
		{K_JOY_DPAD_DOWN, J_DPAD_DOWN},
		{K_JOY_DPAD_LEFT, J_DPAD_LEFT},
		{K_JOY_DPAD_RIGHT, J_DPAD_RIGHT},
	};
	joystick_polls.Append( joystick_poll_t( controllerButtonRemap[ev.cbutton.button][1], ev.cbutton.state == SDL_PRESSED ? 1 : 0 ) );

	res.evType = SE_KEY;
	res.evValue = controllerButtonRemap[ev.cbutton.button][0];
	res.evValue2 = ev.cbutton.state == SDL_PRESSED ? 1 : 0;

-->	joystick_polls.Append( joystick_poll_t( res.evValue, res.evValue2 ) );
	return res;

The first call to joystick_polls.Append() properly adds a mapped joystick J_* value for the joystick buttons. The second call to joystick_polls.Append() incorrectly adds a mapped key K_* value (>= 256) which is out of bounds for idUsercmdGenLocal::Joystick() and causes an assert in that code. Commenting out the above line results in correct behaviour in my testing.

Why is this second call to joystick_polls.Append() in the code? Is there some reason for this that I am missing, or can we comment out or remove this line?

@RobertBeckebans
Copy link
Owner

I haven't tested the gamepad code for a very long time on Linux. I'll have a look at it next week. IIRC most button mappings were wrong compared to the handling on Windows.

@SRSaunders
Copy link
Author

Using a PS4 controller attached via USB, I have tested using Win10+DS4Windows, Manjaro Linux, and macOS Ventura. With my change above (i.e. remove highlighted second call to joystick_polls.Append() in sysEvent_t Sys_GetEvent()), the mappings and behaviour on Linux and macOS (i.e. SDL2) are almost identical to Windows. Seems to work fine.

I don't have a modern Xbox controller to test with, so I can't confirm for that input device.

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

No branches or pull requests

2 participants