Skip to content

Commit 682066e

Browse files
fix crash when controller disconnects on macos 10.15+
1 parent 70a8905 commit 682066e

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CC := $(CLANG) -isysroot $(SYSROOT) -arch $(ARCH) -mmacosx-version-min=$(O
1515
CFLAGS = -O1 -Wall -g -I include/ -I src/ -fvisibility=hidden
1616
LDFLAGS = -shared -Wl,-undefined -Wl,dynamic_lookup -Llib -ldobby
1717

18-
FRAMEWORKS = -framework CoreFoundation
18+
FRAMEWORKS = -framework CoreFoundation -framework IOKit
1919

2020
SOURCES = source/bo3macfix.c source/bo3macnative.m source/utilities.c source/steam.c source/exports.c
2121

scripts/package.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ chmod 0755 staging/BO3MacFix/AppBundleExe
2626
mkdir release
2727
cd staging
2828
echo Compressing files...
29-
tar -czvf ../release/BO3MacFix.tar.gz BO3MacFix
29+
tar --no-mac-metadata --disable-copyfile --exclude='.DS_Store' -czvf ../release/BO3MacFix.tar.gz BO3MacFix
3030
cd ..

source/bo3macfix.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include <sys/mman.h>
1616
#include <sandbox.h>
1717

18+
#include <CoreFoundation/CoreFoundation.h>
19+
#include <IOKit/hid/IOHIDDevice.h>
20+
1821
#include "bo3macnative.h"
1922
#include "codhavoktypes.h"
2023
#include "dobby.h"
@@ -365,6 +368,41 @@ FILE *popen_new(const char *command, const char *mode) {
365368
DYLD_INTERPOSE(system_new, system);
366369
DYLD_INTERPOSE(popen_new, popen);
367370

371+
// this will get called in SDL2.0.7's FreeDevice, and not anywhere else, so it's safe
372+
void IOHIDDeviceUnscheduleFromRunLoop_new(IOHIDDeviceRef device, CFRunLoopRef runLoop, CFStringRef runLoopMode)
373+
{
374+
IOHIDDeviceUnscheduleFromRunLoop(device, runLoop, runLoopMode);
375+
// release the device ref as per https://github.com/libsdl-org/SDL/commit/65fd63369411240dd54cad74916c6a6739e9363b
376+
//printf("releasing!\n");
377+
CFRelease(device);
378+
}
379+
DYLD_INTERPOSE(IOHIDDeviceUnscheduleFromRunLoop_new, IOHIDDeviceUnscheduleFromRunLoop);
380+
381+
// this will get called lots in SDL2.0.7's JoystickDeviceWasAddedCallback, so filter for PrimaryUsagePage
382+
CFTypeRef IOHIDDeviceGetProperty_hook(IOHIDDeviceRef device, CFStringRef key)
383+
{
384+
CFTypeRef orig = IOHIDDeviceGetProperty(device, key);
385+
// that address is the CFString for PrimaryUsagePage
386+
if (orig != NULL && key == (void *)(game_base_address + 0x1b8b420))
387+
{
388+
//printf("device = %p\n", device);
389+
//printf("key = %p\n", key);
390+
// get the actual usage to see if SDL2.0.7 would be retaining it anyway
391+
uint32_t usage = 0;
392+
if (CFNumberGetValue(orig, kCFNumberSInt32Type, &usage))
393+
{
394+
// kHIDUsage_GD_Joystick, kHIDUsage_GD_GamePad, kHIDUsage_GD_MultiAxisController
395+
if ((usage < 9) && ((0x130U >> (usage & 0x1f) & 1) != 0)) {
396+
//printf("controller! retaining\n");
397+
// retain the device ref as per https://github.com/libsdl-org/SDL/commit/65fd63369411240dd54cad74916c6a6739e9363b
398+
CFRetain(device);
399+
}
400+
}
401+
}
402+
return orig;
403+
}
404+
DYLD_INTERPOSE(IOHIDDeviceGetProperty_hook, IOHIDDeviceGetProperty);
405+
368406
int sandbox_init_with_parameters(const char *profile, uint64_t flags, const char *const parameters[], char **errorbuf);
369407

370408
#define EMUPATH_B "C:\\Emu\\AppAssets"

source/versioninfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#define MACFIX_VERSION "1.2-beta1"
1+
#define MACFIX_VERSION "1.2-beta2"
22
//#define MACFIX_DEBUG 1

0 commit comments

Comments
 (0)