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)


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.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.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):

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:

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.