Various hacks for stopping the PWM thread when needed, important if we have a WUP-028 connected with more than one controller

This commit is contained in:
Lior Halphon 2020-05-01 18:16:33 +03:00
parent 4bf252800e
commit 021cdb402d
4 changed files with 16 additions and 2 deletions

View File

@ -381,7 +381,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp)
self.view.mouseHidingEnabled = NO; self.view.mouseHidingEnabled = NO;
GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]);
GB_save_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]); GB_save_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]);
[_view setRumble:false]; [_view setRumble:0];
stopping = false; stopping = false;
} }

View File

@ -116,6 +116,7 @@
} }
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
[lastController setRumbleAmplitude:0]; [lastController setRumbleAmplitude:0];
[lastController _forceStopPWMThread];
[JOYController unregisterListener:self]; [JOYController unregisterListener:self];
} }
- (instancetype)initWithCoder:(NSCoder *)coder - (instancetype)initWithCoder:(NSCoder *)coder
@ -302,6 +303,7 @@
if (![self.window isMainWindow]) return; if (![self.window isMainWindow]) return;
if (controller != lastController) { if (controller != lastController) {
[lastController setRumbleAmplitude:0]; [lastController setRumbleAmplitude:0];
[lastController _forceStopPWMThread];
lastController = controller; lastController = controller;
} }

View File

@ -35,6 +35,7 @@ static NSString const *JOYHatsEmulateButtonsKey = @"JOYHatsEmulateButtons";
- (NSArray<JOYHat *> *) hats; - (NSArray<JOYHat *> *) hats;
- (void)setRumbleAmplitude:(double)amp; - (void)setRumbleAmplitude:(double)amp;
- (void)setPlayerLEDs:(uint8_t)mask; - (void)setPlayerLEDs:(uint8_t)mask;
- (void)_forceStopPWMThread; // Hack
@property (readonly, getter=isConnected) bool connected; @property (readonly, getter=isConnected) bool connected;
@end @end

View File

@ -118,6 +118,7 @@ typedef struct __attribute__((packed)) {
bool _physicallyConnected; bool _physicallyConnected;
bool _logicallyConnected; bool _logicallyConnected;
bool _rumblePWMThreadRunning; bool _rumblePWMThreadRunning;
volatile bool _forceStopPWMThread;
} }
- (instancetype)initWithDevice:(IOHIDDeviceRef) device - (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 /* TODO: This does not handle correctly the case of having a multi-port controller where more than one controller
uses rumble. */ uses rumble. */
unsigned rumbleCounter = 0; unsigned rumbleCounter = 0;
while (self.connected) { while (self.connected && !_forceStopPWMThread) {
if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) { if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) {
break; break;
} }
@ -619,6 +620,7 @@ typedef struct __attribute__((packed)) {
} }
[_rumblePWMThreadLock lock]; [_rumblePWMThreadLock lock];
_rumblePWMThreadRunning = false; _rumblePWMThreadRunning = false;
_forceStopPWMThread = false;
[_rumblePWMThreadLock unlock]; [_rumblePWMThreadLock unlock];
} }
@ -688,6 +690,15 @@ typedef struct __attribute__((packed)) {
return _logicallyConnected && _physicallyConnected; return _logicallyConnected && _physicallyConnected;
} }
- (void)_forceStopPWMThread
{
[_rumblePWMThreadLock lock];
if (_rumblePWMThreadRunning) {
_forceStopPWMThread = true;
}
[_rumblePWMThreadLock unlock];
}
+ (void)controllerAdded:(IOHIDDeviceRef) device + (void)controllerAdded:(IOHIDDeviceRef) device
{ {
NSString *name = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)); NSString *name = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));