Fixed the mapping from raw joystick values to the expected [SDL_JOYSTICK_AXIS_MIN, SDL_JOYSTICK_AXIS_MAX] range. (thanks Tas!) The original code mapped incorrectly from [min, max] to [-32768, 32512], the upper bound being SDL_JOYSTICK_AXIS_MAX - 255 instead of SDL_JOYSTICK_AXIS_MAX.
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
diff --git a/src/joystick/bsd/SDL_bsdjoystick.c b/src/joystick/bsd/SDL_bsdjoystick.c
index f5f13ee..598505e 100644
--- a/src/joystick/bsd/SDL_bsdjoystick.c
+++ b/src/joystick/bsd/SDL_bsdjoystick.c
@@ -677,9 +677,7 @@ BSD_JoystickUpdate(SDL_Joystick *joy)
xmin--;
xmax++;
}
- v = (Sint32) x;
- v -= (xmax + xmin + 1) / 2;
- v *= 32768 / ((xmax - xmin + 1) / 2);
+ v = (((SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN) * ((Sint32)x - xmin) ) / (xmax - xmin)) + SDL_JOYSTICK_AXIS_MIN;
SDL_PrivateJoystickAxis(joy, 0, v);
}
if (SDL_abs(y - gameport.y) > 8) {
@@ -694,9 +692,7 @@ BSD_JoystickUpdate(SDL_Joystick *joy)
ymin--;
ymax++;
}
- v = (Sint32) y;
- v -= (ymax + ymin + 1) / 2;
- v *= 32768 / ((ymax - ymin + 1) / 2);
+ v = (((SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN) * ((Sint32)y - ymin) ) / (ymax - ymin)) + SDL_JOYSTICK_AXIS_MIN;
SDL_PrivateJoystickAxis(joy, 1, v);
}
SDL_PrivateJoystickButton(joy, 0, gameport.b1);
@@ -731,11 +727,7 @@ BSD_JoystickUpdate(SDL_Joystick *joy)
naxe = joy->hwdata->axis_map[joyaxe];
/* scaleaxe */
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
- v -= (hitem.logical_maximum +
- hitem.logical_minimum + 1) / 2;
- v *= 32768 /
- ((hitem.logical_maximum -
- hitem.logical_minimum + 1) / 2);
+ v = (((SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN) * (v - hitem.logical_minimum) ) / (hitem.logical_maximum - hitem.logical_minimum)) + SDL_JOYSTICK_AXIS_MIN;
SDL_PrivateJoystickAxis(joy, naxe, v);
} else if (usage == HUG_HAT_SWITCH) {
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
@@ -765,7 +757,7 @@ BSD_JoystickUpdate(SDL_Joystick *joy)
}
case HUP_BUTTON:
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
- nbutton = HID_USAGE(hitem.usage) - 1; /* SDL buttons are zero-based */
+ nbutton = HID_USAGE(hitem.usage) - 1; /* SDL buttons are zero-based */
SDL_PrivateJoystickButton(joy, nbutton, v);
break;
default: