diff --git a/Cocoa/AppDelegate.m b/Cocoa/AppDelegate.m index bbaa3ae..e966615 100644 --- a/Cocoa/AppDelegate.m +++ b/Cocoa/AppDelegate.m @@ -2,6 +2,7 @@ #include "GBButtons.h" #include #import +#import @implementation AppDelegate { @@ -41,6 +42,11 @@ @"GBCGBModel": @(GB_MODEL_CGB_E), @"GBSGBModel": @(GB_MODEL_SGB2), }]; + + [JOYController startOnRunLoop:[NSRunLoop currentRunLoop] withOptions:@{ + JOYAxes2DEmulateButtonsKey: @YES, + JOYHatsEmulateButtonsKey: @YES, + }]; } - (IBAction)toggleDeveloperMode:(id)sender diff --git a/Cocoa/Document.m b/Cocoa/Document.m index a520d62..eef4c7a 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -74,6 +74,7 @@ enum model { topMargin:(unsigned) topMargin bottomMargin: (unsigned) bottomMargin exposure:(unsigned) exposure; - (void) gotNewSample:(GB_sample_t *)sample; +- (void) rumbleChanged:(double)amp; @end static void vblank(GB_gameboy_t *gb) @@ -131,6 +132,12 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) [self gotNewSample:sample]; } +static void rumbleCallback(GB_gameboy_t *gb, double amp) +{ + Document *self = (__bridge Document *)GB_get_user_data(gb); + [self rumbleChanged:amp]; +} + @implementation Document { GB_gameboy_t gb; @@ -199,6 +206,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) GB_set_highpass_filter_mode(&gb, (GB_highpass_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBHighpassFilter"]); GB_set_rewind_length(&gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRewindLength"]); GB_apu_set_sample_callback(&gb, audioCallback); + GB_set_rumble_callback(&gb, rumbleCallback); } - (void) vblank @@ -244,6 +252,11 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) [audioLock unlock]; } +- (void)rumbleChanged:(double)amp +{ + [_view setRumble:amp]; +} + - (void) run { running = true; @@ -295,6 +308,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) self.audioClient = nil; self.view.mouseHidingEnabled = NO; GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); + [_view setRumble:false]; stopping = false; } @@ -1563,5 +1577,4 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) self.consoleWindow.title = [NSString stringWithFormat:@"Debug Console – %@", [[fileURL path] lastPathComponent]]; } - @end diff --git a/Cocoa/GBButtons.h b/Cocoa/GBButtons.h index 7b2ea5d..1f8b5af 100644 --- a/Cocoa/GBButtons.h +++ b/Cocoa/GBButtons.h @@ -19,6 +19,11 @@ typedef enum : NSUInteger { extern NSString const *GBButtonNames[GBButtonCount]; +static inline NSString *n2s(uint64_t number) +{ + return [NSString stringWithFormat:@"%llx", number]; +} + static inline NSString *button_to_preference_name(GBButton button, unsigned player) { if (player) { diff --git a/Cocoa/GBJoystickListener.h b/Cocoa/GBJoystickListener.h deleted file mode 100644 index 069db10..0000000 --- a/Cocoa/GBJoystickListener.h +++ /dev/null @@ -1,9 +0,0 @@ -#import - -@protocol GBJoystickListener - -- (void) joystick:(NSString *)joystick_name button: (unsigned)button changedState: (bool) state; -- (void) joystick:(NSString *)joystick_name axis: (unsigned)axis movedTo: (signed) value; -- (void) joystick:(NSString *)joystick_name hat: (unsigned)hat changedState: (int8_t) value; - -@end diff --git a/Cocoa/GBPreferencesWindow.h b/Cocoa/GBPreferencesWindow.h index 90eee54..cc308a0 100644 --- a/Cocoa/GBPreferencesWindow.h +++ b/Cocoa/GBPreferencesWindow.h @@ -1,9 +1,10 @@ #import -#import "GBJoystickListener.h" +#import -@interface GBPreferencesWindow : NSWindow +@interface GBPreferencesWindow : NSWindow @property IBOutlet NSTableView *controlsTableView; @property IBOutlet NSPopUpButton *graphicsFilterPopupButton; +@property (strong) IBOutlet NSButton *analogControlsCheckbox; @property (strong) IBOutlet NSButton *aspectRatioCheckbox; @property (strong) IBOutlet NSPopUpButton *highpassFilterPopupButton; @property (strong) IBOutlet NSPopUpButton *colorCorrectionPopupButton; diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index ecf0311..97628f1 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -9,13 +9,14 @@ NSInteger button_being_modified; signed joystick_configuration_state; NSString *joystick_being_configured; - signed last_axis; + bool joypad_wait; NSPopUpButton *_graphicsFilterPopupButton; NSPopUpButton *_highpassFilterPopupButton; NSPopUpButton *_colorCorrectionPopupButton; NSPopUpButton *_rewindPopupButton; NSButton *_aspectRatioCheckbox; + NSButton *_analogControlsCheckbox; NSEventModifierFlags previousModifiers; NSPopUpButton *_dmgPopupButton, *_sgbPopupButton, *_cgbPopupButton; @@ -51,7 +52,7 @@ joystick_configuration_state = -1; [self.configureJoypadButton setEnabled:YES]; [self.skipButton setEnabled:NO]; - [self.configureJoypadButton setTitle:@"Configure Joypad"]; + [self.configureJoypadButton setTitle:@"Configure Controller"]; [super close]; } @@ -184,6 +185,12 @@ [[NSNotificationCenter defaultCenter] postNotificationName:@"GBHighpassFilterChanged" object:nil]; } +- (IBAction)changeAnalogControls:(id)sender +{ + [[NSUserDefaults standardUserDefaults] setBool: [(NSButton *)sender state] == NSOnState + forKey:@"GBAnalogControls"]; +} + - (IBAction)changeAspectRatio:(id)sender { [[NSUserDefaults standardUserDefaults] setBool: [(NSButton *)sender state] != NSOnState @@ -212,7 +219,6 @@ [self.skipButton setEnabled:YES]; joystick_being_configured = nil; [self advanceConfigurationStateMachine]; - last_axis = -1; } - (IBAction) skipButton:(id)sender @@ -223,11 +229,11 @@ - (void) advanceConfigurationStateMachine { joystick_configuration_state++; - if (joystick_configuration_state < GBButtonCount) { - [self.configureJoypadButton setTitle:[NSString stringWithFormat:@"Press Button for %@", GBButtonNames[joystick_configuration_state]]]; + if (joystick_configuration_state == GBUnderclock) { + [self.configureJoypadButton setTitle:@"Press Button for Slo-Mo"]; // Full name is too long :< } - else if (joystick_configuration_state == GBButtonCount) { - [self.configureJoypadButton setTitle:@"Move the Analog Stick"]; + else if (joystick_configuration_state < GBButtonCount) { + [self.configureJoypadButton setTitle:[NSString stringWithFormat:@"Press Button for %@", GBButtonNames[joystick_configuration_state]]]; } else { joystick_configuration_state = -1; @@ -237,112 +243,97 @@ } } -- (void) joystick:(NSString *)joystick_name button: (unsigned)button changedState: (bool) state +- (void)controller:(JOYController *)controller buttonChangedState:(JOYButton *)button { - if (!state) return; + /* Debounce */ + if (joypad_wait) return; + joypad_wait = true; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + joypad_wait = false; + }); + + NSLog(@"%@", button); + + if (!button.isPressed) return; if (joystick_configuration_state == -1) return; if (joystick_configuration_state == GBButtonCount) return; if (!joystick_being_configured) { - joystick_being_configured = joystick_name; + joystick_being_configured = controller.uniqueID; } - else if (![joystick_being_configured isEqualToString:joystick_name]) { + else if (![joystick_being_configured isEqualToString:controller.uniqueID]) { return; } - NSMutableDictionary *all_mappings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"] mutableCopy]; + NSMutableDictionary *instance_mappings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitInstanceMapping"] mutableCopy]; - if (!all_mappings) { - all_mappings = [[NSMutableDictionary alloc] init]; + NSMutableDictionary *name_mappings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitNameMapping"] mutableCopy]; + + + if (!instance_mappings) { + instance_mappings = [[NSMutableDictionary alloc] init]; } - NSMutableDictionary *mapping = [[all_mappings objectForKey:joystick_name] mutableCopy]; + if (!name_mappings) { + name_mappings = [[NSMutableDictionary alloc] init]; + } - if (!mapping) { + NSMutableDictionary *mapping = nil; + if (joystick_configuration_state != 0) { + mapping = [instance_mappings[controller.uniqueID] mutableCopy]; + } + else { mapping = [[NSMutableDictionary alloc] init]; } + - mapping[GBButtonNames[joystick_configuration_state]] = @(button); + static const unsigned gb_to_joykit[] = { + [GBRight]=JOYButtonUsageDPadRight, + [GBLeft]=JOYButtonUsageDPadLeft, + [GBUp]=JOYButtonUsageDPadUp, + [GBDown]=JOYButtonUsageDPadDown, + [GBA]=JOYButtonUsageA, + [GBB]=JOYButtonUsageB, + [GBSelect]=JOYButtonUsageSelect, + [GBStart]=JOYButtonUsageStart, + [GBTurbo]=JOYButtonUsageL1, + [GBRewind]=JOYButtonUsageL2, + [GBUnderclock]=JOYButtonUsageR1, + }; - all_mappings[joystick_name] = mapping; - [[NSUserDefaults standardUserDefaults] setObject:all_mappings forKey:@"GBJoypadMappings"]; - [self refreshJoypadMenu:nil]; + if (joystick_configuration_state == GBUnderclock) { + for (JOYAxis *axis in controller.axes) { + if (axis.value > 0.5) { + mapping[@"AnalogUnderclock"] = @(axis.uniqueID); + } + } + } + + if (joystick_configuration_state == GBTurbo) { + for (JOYAxis *axis in controller.axes) { + if (axis.value > 0.5) { + mapping[@"AnalogTurbo"] = @(axis.uniqueID); + } + } + } + + mapping[n2s(button.uniqueID)] = @(gb_to_joykit[joystick_configuration_state]); + + instance_mappings[controller.uniqueID] = mapping; + name_mappings[controller.deviceName] = mapping; + [[NSUserDefaults standardUserDefaults] setObject:instance_mappings forKey:@"JoyKitInstanceMapping"]; + [[NSUserDefaults standardUserDefaults] setObject:name_mappings forKey:@"JoyKitNameMapping"]; [self advanceConfigurationStateMachine]; } -- (void) joystick:(NSString *)joystick_name axis: (unsigned)axis movedTo: (signed) value +- (NSButton *)analogControlsCheckbox { - if (abs(value) < 0x4000) return; - if (joystick_configuration_state != GBButtonCount) return; - if (!joystick_being_configured) { - joystick_being_configured = joystick_name; - } - else if (![joystick_being_configured isEqualToString:joystick_name]) { - return; - } - - if (last_axis == -1) { - last_axis = axis; - return; - } - - if (axis == last_axis) { - return; - } - - NSMutableDictionary *all_mappings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"] mutableCopy]; - - if (!all_mappings) { - all_mappings = [[NSMutableDictionary alloc] init]; - } - - NSMutableDictionary *mapping = [[all_mappings objectForKey:joystick_name] mutableCopy]; - - if (!mapping) { - mapping = [[NSMutableDictionary alloc] init]; - } - - mapping[@"XAxis"] = @(MIN(axis, last_axis)); - mapping[@"YAxis"] = @(MAX(axis, last_axis)); - - all_mappings[joystick_name] = mapping; - [[NSUserDefaults standardUserDefaults] setObject:all_mappings forKey:@"GBJoypadMappings"]; - [self advanceConfigurationStateMachine]; + return _analogControlsCheckbox; } -- (void) joystick:(NSString *)joystick_name hat: (unsigned)hat changedState: (int8_t) state +- (void)setAnalogControlsCheckbox:(NSButton *)analogControlsCheckbox { - /* Hats are always mapped to the D-pad, ignore them on non-Dpad keys and skip the D-pad configuration if used*/ - if (!state) return; - if (joystick_configuration_state == -1) return; - if (joystick_configuration_state > GBDown) return; - if (!joystick_being_configured) { - joystick_being_configured = joystick_name; - } - else if (![joystick_being_configured isEqualToString:joystick_name]) { - return; - } - - NSMutableDictionary *all_mappings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"] mutableCopy]; - - if (!all_mappings) { - all_mappings = [[NSMutableDictionary alloc] init]; - } - - NSMutableDictionary *mapping = [[all_mappings objectForKey:joystick_name] mutableCopy]; - - if (!mapping) { - mapping = [[NSMutableDictionary alloc] init]; - } - - for (joystick_configuration_state = 0;; joystick_configuration_state++) { - [mapping removeObjectForKey:GBButtonNames[joystick_configuration_state]]; - if (joystick_configuration_state == GBDown) break; - } - - all_mappings[joystick_name] = mapping; - [[NSUserDefaults standardUserDefaults] setObject:all_mappings forKey:@"GBJoypadMappings"]; - [self refreshJoypadMenu:nil]; - [self advanceConfigurationStateMachine]; + _analogControlsCheckbox = analogControlsCheckbox; + [_analogControlsCheckbox setState: [[NSUserDefaults standardUserDefaults] boolForKey:@"GBAnalogControls"]]; } - (NSButton *)aspectRatioCheckbox @@ -361,10 +352,13 @@ [super awakeFromNib]; [self updateBootROMFolderButton]; [[NSDistributedNotificationCenter defaultCenter] addObserver:self.controlsTableView selector:@selector(reloadData) name:(NSString*)kTISNotifySelectedKeyboardInputSourceChanged object:nil]; + [JOYController registerListener:self]; + joystick_configuration_state = -1; } - (void)dealloc { + [JOYController unregisterListener:self]; [[NSDistributedNotificationCenter defaultCenter] removeObserver:self.controlsTableView]; } @@ -483,21 +477,47 @@ return _preferredJoypadButton; } +- (void)controllerConnected:(JOYController *)controller +{ + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self refreshJoypadMenu:nil]; + }); +} + +- (void)controllerDisconnected:(JOYController *)controller +{ + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self refreshJoypadMenu:nil]; + }); +} + - (IBAction)refreshJoypadMenu:(id)sender { - NSArray *joypads = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"] allKeys]; - for (NSString *joypad in joypads) { - if ([self.preferredJoypadButton indexOfItemWithTitle:joypad] == -1) { - [self.preferredJoypadButton addItemWithTitle:joypad]; + bool preferred_is_connected = false; + NSString *player_string = n2s(self.playerListButton.selectedTag); + NSString *selected_controller = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitDefaultControllers"][player_string]; + + [self.preferredJoypadButton removeAllItems]; + [self.preferredJoypadButton addItemWithTitle:@"None"]; + for (JOYController *controller in [JOYController allControllers]) { + [self.preferredJoypadButton addItemWithTitle:[NSString stringWithFormat:@"%@ (%@)", controller.deviceName, controller.uniqueID]]; + + self.preferredJoypadButton.lastItem.identifier = controller.uniqueID; + + if ([controller.uniqueID isEqualToString:selected_controller]) { + preferred_is_connected = true; + [self.preferredJoypadButton selectItem:self.preferredJoypadButton.lastItem]; } } - NSString *player_string = [NSString stringWithFormat: @"%ld", (long)self.playerListButton.selectedTag]; - NSString *selected_joypad = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBDefaultJoypads"][player_string]; - if (selected_joypad && [self.preferredJoypadButton indexOfItemWithTitle:selected_joypad] != -1) { - [self.preferredJoypadButton selectItemWithTitle:selected_joypad]; + if (!preferred_is_connected && selected_controller) { + [self.preferredJoypadButton addItemWithTitle:[NSString stringWithFormat:@"Unavailable Controller (%@)", selected_controller]]; + self.preferredJoypadButton.lastItem.identifier = selected_controller; + [self.preferredJoypadButton selectItem:self.preferredJoypadButton.lastItem]; } - else { + + + if (!selected_controller) { [self.preferredJoypadButton selectItemWithTitle:@"None"]; } [self.controlsTableView reloadData]; @@ -505,18 +525,18 @@ - (IBAction)changeDefaultJoypad:(id)sender { - NSMutableDictionary *default_joypads = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBDefaultJoypads"] mutableCopy]; + NSMutableDictionary *default_joypads = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitDefaultControllers"] mutableCopy]; if (!default_joypads) { default_joypads = [[NSMutableDictionary alloc] init]; } - NSString *player_string = [NSString stringWithFormat: @"%ld", self.playerListButton.selectedTag]; + NSString *player_string = n2s(self.playerListButton.selectedTag); if ([[sender titleOfSelectedItem] isEqualToString:@"None"]) { [default_joypads removeObjectForKey:player_string]; } else { - default_joypads[player_string] = [sender titleOfSelectedItem]; + default_joypads[player_string] = [[sender selectedItem] identifier]; } - [[NSUserDefaults standardUserDefaults] setObject:default_joypads forKey:@"GBDefaultJoypads"]; + [[NSUserDefaults standardUserDefaults] setObject:default_joypads forKey:@"JoyKitDefaultControllers"]; } @end diff --git a/Cocoa/GBView.h b/Cocoa/GBView.h index f4c5e44..20bc7bf 100644 --- a/Cocoa/GBView.h +++ b/Cocoa/GBView.h @@ -1,8 +1,8 @@ #import #include -#import "GBJoystickListener.h" +#import -@interface GBView : NSView +@interface GBView : NSView - (void) flip; - (uint32_t *) pixels; @property GB_gameboy_t *gb; @@ -14,4 +14,5 @@ - (uint32_t *)currentBuffer; - (uint32_t *)previousBuffer; - (void)screenSizeChanged; +- (void)setRumble: (bool)on; @end diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 5a851f3..adc0781 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -1,4 +1,4 @@ -#import +#import #import "GBView.h" #import "GBViewGL.h" #import "GBViewMetal.h" @@ -18,7 +18,10 @@ bool axisActive[2]; bool underclockKeyDown; double clockMultiplier; + double analogClockMultiplier; + bool analogClockMultiplierValid; NSEventModifierFlags previousModifiers; + JOYController *lastController; } + (instancetype)alloc @@ -55,6 +58,7 @@ [self createInternalView]; [self addSubview:self.internalView]; self.internalView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable; + [JOYController registerListener:self]; } - (void)screenSizeChanged @@ -100,6 +104,8 @@ [NSCursor unhide]; } [[NSNotificationCenter defaultCenter] removeObserver:self]; + [lastController setRumbleAmplitude:0]; + [JOYController unregisterListener:self]; } - (instancetype)initWithCoder:(NSCoder *)coder { @@ -147,13 +153,21 @@ - (void) flip { - if (underclockKeyDown && clockMultiplier > 0.5) { - clockMultiplier -= 1.0/16; - GB_set_clock_multiplier(_gb, clockMultiplier); + if (analogClockMultiplierValid && [[NSUserDefaults standardUserDefaults] boolForKey:@"GBAnalogControls"]) { + GB_set_clock_multiplier(_gb, analogClockMultiplier); + if (analogClockMultiplier == 1.0) { + analogClockMultiplierValid = false; + } } - if (!underclockKeyDown && clockMultiplier < 1.0) { - clockMultiplier += 1.0/16; - GB_set_clock_multiplier(_gb, clockMultiplier); + else { + if (underclockKeyDown && clockMultiplier > 0.5) { + clockMultiplier -= 1.0/16; + GB_set_clock_multiplier(_gb, clockMultiplier); + } + if (!underclockKeyDown && clockMultiplier < 1.0) { + clockMultiplier += 1.0/16; + GB_set_clock_multiplier(_gb, clockMultiplier); + } } current_buffer = (current_buffer + 1) % self.numberOfBuffers; } @@ -180,6 +194,7 @@ switch (button) { case GBTurbo: GB_set_turbo_mode(_gb, true, self.isRewinding); + analogClockMultiplierValid = false; break; case GBRewind: @@ -189,6 +204,7 @@ case GBUnderclock: underclockKeyDown = true; + analogClockMultiplierValid = false; break; default: @@ -221,6 +237,7 @@ switch (button) { case GBTurbo: GB_set_turbo_mode(_gb, false, false); + analogClockMultiplierValid = false; break; case GBRewind: @@ -229,6 +246,7 @@ case GBUnderclock: underclockKeyDown = false; + analogClockMultiplierValid = false; break; default: @@ -243,123 +261,97 @@ } } -- (void) joystick:(NSString *)joystick_name button: (unsigned)button changedState: (bool) state +- (void)setRumble:(bool)on { - unsigned player_count = GB_get_player_count(_gb); - - UpdateSystemActivity(UsrActivity); - for (unsigned player = 0; player < player_count; player++) { - NSString *preferred_joypad = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBDefaultJoypads"] - objectForKey:[NSString stringWithFormat:@"%u", player]]; - if (player_count != 1 && // Single player, accpet inputs from all joypads - !(player == 0 && !preferred_joypad) && // Multiplayer, but player 1 has no joypad configured, so it takes inputs from all joypads - ![preferred_joypad isEqualToString:joystick_name]) { - continue; - } - NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"][joystick_name]; - - for (GBButton i = 0; i < GBButtonCount; i++) { - NSNumber *mapped_button = [mapping objectForKey:GBButtonNames[i]]; - if (mapped_button && [mapped_button integerValue] == button) { - switch (i) { - case GBTurbo: - GB_set_turbo_mode(_gb, state, state && self.isRewinding); - break; - - case GBRewind: - self.isRewinding = state; - if (state) { - GB_set_turbo_mode(_gb, false, false); - } - break; - - case GBUnderclock: - underclockKeyDown = state; - break; - - default: - GB_set_key_state_for_player(_gb, (GB_key_t)i, player, state); - break; - } - } - } - } + [lastController setRumbleAmplitude:(double)on]; } -- (void) joystick:(NSString *)joystick_name axis: (unsigned)axis movedTo: (signed) value +- (void)controller:(JOYController *)controller movedAxis:(JOYAxis *)axis { - unsigned player_count = GB_get_player_count(_gb); + if (![self.window isMainWindow]) return; - UpdateSystemActivity(UsrActivity); - for (unsigned player = 0; player < player_count; player++) { - NSString *preferred_joypad = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBDefaultJoypads"] - objectForKey:[NSString stringWithFormat:@"%u", player]]; - if (player_count != 1 && // Single player, accpet inputs from all joypads - !(player == 0 && !preferred_joypad) && // Multiplayer, but player 1 has no joypad configured, so it takes inputs from all joypads - ![preferred_joypad isEqualToString:joystick_name]) { - continue; - } - - NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"][joystick_name]; - NSNumber *x_axis = [mapping objectForKey:@"XAxis"]; - NSNumber *y_axis = [mapping objectForKey:@"YAxis"]; - - if (axis == [x_axis integerValue]) { - if (value > JOYSTICK_HIGH) { - axisActive[0] = true; - GB_set_key_state_for_player(_gb, GB_KEY_RIGHT, player, true); - GB_set_key_state_for_player(_gb, GB_KEY_LEFT, player, false); - } - else if (value < -JOYSTICK_HIGH) { - axisActive[0] = true; - GB_set_key_state_for_player(_gb, GB_KEY_RIGHT, player, false); - GB_set_key_state_for_player(_gb, GB_KEY_LEFT, player, true); - } - else if (axisActive[0] && value < JOYSTICK_LOW && value > -JOYSTICK_LOW) { - axisActive[0] = false; - GB_set_key_state_for_player(_gb, GB_KEY_RIGHT, player, false); - GB_set_key_state_for_player(_gb, GB_KEY_LEFT, player, false); - } - } - else if (axis == [y_axis integerValue]) { - if (value > JOYSTICK_HIGH) { - axisActive[1] = true; - GB_set_key_state_for_player(_gb, GB_KEY_DOWN, player, true); - GB_set_key_state_for_player(_gb, GB_KEY_UP, player, false); - } - else if (value < -JOYSTICK_HIGH) { - axisActive[1] = true; - GB_set_key_state_for_player(_gb, GB_KEY_DOWN, player, false); - GB_set_key_state_for_player(_gb, GB_KEY_UP, player, true); - } - else if (axisActive[1] && value < JOYSTICK_LOW && value > -JOYSTICK_LOW) { - axisActive[1] = false; - GB_set_key_state_for_player(_gb, GB_KEY_DOWN, player, false); - GB_set_key_state_for_player(_gb, GB_KEY_UP, player, false); - } - } + NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitInstanceMapping"][controller.uniqueID]; + if (!mapping) { + mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitNameMapping"][controller.deviceName]; } -} - -- (void) joystick:(NSString *)joystick_name hat: (unsigned)hat changedState: (int8_t) state -{ - unsigned player_count = GB_get_player_count(_gb); - UpdateSystemActivity(UsrActivity); + if ((axis.usage == JOYAxisUsageR1 && !mapping) || + axis.uniqueID == [mapping[@"AnalogUnderclock"] unsignedLongValue]){ + analogClockMultiplier = MIN(MAX(1 - axis.value + 0.2, 1.0 / 3), 1.0); + analogClockMultiplierValid = true; + } + + else if ((axis.usage == JOYAxisUsageL1 && !mapping) || + axis.uniqueID == [mapping[@"AnalogTurbo"] unsignedLongValue]){ + analogClockMultiplier = MIN(MAX(axis.value * 3 + 0.8, 1.0), 3.0); + analogClockMultiplierValid = true; + } +} + +- (void)controller:(JOYController *)controller buttonChangedState:(JOYButton *)button +{ + if (![self.window isMainWindow]) return; + if (controller != lastController) { + [lastController setRumbleAmplitude:0]; + lastController = controller; + } + + + unsigned player_count = GB_get_player_count(_gb); + + IOPMAssertionID assertionID; + IOPMAssertionDeclareUserActivity(CFSTR(""), kIOPMUserActiveLocal, &assertionID); + for (unsigned player = 0; player < player_count; player++) { - NSString *preferred_joypad = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBDefaultJoypads"] - objectForKey:[NSString stringWithFormat:@"%u", player]]; + NSString *preferred_joypad = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitDefaultControllers"] + objectForKey:n2s(player)]; if (player_count != 1 && // Single player, accpet inputs from all joypads !(player == 0 && !preferred_joypad) && // Multiplayer, but player 1 has no joypad configured, so it takes inputs from all joypads - ![preferred_joypad isEqualToString:joystick_name]) { + ![preferred_joypad isEqualToString:controller.uniqueID]) { continue; } - assert(state + 1 < 9); - /* - N NE E SE S SW W NW */ - GB_set_key_state_for_player(_gb, GB_KEY_UP, player, (bool []){0, 1, 1, 0, 0, 0, 0, 0, 1}[state + 1]); - GB_set_key_state_for_player(_gb, GB_KEY_RIGHT, player, (bool []){0, 0, 1, 1, 1, 0, 0, 0, 0}[state + 1]); - GB_set_key_state_for_player(_gb, GB_KEY_DOWN, player, (bool []){0, 0, 0, 0, 1, 1, 1, 0, 0}[state + 1]); - GB_set_key_state_for_player(_gb, GB_KEY_LEFT, player, (bool []){0, 0, 0, 0, 0, 0, 1, 1, 1}[state + 1]); + [controller setPlayerLEDs:1 << player]; + NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitInstanceMapping"][controller.uniqueID]; + if (!mapping) { + mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitNameMapping"][controller.deviceName]; + } + + JOYButtonUsage usage = ((JOYButtonUsage)[mapping[n2s(button.uniqueID)] unsignedIntValue]) ?: button.usage; + if (!mapping && usage >= JOYButtonUsageGeneric0) { + usage = (const JOYButtonUsage[]){JOYButtonUsageY, JOYButtonUsageA, JOYButtonUsageB, JOYButtonUsageX}[(usage - JOYButtonUsageGeneric0) & 3]; + } + + switch (usage) { + + case JOYButtonUsageNone: break; + case JOYButtonUsageA: GB_set_key_state_for_player(_gb, GB_KEY_A, player, button.isPressed); break; + case JOYButtonUsageB: GB_set_key_state_for_player(_gb, GB_KEY_B, player, button.isPressed); break; + case JOYButtonUsageC: break; + case JOYButtonUsageStart: + case JOYButtonUsageX: GB_set_key_state_for_player(_gb, GB_KEY_START, player, button.isPressed); break; + case JOYButtonUsageSelect: + case JOYButtonUsageY: GB_set_key_state_for_player(_gb, GB_KEY_SELECT, player, button.isPressed); break; + case JOYButtonUsageR2: + case JOYButtonUsageL2: + case JOYButtonUsageZ: { + self.isRewinding = button.isPressed; + if (button.isPressed) { + GB_set_turbo_mode(_gb, false, false); + } + break; + } + + case JOYButtonUsageL1: GB_set_turbo_mode(_gb, button.isPressed, button.isPressed && self.isRewinding); break; + + case JOYButtonUsageR1: underclockKeyDown = button.isPressed; break; + case JOYButtonUsageDPadLeft: GB_set_key_state_for_player(_gb, GB_KEY_LEFT, player, button.isPressed); break; + case JOYButtonUsageDPadRight: GB_set_key_state_for_player(_gb, GB_KEY_RIGHT, player, button.isPressed); break; + case JOYButtonUsageDPadUp: GB_set_key_state_for_player(_gb, GB_KEY_UP, player, button.isPressed); break; + case JOYButtonUsageDPadDown: GB_set_key_state_for_player(_gb, GB_KEY_DOWN, player, button.isPressed); break; + + default: + break; + } } } diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 8278ee1..f9dd9c0 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -1,8 +1,8 @@ - + - + @@ -17,7 +17,7 @@ - + @@ -58,6 +58,7 @@ + @@ -369,22 +370,11 @@ - + - - + @@ -393,7 +383,7 @@ - + @@ -441,28 +431,28 @@ -