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

Android: Fix joypad trigger value range #81322

Merged

Conversation

johnnyw
Copy link
Contributor

@johnnyw johnnyw commented Sep 5, 2023

Input::joy_axis converts trigger values to be between 0.0f to 1.0f by default. This is not needed for Android, as values are already within that range, as per Android documentation: https://developer.android.com/reference/android/view/MotionEvent#AXIS_RTRIGGER

This patch prevents this conversion on Android, which caused L2 and R2 triggers to get stuck pressed. #79263

This patch is compatible with 3.x.

Bugsquad edit:

@johnnyw johnnyw requested review from a team as code owners September 5, 2023 01:05
@@ -39,7 +39,7 @@ void AndroidInputHandler::process_joy_event(AndroidInputHandler::JoypadEvent p_e
Input::get_singleton()->joy_button(p_event.device, (JoyButton)p_event.index, p_event.pressed);
break;
case JOY_EVENT_AXIS:
Input::get_singleton()->joy_axis(p_event.device, (JoyAxis)p_event.index, p_event.value);
Input::get_singleton()->joy_axis(p_event.device, (JoyAxis)p_event.index, p_event.value, false);
Copy link
Member

Choose a reason for hiding this comment

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

Warrants adding a comment to clarify that Android axis events are already normalized.

Is that true for all axes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After reading your comment, I did a bit of extra digging. It turns out, they're also already normalized on iOS. So I dug further and found that there are some Android trigger mappings that use the positive axis, and some that use the full range. I've updated the patch to take the controller mapping range into account before applying the correction. I don't have the hardware to test but I think my previous revision would have fixed some Android controllers (such as mine) and broken some others. Taking the specific device mapping into account sounds like a more robust solution.

@akien-mga akien-mga added bug platform:android topic:input cherrypick:3.x Considered for cherry-picking into a future 3.x release cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release labels Sep 5, 2023
@akien-mga akien-mga added this to the 4.2 milestone Sep 5, 2023
@YuriSizov YuriSizov requested review from a team September 6, 2023 09:16
@@ -1267,6 +1268,7 @@ Input::JoyEvent Input::_get_mapped_axis_event(const JoyDeviceMapping &mapping, J
case TYPE_AXIS:
event.index = (int)binding.output.axis.axis;
event.value = value;
r_range = binding.output.axis.range;
Copy link
Member

Choose a reason for hiding this comment

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

Isn't the switch below on axis.range meant to handle this exact issue already? I'm not deeply familiar with that code but I wonder why this normalisation is handled in two different locations.

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 also not super familiar with what's going on here, but my impression is that the private method is converting to match the gamecontrollerdb.txt entry, and joy_axis is normalizing consistently for 0.0 to 1.0. On Android and iOS we should be able to bypass this entirely for the triggers. I wasn't sure about the private method conversion so I didn't want to mess with it, so I figured using the binding as the source of truth for the trigger-specific normalization was a reliable way to get it working.

Maybe the private method can branch on if it's an L/R trigger inside the switch and do the proper conversion for those specific axes only once? Because it seems like the switch in joy_axis is correcting an error introduced by the switch in _get_mapped_axis_event not being specific enough.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've tested this and it works well and makes some sense, but I'm not sure I entirely get what you mean by this comment. Are you trying to say that you're avoiding updating gamecontrollerdb.txt and that it isn't configured to the right axis range? Or are you trying to say that the range is correct in gamecontrollerdb.txt and that this PR is making sure that Godot converts from those values appropriately (which it wasn't before?)

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 believe that the gamecontrollerdb.txt entry is correct (I recall cross-checking across some other repos), but Godot wasn't correctly converting them specifically in the case of the triggers.

Copy link
Contributor

Choose a reason for hiding this comment

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

In which case then it all looks correct to me. I see that @RandomShaper has already marked it for 4.3 milestone but let's focus on getting this into 4.3/master ASAP (after 4.2 release) as it can be understandably annoying if certain controllers don't work correctly on Android. We could then introduce it as a hotfix if it checks out.

Copy link
Member

Choose a reason for hiding this comment

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

Well, we decided to lie on the (maybe too) safe side. If you have tested it and think the changes are good, let's reschedule for 4.2.

Copy link
Member

Choose a reason for hiding this comment

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

This seems ok for 4.2 if we merge it now so we get at least a couple betas/RCs of testing.

Copy link
Contributor

@Eoin-ONeill-Yokai Eoin-ONeill-Yokai left a comment

Choose a reason for hiding this comment

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

I've tested this and it seems to work without regression on my end while resolving the reported issue.

I'll approve it, but I don't think it should be merged until we can be sure that these changes are absolutely necessary to core. See the conversion in question here which asks whether this is a gamecontrollerdb.txt accuracy issue or a failure for us to interpret it correctly.

Once that comment chain is resolved, it should be clear to merge. The code looks clean otherwise.

@@ -1267,6 +1268,7 @@ Input::JoyEvent Input::_get_mapped_axis_event(const JoyDeviceMapping &mapping, J
case TYPE_AXIS:
event.index = (int)binding.output.axis.axis;
event.value = value;
r_range = binding.output.axis.range;
Copy link
Contributor

Choose a reason for hiding this comment

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

I've tested this and it works well and makes some sense, but I'm not sure I entirely get what you mean by this comment. Are you trying to say that you're avoiding updating gamecontrollerdb.txt and that it isn't configured to the right axis range? Or are you trying to say that the range is correct in gamecontrollerdb.txt and that this PR is making sure that Godot converts from those values appropriately (which it wasn't before?)

@RandomShaper RandomShaper modified the milestones: 4.2, 4.3 Oct 17, 2023
@akien-mga akien-mga modified the milestones: 4.3, 4.2 Oct 20, 2023
Copy link
Member

@akien-mga akien-mga left a comment

Choose a reason for hiding this comment

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

Making Eoin's approval "count".

@akien-mga
Copy link
Member

Looks good! Could you squash the commits? See PR workflow for instructions.

`Input::joy_axis` converts trigger values to be between 0.0f to 1.0f by default. This is not needed for Android, as values are already within that range, as per Android documentation: https://developer.android.com/reference/android/view/MotionEvent#AXIS_RTRIGGER

This patch prevents this conversion on Android, which caused L2 and R2 triggers to get stuck pressed. godotengine#79263
@johnnyw johnnyw force-pushed the android_fix_joypad_trigger_range branch from 28d9e1e to d413a02 Compare October 20, 2023 18:33
@akien-mga akien-mga merged commit e6e9b04 into godotengine:master Oct 20, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks! And congrats for your first merged Godot contribution 🎉

@YuriSizov
Copy link
Contributor

Cherry-picked for 4.1.4.

@YuriSizov YuriSizov removed the cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release label Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cherrypick:3.x Considered for cherry-picking into a future 3.x release platform:android topic:input
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Controller Triggers stay pressed on Android
5 participants