Skip to content

Commit 90eca2f

Browse files
seeddspeterkmet
andcommitted
Controllers: a Nintendo pad's face buttons by their labels on the Mac and iOS (PR #650)
GameController names a Switch Pro Controller's and Joy-Con's face buttons by their printed labels, every other pad's by position, so the host's position mapping swapped A/B and X/Y on Nintendo pads. pad_apple.h holds the rule for both front ends (dm_pad_face), with a test; a single Joy-Con held sideways is matched too and is untested (open, in the header). Co-authored-by: Peter Kmet <[email protected]>
1 parent 85578fc commit 90eca2f

8 files changed

Lines changed: 78 additions & 8 deletions

File tree

‎dm/host/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ endif
5050
endif
5151
.PHONY: FORCE
5252
FORCE:
53-
dmhost: $(SRC) $(GFX) ../third_party/stb_vorbis/stb_vorbis.c dm.h hle.h romfs_image.h save_sync.h dm_input.h pad_slots.h hle_haptics.h haptics_apple.h motion_fusion.h motion_apple.h motion_pro.h motion_pro_mac.h front.h $(GFX_H) $(GAME_H) $(GAME_STAMP) $(VPX_LIB)
53+
dmhost: $(SRC) $(GFX) ../third_party/stb_vorbis/stb_vorbis.c dm.h hle.h romfs_image.h save_sync.h dm_input.h pad_slots.h hle_haptics.h haptics_apple.h motion_fusion.h motion_apple.h motion_pro.h motion_pro_mac.h pad_apple.h front.h $(GFX_H) $(GAME_H) $(GAME_STAMP) $(VPX_LIB)
5454
clang $(CFLAGS) -I../gfx/nvn/include -I../third_party/vulkan-headers/include -I$(dir $(GAME_H)) -I$(VPX) -I../third_party/stb_vorbis -fobjc-arc -o $@ $(SRC) $(GFX) $(VPX_LIB) -framework Cocoa -framework QuartzCore -framework Metal -framework MetalFX -framework GameController -framework CoreHaptics -framework IOKit -framework AudioToolbox -framework VideoToolbox -framework CoreMedia -framework CoreVideo -framework ImageIO -framework UniformTypeIdentifiers
5555
clean: # the host and everything make generates for it (gen/: the profile headers, the game stamp and the libvpx build; dmhost_* and *.dSYM beside them, as in .gitignore)
5656
rm -rf gen dmhost dmhost_* *.dSYM

‎dm/host/front_mac.m‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Without DM_WINDOW: the guest runs directly on the current thread, behaving the same as with no front end (windowless tests use this).
33
// The window is fullscreen-capable (the green button and ⌃⌘F); DM_FULLSCREEN=1 starts it that way. ⌘Q quits; the left mouse button is the touchscreen.
44
// Key map (Nintendo layout): WASD left stick, arrow keys D-pad, J=A K=B U=X I=Y, Q=L E=R, Z=ZL C=ZR, F=press left stick G=press right stick, Return=+ Tab=−, Space=shake the controller.
5-
// Controllers: buttons by position as on the iPhone (right A, bottom B, top X, left Y), shoulders L R, triggers ZL ZR, Menu + Options −, both sticks and their presses.
5+
// Controllers: face buttons as on the iPhone, by position (right A, bottom B, top X, left Y) or on a Nintendo pad by its labels (pad_apple.h), shoulders L R, triggers ZL ZR, Menu + Options −, both sticks and their presses.
66
// Each controller is a player of its own (local co-op): the first one connected is player 1, together with the keyboard, the next player 2, and so on up to 8 (pad_slots.h);
77
// DM_PAD_MERGE=1 merges every controller into player 1 instead, DM_KBD_PLAYER=own makes the keyboard player 1 on its own, with the controllers from player 2 on.
88
#import <Cocoa/Cocoa.h>
@@ -17,6 +17,7 @@
1717
#include "haptics_apple.h"
1818
#include "motion_apple.h"
1919
#include "motion_pro_mac.h"
20+
#include "pad_apple.h"
2021
#include <signal.h>
2122
#include <math.h>
2223

