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 2229

Summary: [Android] Handle joystick POV hat separately
Product: SDL Reporter: ny00
Component: joystickAssignee: Gabriel Jacobo <gabomdq>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: dbrady, gabomdq
Version: HG 2.1   
Hardware: All   
OS: Android (All)   
Attachments: Patch for handling of POV hat
Updated patch
Fixed patch
Fixed validation

Description ny00 2013-11-12 12:16:49 UTC
Created attachment 1422 [details]
Patch for handling of POV hat

At the moment the POV hat of a controller is identified by two SDL analog axes, as these are simply the axes AXIS_HAT_X and AXIS_HAT_Y on the Android side.

With this patch, the axes are handled separately. Although the SDL_GameController API can take care of that, there may still be some game or other app where the SDL_Joystick API is used directly, and it can be good to know how to read HAT input in order to pick default assignments, at the least.
Comment 1 ny00 2013-11-12 12:19:02 UTC
P.S. This has been tested with two game controllers that do have POV hats (which are simply d-pads). It can be good to test the patch with a controller that does not report any, though. I don't have such a controller ready.
Comment 2 Gabriel Jacobo 2013-11-12 13:11:46 UTC
This looks ok, can we generalize to "n" hats instead of hardcoding to either 0 or 1?
Comment 3 ny00 2013-11-12 13:50:06 UTC
(In reply to Gabriel Jacobo from comment #2)
> This looks ok, can we generalize to "n" hats instead of hardcoding to either
> 0 or 1?

I'm not sure Android supports controllers with more than 1 hat directly, at least according to this:

http://developer.android.com/reference/android/view/MotionEvent.html

The best way to tell would be to try and pair/connect a controller with two of more hats (as reported on a PC) with an Android-powered device, and see what API Demos tells, for instance.

I suspect that in case they're supported at all, the extra hats are to be reported as totally unrelated axes, the same way that the 5th analog axis of an old controller I've gotten here is reported as AXIS_THROTTLE for some reason.
Comment 4 David Brady 2014-01-23 18:52:51 UTC
Created attachment 1535 [details]
Updated patch

I've updated this patch to work with the latest head.  I've also made it slightly more generic, even though Android does only support one hat stick.

It also depends on the not-yet-committed patch from bug 2358 to sort joystick axes.
Comment 5 Gabriel Jacobo 2014-01-24 13:10:29 UTC
A few comments:

Line 969: joystick.hats.size() should probably be joystick.hats.size() / 2, right?

Android_OnHat: validate x,y >=0 && <= 2

There's some minor spacing issues
Comment 6 David Brady 2014-01-24 18:11:29 UTC
Argh, I swear I ran tabs to spaces on every single file.  But good catch.  I'll fix the patch when I can.
Comment 7 David Brady 2014-01-28 19:03:09 UTC
Created attachment 1541 [details]
Fixed patch

Here's another crack at this patch.  I couldn't find any tabs, but I corrected some whitespace stuff that looked a little different from the existing code.
Comment 8 Gabriel Jacobo 2014-01-28 23:16:57 UTC
> int hatX = Math.round(event.getAxisValue( joystick.hats.get(i).getAxis(), actionPointerIndex ) );

Should this value be normalized (like we do with regular axes) to -1...1 using range.getMin() and range.getRange() or is it guaranteed to always be -1, 0, 1?

> if (x >= 0 && x <=2 && y >= 0 && y <= 2) {
> SDL_PrivateJoystickHat(item->joystick, hat_id, position_map[y+1][x+1] );

One of the above is wrong, if x,y can be 0,1,2, position_map should use them directly. If they are -1,0,1, then the validation in the if statement is wrong.
Comment 9 David Brady 2014-01-28 23:28:13 UTC
Created attachment 1543 [details]
Fixed validation

Fixed the validation code.  And yes, the hat stick values are supposed to be normalised already.

http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_HAT_X
Comment 10 Gabriel Jacobo 2014-01-29 13:11:21 UTC
Fixed, thanks! https://hg.libsdl.org/SDL/rev/8372466c003c