The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Identify Joystick Axis Type
damlock


Joined: 06 Feb 2017
Posts: 1
Dear All,

SDL identifies the axis with an index. Usually index=0 is the X axis and 1 is the Y. The other Axis (Z,RX,RY,RZ and Slider) have not a fixed index and from the documentation of Joystick I've found no function that allows to identify the axis type from the index. For example in one joystick the index 2 is the Z in other joysticks is RX.

I've had a look at the SDL source code (function AddHIDElement) and see that the following switch statement allocates a new axis but it seems to discard the "usage" axis information:

case kIOHIDElementTypeInput_Axis: {
switch (usagePage) { /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
case kHIDPage_GenericDesktop:
switch (usage) {
case kHIDUsage_GD_X:
case kHIDUsage_GD_Y:
case kHIDUsage_GD_Z:
case kHIDUsage_GD_Rx:
case kHIDUsage_GD_Ry:
case kHIDUsage_GD_Rz:
case kHIDUsage_GD_Slider:
case kHIDUsage_GD_Dial:
case kHIDUsage_GD_Wheel:
if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) {
pDevice->axes++;
headElement = &(pDevice->firstAxis);
}
}
break;

Is my analysis correct? If yes, I think adding the "usage" can be a very useful feature to add in future SDL releases, for example including in the event the property axisenum (example: e.jaxis.axisenum) to get the event axis type. This will allow the developer to setup a default joystick configuration that immediately works without doing any manual mapping (for example a flight simulator can immediately work with the basic axis types: X,Y,RZ and Slider.)

Thanks for your time and keep up the wonderful work with SDL!! Smile
bram


Joined: 07 Feb 2017
Posts: 2
Have you see the Game Controller API?
https://wiki.libsdl.org/CategoryGameController

It will give you the usage information you require.
You can test for things like:

SDL_CONTROLLER_AXIS_LEFTX
SDL_CONTROLLER_AXIS_LEFTY
SDL_CONTROLLER_AXIS_RIGHTX
SDL_CONTROLLER_AXIS_RIGHTY
SDL_CONTROLLER_AXIS_TRIGGERLEFT
...etc