gdx-controllers icon indicating copy to clipboard operation
gdx-controllers copied to clipboard

haptic support on iOS

Open orangepascal opened this issue 3 years ago • 3 comments

Lost controller support in my games on iOS15 (and 14) and traced it back to this code:

        if (Foundation.getMajorSystemVersion() >= 14) try {
            hapticEngine = controller.getHaptics().createEngine(GCHapticsLocality.Default);
            hapticEngine.retain();
        } catch (Throwable t) {
            Gdx.app.error("Controllers", "Failed to create haptics engine", t);
        }

Tried various controllers, and I'm guessing they just don't have haptic support (or don't provide it to iOS). And from the Apple documents it says that getHaptics() might return nil if not available.

So should this get a check first? Because right now the controller isn't created because it throws the error for the haptics engine, even tho the controller will work just fine (it does on iOS<14 when this code is skipped)

(for completeness, error given on ios15: Controllers: Failed to create haptics engine java.lang.NullPointerException at com.badlogic.gdx.controllers.IosController.(IosController.java:83) at com.badlogic.gdx.controllers.IosControllerManager.onControllerConnect(IosControllerManager.java:116) at com.badlogic.gdx.controllers.IosControllerManager$1.invoke(IosControllerManager.java:89) at com.badlogic.gdx.controllers.IosControllerManager$1.invoke(IosControllerManager.java:86) at org.robovm.apple.gamecontroller.GCController$Notifications$1.invoke(GCController.java:49) at org.robovm.apple.gamecontroller.GCController$Notifications$1.invoke(GCController.java:46) at org.robovm.apple.foundation.NSNotificationCenter$$BlockMarshaler0.invoked(Unknown Source) at org.robovm.apple.uikit.UIApplication.main(Native Method) at org.robovm.apple.uikit.UIApplication.main(UIApplication.java:433) at com.orangepixel.residual.IOSLauncher.main(IOSLauncher.java:35) )

tested with a MadCatz mfi controller but also a standard PS4 - both work fine if running on iOS13

orangepascal avatar Sep 09 '22 13:09 orangepascal

Yes, check might be needed - I only have an Xbox controller to test with so did not notice the problem.

MrStahlfelge avatar Sep 09 '22 13:09 MrStahlfelge

Wanted to do it myself, but ran into a bunch of version mismatches trying to build things (gradle,maven,jdk,androidsdk,etc) - and currently in a big game-update flow where things work, so don't want to update or change ANY versions right now :D

hopefully someone with contribution skills can do a quick fix for a nightly? wink wink, please,please

(think it's a matter of checking if controller.getHaptics() returns null before calling createEngine)

orangepascal avatar Sep 10 '22 06:09 orangepascal

Tested the fix on a local version of the library, and it works and makes sure all non-haptic supporting controllers don't break the whole controller support.

if (Foundation.getMajorSystemVersion() >= 14) try { if (controller.getHaptics()!=null) { hapticEngine = controller.getHaptics().createEngine(GCHapticsLocality.Default); hapticEngine.retain(); } } catch (Throwable t) { Gdx.app.error("Controllers", "Failed to create haptics engine", t); }

If someone more familiar with git+libgdx could push a proper code update so I don't mess things up that would be great :) For now I'll use my modified lib so I can push updates to my games.

orangepascal avatar Sep 28 '22 10:09 orangepascal

It would have been enough when you had just opened a PR with the change. I just made the button click for you.

MrStahlfelge avatar Apr 27 '23 07:04 MrStahlfelge