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, some issue with InputDevice checks #2718

Closed
SDLBugzilla opened this issue Feb 11, 2021 · 1 comment
Closed

Android, some issue with InputDevice checks #2718

SDLBugzilla opened this issue Feb 11, 2021 · 1 comment

Comments

@SDLBugzilla
Copy link
Collaborator

This bug report was migrated from our old Bugzilla tracker.

These attachments are available in the static archive:

Reported in version: don't know
Reported for operating system, platform: Android (All), All

Comments on the original bug report:

On 2017-11-13 20:41:50 +0000, Sylvain wrote:

Created attachment 3086
patch

There are some strange bitwise in SDL2 android port.

In android java class InputDevice, there are constants named SOURCE_CLASS_something and SOURCE_something.
It describres input devices that can be connected like touchscreen, mouse, joystick and so on.
See https://developer.android.com/reference/android/view/InputDevice.html

A "SOURCE_something" can belong to a "SOURCE_CLASS_something".
(it can be 0 class, like SOURCE_TOUCH_NAVIGATION or SOURCE_ROTARY_ENCODE who have no class, all others are 1. But we even could consider a source can belong to N class).
Anyway, per CLASS, they are potentially several SOURCE_something.

A CLASS is bitfield with only 1 bit set.
A SOURCE is a 1 bit or'ed with (0/1/N?) CLASS.

Then, you can have some value to match with. Those comes from event.getSource() and device.getSources().
Note the S of getSources(). It provides a value where sources are OR'ed ! So "sources" is a set of several source(s) and several classes.

Here's come the strangeness in SDLActivity and SDLControllerManager:

No bug, but no need to check that much to filter by CLASS:
if ((sources & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK) {
Because it's 1 bit, it's enough to do:
if ((sources & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {

When it's written:
if ((event.getSource() & InputDevice.SOURCE_KEYBOARD) != 0) {
Here you don't check that you have exactly a SOURCE_KEYBOARD as input. In fact, you check that you have a Source that belongs to the classe(s) of the Keyboard (which is CLASS_BUTTON).
For instance, SOURCE_GAMEPAD or SOURCE_DPAD, which also belong to SOURCE_CLASS_BUTTON, would also match the previous condition.

In fact, it's equivalent to:
if ((event.getSource() & InputDevice.SOURCE_CLASS_BUTTON) != 0) {
If you really want to check that you have source_keyboard, you should write:
if (event.getSource() == InputDevice.SOURCE_KEYBOARD) {

Same for SOURCE_MOUSE:
if ((event.getSource() & InputDevice.SOURCE_MOUSE) != 0) {
Is in fact:
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {

Then, there is SDL java inputGetInputDeviceIds() which is called to register touch devices.
It used to be needed to detect joystick and was generic for this purpose.
But now, sources is hardcoded in native code to "InputDevice.SOURCE_TOUCHSCREEN".

Moreover, when you do:
if ((device != null) && ((device.getSources() & sources) != 0)) {
you allow anything of the same class as SOURCE_TOUCHSCREEN (which is SOURCE_CLASS_POINTER).
So you allow : SOURCE_MOUSE, SOURCE_STYLUS.
For instance, an USB Mouse device get added as SDL_Touch device, which is wrong.
(But maybe a SOURCE_STYLUS should be added ?)

Here's a patch that correct previous things. I gave try to see that it was working.
I modified the condition so that it's written the way it's currently running.

Exception for the inputGetInputDeviceIds(), where I only add the SOURCE_TOUCHSCREEN.
(anyway, any nativeOnTouch would add any touch device underneath)

I also made the inputGetInputDeviceIds() simplier (and it adds the device name).

At the beginning of the patch, there is also a debug function you may want to use, but not to commit.

On 2019-01-10 20:44:42 +0000, Sylvain wrote:

In https://hg.libsdl.org/SDL/rev/24bbe1496ef7

Part 4 "add name for Touch devices and simplification" is added,

But not

Moreover, when you do:
if ((device != null) && ((device.getSources() & sources) != 0)) {
you allow anything of the same class as SOURCE_TOUCHSCREEN (which is > SOURCE_CLASS_POINTER).
So you allow : SOURCE_MOUSE, SOURCE_STYLUS.
For instance, an USB Mouse device get added as SDL_Touch device, which is wrong.
(But maybe a SOURCE_STYLUS should be added ?)

On 2019-01-17 12:43:56 +0000, Sylvain wrote:

The part 1 is in:
https://hg.libsdl.org/SDL/rev/8ca43abe9870

@1bsyl
Copy link
Contributor

1bsyl commented Feb 7, 2022

This fix part 4,

1bsyl added a commit that referenced this issue Feb 8, 2022
1bsyl added a commit that referenced this issue Feb 8, 2022
@1bsyl 1bsyl closed this as completed Feb 8, 2022
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