Are you only connecting the controller with USB, or are you using Bluetooth? Could you try using the other to see if that helps? There was a bug where the PS4 DS controller wasn’t being detected using Bluetooth but a USB cable worked. Here it is. Another thing to try would be to see if it’s being registered as a joystick for some odd reason.
After looking at this and your Stack Overflow question again, I’ve got a quick question, what do you mean by:
either the number of joysticks is 0, or everything is recognized.
Do you mean that the controller doesn’t always work when you try to detect it?