We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2217

Summary: [Android] Joystick axes handling is broken
Product: SDL Reporter: ny00
Component: joystickAssignee: Gabriel Jacobo <gabomdq>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: gabomdq
Version: HG 2.1   
Hardware: All   
OS: Android (All)   

Description ny00 2013-11-07 14:28:54 UTC
Concerning the recent inclusion of joystick support for Android, it is the case that the testjoystick app does not report joystick motion changes for me (although button events arrive as expected).

Even the addition of the function call SDL_JoystickEventState(SDL_ENABLE), right after SDL_Init, doesn't seem to help.

Looking at the Java file, though, it seems like there is more to improve. In particular, the function onGenericMotion from SDLGenericMotionHandler_API12 currently handles only two possible axes of a controller: MotionEvent.AXIS_X (as axis no. 0) and MotionEvent.AXIS_Y (as 1).

The correct behavior would be to update info for the correct axis.

For a patch idea, one can take a look at one of my old patches (currently marked as obsolete) from Bug report 1700. For instance, the most recent patch (from March 23rd).

Note that there are a few limitations with that patch when it comes to handling analog axes:
- No normalization is done with respect to the info returned by getMotionRange (or getMotionRanges). For instance, I have this old unusual kind of USB controller with 5 analog axes and 1 HAT switch. Although the 5th axis belongs to a usual analog stick, for some reason Android reports it as MotionEvent.AXIS_THROTTLE, with the range of [0.0,1.0]. When the stick is centered, a value close to 0.5 is reported.
- The HAT switch, recognized by analog axes MotionEvent.AXIS_HAT_X and MotionEvent.AXIS_HAT_Y, is naturally translated to two SDL joystick analog axes, rather than a HAT switch.

Of course, if the SDL_GameController API is used then chances are that there's no need to fix these, although it can probably improve consistency across platforms by some level (including SDL_GameController mappings).
Comment 1 ny00 2013-11-11 11:32:24 UTC
Status update: Looks like some recent changes to the code have made the first two axes functional, and testjoystick reports input from all buttons, as well as the same two axes (or AXIS_X and AXIS_Y in the Android language).

It is still the case that the rest of the axes, including the HAT switch, seem to be ignored, though.
Comment 2 Gabriel Jacobo 2013-11-11 13:16:27 UTC
Fixed: https://hg.libsdl.org/SDL/rev/965b7e09c5a8
Comment 3 ny00 2013-11-11 14:20:49 UTC
Excellent! I have tested a recent revision of SDL (possibly not the very last one) that includes this changeset on two Android-powered devices, with two USB controllers (via USB-OTG). For all analog axes, including the HAT switches of the controllers, I see illustrations of the input values as expected. The same applies to the binary buttons, of course. So thanks!

Two notes to add:
- The axis of one of the controller identified as MotionEvent.AXIS_THROTTLE (which as written both is the 5th and is actually a part of an analog stick) has its motion range normalized as expected. If there is actually a controller for which it should not be done, guess the game should let the player handle it in its own way; Or alternatively, if applicable, use the SDL_GameController API.
- With the current state of SDL2 in HG, true controller support should be easier to implement in a cross-platform manner, not just for Windows, OS X and (desktop) Linux, but also for Android. I'm not yet sure about that, but this can be a big step forward!