Skip to content

Cocoa Port: Fix HID controller crashes and spurious input - #992

Open
dev-jpoliveira wants to merge 1 commit into
TASEmulators:masterfrom
dev-jpoliveira:cocoa-hid-controller-fixes
Open

dev-jpoliveira wants to merge 1 commit into
TASEmulators:masterfrom
dev-jpoliveira:cocoa-hid-controller-fixes

Conversation

@dev-jpoliveira

Copy link
Copy Markdown

Problem and reproduction

On macOS 26.6.2 / Apple Silicon, mapping a Nintendo Switch Pro Controller (VID 057e, PID 2009) in the Cocoa frontend exposed two paths:

  1. With Increase controller compatibility disabled, the status bar showed 0x0000/0xFFFFFFFF (nan) and rapidly changing button inputs while the controller was idle. Selecting a command could bind an input without a physical press.
  2. With compatibility enabled, pressing a controller input while mapping could crash at CFNumberGetValue. The macOS synthetic controller does not necessarily provide a LocationID, but the device-code helper read it unconditionally.

These symptoms were reproduced with 0.9.13. The affected parser code is still present in master (base 1275dc6). A ROM is not needed: connect the controller, open Settings → Input, select Up, then press and release the D-pad direction.

Proposed fix

  • Validate optional vendor/product/location properties before reading them as CFNumber; fall back to zero for missing or incorrectly typed numeric properties.
  • Reject unsupported element types, undefined usages/pages and unusable logical ranges before normalization. Raw report placeholders with a zero-width range must not become button/axis commands or produce NaN.
  • Replace resize() followed by push_back() with reserve() when expanding hats and digital axis thresholds. The old code returned extra empty entries (eight instead of four for a hat; four instead of two for an axis).
  • Skip the Switch Pro Controller's proprietary 0x30 report in the generic HID element parser. Its payload starts with a counter and battery/connection data before the buttons; interpreting it through the generic descriptor produces spurious presses. The filter is restricted to this VID/PID and report ID. Standard 0x3f reports and other devices remain eligible for parsing.

The Switch path uses the decoded synthetic controller supplied by macOS compatibility (GamePad-1). This change does not implement a Nintendo protocol decoder and does not automatically enable compatibility. It always discards the specified proprietary report, whether or not a synthetic device is currently available.

Protocol reference: SDL Switch report IDs and packet layouts.

Validation

  • Added a standalone macOS regression check: python3 desmume/src/frontend/cocoa/tests/test_hid_input.py.
  • All 15 cases pass with this change. Against the unmodified base, 12 cases fail and three valid-input controls pass.
  • The check compiles the production parser functions and structs, stubbing the IOKit boundary and command lookup. It covers missing/wrong property types, raw/invalid elements, logical ranges, buttons, hats, axis expansion and the report filter's boundaries. It needs Xcode command-line tools and Python 3, with no third-party Python packages or controller attached.
  • Built the current master plus this patch successfully with Xcode 26.4.1, the Apple Silicon Latest scheme, Release configuration and macOS 11.0 deployment target.
  • Hardware validation of the equivalent 0.9.13-based patch: USB A and D-pad Up presses/releases were received through GamePad-1, mapped without a crash and remained mapped after a full app restart. No ROM or save was used in this validation. The user subsequently reported successful operation following a Bluetooth setup test; that Bluetooth result was not independently instrumented.

The master-based build has not been physically tested. Other controller models, multiple synthetic controllers and older macOS versions have not been physically tested. Production code avoids Objective-C numeric literals to preserve compatibility with the legacy compiler targets.

Validate optional numeric device properties before reading them, and
reject unusable elements before normalizing their values. Reserve hat
and axis output capacity instead of inserting empty input records.

Ignore the Switch Pro Controller proprietary 0x30 report in the generic
HID parser; decoded input remains available through macOS controller
compatibility. Preserve standard reports and other device IDs.

Add a standalone Cocoa parser regression check covering 15 cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant