Skip to content

Commit

Permalink
[macOS] Remap Switch controller buttons.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruvzg committed Feb 29, 2024
1 parent df78c06 commit afa400f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
2 changes: 2 additions & 0 deletions platform/macos/joypad_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
65 changes: 57 additions & 8 deletions platform/macos/joypad_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit afa400f

Please sign in to comment.