@@ -123,9 +124,8 @@ static void pad_read(GCExtendedGamepad *g) {
123124
if (!g_input_on) return;
124125
int p = g_merge ? 0 : pad_slot_of(&g_slots, (__bridge const void *)g.controller); if (p < 0) return;
125126
dm_haptics_apple_controller(p, g.controller);
126-
uint64_t m = 0;
127+
uint64_t m = dm_pad_face(g); // by position, a Nintendo pad by its labels
127128
#define K(btn, bit) if (btn.isPressed) m |= bit
128-
K(g.buttonB, DM_BTN_A); K(g.buttonA, DM_BTN_B); K(g.buttonY, DM_BTN_X); K(g.buttonX, DM_BTN_Y); // by position: right A, bottom B, top X, left Y
129129
K(g.leftShoulder, DM_BTN_L); K(g.rightShoulder, DM_BTN_R); K(g.leftTrigger, DM_BTN_ZL); K(g.rightTrigger, DM_BTN_ZR);
130130
K(g.buttonMenu, DM_BTN_PLUS); K(g.buttonOptions, DM_BTN_MINUS); K(g.leftThumbstickButton, DM_BTN_LS); K(g.rightThumbstickButton, DM_BTN_RS); // nil on pads without them: not pressed
131131
K(g.dpad.left, DM_BTN_LEFT); K(g.dpad.up, DM_BTN_UP); K(g.dpad.right, DM_BTN_RIGHT); K(g.dpad.down, DM_BTN_DOWN);

‎dm/host/pad_apple.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// A physical controller's face buttons as Switch buttons, shared by the Mac front end (front_mac.m) and the iOS gamepad (DmGamepad.m). GameController names most pads' face
2+
// buttons by position, as on an Xbox pad (buttonA bottom, buttonB right, buttonX left, buttonY top), but a Nintendo pad's by their printed labels (buttonA is the right
3+
// button, marked A): a Switch Pro Controller (or a pad that presents itself as one) and Joy-Con (productCategory "Nintendo Switch Joy-Con (L)", "(R)", "(L/R)").
4+
// The Switch's layout is right A, bottom B, top X, left Y, so a Nintendo pad maps name for name and any other pad by position. Checked with a Pro Controller on the Mac
5+
// and on iOS (PR #650). Open: a single Joy-Con held sideways is matched too and has not been tried; if GameController keeps its labels there instead of turning them
6+
// with the controller, its face buttons come out a quarter turn off.
7+
#pragma once
8+
#import <GameController/GameController.h>
9+
#include <stdint.h>
10+
#include "dm_input.h"
11+
12+
static inline int dm_pad_labelled(GCController *c) {
13+
NSString *k = c.productCategory;
14+
return [k isEqualToString:@"Switch Pro Controller"] || [k containsString:@"Joy-Con"];
15+
}
16+
// DM_BTN_A / B / X / Y of the pad's pressed face buttons
17+
static inline uint64_t dm_pad_face(GCExtendedGamepad *g) {
18+
GCControllerButtonInput *a = g.buttonA, *b = g.buttonB, *x = g.buttonX, *y = g.buttonY;
19+
if (!dm_pad_labelled(g.controller)) { GCControllerButtonInput *t = a; a = b; b = t; t = x; x = y; y = t; } // by position: right A, bottom B, top X, left Y
20+
return (a.isPressed ? DM_BTN_A : 0) | (b.isPressed ? DM_BTN_B : 0) | (x.isPressed ? DM_BTN_X : 0) | (y.isPressed ? DM_BTN_Y : 0);
21+
}

‎dm/host/tests/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ run: $(B)/t_language $(B)/t_motion $(B)/t_heap_size $(B)/t_touch $(B)/t_gesture
4444
$(B)/t_movie_vpx
4545
$(B)/t_motion
4646
$(B)/t_motion_apple
47+
$(B)/t_pad_apple
4748
$(B)/t_motion_pro
4849
$(B)/t_motion_pro_mac
4950
TMPDIR="$(abspath $(B))" $(B)/t_hid
@@ -145,6 +146,9 @@ $(B)/t_motion: t_motion.c ../motion_fusion.h | $(B)
145146
run: $(B)/t_motion_apple
146147
$(B)/t_motion_apple: t_motion_apple.m ../motion_apple.h ../dm_input.h | $(B)
147148
$(CLANG) -O1 -g -fobjc-arc -Wall -Werror -I.. -o $@ t_motion_apple.m -framework Foundation -framework GameController
149+
run: $(B)/t_pad_apple
150+
$(B)/t_pad_apple: t_pad_apple.m ../pad_apple.h ../dm_input.h | $(B)
151+
$(CLANG) -O1 -g -fobjc-arc -Wall -Werror -I.. -o $@ t_pad_apple.m -framework Foundation -framework GameController
148152
run: $(B)/t_motion_pro $(B)/t_motion_pro_mac
149153
$(B)/t_motion_pro: t_motion_pro.c ../motion_pro.h | $(B)
150154
$(CLANG) -O1 -g -Wall -Werror -fsanitize=address,undefined -o $@ t_motion_pro.c

‎dm/host/tests/t_pad_apple.m‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Face buttons of the shared GameController adapter (pad_apple.h) with fake pads: by position on most pads, by label on a Nintendo pad; no physical controller is needed.
2+
#import "../pad_apple.h"
3+
#include <assert.h>
4+
#include <stdio.h>
5+
6+
@interface TestButton : NSObject
7+
@property BOOL isPressed;
8+
@end
9+
@implementation TestButton
10+
@end
11+
@interface TestController : NSObject
12+
@property NSString *productCategory;
13+
@end
14+
@implementation TestController
15+
@end
16+
@interface TestPad : NSObject
17+
@property TestController *controller;
18+
@property TestButton *buttonA, *buttonB, *buttonX, *buttonY;
19+
@end
20+
@implementation TestPad
21+
@end
22+
23+
// The Switch buttons of a pad of this category with only the named GameController button pressed
24+
static uint64_t face(NSString *category, char pressed) {
25+
TestPad *g = [TestPad new]; g.controller = [TestController new]; g.controller.productCategory = category;
26+
g.buttonA = [TestButton new]; g.buttonB = [TestButton new]; g.buttonX = [TestButton new]; g.buttonY = [TestButton new];
27+
TestButton *b = pressed == 'A' ? g.buttonA : pressed == 'B' ? g.buttonB : pressed == 'X' ? g.buttonX : g.buttonY;
28+
b.isPressed = YES;
29+
return dm_pad_face((GCExtendedGamepad *)(id)g);
30+
}
31+
32+
int main(void) { @autoreleasepool {
33+
for (NSString *k in @[@"Xbox One", @"DualSense", @"HID"]) { // by position: buttonA bottom (B), buttonB right (A), buttonX left (Y), buttonY top (X)
34+
assert(face(k, 'A') == DM_BTN_B); assert(face(k, 'B') == DM_BTN_A); assert(face(k, 'X') == DM_BTN_Y); assert(face(k, 'Y') == DM_BTN_X);
35+
}
36+
assert(face(nil, 'B') == DM_BTN_A); // no category: by position
37+
for (NSString *k in @[@"Switch Pro Controller", @"Nintendo Switch Joy-Con (L/R)", @"Nintendo Switch Joy-Con (R)"]) { // by label
38+
assert(face(k, 'A') == DM_BTN_A); assert(face(k, 'B') == DM_BTN_B); assert(face(k, 'X') == DM_BTN_X); assert(face(k, 'Y') == DM_BTN_Y);
39+
}
40+
TestPad *g = [TestPad new]; // nothing pressed, buttons nil: none
41+
assert(dm_pad_face((GCExtendedGamepad *)(id)g) == 0);
42+
printf("t_pad_apple OK\n");
43+
} return 0; }

‎dm/ios/Sources/DmGamepad.m‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// then in 3D the right stick for a touch in the right third.
1111
// Everything hugs the edges and corners to leave the picture room; controls sit over the picture, one CAShapeLayer each: a press only swaps the fill, a stick only moves its knob, nothing redraws the whole screen.
1212
// Touches no control takes (in 3D, those outside the right third) go to the game's touch screen (pushGame; Super Mario Galaxy's pointer in handheld mode).
13-
// The virtual gamepad (layout button included) hides while a physical controller is connected and comes back when it is removed; meanwhile every touch goes to the game. Physical controllers keep every button (D-pad and right stick included) and map by position: bottom = B, right = A, left = Y, top = X (same positions as on a Switch controller).
13+
// The virtual gamepad (layout button included) hides while a physical controller is connected and comes back when it is removed; meanwhile every touch goes to the game. Physical controllers keep every button (D-pad and right stick included) and map by position: bottom = B, right = A, left = Y, top = X (same positions as on a Switch controller); a Nintendo pad maps by its labels (pad_apple.h).
1414
// Each physical controller is a player of its own (local co-op), as on the Mac: the first one connected is player 1, together with the touch gamepad, the next player 2, and so on up to 8; see "Physical controllers" below.
1515
// Shaking the phone shakes the controller (DM_BTN_SHAKE: its six-axis sensor, hle_hid.c), whichever controls are in use; see "Shaking the phone" below.
1616
#import <UIKit/UIKit.h>
@@ -25,6 +25,7 @@
2525
#include "haptics_apple.h"
2626
#include "motion_apple.h"
2727
#include "motion_fusion.h"
28+
#include "pad_apple.h"
2829
#include "pad_slots.h"
2930
#include <stdatomic.h>
3031

@@ -465,9 +466,9 @@ - (void)refresh { // pressed state → fill, sticks → knob position
465466
static void read_pad(GCExtendedGamepad *g) {
466467
int p = g_merge ? 0 : pad_slot_of(&g_slots, (__bridge const void *)g.controller); if (p < 0) return;
467468
dm_haptics_apple_controller(p, g.controller);
468-
uint64_t m = 0;
469+
_Static_assert(DM_BTN_A == 1u << B_A && DM_BTN_B == 1u << B_B && DM_BTN_X == 1u << B_X && DM_BTN_Y == 1u << B_Y, "face button bits");
470+
uint64_t m = dm_pad_face(g); // by position, a Nintendo pad by its labels
469471
#define K(btn, bit) if (btn.isPressed) m |= 1ull << bit
470-
K(g.buttonB, B_A); K(g.buttonA, B_B); K(g.buttonY, B_X); K(g.buttonX, B_Y); // by position: right A, bottom B, top X, left Y
471472
K(g.leftShoulder, B_L); K(g.rightShoulder, B_R); K(g.leftTrigger, B_ZL); K(g.rightTrigger, B_ZR);
472473
K(g.buttonMenu, B_PLUS); if (g.buttonOptions) K(g.buttonOptions, B_MINUS);
473474
if (g.leftThumbstickButton) K(g.leftThumbstickButton, B_LS); if (g.rightThumbstickButton) K(g.rightThumbstickButton, B_RS);

‎dm/tools/impact_map.tsv‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ dm/host/hle.c hle:* the HLE tables
123123
dm/host/front_mac.m present:* the Mac front end
124124
dm/host/dm_input.h hle:*hid* the shared input interface
125125
dm/host/motion_apple.h hle:*SixAxisSensor* controller motion supplied by GameController
126+
dm/host/pad_apple.h hle:*hid* controller face buttons from GameController
126127
dm/host/motion_fusion.h hle:*SixAxisSensor* six-axis motion mapping and orientation
127128
dm/host/motion_pro.h hle:*SixAxisSensor* passive Switch Pro Controller IMU decoder
128129
dm/host/motion_pro_mac.h hle:*SixAxisSensor* Mac Pro Controller raw motion transport and routing

0 commit comments

Comments
 (0)