Apple MFi gamepad support?

FYI, just added Lime Joystick events. These work alongside Gamepad events, so if something is not registering properly as a gamepad, or just want to see the raw events coming from the device, should be possible to watch now :smile:

lime.ui.Joystick.onConnect (function (joystick) {
    
    joystick.onButtonDown.add (function (button) { trace ("Button Down: " + button); });
    
});

(similarly, onAxisMove, onTrackballMove, onButtonUp, onDisconnect)

Super!

It works like a charm! Iā€™m getting the events for A, B and X buttons which is just perfect. Gonna try it in OpenFL/HaxeFlixel now :wink:

Does this work in the current Lime, or do we need a newer SDL? Using the latest from the ā€œnative-toolkitā€ submodule, but I think its before the MFi game controller commit. Now that things are working, though, should be possible to update. Also, might be able to begin including tvOS builds with Lime by default. Do you think weā€™re there yet?

It works with Lime and the latest SDL from their repo + my tvOS branch, so no, weā€™re not there yet. I need @slime73 to merge my changes to his AppleTV branch and get that whole stuff into the mainstream SDL repo before we can actually distribute something for tvOS.

@singmajesty how do you consume those events with OpenFL?

You can obviously connect to Lime events directly, even from OpenFL. Internally, the new joystick events are ignored for the moment (since the gamepad events are better, in general) and OpenFL GameInput is connected to those.

If you choose to use that API, it looks like this:

import openfl.events.Event;
import openfl.events.GameInputEvent;
import openfl.ui.GameInput;

...

var input = new GameInput ();
input.addEventListener (GameInputEvent.DEVICE_ADDED, function (e) {
    
    trace ("Connected game input: " + e.device);
    
    for (i in 0...e.device.numControls) {
        
        var control = e.device.getControlAt (i);
        control.addEventListener (Event.CHANGE, function (e) trace ("Control " + control.id + ": " + control.value));
        
    }
    
});

Thanks @singmajesty
It looks like something is broken between OpenFL and Lime as I get no events being fired by any of the controls of the Apple TV Remote. But if I hook up Lime directly, it works.

Hereā€™s the code Iā€™m using (yours):

https://github.com/tanis2000/openfl-test-tvos/blob/master/Source/Main.hx

Do you mean, that once OpenFL is hooked up, you donā€™t get any of the Lime events (using lime.ui.Joystick directly), or do you mean that the OpenFL GameInput events do not fire?

Oh, are you sure you are not using -Dlegacy? Flixel (for example) defines it by default ā€“ legacy doesnā€™t have Lime 2 APIs

The second :smile:
GameInput events do not fire

Iā€™m using -Dnext with HaxeFlixel or I canā€™t even target tvOS.
If HaxeFlixel is consuming events just like OpenFL, it might be the same problem as those GameInput events of OpenFL not firing.

Oh, gotcha. Did you get Gamepad events?

OpenFL GameInput is based on Lime Gamepad, so if you get Joystick but no Gamepad, it means that the device is not wired up with device mappings so that SDL recognizes it as a gamepad. Have you gotten Gamepad events firing in Lime yet?

On Lime I get the following:

  • Gamepad events: DPAD_DOWN, DPAD_UP, DPAD_LEFT, DPAD_RIGHT, A, B, X
  • Joystick events: Axis 0, Axis 1, Button 0, Button 1, Button 2

So the input works correctly both as Joystick and as Gamepad.

I found the problem: I didnā€™t set the device as enabled.
e.device.enabled = true; did the trick to get it all working in OpenFL.

Thatā€™s curious, I think thatā€™s supposed to be true by default? Maybe a little bug somewhere?

No idea. I checked HaxeFlixelā€™s code and when a device gets connected it forcibly sets enabled to true. So I guess itā€™s been there for a while :smile:

Now Iā€™m trying to understand why HaxeFlixel is seeing the gamepad and the buttons come through but they do not get to my code. I wonder if I should remap the buttons somewhere in HaxeFlixelā€™s code. Gonna dig more into it.

Oh, found this in the docs:

Enable or disable an input device. Devices are initially disabled by default (enabled set to false). You must explicitly enable a device, by setting enabled to true, before you can get control values from the device.

hah, so thatā€™s how it should work :wink:

Got it all working in HaxeFlixel as well now :wink:

And hereā€™s the final result: https://twitter.com/santinellival/status/651789487673110528

I still miss the correct mapping for MFi controllers that arenā€™t the Apple TV Remote though.

The bad thing is that the Mode demo runs extremely slow. And thatā€™s weird. Iā€™ve got some Unity projects running on the new Apple TV and they have a super high framerate.

Would you mind trying BunnyMark?

Of course! :slight_smile:

Iā€™m installing the new beta of XCodeā€¦ waiting for it to finish.
Iā€™ll run both the old BunnyMark as suggested by Justo Delgado (it uses the same Tilesheet stuff currently used by Flixel) and the latest and check if thereā€™s any difference.

Iā€™ll keep you posted.