From a05650325095675af8a1a373963c8d9b89cb89c2 Mon Sep 17 00:00:00 2001 From: HSMM Date: Tue, 1 Dec 2026 01:23:05 +0300 Subject: [PATCH] Headset buttons: input values come through the HID manager (no crash at launch on macOS 27) On macOS 27 a device handed to the matching callback is already activated, and registering an input value callback on it trips an IOKit assertion (SIGTRAP on every launch). The callback is now registered on the manager before activation; release() no longer touches device callbacks. Fixes #14 --- .../MacroSIPApp/Platform/HeadsetButtons.swift | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Sources/MacroSIPApp/Platform/HeadsetButtons.swift b/Sources/MacroSIPApp/Platform/HeadsetButtons.swift index 99205ac..e45eea9 100644 --- a/Sources/MacroSIPApp/Platform/HeadsetButtons.swift +++ b/Sources/MacroSIPApp/Platform/HeadsetButtons.swift @@ -163,6 +163,14 @@ final class HeadsetButtons: @unchecked Sendable { guard let context else { return } Unmanaged.fromOpaque(context).takeUnretainedValue().deviceRemoved(device) }, context) + // Input values come through the manager, registered before activation: on macOS 27 a device + // handed to the matching callback is already activated, and registering a callback on it + // trips an IOKit assertion ("Device has already been activated/cancelled", SIGTRAP at launch). + IOHIDManagerSetInputValueMatching(m, [kIOHIDElementUsagePageKey: Int(HeadsetReportDecoder.telephonyPage)] as CFDictionary) + IOHIDManagerRegisterInputValueCallback(m, { context, _, _, value in + guard let context else { return } + Unmanaged.fromOpaque(context).takeUnretainedValue().inputValue(value) + }, context) IOHIDManagerSetDispatchQueue(m, queue) // devices it finds are scheduled on the same queue // After stop(): the manager and this controller live until the last queued callback is done. IOHIDManagerSetCancelHandler(m) { _ = m; _ = self } @@ -253,11 +261,6 @@ final class HeadsetButtons: @unchecked Sendable { AppLog.app.error("headset buttons: cannot open \(shown) (0x\(String(UInt32(bitPattern: status), radix: 16)))") return } - IOHIDDeviceSetInputValueMatching(device, [kIOHIDElementUsagePageKey: Int(HeadsetReportDecoder.telephonyPage)] as CFDictionary) - IOHIDDeviceRegisterInputValueCallback(device, { context, _, _, value in - guard let context else { return } - Unmanaged.fromOpaque(context).takeUnretainedValue().inputValue(value) - }, Unmanaged.passUnretained(self).toOpaque()) devices[k] = Device(ref: device, name: shown, decoder: HeadsetReportDecoder(hookHasPreferredState: momentary), leds: leds) order.append(k) AppLog.app.info("headset buttons: \(shown) connected, hook \(momentary ? "button" : "switch"), controls \(usages), LEDs \(leds.keys.sorted().map { "0x" + String($0, radix: 16) }.joined(separator: " "))") @@ -275,10 +278,9 @@ final class HeadsetButtons: @unchecked Sendable { publishDevice() } - /// Stops the callbacks of a device and closes it (removal, stop). + /// Closes a device (removal, stop). Its values stop with it; `inputValue` ignores unknown devices. private func release(_ key: UInt) { guard let d = devices[key] else { return } - IOHIDDeviceRegisterInputValueCallback(d.ref, nil, nil) IOHIDDeviceClose(d.ref, IOOptionBits(kIOHIDOptionsTypeNone)) }