HaxeFlixel / flixel

Free, cross-platform 2D game engine powered by Haxe and OpenFL
https://haxeflixel.com/
MIT License
1.99k stars 439 forks source link

Xbox One Controller not working on MacOS cpp builds #3197

Open increpare opened 4 months ago

increpare commented 4 months ago

Version/Build: information (checked for all listed platforms):

Brief description:

Controller support isn't working for me for cpp flixel builds on MacOS (ok in browser). Works on windows fine.

Long description:

I have made five test programs as part of trying to verify this bug: FLIX - FlixelControllerTest ( Uses FlxGamepad. Opens the Flixel debugger at launch and logs when the left stick is moved or the a button is pressed ). Bildschirmfoto 2024-07-03 um 14 33 08

LIME - LimeControllerTest ( Uses lime.ui.Gamepad. This traces all controller events to the console ). Bildschirmfoto 2024-07-03 um 14 33 43

SDL - SDL_Test (C example with SDL2. Visualises input in a window, and outputs to console. Should be further along the dependency chain still I think. It only runs for me from within xcode 🤷 ). Bildschirmfoto 2024-07-03 um 14 34 10

JS - JsWebExample (basic html/js Controller-API example, displays all inputs in browser window ). Bildschirmfoto 2024-07-03 um 14 34 37

