Where/how does SDL access gryoscopes and accelerometers?

I’m trying to implement the same functionality within the Godot game engine, which doesn’t use SDL but adheres to its controller mapping format.

For context, I’m focusing on Dualsense and Linux first because that’s what I have.

If I could just see where the motion data is polled from the device and stored, I could re-create that process within Godot. My search of src/joystick/hidapi/SDL_hidapi_ps5.c has been a struggle so far: I’m not even 100% sure that’s where the polling happens. The structure of SDL (where it starts, where it ends) is beyond me for now, but I know that somewhere is a method that executes the singular task of bringing values from the Dualsense’s motion sensors into working RAM.

For all I know, I’ve passed over exactly what I’m looking for without even realizing it!

Does SDL itself employ a 3rd party DLL? None of my search queries have returned anyone trying to access the gyroscope and/or accelerometer of a Dualsense; only the buttons and joysticks. Directly accessing motion data in a human input device doesn’t seem to be a hot topic.

Windows-specific code would even be of some assistance, at my stage. Translating from one operating system to another isn’t an impossible feat, granted you know what to even translate. Any guidance you can provide is greatly appreciated. Thank you for your time.

SDL have support for DualSense gyroscope.
You can make use of it by processing SDL_CONTROLLERSENSORUPDATE event. See SDL/testgamecontroller.c at 419ae29d90d3934e1a382cd693ac60a1f492e43a · libsdl-org/SDL · GitHub for example.

SDL comes with the hidapi library (see either src/hidapi or GitHub - libusb/hidapi: A Simple library for communicating with USB and Bluetooth HID devices on Linux, Mac and Windows.) for these controllers.

The functions under src/joysitck/hidapi are then basically handling device specific work for the overlaying code in src/joystick.

Here are some places to look at:
The events for sensor updates are generated here:

For the PS5 controller, you could start from here:

hidapi example code

SDL abstracts quite a bit. You could probably get started by seeing what they do for the controllers, and using hidapi directly. This also comes with the additional benefit that hidapi can work on all operating systems.

Good luck!

It’s looking like the more efficient solution will be to make a C++ extension to wrap SDL2 and get the motion control data through that. Godot uses whichever input system is built into the OS, and Xinput has zero support for gyro and movement. Whatever functionality I added wouldn’t be cross-platform.

Thanks, Windows. :stuck_out_tongue:

I’ve read that SDL2 can function in a “controller only”-capacity, so I’ll start looking into that. Ideally, I’d like to make the library’s footprint as small as possible, since it’s going to be an add-on to a full engine.