joystick: Fix endianness issues in Xbox HIDAPI drivers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
diff --git a/src/joystick/hidapi/SDL_hidapi_xbox360.c b/src/joystick/hidapi/SDL_hidapi_xbox360.c
index 28e04cf..53b8891 100644
--- a/src/joystick/hidapi/SDL_hidapi_xbox360.c
+++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c
@@ -263,16 +263,16 @@ HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXbox360
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
axis = ((int)data[5] * 257) - 32768;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
- axis = *(Sint16*)(&data[6]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[6]));
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
- axis = *(Sint16*)(&data[8]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[8]));
if (invert_y_axes) {
axis = ~axis;
}
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
- axis = *(Sint16*)(&data[10]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[10]));
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
- axis = *(Sint16*)(&data[12]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[12]));
if (invert_y_axes) {
axis = ~axis;
}
diff --git a/src/joystick/hidapi/SDL_hidapi_xbox360w.c b/src/joystick/hidapi/SDL_hidapi_xbox360w.c
index 0c7dfef..3d9895b 100644
--- a/src/joystick/hidapi/SDL_hidapi_xbox360w.c
+++ b/src/joystick/hidapi/SDL_hidapi_xbox360w.c
@@ -229,16 +229,16 @@ HIDAPI_DriverXbox360W_HandleStatePacket(SDL_Joystick *joystick, SDL_hid_device *
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
axis = ((int)data[5] * 257) - 32768;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
- axis = *(Sint16*)(&data[6]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[6]));
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
- axis = *(Sint16*)(&data[8]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[8]));
if (invert_y_axes) {
axis = ~axis;
}
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
- axis = *(Sint16*)(&data[10]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[10]));
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
- axis = *(Sint16*)(&data[12]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[12]));
if (invert_y_axes) {
axis = ~axis;
}
diff --git a/src/joystick/hidapi/SDL_hidapi_xboxone.c b/src/joystick/hidapi/SDL_hidapi_xboxone.c
index 35635b7..3a3eb9c 100644
--- a/src/joystick/hidapi/SDL_hidapi_xboxone.c
+++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c
@@ -605,7 +605,7 @@ HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXboxOne
}
}
- axis = ((int)*(Sint16*)(&data[6]) * 64) - 32768;
+ axis = ((int)SDL_SwapLE16(*(Sint16*)(&data[6])) * 64) - 32768;
if (axis == 32704) {
axis = 32767;
}
@@ -614,7 +614,7 @@ HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXboxOne
}
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
- axis = ((int)*(Sint16*)(&data[8]) * 64) - 32768;
+ axis = ((int)SDL_SwapLE16(*(Sint16*)(&data[8])) * 64) - 32768;
if (axis == -32768 && size == 30 && (data[22] & 0x40) != 0) {
axis = 32767;
}
@@ -623,13 +623,13 @@ HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXboxOne
}
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
- axis = *(Sint16*)(&data[10]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[10]));
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
- axis = *(Sint16*)(&data[12]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[12]));
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, ~axis);
- axis = *(Sint16*)(&data[14]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[14]));
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
- axis = *(Sint16*)(&data[16]);
+ axis = SDL_SwapLE16(*(Sint16*)(&data[16]));
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, ~axis);
SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
@@ -826,25 +826,25 @@ HIDAPI_DriverXboxOneBluetooth_HandleStatePacket(SDL_Joystick *joystick, SDL_Driv
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
}
- axis = ((int)*(Sint16*)(&data[9]) * 64) - 32768;
+ axis = ((int)SDL_SwapLE16(*(Sint16*)(&data[9])) * 64) - 32768;
if (axis == 32704) {
axis = 32767;
}
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
- axis = ((int)*(Sint16*)(&data[11]) * 64) - 32768;
+ axis = ((int)SDL_SwapLE16(*(Sint16*)(&data[11])) * 64) - 32768;
if (axis == 32704) {
axis = 32767;
}
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
- axis = (int)*(Uint16*)(&data[1]) - 0x8000;
+ axis = (int)SDL_SwapLE16(*(Uint16*)(&data[1])) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
- axis = (int)*(Uint16*)(&data[3]) - 0x8000;
+ axis = (int)SDL_SwapLE16(*(Uint16*)(&data[3])) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
- axis = (int)*(Uint16*)(&data[5]) - 0x8000;
+ axis = (int)SDL_SwapLE16(*(Uint16*)(&data[5])) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
- axis = (int)*(Uint16*)(&data[7]) - 0x8000;
+ axis = (int)SDL_SwapLE16(*(Uint16*)(&data[7])) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));