SDL Changes to support clean reads CR: saml
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
diff --git a/src/joystick/darwin/SDL_sysjoystick.c b/src/joystick/darwin/SDL_sysjoystick.c
index 46f6e8c..abfb1c6 100644
--- a/src/joystick/darwin/SDL_sysjoystick.c
+++ b/src/joystick/darwin/SDL_sysjoystick.c
@@ -105,10 +105,11 @@ FreeDevice(recDevice *removeDevice)
return pDeviceNext;
}
-static SInt32
-GetHIDElementState(recDevice *pDevice, recElement *pElement)
+static SDL_bool
+GetHIDElementState(recDevice *pDevice, recElement *pElement, SInt32 *pValue)
{
SInt32 value = 0;
+ int returnValue = SDL_FALSE;
if (pDevice && pElement) {
IOHIDValueRef valueRef;
@@ -122,25 +123,34 @@ GetHIDElementState(recDevice *pDevice, recElement *pElement)
if (value > pElement->maxReport) {
pElement->maxReport = value;
}
+ *pValue = value;
+
+ returnValue = SDL_TRUE;
}
}
-
- return value;
+ return returnValue;
}
-static SInt32
-GetHIDScaledCalibratedState(recDevice * pDevice, recElement * pElement, SInt32 min, SInt32 max)
+static SDL_bool
+GetHIDScaledCalibratedState(recDevice * pDevice, recElement * pElement, SInt32 min, SInt32 max, SInt32 *pValue)
{
const float deviceScale = max - min;
const float readScale = pElement->maxReport - pElement->minReport;
- const SInt32 value = GetHIDElementState(pDevice, pElement);
- if (readScale == 0) {
- return value; /* no scaling at all */
- }
- return ((value - pElement->minReport) * deviceScale / readScale) + min;
+ int returnValue = SDL_FALSE;
+ if (GetHIDElementState(pDevice, pElement, pValue))
+ {
+ if (readScale == 0) {
+ returnValue = SDL_TRUE; /* no scaling at all */
+ }
+ else
+ {
+ *pValue = ((*pValue - pElement->minReport) * deviceScale / readScale) + min;
+ returnValue = SDL_TRUE;
+ }
+ }
+ return returnValue;
}
-
static void
JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
{
@@ -698,9 +708,14 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
element = device->firstAxis;
i = 0;
+
+ int goodRead = SDL_FALSE;
while (element) {
- value = GetHIDScaledCalibratedState(device, element, -32768, 32767);
- SDL_PrivateJoystickAxis(joystick, i, value);
+ goodRead = GetHIDScaledCalibratedState(device, element, -32768, 32767, &value);
+ if (goodRead) {
+ SDL_PrivateJoystickAxis(joystick, i, value);
+ }
+
element = element->pNext;
++i;
}
@@ -708,63 +723,70 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
element = device->firstButton;
i = 0;
while (element) {
- value = GetHIDElementState(device, element);
- if (value > 1) { /* handle pressure-sensitive buttons */
- value = 1;
+ goodRead = GetHIDElementState(device, element, &value);
+ if (goodRead) {
+ if (value > 1) { /* handle pressure-sensitive buttons */
+ value = 1;
+ }
+ SDL_PrivateJoystickButton(joystick, i, value);
}
- SDL_PrivateJoystickButton(joystick, i, value);
+
element = element->pNext;
++i;
}
element = device->firstHat;
i = 0;
+
while (element) {
Uint8 pos = 0;
range = (element->max - element->min + 1);
- value = GetHIDElementState(device, element) - element->min;
- if (range == 4) { /* 4 position hatswitch - scale up value */
- value *= 2;
- } else if (range != 8) { /* Neither a 4 nor 8 positions - fall back to default position (centered) */
- value = -1;
- }
- switch (value) {
- case 0:
- pos = SDL_HAT_UP;
- break;
- case 1:
- pos = SDL_HAT_RIGHTUP;
- break;
- case 2:
- pos = SDL_HAT_RIGHT;
- break;
- case 3:
- pos = SDL_HAT_RIGHTDOWN;
- break;
- case 4:
- pos = SDL_HAT_DOWN;
- break;
- case 5:
- pos = SDL_HAT_LEFTDOWN;
- break;
- case 6:
- pos = SDL_HAT_LEFT;
- break;
- case 7:
- pos = SDL_HAT_LEFTUP;
- break;
- default:
- /* Every other value is mapped to center. We do that because some
- * joysticks use 8 and some 15 for this value, and apparently
- * there are even more variants out there - so we try to be generous.
- */
- pos = SDL_HAT_CENTERED;
- break;
- }
-
- SDL_PrivateJoystickHat(joystick, i, pos);
+ goodRead = GetHIDElementState(device, element, &value);
+ if (goodRead) {
+ value -= element->min;
+ if (range == 4) { /* 4 position hatswitch - scale up value */
+ value *= 2;
+ } else if (range != 8) { /* Neither a 4 nor 8 positions - fall back to default position (centered) */
+ value = -1;
+ }
+ switch (value) {
+ case 0:
+ pos = SDL_HAT_UP;
+ break;
+ case 1:
+ pos = SDL_HAT_RIGHTUP;
+ break;
+ case 2:
+ pos = SDL_HAT_RIGHT;
+ break;
+ case 3:
+ pos = SDL_HAT_RIGHTDOWN;
+ break;
+ case 4:
+ pos = SDL_HAT_DOWN;
+ break;
+ case 5:
+ pos = SDL_HAT_LEFTDOWN;
+ break;
+ case 6:
+ pos = SDL_HAT_LEFT;
+ break;
+ case 7:
+ pos = SDL_HAT_LEFTUP;
+ break;
+ default:
+ /* Every other value is mapped to center. We do that because some
+ * joysticks use 8 and some 15 for this value, and apparently
+ * there are even more variants out there - so we try to be generous.
+ */
+ pos = SDL_HAT_CENTERED;
+ break;
+ }
+ SDL_PrivateJoystickHat(joystick, i, pos);
+ }
+
element = element->pNext;
++i;
}