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)) }