Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[macOS] Remap Switch controller buttons. #89001

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading