0.2.0-dev Download ZIP
1141 lines ·35.5 KB Raw
| 1 | /* |
| 2 | Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org> |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any damages |
| 7 | arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any purpose, |
| 10 | including commercial applications, and to alter it and redistribute it |
| 11 | freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must not |
| 14 | claim that you wrote the original software. If you use this software |
| 15 | in a product, an acknowledgment in the product documentation would be |
| 16 | appreciated but is not required. |
| 17 | 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | misrepresented as being the original software. |
| 19 | 3. This notice may not be removed or altered from any source distribution. |
| 20 | */ |
| 21 | #include "SDL_internal.h" |
| 22 | |
| 23 | #ifdef SDL_JOYSTICK_IOKIT |
| 24 | |
| 25 | #include "../SDL_sysjoystick.h" |
| 26 | #include "../SDL_joystick_c.h" |
| 27 | #include "SDL_iokitjoystick_c.h" |
| 28 | #include "../hidapi/SDL_hidapijoystick_c.h" |
| 29 | #include "../../haptic/darwin/SDL_syshaptic_c.h" // For haptic hot plugging |
| 30 | #include "../usb_ids.h" |
| 31 | #include "../../SDL_hints_c.h" |
| 32 | #include <IOKit/IOKitLib.h> |
| 33 | |
| 34 | #define SDL_JOYSTICK_RUNLOOP_MODE CFSTR("SDLJoystick") |
| 35 | |
| 36 | #define CONVERT_MAGNITUDE(x) (((x)*10000) / 0x7FFF) |
| 37 | |
| 38 | // The base object of the HID Manager API |
| 39 | static IOHIDManagerRef hidman = NULL; |
| 40 | |
| 41 | // Linked list of all available devices |
| 42 | static recDevice *gpDeviceList = NULL; |
| 43 | |
| 44 | void FreeRumbleEffectData(FFEFFECT *effect) |
| 45 | { |
| 46 | if (!effect) { |
| 47 | return; |
| 48 | } |
| 49 | SDL_free(effect->rgdwAxes); |
| 50 | SDL_free(effect->rglDirection); |
| 51 | SDL_free(effect->lpvTypeSpecificParams); |
| 52 | SDL_free(effect); |
| 53 | } |
| 54 | |
| 55 | FFEFFECT *CreateRumbleEffectData(Sint16 magnitude) |
| 56 | { |
| 57 | FFEFFECT *effect; |
| 58 | FFPERIODIC *periodic; |
| 59 | |
| 60 | // Create the effect |
| 61 | effect = (FFEFFECT *)SDL_calloc(1, sizeof(*effect)); |
| 62 | if (!effect) { |
| 63 | return NULL; |
| 64 | } |
| 65 | effect->dwSize = sizeof(*effect); |
| 66 | effect->dwGain = 10000; |
| 67 | effect->dwFlags = FFEFF_OBJECTOFFSETS; |
| 68 | effect->dwDuration = SDL_MAX_RUMBLE_DURATION_MS * 1000; // In microseconds. |
| 69 | effect->dwTriggerButton = FFEB_NOTRIGGER; |
| 70 | |
| 71 | effect->cAxes = 2; |
| 72 | effect->rgdwAxes = (DWORD *)SDL_calloc(effect->cAxes, sizeof(DWORD)); |
| 73 | if (!effect->rgdwAxes) { |
| 74 | FreeRumbleEffectData(effect); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | effect->rglDirection = (LONG *)SDL_calloc(effect->cAxes, sizeof(LONG)); |
| 79 | if (!effect->rglDirection) { |
| 80 | FreeRumbleEffectData(effect); |
| 81 | return NULL; |
| 82 | } |
| 83 | effect->dwFlags |= FFEFF_CARTESIAN; |
| 84 | |
| 85 | periodic = (FFPERIODIC *)SDL_calloc(1, sizeof(*periodic)); |
| 86 | if (!periodic) { |
| 87 | FreeRumbleEffectData(effect); |
| 88 | return NULL; |
| 89 | } |
| 90 | periodic->dwMagnitude = CONVERT_MAGNITUDE(magnitude); |
| 91 | periodic->dwPeriod = 1000000; |
| 92 | |
| 93 | effect->cbTypeSpecificParams = sizeof(*periodic); |
| 94 | effect->lpvTypeSpecificParams = periodic; |
| 95 | |
| 96 | return effect; |
| 97 | } |
| 98 | |
| 99 | static recDevice *GetDeviceForIndex(int device_index) |
| 100 | { |
| 101 | recDevice *device = gpDeviceList; |
| 102 | while (device) { |
| 103 | if (!device->removed) { |
| 104 | if (device_index == 0) { |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | --device_index; |
| 109 | } |
| 110 | device = device->pNext; |
| 111 | } |
| 112 | return device; |
| 113 | } |
| 114 | |
| 115 | static void FreeElementList(recElement *pElement) |
| 116 | { |
| 117 | while (pElement) { |
| 118 | recElement *pElementNext = pElement->pNext; |
| 119 | SDL_free(pElement); |
| 120 | pElement = pElementNext; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static recDevice *FreeDevice(recDevice *removeDevice) |
| 125 | { |
| 126 | recDevice *pDeviceNext = NULL; |
| 127 | if (removeDevice) { |
| 128 | if (removeDevice->deviceRef) { |
| 129 | if (removeDevice->runLoopAttached) { |
| 130 | /* Calling IOHIDDeviceUnscheduleFromRunLoop without a prior, |
| 131 | * paired call to IOHIDDeviceScheduleWithRunLoop can lead |
| 132 | * to crashes in MacOS 10.14.x and earlier. This doesn't |
| 133 | * appear to be a problem in MacOS 10.15.x, but we'll |
| 134 | * do it anyways. (Part-of fix for Bug 5034) |
| 135 | */ |
| 136 | IOHIDDeviceUnscheduleFromRunLoop(removeDevice->deviceRef, CFRunLoopGetCurrent(), SDL_JOYSTICK_RUNLOOP_MODE); |
| 137 | } |
| 138 | CFRelease(removeDevice->deviceRef); |
| 139 | removeDevice->deviceRef = NULL; |
| 140 | } |
| 141 | |
| 142 | /* clear out any reference to removeDevice from an associated, |
| 143 | * live instance of SDL_Joystick (Part-of fix for Bug 5034) |
| 144 | */ |
| 145 | SDL_LockJoysticks(); |
| 146 | if (removeDevice->joystick) { |
| 147 | removeDevice->joystick->hwdata = NULL; |
| 148 | } |
| 149 | SDL_UnlockJoysticks(); |
| 150 | |
| 151 | // save next device prior to disposing of this device |
| 152 | pDeviceNext = removeDevice->pNext; |
| 153 | |
| 154 | if (gpDeviceList == removeDevice) { |
| 155 | gpDeviceList = pDeviceNext; |
| 156 | } else if (gpDeviceList) { |
| 157 | recDevice *device; |
| 158 | |
| 159 | for (device = gpDeviceList; device; device = device->pNext) { |
| 160 | if (device->pNext == removeDevice) { |
| 161 | device->pNext = pDeviceNext; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | removeDevice->pNext = NULL; |
| 167 | |
| 168 | // free element lists |
| 169 | FreeElementList(removeDevice->firstAxis); |
| 170 | FreeElementList(removeDevice->firstButton); |
| 171 | FreeElementList(removeDevice->firstHat); |
| 172 | |
| 173 | SDL_free(removeDevice); |
| 174 | } |
| 175 | return pDeviceNext; |
| 176 | } |
| 177 | |
| 178 | static bool GetHIDElementState(recDevice *pDevice, recElement *pElement, SInt32 *pValue) |
| 179 | { |
| 180 | SInt32 value = 0; |
| 181 | bool result = false; |
| 182 | |
| 183 | if (pDevice && pDevice->deviceRef && pElement) { |
| 184 | IOHIDValueRef valueRef; |
| 185 | if (IOHIDDeviceGetValue(pDevice->deviceRef, pElement->elementRef, &valueRef) == kIOReturnSuccess) { |
| 186 | value = (SInt32)IOHIDValueGetIntegerValue(valueRef); |
| 187 | |
| 188 | // record min and max for auto calibration |
| 189 | if (value < pElement->minReport) { |
| 190 | pElement->minReport = value; |
| 191 | } |
| 192 | if (value > pElement->maxReport) { |
| 193 | pElement->maxReport = value; |
| 194 | } |
| 195 | *pValue = value; |
| 196 | |
| 197 | result = true; |
| 198 | } |
| 199 | } |
| 200 | return result; |
| 201 | } |
| 202 | |
| 203 | static bool GetHIDScaledCalibratedState(recDevice *pDevice, recElement *pElement, SInt32 min, SInt32 max, SInt32 *pValue) |
| 204 | { |
| 205 | const float deviceScale = max - min; |
| 206 | const float readScale = pElement->maxReport - pElement->minReport; |
| 207 | bool result = false; |
| 208 | if (GetHIDElementState(pDevice, pElement, pValue)) { |
| 209 | if (readScale == 0) { |
| 210 | result = true; // no scaling at all |
| 211 | } else { |
| 212 | *pValue = (Sint32)(((*pValue - pElement->minReport) * deviceScale / readScale) + min); |
| 213 | result = true; |
| 214 | } |
| 215 | } |
| 216 | return result; |
| 217 | } |
| 218 | |
| 219 | static bool GetHIDScaledCalibratedState_NACON_Revolution_X_Unlimited(recDevice *pDevice, recElement *pElement, SInt32 min, SInt32 max, SInt32 *pValue) |
| 220 | { |
| 221 | if (pElement->minReport == 0 && pElement->maxReport == 255) { |
| 222 | return GetHIDScaledCalibratedState(pDevice, pElement, min, max, pValue); |
| 223 | } |
| 224 | |
| 225 | // This device thumbstick axes have an unusual axis range that |
| 226 | // doesn't work with GetHIDScaledCalibratedState() above. |
| 227 | // |
| 228 | // See https://github.com/libsdl-org/SDL/issues/13143 for details |
| 229 | if (GetHIDElementState(pDevice, pElement, pValue)) { |
| 230 | if (*pValue >= 0) { |
| 231 | // Negative axis values range from 32767 (at rest) to 0 (minimum) |
| 232 | *pValue = -32767 + *pValue; |
| 233 | } else if (*pValue < 0) { |
| 234 | // Positive axis values range from -32768 (at rest) to 0 (maximum) |
| 235 | *pValue = 32768 + *pValue; |
| 236 | } |
| 237 | return true; |
| 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | static void JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender) |
| 243 | { |
| 244 | recDevice *device = (recDevice *)ctx; |
| 245 | device->removed = true; |
| 246 | if (device->deviceRef) { |
| 247 | // deviceRef was invalidated due to the remove |
| 248 | CFRelease(device->deviceRef); |
| 249 | device->deviceRef = NULL; |
| 250 | } |
| 251 | if (device->ffeffect_ref) { |
| 252 | FFDeviceReleaseEffect(device->ffdevice, device->ffeffect_ref); |
| 253 | device->ffeffect_ref = NULL; |
| 254 | } |
| 255 | if (device->ffeffect) { |
| 256 | FreeRumbleEffectData(device->ffeffect); |
| 257 | device->ffeffect = NULL; |
| 258 | } |
| 259 | if (device->ffdevice) { |
| 260 | FFReleaseDevice(device->ffdevice); |
| 261 | device->ffdevice = NULL; |
| 262 | device->ff_initialized = false; |
| 263 | } |
| 264 | #ifdef SDL_HAPTIC_IOKIT |
| 265 | MacHaptic_MaybeRemoveDevice(device->ffservice); |
| 266 | #endif |
| 267 | |
| 268 | SDL_PrivateJoystickRemoved(device->instance_id); |
| 269 | } |
| 270 | |
| 271 | static void AddHIDElement(const void *value, void *parameter); |
| 272 | |
| 273 | // Call AddHIDElement() on all elements in an array of IOHIDElementRefs |
| 274 | static void AddHIDElements(CFArrayRef array, recDevice *pDevice) |
| 275 | { |
| 276 | const CFRange range = { 0, CFArrayGetCount(array) }; |
| 277 | CFArrayApplyFunction(array, range, AddHIDElement, pDevice); |
| 278 | } |
| 279 | |
| 280 | static bool ElementAlreadyAdded(const IOHIDElementCookie cookie, const recElement *listitem) |
| 281 | { |
| 282 | while (listitem) { |
| 283 | if (listitem->cookie == cookie) { |
| 284 | return true; |
| 285 | } |
| 286 | listitem = listitem->pNext; |
| 287 | } |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | // See if we care about this HID element, and if so, note it in our recDevice. |
| 292 | static void AddHIDElement(const void *value, void *parameter) |
| 293 | { |
| 294 | recDevice *pDevice = (recDevice *)parameter; |
| 295 | IOHIDElementRef refElement = (IOHIDElementRef)value; |
| 296 | const CFTypeID elementTypeID = refElement ? CFGetTypeID(refElement) : 0; |
| 297 | |
| 298 | if (refElement && (elementTypeID == IOHIDElementGetTypeID())) { |
| 299 | const IOHIDElementCookie cookie = IOHIDElementGetCookie(refElement); |
| 300 | const uint32_t usagePage = IOHIDElementGetUsagePage(refElement); |
| 301 | const uint32_t usage = IOHIDElementGetUsage(refElement); |
| 302 | recElement *element = NULL; |
| 303 | recElement **headElement = NULL; |
| 304 | |
| 305 | // look at types of interest |
| 306 | switch (IOHIDElementGetType(refElement)) { |
| 307 | case kIOHIDElementTypeInput_Misc: |
| 308 | case kIOHIDElementTypeInput_Button: |
| 309 | case kIOHIDElementTypeInput_Axis: |
| 310 | { |
| 311 | switch (usagePage) { // only interested in kHIDPage_GenericDesktop and kHIDPage_Button |
| 312 | case kHIDPage_GenericDesktop: |
| 313 | switch (usage) { |
| 314 | case kHIDUsage_GD_X: |
| 315 | case kHIDUsage_GD_Y: |
| 316 | case kHIDUsage_GD_Z: |
| 317 | case kHIDUsage_GD_Rx: |
| 318 | case kHIDUsage_GD_Ry: |
| 319 | case kHIDUsage_GD_Rz: |
| 320 | case kHIDUsage_GD_Slider: |
| 321 | case kHIDUsage_GD_Dial: |
| 322 | case kHIDUsage_GD_Wheel: |
| 323 | if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) { |
| 324 | element = (recElement *)SDL_calloc(1, sizeof(recElement)); |
| 325 | if (element) { |
| 326 | pDevice->axes++; |
| 327 | headElement = &(pDevice->firstAxis); |
| 328 | } |
| 329 | } |
| 330 | break; |
| 331 | |
| 332 | case kHIDUsage_GD_Hatswitch: |
| 333 | if (!ElementAlreadyAdded(cookie, pDevice->firstHat)) { |
| 334 | element = (recElement *)SDL_calloc(1, sizeof(recElement)); |
| 335 | if (element) { |
| 336 | pDevice->hats++; |
| 337 | headElement = &(pDevice->firstHat); |
| 338 | } |
| 339 | } |
| 340 | break; |
| 341 | case kHIDUsage_GD_DPadUp: |
| 342 | case kHIDUsage_GD_DPadDown: |
| 343 | case kHIDUsage_GD_DPadRight: |
| 344 | case kHIDUsage_GD_DPadLeft: |
| 345 | case kHIDUsage_GD_Start: |
| 346 | case kHIDUsage_GD_Select: |
| 347 | case kHIDUsage_GD_SystemMainMenu: |
| 348 | if (!ElementAlreadyAdded(cookie, pDevice->firstButton)) { |
| 349 | element = (recElement *)SDL_calloc(1, sizeof(recElement)); |
| 350 | if (element) { |
| 351 | pDevice->buttons++; |
| 352 | headElement = &(pDevice->firstButton); |
| 353 | } |
| 354 | } |
| 355 | break; |
| 356 | } |
| 357 | break; |
| 358 | |
| 359 | case kHIDPage_Simulation: |
| 360 | switch (usage) { |
| 361 | case kHIDUsage_Sim_Rudder: |
| 362 | case kHIDUsage_Sim_Throttle: |
| 363 | case kHIDUsage_Sim_Accelerator: |
| 364 | case kHIDUsage_Sim_Brake: |
| 365 | if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) { |
| 366 | element = (recElement *)SDL_calloc(1, sizeof(recElement)); |
| 367 | if (element) { |
| 368 | pDevice->axes++; |
| 369 | headElement = &(pDevice->firstAxis); |
| 370 | } |
| 371 | } |
| 372 | break; |
| 373 | |
| 374 | default: |
| 375 | break; |
| 376 | } |
| 377 | break; |
| 378 | |
| 379 | case kHIDPage_Button: |
| 380 | case kHIDPage_Consumer: // e.g. 'pause' button on Steelseries MFi gamepads. |
| 381 | if (!ElementAlreadyAdded(cookie, pDevice->firstButton)) { |
| 382 | element = (recElement *)SDL_calloc(1, sizeof(recElement)); |
| 383 | if (element) { |
| 384 | pDevice->buttons++; |
| 385 | headElement = &(pDevice->firstButton); |
| 386 | } |
| 387 | } |
| 388 | break; |
| 389 | |
| 390 | default: |
| 391 | break; |
| 392 | } |
| 393 | } break; |
| 394 | |
| 395 | case kIOHIDElementTypeCollection: |
| 396 | { |
| 397 | CFArrayRef array = IOHIDElementGetChildren(refElement); |
| 398 | if (array) { |
| 399 | AddHIDElements(array, pDevice); |
| 400 | } |
| 401 | } break; |
| 402 | |
| 403 | default: |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | if (element && headElement) { // add to list |
| 408 | recElement *elementPrevious = NULL; |
| 409 | recElement *elementCurrent = *headElement; |
| 410 | while (elementCurrent && usage >= elementCurrent->usage) { |
| 411 | elementPrevious = elementCurrent; |
| 412 | elementCurrent = elementCurrent->pNext; |
| 413 | } |
| 414 | if (elementPrevious) { |
| 415 | elementPrevious->pNext = element; |
| 416 | } else { |
| 417 | *headElement = element; |
| 418 | } |
| 419 | |
| 420 | element->elementRef = refElement; |
| 421 | element->usagePage = usagePage; |
| 422 | element->usage = usage; |
| 423 | element->pNext = elementCurrent; |
| 424 | |
| 425 | element->minReport = element->min = (SInt32)IOHIDElementGetLogicalMin(refElement); |
| 426 | element->maxReport = element->max = (SInt32)IOHIDElementGetLogicalMax(refElement); |
| 427 | element->cookie = IOHIDElementGetCookie(refElement); |
| 428 | |
| 429 | pDevice->elements++; |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | static int GetSteamVirtualGamepadSlot(Uint16 vendor_id, Uint16 product_id, const char *product_string) |
| 435 | { |
| 436 | int slot = -1; |
| 437 | |
| 438 | if (vendor_id == USB_VENDOR_MICROSOFT && product_id == USB_PRODUCT_XBOX360_WIRED_CONTROLLER) { |
| 439 | // Gamepad name is "GamePad-N", where N is slot + 1 |
| 440 | if (SDL_sscanf(product_string, "GamePad-%d", &slot) == 1) { |
| 441 | slot -= 1; |
| 442 | } |
| 443 | } |
| 444 | return slot; |
| 445 | } |
| 446 | |
| 447 | static bool IsControlledBy360ControllerDriver(IOHIDDeviceRef hidDevice) |
| 448 | { |
| 449 | bool controlled_by_360controller = false; |
| 450 | io_service_t service = IOHIDDeviceGetService(hidDevice); |
| 451 | if (service != MACH_PORT_NULL) { |
| 452 | controlled_by_360controller = IOObjectConformsTo(service, "Xbox360ControllerClass"); |
| 453 | } |
| 454 | return controlled_by_360controller; |
| 455 | } |
| 456 | |
| 457 | static bool GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice) |
| 458 | { |
| 459 | Sint32 vendor = 0; |
| 460 | Sint32 product = 0; |
| 461 | Sint32 version = 0; |
| 462 | char *name; |
| 463 | char manufacturer_string[256]; |
| 464 | char product_string[256]; |
| 465 | CFTypeRef refCF = NULL; |
| 466 | CFArrayRef array = NULL; |
| 467 | |
| 468 | // get usage page and usage |
| 469 | refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsagePageKey)); |
| 470 | if (refCF) { |
| 471 | CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usagePage); |
| 472 | } |
| 473 | if (pDevice->usagePage != kHIDPage_GenericDesktop) { |
| 474 | return false; // Filter device list to non-keyboard/mouse stuff |
| 475 | } |
| 476 | |
| 477 | refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsageKey)); |
| 478 | if (refCF) { |
| 479 | CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usage); |
| 480 | } |
| 481 | |
| 482 | if ((pDevice->usage != kHIDUsage_GD_Joystick && |
| 483 | pDevice->usage != kHIDUsage_GD_GamePad && |
| 484 | pDevice->usage != kHIDUsage_GD_MultiAxisController)) { |
| 485 | return false; // Filter device list to non-keyboard/mouse stuff |
| 486 | } |
| 487 | |
| 488 | /* Make sure we retain the use of the IOKit-provided device-object, |
| 489 | lest the device get disconnected and we try to use it. (Fixes |
| 490 | SDL-Bugzilla #4961, aka. https://bugzilla.libsdl.org/show_bug.cgi?id=4961 ) |
| 491 | */ |
| 492 | CFRetain(hidDevice); |
| 493 | |
| 494 | /* Now that we've CFRetain'ed the device-object (for our use), we'll |
| 495 | save the reference to it. |
| 496 | */ |
| 497 | pDevice->deviceRef = hidDevice; |
| 498 | |
| 499 | refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVendorIDKey)); |
| 500 | if (refCF) { |
| 501 | CFNumberGetValue(refCF, kCFNumberSInt32Type, &vendor); |
| 502 | } |
| 503 | |
| 504 | refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductIDKey)); |
| 505 | if (refCF) { |
| 506 | CFNumberGetValue(refCF, kCFNumberSInt32Type, &product); |
| 507 | } |
| 508 | |
| 509 | refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVersionNumberKey)); |
| 510 | if (refCF) { |
| 511 | CFNumberGetValue(refCF, kCFNumberSInt32Type, &version); |
| 512 | } |
| 513 | |
| 514 | if (!IsControlledBy360ControllerDriver(hidDevice) && SDL_IsJoystickXboxOne(vendor, product)) { |
| 515 | // We can't actually use this API for Xbox controllers without the 360Controller driver |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | if (SDL_IsJoystickSteamVirtualGamepad(vendor, product, version)) { |
| 520 | if (IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVirtualHIDevice)) != kCFBooleanTrue) { |
| 521 | // This is a real Xbox 360 controller, adjust the version so it's not detected as a Steam virtual gamepad |
| 522 | version = 1; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // get device name |
| 527 | refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDManufacturerKey)); |
| 528 | if ((!refCF) || (!CFStringGetCString(refCF, manufacturer_string, sizeof(manufacturer_string), kCFStringEncodingUTF8))) { |
| 529 | manufacturer_string[0] = '\0'; |
| 530 | } |
| 531 | refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductKey)); |
| 532 | if ((!refCF) || (!CFStringGetCString(refCF, product_string, sizeof(product_string), kCFStringEncodingUTF8))) { |
| 533 | product_string[0] = '\0'; |
| 534 | } |
| 535 | name = SDL_CreateJoystickName(vendor, product, manufacturer_string, product_string); |
| 536 | if (name) { |
| 537 | SDL_strlcpy(pDevice->product, name, sizeof(pDevice->product)); |
| 538 | SDL_free(name); |
| 539 | } |
| 540 | |
| 541 | if (SDL_ShouldIgnoreJoystick(vendor, product, version, pDevice->product)) { |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | if (SDL_JoystickHandledByAnotherDriver(&SDL_DARWIN_JoystickDriver, vendor, product, version, pDevice->product)) { |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | pDevice->guid = SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_USB, (Uint16)vendor, (Uint16)product, (Uint16)version, manufacturer_string, product_string, 0, 0); |
| 550 | pDevice->steam_virtual_gamepad_slot = GetSteamVirtualGamepadSlot((Uint16)vendor, (Uint16)product, product_string); |
| 551 | |
| 552 | if (vendor == USB_VENDOR_NACON_ALT && |
| 553 | product == USB_PRODUCT_NACON_REVOLUTION_X_UNLIMITED_BT) { |
| 554 | pDevice->nacon_revolution_x_unlimited = true; |
| 555 | } |
| 556 | |
| 557 | array = IOHIDDeviceCopyMatchingElements(hidDevice, NULL, kIOHIDOptionsTypeNone); |
| 558 | if (array) { |
| 559 | AddHIDElements(array, pDevice); |
| 560 | CFRelease(array); |
| 561 | } |
| 562 | |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | static bool JoystickAlreadyKnown(IOHIDDeviceRef ioHIDDeviceObject) |
| 567 | { |
| 568 | recDevice *i; |
| 569 | |
| 570 | #ifdef SDL_JOYSTICK_MFI |
| 571 | extern bool IOS_SupportedHIDDevice(IOHIDDeviceRef device); |
| 572 | if (!IsControlledBy360ControllerDriver(ioHIDDeviceObject) && IOS_SupportedHIDDevice(ioHIDDeviceObject)) { |
| 573 | return true; |
| 574 | } |
| 575 | #endif |
| 576 | |
| 577 | for (i = gpDeviceList; i; i = i->pNext) { |
| 578 | if (i->deviceRef == ioHIDDeviceObject) { |
| 579 | return true; |
| 580 | } |
| 581 | } |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | static void JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject) |
| 586 | { |
| 587 | recDevice *device; |
| 588 | io_service_t ioservice; |
| 589 | |
| 590 | if (res != kIOReturnSuccess) { |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | if (JoystickAlreadyKnown(ioHIDDeviceObject)) { |
| 595 | return; // IOKit sent us a duplicate. |
| 596 | } |
| 597 | |
| 598 | device = (recDevice *)SDL_calloc(1, sizeof(recDevice)); |
| 599 | if (!device) { |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | if (!GetDeviceInfo(ioHIDDeviceObject, device)) { |
| 604 | FreeDevice(device); |
| 605 | return; // not a device we care about, probably. |
| 606 | } |
| 607 | |
| 608 | // Get notified when this device is disconnected. |
| 609 | IOHIDDeviceRegisterRemovalCallback(ioHIDDeviceObject, JoystickDeviceWasRemovedCallback, device); |
| 610 | IOHIDDeviceScheduleWithRunLoop(ioHIDDeviceObject, CFRunLoopGetCurrent(), SDL_JOYSTICK_RUNLOOP_MODE); |
| 611 | device->runLoopAttached = true; |
| 612 | |
| 613 | // Allocate an instance ID for this device |
| 614 | device->instance_id = SDL_GetNextObjectID(); |
| 615 | |
| 616 | // We have to do some storage of the io_service_t for SDL_OpenHapticFromJoystick |
| 617 | ioservice = IOHIDDeviceGetService(ioHIDDeviceObject); |
| 618 | if ((ioservice) && (FFIsForceFeedback(ioservice) == FF_OK)) { |
| 619 | device->ffservice = ioservice; |
| 620 | #ifdef SDL_HAPTIC_IOKIT |
| 621 | MacHaptic_MaybeAddDevice(ioservice); |
| 622 | #endif |
| 623 | } |
| 624 | |
| 625 | // Add device to the end of the list |
| 626 | if (!gpDeviceList) { |
| 627 | gpDeviceList = device; |
| 628 | } else { |
| 629 | recDevice *curdevice; |
| 630 | |
| 631 | curdevice = gpDeviceList; |
| 632 | while (curdevice->pNext) { |
| 633 | curdevice = curdevice->pNext; |
| 634 | } |
| 635 | curdevice->pNext = device; |
| 636 | } |
| 637 | |
| 638 | SDL_PrivateJoystickAdded(device->instance_id); |
| 639 | } |
| 640 | |
| 641 | static bool ConfigHIDManager(CFArrayRef matchingArray) |
| 642 | { |
| 643 | CFRunLoopRef runloop = CFRunLoopGetCurrent(); |
| 644 | |
| 645 | if (IOHIDManagerOpen(hidman, kIOHIDOptionsTypeNone) != kIOReturnSuccess) { |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | IOHIDManagerSetDeviceMatchingMultiple(hidman, matchingArray); |
| 650 | IOHIDManagerRegisterDeviceMatchingCallback(hidman, JoystickDeviceWasAddedCallback, NULL); |
| 651 | IOHIDManagerScheduleWithRunLoop(hidman, runloop, SDL_JOYSTICK_RUNLOOP_MODE); |
| 652 | |
| 653 | while (CFRunLoopRunInMode(SDL_JOYSTICK_RUNLOOP_MODE, 0, TRUE) == kCFRunLoopRunHandledSource) { |
| 654 | // no-op. Callback fires once per existing device. |
| 655 | } |
| 656 | |
| 657 | // future hotplug events will come through SDL_JOYSTICK_RUNLOOP_MODE now. |
| 658 | |
| 659 | return true; // good to go. |
| 660 | } |
| 661 | |
| 662 | static CFDictionaryRef CreateHIDDeviceMatchDictionary(const UInt32 page, const UInt32 usage, int *okay) |
| 663 | { |
| 664 | CFDictionaryRef result = NULL; |
| 665 | CFNumberRef pageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page); |
| 666 | CFNumberRef usageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage); |
| 667 | const void *keys[2] = { (void *)CFSTR(kIOHIDDeviceUsagePageKey), (void *)CFSTR(kIOHIDDeviceUsageKey) }; |
| 668 | const void *vals[2] = { (void *)pageNumRef, (void *)usageNumRef }; |
| 669 | |
| 670 | if (pageNumRef && usageNumRef) { |
| 671 | result = CFDictionaryCreate(kCFAllocatorDefault, keys, vals, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); |
| 672 | } |
| 673 | |
| 674 | if (pageNumRef) { |
| 675 | CFRelease(pageNumRef); |
| 676 | } |
| 677 | if (usageNumRef) { |
| 678 | CFRelease(usageNumRef); |
| 679 | } |
| 680 | |
| 681 | if (!result) { |
| 682 | *okay = 0; |
| 683 | } |
| 684 | |
| 685 | return result; |
| 686 | } |
| 687 | |
| 688 | static bool CreateHIDManager(void) |
| 689 | { |
| 690 | bool result = false; |
| 691 | int okay = 1; |
| 692 | const void *vals[] = { |
| 693 | (void *)CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick, &okay), |
| 694 | (void *)CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad, &okay), |
| 695 | (void *)CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController, &okay), |
| 696 | }; |
| 697 | const size_t numElements = SDL_arraysize(vals); |
| 698 | CFArrayRef array = okay ? CFArrayCreate(kCFAllocatorDefault, vals, numElements, &kCFTypeArrayCallBacks) : NULL; |
| 699 | size_t i; |
| 700 | |
| 701 | for (i = 0; i < numElements; i++) { |
| 702 | if (vals[i]) { |
| 703 | CFRelease((CFTypeRef)vals[i]); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | if (array) { |
| 708 | hidman = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); |
| 709 | if (hidman != NULL) { |
| 710 | result = ConfigHIDManager(array); |
| 711 | } |
| 712 | CFRelease(array); |
| 713 | } |
| 714 | |
| 715 | return result; |
| 716 | } |
| 717 | |
| 718 | static bool DARWIN_JoystickInit(void) |
| 719 | { |
| 720 | if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_IOKIT, true)) { |
| 721 | return true; |
| 722 | } |
| 723 | |
| 724 | if (!CreateHIDManager()) { |
| 725 | return SDL_SetError("Joystick: Couldn't initialize HID Manager"); |
| 726 | } |
| 727 | |
| 728 | return true; |
| 729 | } |
| 730 | |
| 731 | static int DARWIN_JoystickGetCount(void) |
| 732 | { |
| 733 | recDevice *device = gpDeviceList; |
| 734 | int nJoySticks = 0; |
| 735 | |
| 736 | while (device) { |
| 737 | if (!device->removed) { |
| 738 | nJoySticks++; |
| 739 | } |
| 740 | device = device->pNext; |
| 741 | } |
| 742 | |
| 743 | return nJoySticks; |
| 744 | } |
| 745 | |
| 746 | static void DARWIN_JoystickDetect(void) |
| 747 | { |
| 748 | recDevice *device = gpDeviceList; |
| 749 | while (device) { |
| 750 | if (device->removed) { |
| 751 | device = FreeDevice(device); |
| 752 | } else { |
| 753 | device = device->pNext; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if (hidman) { |
| 758 | /* run this after the checks above so we don't set device->removed and delete the device before |
| 759 | DARWIN_JoystickUpdate can run to clean up the SDL_Joystick object that owns this device */ |
| 760 | while (CFRunLoopRunInMode(SDL_JOYSTICK_RUNLOOP_MODE, 0, TRUE) == kCFRunLoopRunHandledSource) { |
| 761 | // no-op. Pending callbacks will fire in CFRunLoopRunInMode(). |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | static bool DARWIN_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name) |
| 767 | { |
| 768 | // We don't override any other drivers |
| 769 | return false; |
| 770 | } |
| 771 | |
| 772 | static const char *DARWIN_JoystickGetDeviceName(int device_index) |
| 773 | { |
| 774 | recDevice *device = GetDeviceForIndex(device_index); |
| 775 | return device ? device->product : "UNKNOWN"; |
| 776 | } |
| 777 | |
| 778 | static const char *DARWIN_JoystickGetDevicePath(int device_index) |
| 779 | { |
| 780 | return NULL; |
| 781 | } |
| 782 | |
| 783 | static int DARWIN_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index) |
| 784 | { |
| 785 | recDevice *device = GetDeviceForIndex(device_index); |
| 786 | return device ? device->steam_virtual_gamepad_slot : -1; |
| 787 | } |
| 788 | |
| 789 | static int DARWIN_JoystickGetDevicePlayerIndex(int device_index) |
| 790 | { |
| 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | static void DARWIN_JoystickSetDevicePlayerIndex(int device_index, int player_index) |
| 795 | { |
| 796 | } |
| 797 | |
| 798 | static SDL_GUID DARWIN_JoystickGetDeviceGUID(int device_index) |
| 799 | { |
| 800 | recDevice *device = GetDeviceForIndex(device_index); |
| 801 | SDL_GUID guid; |
| 802 | if (device) { |
| 803 | guid = device->guid; |
| 804 | } else { |
| 805 | SDL_zero(guid); |
| 806 | } |
| 807 | return guid; |
| 808 | } |
| 809 | |
| 810 | static SDL_JoystickID DARWIN_JoystickGetDeviceInstanceID(int device_index) |
| 811 | { |
| 812 | recDevice *device = GetDeviceForIndex(device_index); |
| 813 | return device ? device->instance_id : 0; |
| 814 | } |
| 815 | |
| 816 | static bool DARWIN_JoystickOpen(SDL_Joystick *joystick, int device_index) |
| 817 | { |
| 818 | recDevice *device = GetDeviceForIndex(device_index); |
| 819 | |
| 820 | joystick->hwdata = device; |
| 821 | device->joystick = joystick; |
| 822 | joystick->name = device->product; |
| 823 | |
| 824 | joystick->naxes = device->axes; |
| 825 | joystick->nhats = device->hats; |
| 826 | joystick->nbuttons = device->buttons; |
| 827 | |
| 828 | if (device->ffservice) { |
| 829 | SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, true); |
| 830 | } |
| 831 | |
| 832 | return true; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Like strerror but for force feedback errors. |
| 837 | */ |
| 838 | static const char *FFStrError(unsigned int err) |
| 839 | { |
| 840 | switch (err) { |
| 841 | case FFERR_DEVICEFULL: |
| 842 | return "device full"; |
| 843 | // This should be valid, but for some reason isn't defined... |
| 844 | /* case FFERR_DEVICENOTREG: |
| 845 | return "device not registered"; */ |
| 846 | case FFERR_DEVICEPAUSED: |
| 847 | return "device paused"; |
| 848 | case FFERR_DEVICERELEASED: |
| 849 | return "device released"; |
| 850 | case FFERR_EFFECTPLAYING: |
| 851 | return "effect playing"; |
| 852 | case FFERR_EFFECTTYPEMISMATCH: |
| 853 | return "effect type mismatch"; |
| 854 | case FFERR_EFFECTTYPENOTSUPPORTED: |
| 855 | return "effect type not supported"; |
| 856 | case FFERR_GENERIC: |
| 857 | return "undetermined error"; |
| 858 | case FFERR_HASEFFECTS: |
| 859 | return "device has effects"; |
| 860 | case FFERR_INCOMPLETEEFFECT: |
| 861 | return "incomplete effect"; |
| 862 | case FFERR_INTERNAL: |
| 863 | return "internal fault"; |
| 864 | case FFERR_INVALIDDOWNLOADID: |
| 865 | return "invalid download id"; |
| 866 | case FFERR_INVALIDPARAM: |
| 867 | return "invalid parameter"; |
| 868 | case FFERR_MOREDATA: |
| 869 | return "more data"; |
| 870 | case FFERR_NOINTERFACE: |
| 871 | return "interface not supported"; |
| 872 | case FFERR_NOTDOWNLOADED: |
| 873 | return "effect is not downloaded"; |
| 874 | case FFERR_NOTINITIALIZED: |
| 875 | return "object has not been initialized"; |
| 876 | case FFERR_OUTOFMEMORY: |
| 877 | return "out of memory"; |
| 878 | case FFERR_UNPLUGGED: |
| 879 | return "device is unplugged"; |
| 880 | case FFERR_UNSUPPORTED: |
| 881 | return "function call unsupported"; |
| 882 | case FFERR_UNSUPPORTEDAXIS: |
| 883 | return "axis unsupported"; |
| 884 | |
| 885 | default: |
| 886 | return "unknown error"; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | static bool DARWIN_JoystickInitRumble(recDevice *device, Sint16 magnitude) |
| 891 | { |
| 892 | HRESULT result; |
| 893 | |
| 894 | if (!device->ffdevice) { |
| 895 | result = FFCreateDevice(device->ffservice, &device->ffdevice); |
| 896 | if (result != FF_OK) { |
| 897 | return SDL_SetError("Unable to create force feedback device from service: %s", FFStrError(result)); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | // Reset and then enable actuators |
| 902 | result = FFDeviceSendForceFeedbackCommand(device->ffdevice, FFSFFC_RESET); |
| 903 | if (result != FF_OK) { |
| 904 | return SDL_SetError("Unable to reset force feedback device: %s", FFStrError(result)); |
| 905 | } |
| 906 | |
| 907 | result = FFDeviceSendForceFeedbackCommand(device->ffdevice, FFSFFC_SETACTUATORSON); |
| 908 | if (result != FF_OK) { |
| 909 | return SDL_SetError("Unable to enable force feedback actuators: %s", FFStrError(result)); |
| 910 | } |
| 911 | |
| 912 | // Create the effect |
| 913 | device->ffeffect = CreateRumbleEffectData(magnitude); |
| 914 | if (!device->ffeffect) { |
| 915 | return false; |
| 916 | } |
| 917 | |
| 918 | result = FFDeviceCreateEffect(device->ffdevice, kFFEffectType_Sine_ID, |
| 919 | device->ffeffect, &device->ffeffect_ref); |
| 920 | if (result != FF_OK) { |
| 921 | return SDL_SetError("Haptic: Unable to create effect: %s", FFStrError(result)); |
| 922 | } |
| 923 | return true; |
| 924 | } |
| 925 | |
| 926 | static bool DARWIN_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble) |
| 927 | { |
| 928 | HRESULT result; |
| 929 | recDevice *device = joystick->hwdata; |
| 930 | |
| 931 | // Scale and average the two rumble strengths |
| 932 | Sint16 magnitude = (Sint16)(((low_frequency_rumble / 2) + (high_frequency_rumble / 2)) / 2); |
| 933 | |
| 934 | if (!device) { |
| 935 | return SDL_SetError("Rumble failed, device disconnected"); |
| 936 | } |
| 937 | |
| 938 | if (!device->ffservice) { |
| 939 | return SDL_Unsupported(); |
| 940 | } |
| 941 | |
| 942 | if (device->ff_initialized) { |
| 943 | FFPERIODIC *periodic = ((FFPERIODIC *)device->ffeffect->lpvTypeSpecificParams); |
| 944 | periodic->dwMagnitude = CONVERT_MAGNITUDE(magnitude); |
| 945 | |
| 946 | result = FFEffectSetParameters(device->ffeffect_ref, device->ffeffect, |
| 947 | (FFEP_DURATION | FFEP_TYPESPECIFICPARAMS)); |
| 948 | if (result != FF_OK) { |
| 949 | return SDL_SetError("Unable to update rumble effect: %s", FFStrError(result)); |
| 950 | } |
| 951 | } else { |
| 952 | if (!DARWIN_JoystickInitRumble(device, magnitude)) { |
| 953 | return false; |
| 954 | } |
| 955 | device->ff_initialized = true; |
| 956 | } |
| 957 | |
| 958 | result = FFEffectStart(device->ffeffect_ref, 1, 0); |
| 959 | if (result != FF_OK) { |
| 960 | return SDL_SetError("Unable to run the rumble effect: %s", FFStrError(result)); |
| 961 | } |
| 962 | return true; |
| 963 | } |
| 964 | |
| 965 | static bool DARWIN_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble) |
| 966 | { |
| 967 | return SDL_Unsupported(); |
| 968 | } |
| 969 | |
| 970 | static bool DARWIN_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue) |
| 971 | { |
| 972 | return SDL_Unsupported(); |
| 973 | } |
| 974 | |
| 975 | static bool DARWIN_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size) |
| 976 | { |
| 977 | return SDL_Unsupported(); |
| 978 | } |
| 979 | |
| 980 | static bool DARWIN_JoystickSetSensorsEnabled(SDL_Joystick *joystick, bool enabled) |
| 981 | { |
| 982 | return SDL_Unsupported(); |
| 983 | } |
| 984 | |
| 985 | static void DARWIN_JoystickUpdate(SDL_Joystick *joystick) |
| 986 | { |
| 987 | recDevice *device = joystick->hwdata; |
| 988 | recElement *element; |
| 989 | SInt32 value, range; |
| 990 | int i, goodRead = false; |
| 991 | Uint64 timestamp = SDL_GetTicksNS(); |
| 992 | |
| 993 | if (!device) { |
| 994 | return; |
| 995 | } |
| 996 | |
| 997 | if (device->removed) { // device was unplugged; ignore it. |
| 998 | if (joystick->hwdata) { |
| 999 | joystick->hwdata = NULL; |
| 1000 | } |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | element = device->firstAxis; |
| 1005 | i = 0; |
| 1006 | |
| 1007 | while (element) { |
| 1008 | if (device->nacon_revolution_x_unlimited) { |
| 1009 | goodRead = GetHIDScaledCalibratedState_NACON_Revolution_X_Unlimited(device, element, -32768, 32767, &value); |
| 1010 | } else { |
| 1011 | goodRead = GetHIDScaledCalibratedState(device, element, -32768, 32767, &value); |
| 1012 | } |
| 1013 | if (goodRead) { |
| 1014 | SDL_SendJoystickAxis(timestamp, joystick, i, value); |
| 1015 | } |
| 1016 | |
| 1017 | element = element->pNext; |
| 1018 | ++i; |
| 1019 | } |
| 1020 | |
| 1021 | element = device->firstButton; |
| 1022 | i = 0; |
| 1023 | while (element) { |
| 1024 | goodRead = GetHIDElementState(device, element, &value); |
| 1025 | if (goodRead) { |
| 1026 | SDL_SendJoystickButton(timestamp, joystick, i, (value != 0)); |
| 1027 | } |
| 1028 | |
| 1029 | element = element->pNext; |
| 1030 | ++i; |
| 1031 | } |
| 1032 | |
| 1033 | element = device->firstHat; |
| 1034 | i = 0; |
| 1035 | |
| 1036 | while (element) { |
| 1037 | Uint8 pos = 0; |
| 1038 | |
| 1039 | range = (element->max - element->min + 1); |
| 1040 | goodRead = GetHIDElementState(device, element, &value); |
| 1041 | if (goodRead) { |
| 1042 | value -= element->min; |
| 1043 | if (range == 4) { // 4 position hatswitch - scale up value |
| 1044 | value *= 2; |
| 1045 | } else if (range != 8) { // Neither a 4 nor 8 positions - fall back to default position (centered) |
| 1046 | value = -1; |
| 1047 | } |
| 1048 | switch (value) { |
| 1049 | case 0: |
| 1050 | pos = SDL_HAT_UP; |
| 1051 | break; |
| 1052 | case 1: |
| 1053 | pos = SDL_HAT_RIGHTUP; |
| 1054 | break; |
| 1055 | case 2: |
| 1056 | pos = SDL_HAT_RIGHT; |
| 1057 | break; |
| 1058 | case 3: |
| 1059 | pos = SDL_HAT_RIGHTDOWN; |
| 1060 | break; |
| 1061 | case 4: |
| 1062 | pos = SDL_HAT_DOWN; |
| 1063 | break; |
| 1064 | case 5: |
| 1065 | pos = SDL_HAT_LEFTDOWN; |
| 1066 | break; |
| 1067 | case 6: |
| 1068 | pos = SDL_HAT_LEFT; |
| 1069 | break; |
| 1070 | case 7: |
| 1071 | pos = SDL_HAT_LEFTUP; |
| 1072 | break; |
| 1073 | default: |
| 1074 | /* Every other value is mapped to center. We do that because some |
| 1075 | * joysticks use 8 and some 15 for this value, and apparently |
| 1076 | * there are even more variants out there - so we try to be generous. |
| 1077 | */ |
| 1078 | pos = SDL_HAT_CENTERED; |
| 1079 | break; |
| 1080 | } |
| 1081 | |
| 1082 | SDL_SendJoystickHat(timestamp, joystick, i, pos); |
| 1083 | } |
| 1084 | |
| 1085 | element = element->pNext; |
| 1086 | ++i; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | static void DARWIN_JoystickClose(SDL_Joystick *joystick) |
| 1091 | { |
| 1092 | recDevice *device = joystick->hwdata; |
| 1093 | if (device) { |
| 1094 | device->joystick = NULL; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | static void DARWIN_JoystickQuit(void) |
| 1099 | { |
| 1100 | while (FreeDevice(gpDeviceList)) { |
| 1101 | // spin |
| 1102 | } |
| 1103 | |
| 1104 | if (hidman) { |
| 1105 | IOHIDManagerUnscheduleFromRunLoop(hidman, CFRunLoopGetCurrent(), SDL_JOYSTICK_RUNLOOP_MODE); |
| 1106 | IOHIDManagerClose(hidman, kIOHIDOptionsTypeNone); |
| 1107 | CFRelease(hidman); |
| 1108 | hidman = NULL; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | static bool DARWIN_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out) |
| 1113 | { |
| 1114 | return false; |
| 1115 | } |
| 1116 | |
| 1117 | SDL_JoystickDriver SDL_DARWIN_JoystickDriver = { |
| 1118 | DARWIN_JoystickInit, |
| 1119 | DARWIN_JoystickGetCount, |
| 1120 | DARWIN_JoystickDetect, |
| 1121 | DARWIN_JoystickIsDevicePresent, |
| 1122 | DARWIN_JoystickGetDeviceName, |
| 1123 | DARWIN_JoystickGetDevicePath, |
| 1124 | DARWIN_JoystickGetDeviceSteamVirtualGamepadSlot, |
| 1125 | DARWIN_JoystickGetDevicePlayerIndex, |
| 1126 | DARWIN_JoystickSetDevicePlayerIndex, |
| 1127 | DARWIN_JoystickGetDeviceGUID, |
| 1128 | DARWIN_JoystickGetDeviceInstanceID, |
| 1129 | DARWIN_JoystickOpen, |
| 1130 | DARWIN_JoystickRumble, |
| 1131 | DARWIN_JoystickRumbleTriggers, |
| 1132 | DARWIN_JoystickSetLED, |
| 1133 | DARWIN_JoystickSendEffect, |
| 1134 | DARWIN_JoystickSetSensorsEnabled, |
| 1135 | DARWIN_JoystickUpdate, |
| 1136 | DARWIN_JoystickClose, |
| 1137 | DARWIN_JoystickQuit, |
| 1138 | DARWIN_JoystickGetGamepadMapping |
| 1139 | }; |
| 1140 | |
| 1141 | #endif // SDL_JOYSTICK_IOKIT |