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 2686

Summary: Haptic coding bugs and fixes for Linux FF: inverted direction and faulty ranged conditional effect parameters
Product: SDL Reporter: Elias Vanderstuyft <Elias.vds>
Component: hapticAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: blocker    
Priority: P2 CC: Elias.vds
Version: HG 2.1   
Hardware: All   
OS: Linux   
Attachments: Derivation of DInput 'polar' direction -> Linux direction conversion table
Don't interpret a direction of polar 35999 (or linux-direction 0xFFFF) as "unsupported type".
Fix the misconception that DInput's POLAR direction doesn't match Linux's direction.
Explicitly avoid floating point arithmetic if it's not needed.
Fix the misconception that Linux' saturation and deadband parameters - on which the corresponding SDL parameters were based - use only half of the possible range
Fix a number of clamping bugs for Windows haptics, by using the Darwin haptics code
Add some missing haptic types to test, and fix wrong array-sizes

Description Elias Vanderstuyft 2014-08-15 14:55:39 UTC
Created attachment 1815 [details]
Derivation of DInput 'polar' direction -> Linux direction conversion table

The initial problem description is written here:
    http://comments.gmane.org/gmane.comp.lib.sdl/65475
And the solution is provided here:
    http://permalink.gmane.org/gmane.comp.lib.sdl/67651
However I was advised to post this on bugzilla.

Here is the summarized version of the initial problem description:

1) One key point, is that Linux directions are defined as the direction of applied force, whereas DInput (and thus also SDL) directions are defined as the direction to counteract the resulting force (=> reversed).
Taking that into account, and some additional differences, we can summarize it and write a conversion table, this is done in the attached "DInputToLinuxDirectionConversion.txt" file.

2) SDL's haptic API follows the Linux FF API in the case of conditional effect parameters,
however the documentation (of both SDL and Linux) is not very clear about the allowed ranges.
I contacted Anssi Hannula to get certainty about these parameters, and he summarized:
- offset:    [-10000, 10000] (dinput)    ->    [-0x7FFF, 0x7FFF] (linux)
- deadband:  [     0, 10000] (dinput)    ->    [      0, 0xFFFF] (linux)
- p/n-coeff: [-10000, 10000] (dinput)    ->    [-0x7FFF, 0x7FFF] (linux)
- p/n-sat:   [     0, 10000] (dinput)    ->    [      0, 0xFFFF] (linux)

I will post the (6) patches as attachments in the next posts,
they also fix some additional things not specifically mentioned in the problem description,
but you'll find them in the patch summaries.
The patches are built on top of each-other.
Here is a summary and more detailed description of all patches:

---

vanilla-rev1.diff: Don't interpret a direction of polar 35999 (or linux-direction 0xFFFF) as "unsupported type".

rev1-rev2.diff: Fix the misconception that DInput's POLAR direction doesn't match Linux's direction.
    Note: this also solves a rounding bug that e.g. caused zero direction to give non-zero output of the X-force value.

rev2-rev3.diff: Explicitly avoid floating point arithmetic if it's not needed.
    This appears to be the common case in games, especially since the existence of multi-axes force-feedback devices is extremely small.

rev3-rev4.diff: Fix the misconception that Linux' saturation and deadband parameters - on which the corresponding SDL parameters were based - use only half of the possible range
    Note: untested on Darwin and Windows

rev4-rev5.diff: Fix a number of clamping bugs for Windows haptics, by using the Darwin haptics code
    Note: untested on Windows

rev5-rev6.diff: Add some missing haptic types to test, and fix wrong array-sizes

---
Comment 1 Elias Vanderstuyft 2014-08-15 14:57:30 UTC
Created attachment 1816 [details]
Don't interpret a direction of polar 35999 (or linux-direction 0xFFFF) as "unsupported type".
Comment 2 Elias Vanderstuyft 2014-08-15 14:58:11 UTC
Created attachment 1817 [details]
Fix the misconception that DInput's POLAR direction doesn't match Linux's direction.

Note: this also solves a rounding bug that e.g. caused zero direction to give non-zero output of the X-force value.
Comment 3 Elias Vanderstuyft 2014-08-15 14:59:28 UTC
Created attachment 1818 [details]
Explicitly avoid floating point arithmetic if it's not needed.

This appears to be the common case in games, especially since the existence of multi-axes force-feedback devices is extremely small.
Comment 4 Elias Vanderstuyft 2014-08-15 15:00:05 UTC
Created attachment 1819 [details]
Fix the misconception that Linux' saturation and deadband parameters - on which the corresponding SDL parameters were based - use only half of the possible range

Note: untested on Darwin and Windows
Comment 5 Elias Vanderstuyft 2014-08-15 15:00:26 UTC
Created attachment 1821 [details]
Fix a number of clamping bugs for Windows haptics, by using the Darwin haptics code

Note: untested on Windows
Comment 6 Elias Vanderstuyft 2014-08-15 15:00:49 UTC
Created attachment 1822 [details]
Add some missing haptic types to test, and fix wrong array-sizes
Comment 7 Ryan C. Gordon 2014-08-16 21:01:01 UTC
These are all in revision control now, thanks!

    https://hg.libsdl.org/SDL/rev/2412ba127e23
    https://hg.libsdl.org/SDL/rev/a57a1d70ae07
    https://hg.libsdl.org/SDL/rev/16b5dda99337
    https://hg.libsdl.org/SDL/rev/8973a237f360
    https://hg.libsdl.org/SDL/rev/8e04e17c7af3
    https://hg.libsdl.org/SDL/rev/0c1a46c018aa

I _really_ appreciate that you stuck with this work so long and kept reminding us to get these fixes in. I'm glad you kept at it.

--ryan.
Comment 8 Elias Vanderstuyft 2014-08-16 21:38:15 UTC
(In reply to Ryan C. Gordon from comment #7)
> These are all in revision control now, thanks!
> 
>     https://hg.libsdl.org/SDL/rev/2412ba127e23
>     https://hg.libsdl.org/SDL/rev/a57a1d70ae07
>     https://hg.libsdl.org/SDL/rev/16b5dda99337
>     https://hg.libsdl.org/SDL/rev/8973a237f360
>     https://hg.libsdl.org/SDL/rev/8e04e17c7af3
>     https://hg.libsdl.org/SDL/rev/0c1a46c018aa
> 
> I _really_ appreciate that you stuck with this work so long and kept
> reminding us to get these fixes in. I'm glad you kept at it.

No problem, it was a pleasure to me! ;)
This project deserves good haptic support.

I still have to commit my equivalent patches for Wine (there are 30+ of them!), I'm afraid getting those in will be much harder, wish me luck (and time) ;-)
Comment 9 Sam Lantinga 2014-08-17 01:54:04 UTC
Good luck! :)
Comment 10 Elias Vanderstuyft 2014-08-17 08:00:16 UTC
(In reply to Sam Lantinga from comment #9)
> Good luck! :)

Thanks!