Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Sources/MacroSIPApp/Platform/HeadsetButtons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ final class HeadsetButtons: @unchecked Sendable {
guard let context else { return }
Unmanaged<HeadsetButtons>.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<HeadsetButtons>.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 }
Expand Down Expand Up @@ -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<HeadsetButtons>.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: " "))")
Expand All @@ -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))
}

Expand Down
Loading