From f3fd1ffb99e44adde00a1c07acfa67a55e2e689e Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 20 Oct 2021 15:18:03 -0700 Subject: [PATCH] Fixed crash if SDL API functions are called on a disconnected controller on iOS --- src/joystick/iphoneos/SDL_mfijoystick.m | 32 ++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/joystick/iphoneos/SDL_mfijoystick.m b/src/joystick/iphoneos/SDL_mfijoystick.m index 6563c9141..d0d1bf58d 100644 --- a/src/joystick/iphoneos/SDL_mfijoystick.m +++ b/src/joystick/iphoneos/SDL_mfijoystick.m @@ -1268,6 +1268,10 @@ IOS_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 h #ifdef ENABLE_MFI_RUMBLE SDL_JoystickDeviceItem *device = joystick->hwdata; + if (device == NULL) { + return SDL_SetError("Controller is no longer connected"); + } + if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) { if (!device->rumble && device->controller && device->controller.haptics) { SDL_RumbleContext *rumble = IOS_JoystickInitRumble(device->controller); @@ -1294,6 +1298,10 @@ IOS_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 ri #ifdef ENABLE_MFI_RUMBLE SDL_JoystickDeviceItem *device = joystick->hwdata; + if (device == NULL) { + return SDL_SetError("Controller is no longer connected"); + } + if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) { if (!device->rumble && device->controller && device->controller.haptics) { SDL_RumbleContext *rumble = IOS_JoystickInitRumble(device->controller); @@ -1319,8 +1327,14 @@ IOS_JoystickHasLED(SDL_Joystick *joystick) { #ifdef ENABLE_MFI_LIGHT @autoreleasepool { + SDL_JoystickDeviceItem *device = joystick->hwdata; + + if (device == NULL) { + return SDL_FALSE; + } + if (@available(macos 11.0, iOS 14.0, tvOS 14.0, *)) { - GCController *controller = joystick->hwdata->controller; + GCController *controller = device->controller; GCDeviceLight *light = controller.light; if (light) { return SDL_TRUE; @@ -1337,8 +1351,14 @@ IOS_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue) { #ifdef ENABLE_MFI_LIGHT @autoreleasepool { + SDL_JoystickDeviceItem *device = joystick->hwdata; + + if (device == NULL) { + return SDL_SetError("Controller is no longer connected"); + } + if (@available(macos 11.0, iOS 14.0, tvOS 14.0, *)) { - GCController *controller = joystick->hwdata->controller; + GCController *controller = device->controller; GCDeviceLight *light = controller.light; if (light) { light.color = [[GCColor alloc] initWithRed:(float)red / 255.0f @@ -1364,8 +1384,14 @@ IOS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) { #ifdef ENABLE_MFI_SENSORS @autoreleasepool { + SDL_JoystickDeviceItem *device = joystick->hwdata; + + if (device == NULL) { + return SDL_SetError("Controller is no longer connected"); + } + if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) { - GCController *controller = joystick->hwdata->controller; + GCController *controller = device->controller; GCMotion *motion = controller.motion; if (motion) { motion.sensorsActive = enabled ? YES : NO;