AmatCoder / mednaffe

A front-end (GUI) for mednafen emulator
GNU General Public License v3.0
429 stars 35 forks source link

Joystick mapping support for MacOS using SDL2 #175

Open ScrelliCopter opened 11 months ago

ScrelliCopter commented 11 months ago

This PR adds the ability to map joystick inputs on MacOS by adding an SDL2 backend to match Mednafen.

The base implementation uses SDL_Joystick like Mednafen does but I also have a somewhat experimental path that opportunistically uses SDL_GameController when a mapping is available, this is mainly to work around having no real way to identify triggers and as a side benefit mappings can trivially have nice names that adapt to whichever controller type the gamecontroller subsystem identifies. In spite of that the gamecontroller path is relatively complex due to having to parse the mapping manually (all of the real functionality for exploring mappings is all private) and also currently makes a few assumptions such as the polarity of the triggers and devices marked as mapped not having more buttons and/or axes than gamecontroller can express.

A better option with SDL_Joystick may be to store initial axis positions when the user starts mapping and deduce triggers and polarity from there. Currently there is no hook as far as I can tell for a joystick impl to know when the user begins mapping and I didn't look into hacking one in, OTOH SDL_JoystickGetAxisInitialState is unreliable because it just records the state of axes on launch and when the user starts the program accidentally depressing a trigger or twiddling a stick they're going to be in for a nasty surprise.

This has been tested on Mac with a handful of different PS and Xbox controllers and they all seem to be mappable, the code should work on other Unix though it wouldn't be useful on platforms like Linux that already have a provider. I'd appreciate all the testing this can get though especially with more exotic controllers than I have access to.

AmatCoder commented 7 months ago

Wow thanks for this!

I barely have time for this project but I try to review it. Also this could be useful for BSD distributions!