From fd750656ff257697dbe996a6a627ffe1dd90ad57 Mon Sep 17 00:00:00 2001
From: Daniel Brookman <[EMAIL REDACTED]>
Date: Mon, 2 May 2022 15:22:46 -0400
Subject: [PATCH] Fix crash when reading background events for MFi controllers
on macOS
Enabling GCController.shouldMonitorBackgroundEvents to read background events
for MFi controllers before receiving the first GCControllerDidConnectNotification
is apparently a no-go on macOS (12.3.1 for me), and would crash on attempt.
Apple's documentation is... not great, and doesn't point this out.
This waits for IOS_AddMFIJoystickDevice() to get called down the chain from GCControllerDidConnectNotification, and enables GCController.shouldMonitorBackgroundEvents
if it hadn't been already.
On iOS and tvOS, GCController.shouldMonitorBackgroundEvents is ignored, so
there's no need to check their versions.
---
src/joystick/iphoneos/SDL_mfijoystick.m | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/joystick/iphoneos/SDL_mfijoystick.m b/src/joystick/iphoneos/SDL_mfijoystick.m
index 38ab3fe605d..666c1c1cdcb 100644
--- a/src/joystick/iphoneos/SDL_mfijoystick.m
+++ b/src/joystick/iphoneos/SDL_mfijoystick.m
@@ -180,6 +180,10 @@ @interface GCMicroGamepad (SDL)
static BOOL
IOS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controller)
{
+ if ((@available(macOS 11.3, *)) && !GCController.shouldMonitorBackgroundEvents) {
+ GCController.shouldMonitorBackgroundEvents = YES;
+ }
+
Uint16 *guid16 = (Uint16 *)device->guid.data;
Uint16 vendor = 0;
Uint16 product = 0;
@@ -588,10 +592,6 @@ static int is_macos11(void)
return 0;
}
- if (@available(macOS 11.3, iOS 14.5, tvOS 14.5, *)) {
- GCController.shouldMonitorBackgroundEvents = YES;
- }
-
/* For whatever reason, this always returns an empty array on
macOS 11.0.1 */
for (GCController *controller in [GCController controllers]) {