laamaa / m8c

Cross-platform M8 tracker headless client
Other
380 stars 81 forks source link

add support for SDL_Joystick #76

Closed yatli closed 2 years ago

yatli commented 2 years ago

The code to expose a USB joystick endpoint is a part of the STM32Duino library, I have forked it so it's still possible to hack the descriptors using the USB standard tools... but it's a lot of hassle :) (Or, am I missing your point? I don't know a lot about SDL)

Other programs like RetroArch and Tic80 (to be fair they aren't exactly SDL) accepted the controller fine, so I guess it would make sense to add support here too.

Could be related: https://github.com/laamaa/m8c/issues/51

smootalicious commented 2 years ago

I see what you are saying. Basically the joystick API is a superset of the GameController api. Defining the joystick as a gamecontroller in the gamecontroller DB would make it just work, but I understand that can be daunting. The code seems like it would work without negatively affecting anything. Unsure how it would affect performance.

smootalicious commented 2 years ago

To be more clear - I understand this is more of a pain than just having the generic joystick api code in place, but you can define the joystick for m8c to use by adding an entry in the gamecontroller db. You can use a tool to create the details of the entry - https://generalarcade.com/gamepadtool/

yatli commented 2 years ago

Okay so there is a way to make SDL think a controller is a gamepad not a joyful stick, cool :)

Basically the joystick API is a superset of the GameController api

Yeah, I just realized that too. I used sdl-jstest to make sense of button ids, and in their code they treat everything as joysticks: https://github.com/Grumbel/sdl-jstest/blob/6d29d45a06b1b465fe5ee1779b80e1de8c37fff2/sdl2-jstest.c#L139

Perhaps we could do that too?

smootalicious commented 2 years ago

I made some changes in my fork that incorporates one of your changes - init the game controllers in main - that was a good catch. Also reading the database has to happen before assigning the joystick or it doesn't register as a gamepad. Check out my latest 3 commits in my fork.

smootalicious commented 2 years ago

I've looked into this and done some testing and it seems that the way it works is If the joystick is a gamecontroller (properly defined as a gamecontroller in the SDL db) than everything works fine and that is the new preferred SDL way. The problem is if it isn't defined it can be a bit of a pain to define it but it is possible with a some effort. The alternative is to add a bunch of fallback joystick code like you have done. That complicates the code and the maintenance of the code. It would be up to laamaa to decide if supporting undefined joysticks is something that is worth the extra code.

yatli commented 2 years ago

Thanks for looking into the details, and this is a great summary!

laamaa commented 2 years ago

Thanks for the insights. In my opinion it probably would be a good idea to just fix the order that the game controller db is loaded before game controllers are initialized so custom mappings would be recognized properly (currently needs reconnecting), I'd rather not add many new features currently as I don't have much time for maintaining the project anyway :/

The readme could also probably be improved a bit on how the controller stuff works & how mappings should be made

laamaa commented 2 years ago

game controller db initialization changed in PR #77, probably don't want to add a sdl_joystick fallback for now.