Mac: a HID pad GameController says it lists waits two seconds for it, and is read as HID if it never comes (PR #859)

+[GCController supportsHIDDevice:] answers YES for the 8BitDo Ultimate 2C, which GameController then never lists, so the pad got no input (issue #965; also a PowerA NANO, a GameSir G7 Pro and a Stadia controller). Such a pad now waits HIDPAD_GC_WAIT for a GCController of its name, is read as HID without one, and is handed back if that controller connects later. Microsoft's, Sony's and Nintendo's pads stay with GameController without the wait: their GameController names need not be their HID product strings (a DualShock 4 is "Wireless Controller", combined Joy-Cons one controller for two devices), so the name match cannot vouch for them.

Co-authored-by: Lars van der Niet <[email protected]>
This commit is contained in:
seeddsandLars van der Niet committed 2026-11-07 14:53:58 +08:00
1 parent 10fd970548
commit 463d21f7a3
3 files changed
+54 -21

No files matched your search

+1
View File
@@ -1399,6 +1399,7 @@ void front_run(void (*guest)(void)) {
[NSNotificationCenter.defaultCenter addObserverForName:nm object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *n) {
GCController *c = n.object; int conn = n.name == GCControllerDidConnectNotification;
if (!c.extendedGamepad || g_merge) LOG("controller %s: %s%s", conn ? "connected" : "disconnected", pad_name(c), c.extendedGamepad ? "" : " (no extended gamepad profile: not used)");
if (conn) hidpad_gc_connected(c); // a HID pad of its name that only waited for it (pad_hid_mac.h)
pads_changed(conn ? nil : c);
if (conn) hq_log_setting(); }];
dm_haptics_apple_init();
+52 -20
View File
@@ -1,7 +1,9 @@
// Mac: raw HID gamepads that GameController does not list (pad_hid.h, GitHub issue #637), read on the main run loop alongside front_mac.m's GameController players.
// Each device takes a player slot as a GameController controller does; front_mac.m defines the slot side (hidpad_attach / hidpad_detach / hidpad_read).
// Skipped, so no controller is read twice: GameController's synthetic devices, and every device GameController says it will list itself (+[GCController supportsHIDDevice:]:
// Xbox, PlayStation, Switch and MFi pads of any vendor). DM_HID_PADS=0 turns this off.
// Skipped, so no controller is read twice: GameController's synthetic devices, and a device GameController lists itself. +[GCController supportsHIDDevice:] is only a hint for
// that: it answers YES for the 8BitDo Ultimate 2C (2dc8:301b), which GameController then never lists (GitHub issue #965). So a device it says YES for waits HIDPAD_GC_WAIT
// seconds for a GCController of its name; without one it is read as HID, and dropped again if that GCController connects later. Microsoft's, Sony's and Nintendo's pads
// (hidpad_gc_native) are trusted without the wait: GameController supports them itself, under names that need not be their HID product strings. DM_HID_PADS=0 turns this off.
#pragma once
#import <IOKit/hid/IOHIDManager.h>
#import <GameController/GameController.h>
@@ -9,10 +11,12 @@
#include "pad_hid.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct hid_pad { IOHIDDeviceRef hid; pad_hid st; char name[64]; struct hid_pad *next; } hid_pad;
typedef struct hid_pad { IOHIDDeviceRef hid; pad_hid st; char name[64]; int vid, pid, gc_hint; struct hid_pad *next; } hid_pad;
static IOHIDManagerRef g_hidpad_manager;
static hid_pad *g_hidpads;
static hid_pad *g_hidpads, *g_hidpads_waiting; // waiting: GameController says it lists the device (gc_hint); read as HID if it has not after HIDPAD_GC_WAIT
#define HIDPAD_GC_WAIT 2.0
static int g_hidpad_log;
static void hidpad_attach(hid_pad *d);
static void hidpad_detach(hid_pad *d);
@@ -36,35 +40,63 @@ static void hidpad_value(void *ctx, IOReturn result, void *sender, IOHIDValueRef
}
hidpad_read(d);
}
static hid_pad *hidpad_unlink(hid_pad **list, IOHIDDeviceRef hid) {
while (*list && (*list)->hid != hid) list = &(*list)->next;
hid_pad *d = *list; if (d) *list = d->next; return d;
}
// Whether GameController lists a controller of this name (its vendorName is the HID product string). A second pad of the same model counts as listed with the first
static int hidpad_gc_listed(const char *name) { for (GCController *c in GCController.controllers) if (c.vendorName && !strcmp(c.vendorName.UTF8String, name)) return 1; return 0; }
// The vendors whose pads GameController supports itself (Xbox, PlayStation, Switch controllers). The name match cannot vouch for them: a DualShock 4's HID product string is
// "Wireless Controller", and combined Joy-Cons are one GCController for two HID devices, so each would also be read as HID after the wait and take a player of its own
static int hidpad_gc_native(int vid) { return vid == 0x045e || vid == 0x054c || vid == 0x057e; }
static void hidpad_drop(hid_pad *d) { IOHIDDeviceRegisterInputValueCallback(d->hid, NULL, NULL); CFRelease(d->hid); free(d); }
static void hidpad_use(hid_pad *d) {
d->next = g_hidpads; g_hidpads = d;
IOHIDDeviceRegisterInputValueCallback(d->hid, hidpad_value, d);
hidpad_attach(d);
// HID has no standard button numbering: the default map is 8BitDo's, so another vendor's pad may have its buttons in other places
if (d->vid != PAD_HID_DEFAULT_VENDOR && !getenv("DM_HID_MAP"))
LOG("controller '%s' (HID, %04x:%04x): buttons use 8BitDo's numbering; if they are in the wrong places, DM_HID_LOG=1 logs each button's number for a DM_HID_MAP", d->name, d->vid, d->pid);
}
static void hidpad_found(void *ctx, IOReturn result, void *sender, IOHIDDeviceRef hid) {
(void)ctx; (void)sender;
if (result != kIOReturnSuccess || IOHIDDeviceGetProperty(hid, CFSTR(kIOHIDGCSyntheticDeviceKey)) == kCFBooleanTrue) return;
for (hid_pad *d = g_hidpads; d; d = d->next) if (d->hid == hid) return;
if ([GCController supportsHIDDevice:hid]) return; // front_mac.m reads it through its GCController (once per device: the call is not cheap)
NSNumber *vendor = (__bridge NSNumber *)IOHIDDeviceGetProperty(hid, CFSTR(kIOHIDVendorIDKey)), *product = (__bridge NSNumber *)IOHIDDeviceGetProperty(hid, CFSTR(kIOHIDProductIDKey));
int vid = vendor.intValue, pid = product.intValue;
for (hid_pad *d = g_hidpads_waiting; d; d = d->next) if (d->hid == hid) return;
hid_pad *d = calloc(1, sizeof *d); if (!d) { LOG("[RED] HID gamepad: cannot allocate device state"); return; }
pad_hid_init(&d->st, getenv("DM_HID_MAP"));
NSString *name = (__bridge NSString *)IOHIDDeviceGetProperty(hid, CFSTR(kIOHIDProductKey));
snprintf(d->name, sizeof d->name, "%s", name.UTF8String ?: "HID gamepad");
d->vid = [(__bridge NSNumber *)IOHIDDeviceGetProperty(hid, CFSTR(kIOHIDVendorIDKey)) intValue];
d->pid = [(__bridge NSNumber *)IOHIDDeviceGetProperty(hid, CFSTR(kIOHIDProductIDKey)) intValue];
NSArray *els = CFBridgingRelease(IOHIDDeviceCopyMatchingElements(hid, NULL, kIOHIDOptionsTypeNone));
for (id o in els) { IOHIDElementRef e = (__bridge IOHIDElementRef)o; int a = pad_hid_axis_of(IOHIDElementGetUsagePage(e), IOHIDElementGetUsage(e)); if (a >= 0) d->st.has[a] = 1; }
d->hid = (IOHIDDeviceRef)CFRetain(hid); d->next = g_hidpads; g_hidpads = d;
IOHIDDeviceRegisterInputValueCallback(hid, hidpad_value, d);
hidpad_attach(d);
// HID has no standard button numbering: the default map is 8BitDo's, so another vendor's pad may have its buttons in other places
if (vid != PAD_HID_DEFAULT_VENDOR && !getenv("DM_HID_MAP"))
LOG("controller '%s' (HID, %04x:%04x): buttons use 8BitDo's numbering; if they are in the wrong places, DM_HID_LOG=1 logs each button's number for a DM_HID_MAP", d->name, vid, pid);
d->hid = (IOHIDDeviceRef)CFRetain(hid);
d->gc_hint = [GCController supportsHIDDevice:hid]; // once per device: the call is not cheap
if (!d->gc_hint) { hidpad_use(d); return; }
if (hidpad_gc_native(d->vid) || hidpad_gc_listed(d->name)) { hidpad_drop(d); return; } // front_mac.m reads it through its GCController
d->next = g_hidpads_waiting; g_hidpads_waiting = d;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(HIDPAD_GC_WAIT * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
hid_pad *w = hidpad_unlink(&g_hidpads_waiting, hid); if (!w) return; // removed meanwhile
if (hidpad_gc_listed(w->name)) { hidpad_drop(w); return; }
LOG("controller '%s' (HID, %04x:%04x): GameController says it lists it but has not after %g s, so it is read as HID", w->name, w->vid, w->pid, HIDPAD_GC_WAIT);
hidpad_use(w);
});
}
static void hidpad_removed(void *ctx, IOReturn result, void *sender, IOHIDDeviceRef hid) {
(void)ctx; (void)result; (void)sender;
hid_pad **link = &g_hidpads;
while (*link && (*link)->hid != hid) link = &(*link)->next;
if (!*link) return;
hid_pad *d = *link; *link = d->next;
IOHIDDeviceRegisterInputValueCallback(hid, NULL, NULL);
hidpad_detach(d);
CFRelease(d->hid); free(d);
hid_pad *d = hidpad_unlink(&g_hidpads, hid);
if (d) hidpad_detach(d); else d = hidpad_unlink(&g_hidpads_waiting, hid);
if (d) hidpad_drop(d);
}
// A GCController connected (front_mac.m): a HID pad of its name that GameController had only promised is read through it from now on.
// ponytail: matched by name, so two pads of one model that both fell back to HID are both dropped when GameController lists one; a per-device link would need an API GameController does not give
static void hidpad_gc_connected(GCController *c) {
const char *name = c.vendorName.UTF8String; if (!name) return;
for (hid_pad **link = &g_hidpads; *link;) {
hid_pad *d = *link; if (!d->gc_hint || strcmp(d->name, name)) { link = &d->next; continue; }
*link = d->next; LOG("controller '%s' (HID) is now a GameController controller: read through it", d->name); hidpad_detach(d); hidpad_drop(d);
}
}
static void hidpad_start(void) {
const char *e = getenv("DM_HID_PADS"); if (e && !atoi(e)) return;
+1 -1
View File
@@ -42,7 +42,7 @@ Lines like `DM_OPMODE=0` (handheld mode: lower resolution, higher frame rate) in
A Mac app shows the game's settings in a window before the game starts ([The Mac settings window](settings.md#the-mac-settings-window)). Its `env.txt` is `~/Library/Application Support/nxnative/<title ID>/env.txt`, and its log is `~/Library/Logs/nxnative/<title ID>.log`. [NX2Apple](https://github.com/LNPANDY/NX2Apple)'s library can also start it with the settings you pick there.
A controller macOS pairs but Apple's controller support does not list, such as an 8BitDo in its own mode (Ultimate 2C Wireless over Bluetooth), is read as a raw HID gamepad, with full analog sticks: it takes a player as any other controller does, and the log says `controller '<name>' (HID) connected`. Its buttons follow 8BitDo's numbering by default; another maker's pad may number them differently, and the log then says so. `DM_HID_MAP=A=1,B=2,X=4,Y=5` in `env.txt` moves them (here: the printed letters instead of the positions), `DM_HID_LOG=1` logs the number of each button pressed, and `DM_HID_PADS=0` turns this off.
A controller macOS pairs but Apple's controller support does not list, such as an 8BitDo in its own mode (Ultimate 2C Wireless over Bluetooth), is read as a raw HID gamepad, with full analog sticks: it takes a player as any other controller does, and the log says `controller '<name>' (HID) connected`. A pad Apple's support says it will list gets two seconds to appear there first, so it is never read twice (an Xbox, PlayStation or Nintendo controller is always left to Apple's support); if it never appears, it is read as HID after that wait, and the log says so. Its buttons follow 8BitDo's numbering by default; another maker's pad may number them differently, and the log then says so. `DM_HID_MAP=A=1,B=2,X=4,Y=5` in `env.txt` moves them (here: the printed letters instead of the positions), `DM_HID_LOG=1` logs the number of each button pressed, and `DM_HID_PADS=0` turns this off.
To quit with a controller, hold its Home button (the logo button on an Xbox or PlayStation controller) for 2 seconds, then press A to quit or B to go back to the game. Quitting this way is the same as ⌘Q: progress the game has not saved is lost. If holding Home opens the Games app instead, macOS keeps the Home button for itself: its controller settings for the Home button decide whether apps may use it. `DM_HOME_QUIT=0` in `env.txt` turns the hold off.