DC - Dead Cells (I just wanted a Haxe example in a different engine, in this case Dead Cells, that is made with heaps.io - I briefly tried but wasn't able to get https://heaps.io/samples/pad.html compiling on my computer).

Source Code:

The source code for FLIX/LIME/SDL/JS examples is in this repo:

https://github.com/increpare/ControllerBugTestSuite

For reference, because it pertains the most, here's the core of the Flixel source code example:

import flixel.FlxG;
import flixel.FlxState;
import flixel.input.gamepad.FlxGamepad;

class PlayState extends FlxState
{
    public function onDeviceConnected(gp:FlxGamepad){
        FlxG.log.add("A gamepad has been connected!");
    }

    override public function create():Void 
    {
        super.create();
        FlxG.debugger.visible=true;
        FlxG.gamepads.deviceConnected.add(onDeviceConnected);
    }

    override public function update(elapsed:Float):Void 
    {
        super.update(elapsed);

        // Important: can be null if there's no active gamepad yet!
        var gamepad:FlxGamepad = FlxG.gamepads.lastActive;
        if (gamepad != null)
        {
            updateGamepadInput(gamepad);
        }
    }

    function updateGamepadInput(gamepad:FlxGamepad):Void
    {
        //trace currently pressed buttons
        if (gamepad.pressed.A)
        {
            FlxG.log.add("The A button has been pressed.");
        }

        if (gamepad.analog.justMoved.LEFT_STICK_X)
        {
            FlxG.log.add("The x axis of the left analog stick of the controller has been moved.");
        }
    }
}

Testing

I test them on the following platforms:

**MB1**: My MacBook 
**MB2**: My friend's MacBook
**WIN**: My windows machine

I test them with the following builds:

**SO**: standalone
**BO**: browser

Setup:

Results

Firstly, the results on windows, where everything should work (Y=controller input works, N = controller input doesn't work):

WIN-SO-FLIX:    Y
WIN-SO-LIME:    Y
WIN-SO-DC:  Y

WIN-BR-FLIX:    Y
WIN-BR-LIME:    Y
WIN-BR-JS:  Y

Then on my MacBook

MB1-SO-FLIX:    N
MB1-SO-LIME:    N
MB1-SO-SDL: Y
MB1-SO-DC:  Y

MB1-BR-FLIX:    Y
MB1-BR-LIME:    Y
MB1-BR-JS:  Y

Then on my friend's MacBook (Macs are so weird with permissions it was worth double-checking on another computer for differences)

MB2-SO-FLIX:    N

MB2-BR-FLIX:    Y
MB2-BR-LIME:    Y
MB2-BR-JS:  Y
Geokureli commented 4 months ago

Are all of your tests done via bluetooth or did you also try physically connecting them? I wanna say in the past I've had trouble using controllers on my mac with BT even outside of flixel testing and also on my browser, I'll give the suite a try. I don't have an xbox controller but I have PS4 and Switch Pro controllers.

This is likely something that will need to be fixed in lime, but its never a bad idea to start here

Edit: I'm not familiar with running SDL projects, should I be using Xcode directly for that?

increpare commented 4 months ago

Are all of your tests done via bluetooth or did you also try physically connecting them?

@Geokureli Hi! I was not able to get either Xbox or Xbox Elite controller working at all in wired usb mode (I even installed the open source drives that I used to use https://github.com/360Controller/360Controller, but it didn't make a difference). All the (slightly older versions of the tests I used yesterday) failed on MacOS. On Windows in my main project I haven't had any problems with wired Xbox controllers (nor with my wired 8BitDo controller).

I just tried the 8BitDo controller in wireless mode and it looks to work wirelessly.

It is detected by MacOS as a gamepad:

Bildschirmfoto 2024-07-03 um 16 42 29

And here are the test results:

MB1-SO-FLIX-8BITDO: Y
MB1-SO-LIME-8BITDO: Y
MB1-SO-SDL-8BITDO:  Y
MB1-SO-DC-8BITDO:   Y

MB1-BR-FLIX-8BITDO: Y
MB1-BR-LIME-8BITDO: Y
MB1-BR-JS-8BITDO:   Y

(note, I accidentally ran the SO tests with Steam running in the background, which technically breaks my protocol, but my controller just ran out of battery...)

This is likely something that will need to be fixed in lime, but its never a bad idea to start here

Should I just duplicate the bug report to lime? I could remove the flixel-specific examples to save them some effort.

increpare commented 4 months ago

Edit: I'm not familiar with running SDL projects, should I be using Xcode directly for that?

I used Xcode (which I'm also not so familiar with anymore) - I had followed the http://lazyfoo.net tutorial - it should be a case of opening up the linked .xcodeproj file and hitting build.

Bildschirmfoto 2024-07-03 um 17 14 00

However, maybe I've messed things up slightly and Xcode has some global paths set or the like. To help verify that things are okay, the opened project should have this structure in XCode (I guess the products folder only appears when you actually do a build).

Bildschirmfoto 2024-07-03 um 17 14 38

I'm very happy to try prepare a different SDL build if you can specific a different process/set of build tools you'd prefer!

Geokureli commented 4 months ago

it should be a case of opening up the linked .xcodeproj file and hitting build.

I'll try that, thanks

I just tried my PS4 with the lime and flixel projects and it seems to work, wirelessly.

Screenshot 2024-07-03 at 10 12 28 AM Screenshot 2024-07-03 at 10 22 10 AM

One of your images shows a PS4 controller was connected, but you never mention whether that was tested with these projects, so I assume all your controllers had the same results for you. I still plan to try my switch cons and I can probably borrow an Xbox controller. I'll also try xcode after it finishes updating

increpare commented 4 months ago

Thanks for having a look!

Hmm, given your test results, and my test results, I wonder if it's to do with controller mappings (SDL_gamecontrollerdb.h).

I couldn't find lime 8.1.2 on GitHub, but lime 8.2.0-dev relies on SDL 2.0.12 according to this page, released on 11.03.2020.

I see then this issue, Added mapping for Xbox One S controller with newer firmware on macOS - submitted to the SDL 2.0.22 release branch (there are of course more recent bugs relating to xbox controllers and MacOS).

I think SDL loads controller mappings of its own, but lime loads some here: https://github.com/openfl/lime/blob/b99eff85c1a559a010d425be789b266b085b7d9c/src/lime/ui/Window.hx#L231

In the lime test, I tried calling this at the start of Main::init() to load the most recent mappings:

var mappings=[
    //https://github.com/libsdl-org/SDL/blob/9d47daef0a674b06f97be383f67b6e7bf406f41b/src/joystick/SDL_gamepad_db.h#L463
    "030000005e040000200b000011050000,Xbox Wireless Controller,platform:Mac OS X,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b3,y:b4,",
    "030000005e040000e002000000000000,Xbox Wireless Controller,platform:Mac OS X,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b8,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b9,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,",
    "030000005e040000e002000003090000,Xbox Wireless Controller,platform:Mac OS X,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b8,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b9,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,",
    "030000005e040000ea02000000000000,Xbox Wireless Controller,platform:Mac OS X,a:b0,b:b1,back:b9,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b10,leftshoulder:b4,leftstick:b6,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b7,righttrigger:a5,rightx:a3,righty:a4,start:b8,x:b2,y:b3,",
    "030000005e040000fd02000003090000,Xbox Wireless Controller,platform:Mac OS X,a:b0,b:b1,back:b16,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b15,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b3,y:b4,",          
];
Gamepad.addMappings(mappings);

But it doesn't make a difference. (I also tried moving it to before Main::new's call to super, in case you have to call it very early, but that made no difference).

I also tried with lines 1072-1096 from the SDL_GameControllerDB file, but it didn't work either.

====

One of your images shows a PS4 controller was connected, but you never mention whether that was tested with these projects, so I assume all your controllers had the same results for you.

They had very diverse and somewhat confusing results in early and more informal tests (though the Xbox Elite had the same behaviour as the regular Xbox one controller). For the bug report I wanted to focus on one controller and be really certain I wasn't talking nonsense.

But now I have a protocol established I can try them again. Indeed, see the 8BitDo results in a comment above - it works fine wirelessly on Mac. The PS controller is a friend's. I'll try at the next possibility, and report back (probably within the next week :) ).

increpare commented 4 months ago

Checking out a PS5 controller in bluetooth-mode:

MB1-SO-FLIX-PS5-BT: N
MB1-SO-LIME-PS5-BT: N
MB1-SO-SDL-PS5-BT:  Y
MB1-SO-DC-PS5-BT:   Y

MB1-BR-FLIX-PS5-BT: Y
MB1-BR-LIME-PS5-BT: Y
MB1-BR-JS-PS5-BT:   Y

(Doubting my sanity, I double checked and the 8BitDo bluetooth controller I tried before still works in the bluetooth test cases where the PS5 controller fails... ).

Checking it out when it's plugged in via a cable:

MB1-SO-FLIX-PS5-USB:    N
MB1-SO-LIME-PS5-USB:    N
MB1-SO-SDL-PS5-USB: Y
MB1-SO-DC-PS5-USB:  Y

MB1-BR-FLIX-PS5-USB:    Y
MB1-BR-LIME-PS5-USB:    Y
MB1-BR-JS-PS5-USB:  Y

@Geokureli You're on MacOS Somona (V14) right? x86 or ARM? I don't think I've done too much violence to my operating system that I'd expect to have different results for the same hardware. 😅

Geokureli commented 4 months ago

You're on MacOS Somona (V14) right? x86 or ARM?

Ventura 13.6.7 x86 (intel is x86, right?)

I'm away from home for the next 2 weeks and i wasn't able to borrow a friend's xbox controller, plus it was a 360 controller, anyway. I may just buy one and return it, if the policy allows it

increpare commented 4 months ago

Ventura 13.6.7 x86 (intel is x86, right?) Yip. Oh dear, who knows what difference might be producing such different results ^^

(I'm more than happy to sponsor the purchase of an xbox controller if you like - just email me the amount at analytic@gmail.com :) )