From afa400f2821a875f1cc350f2ded553fdaeb107c1 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Thu, 29 Feb 2024 11:25:45 +0200 Subject: [PATCH] [macOS] Remap Switch controller buttons. --- platform/macos/joypad_macos.h | 2 ++ platform/macos/joypad_macos.mm | 65 +++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/platform/macos/joypad_macos.h b/platform/macos/joypad_macos.h index b37a1b24f3fa..4b2c9ec53583 100644 --- a/platform/macos/joypad_macos.h +++ b/platform/macos/joypad_macos.h @@ -61,6 +61,8 @@ API_AVAILABLE(macosx(11)) // however haptics (vibrations) are only supported in macOS 11+. @interface Joypad : NSObject +@property(assign, nonatomic) BOOL switch_joycon; +@property(assign, nonatomic) BOOL switch_pro; @property(assign, nonatomic) BOOL force_feedback; @property(assign, nonatomic) NSInteger ff_effect_timestamp; @property(strong, nonatomic) GCController *controller; diff --git a/platform/macos/joypad_macos.mm b/platform/macos/joypad_macos.mm index 2420bd73fb0d..1e70215d6654 100644 --- a/platform/macos/joypad_macos.mm +++ b/platform/macos/joypad_macos.mm @@ -127,6 +127,13 @@ - (instancetype)init:(GCController *)controller { } self.ff_effect_timestamp = 0; + if (@available(macOS 10.15, *)) { + self.switch_joycon = (controller != nil) ? ([controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (L)"] || [controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (R)"]) : NO; + self.switch_pro = (controller != nil) ? ([controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (L/R)"] || [controller.productCategory isEqualToString:@"Switch Pro Controller"]) : NO; + } else { + self.switch_joycon = NO; + self.switch_pro = NO; + } return self; } @@ -311,6 +318,19 @@ - (void)dealloc { return final_keys; } +- (Joypad *)getJoypadForController:(GCController *)controller { + NSArray *keys = [self.connectedJoypads allKeys]; + + for (NSNumber *key in keys) { + Joypad *joypad = [self.connectedJoypads objectForKey:key]; + if (joypad.controller == controller) { + return joypad; + } + } + + return nullptr; +} + - (int)getJoyIdForController:(GCController *)controller { NSArray *keys = [self getAllKeysForController:controller]; @@ -439,19 +459,48 @@ - (void)setControllerInputHandler:(GCController *)controller { _strongify(controller); int joy_id = [self getJoyIdForController:controller]; + Joypad *joypad = [self getJoypadForController:controller]; + bool switch_joycon = (joypad != nil) ? joypad.switch_joycon : false; + bool switch_pro = (joypad != nil) ? joypad.switch_pro : false; if (element == gamepad.buttonA) { - Input::get_singleton()->joy_button(joy_id, JoyButton::A, - gamepad.buttonA.isPressed); + if (switch_pro) { + Input::get_singleton()->joy_button(joy_id, JoyButton::B, + gamepad.buttonA.isPressed); + } else { + Input::get_singleton()->joy_button(joy_id, JoyButton::A, + gamepad.buttonA.isPressed); + } } else if (element == gamepad.buttonB) { - Input::get_singleton()->joy_button(joy_id, JoyButton::B, - gamepad.buttonB.isPressed); + if (switch_pro) { + Input::get_singleton()->joy_button(joy_id, JoyButton::A, + gamepad.buttonB.isPressed); + } else if (switch_joycon) { + Input::get_singleton()->joy_button(joy_id, JoyButton::X, + gamepad.buttonB.isPressed); + } else { + Input::get_singleton()->joy_button(joy_id, JoyButton::B, + gamepad.buttonB.isPressed); + } } else if (element == gamepad.buttonX) { - Input::get_singleton()->joy_button(joy_id, JoyButton::X, - gamepad.buttonX.isPressed); + if (switch_pro) { + Input::get_singleton()->joy_button(joy_id, JoyButton::B, + gamepad.buttonX.isPressed); + } else if (switch_joycon) { + Input::get_singleton()->joy_button(joy_id, JoyButton::Y, + gamepad.buttonX.isPressed); + } else { + Input::get_singleton()->joy_button(joy_id, JoyButton::X, + gamepad.buttonX.isPressed); + } } else if (element == gamepad.buttonY) { - Input::get_singleton()->joy_button(joy_id, JoyButton::Y, - gamepad.buttonY.isPressed); + if (switch_pro) { + Input::get_singleton()->joy_button(joy_id, JoyButton::X, + gamepad.buttonY.isPressed); + } else { + Input::get_singleton()->joy_button(joy_id, JoyButton::Y, + gamepad.buttonY.isPressed); + } } else if (element == gamepad.leftShoulder) { Input::get_singleton()->joy_button(joy_id, JoyButton::LEFT_SHOULDER, gamepad.leftShoulder.isPressed);