From 021cdb402dd837c1002c7a296a638c3c2f1e8f61 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 18:16:33 +0300 Subject: [PATCH] Various hacks for stopping the PWM thread when needed, important if we have a WUP-028 connected with more than one controller --- Cocoa/Document.m | 2 +- Cocoa/GBView.m | 2 ++ JoyKit/JOYController.h | 1 + JoyKit/JOYController.m | 13 ++++++++++++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 90a1dc84..ff47cd9f 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -381,7 +381,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) self.view.mouseHidingEnabled = NO; GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); GB_save_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]); - [_view setRumble:false]; + [_view setRumble:0]; stopping = false; } diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index e733731d..d342497d 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -116,6 +116,7 @@ } [[NSNotificationCenter defaultCenter] removeObserver:self]; [lastController setRumbleAmplitude:0]; + [lastController _forceStopPWMThread]; [JOYController unregisterListener:self]; } - (instancetype)initWithCoder:(NSCoder *)coder @@ -302,6 +303,7 @@ if (![self.window isMainWindow]) return; if (controller != lastController) { [lastController setRumbleAmplitude:0]; + [lastController _forceStopPWMThread]; lastController = controller; } diff --git a/JoyKit/JOYController.h b/JoyKit/JOYController.h index 9ed7cf7b..9363e364 100644 --- a/JoyKit/JOYController.h +++ b/JoyKit/JOYController.h @@ -35,6 +35,7 @@ static NSString const *JOYHatsEmulateButtonsKey = @"JOYHatsEmulateButtons"; - (NSArray *) hats; - (void)setRumbleAmplitude:(double)amp; - (void)setPlayerLEDs:(uint8_t)mask; +- (void)_forceStopPWMThread; // Hack @property (readonly, getter=isConnected) bool connected; @end diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index f9f07752..02680546 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -118,6 +118,7 @@ typedef struct __attribute__((packed)) { bool _physicallyConnected; bool _logicallyConnected; bool _rumblePWMThreadRunning; + volatile bool _forceStopPWMThread; } - (instancetype)initWithDevice:(IOHIDDeviceRef) device @@ -608,7 +609,7 @@ typedef struct __attribute__((packed)) { /* TODO: This does not handle correctly the case of having a multi-port controller where more than one controller uses rumble. */ unsigned rumbleCounter = 0; - while (self.connected) { + while (self.connected && !_forceStopPWMThread) { if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) { break; } @@ -619,6 +620,7 @@ typedef struct __attribute__((packed)) { } [_rumblePWMThreadLock lock]; _rumblePWMThreadRunning = false; + _forceStopPWMThread = false; [_rumblePWMThreadLock unlock]; } @@ -688,6 +690,15 @@ typedef struct __attribute__((packed)) { return _logicallyConnected && _physicallyConnected; } +- (void)_forceStopPWMThread +{ + [_rumblePWMThreadLock lock]; + if (_rumblePWMThreadRunning) { + _forceStopPWMThread = true; + } + [_rumblePWMThreadLock unlock]; +} + + (void)controllerAdded:(IOHIDDeviceRef) device { NSString *name = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));