Skip to content

Commit 710a519

Browse files
committed
Reload mach port run loop is interrupted
1 parent b9d8e37 commit 710a519

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

Sources/MachPort/MachPortEventController.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public final class MachPortEventController: MachPortEventPublisher {
1616
private var machPort: CFMachPort?
1717
private var runLoopSource: CFRunLoopSource?
1818
private var lhs: Bool = true
19+
private var currentMode: CFRunLoopMode = .commonModes
1920

2021
private let eventSourceId: CGEventSourceStateID
2122
private let signature: Int64
@@ -49,6 +50,7 @@ public final class MachPortEventController: MachPortEventPublisher {
4950
let machPort = try createMachPort()
5051
self.eventSource = try CGEventSource.create(eventSourceId)
5152
self.machPort = machPort
53+
self.currentMode = mode
5254
self.runLoopSource = try CFRunLoopSource.create(with: machPort)
5355

5456
CFRunLoopAddSource(runLoop, runLoopSource, mode)
@@ -92,6 +94,10 @@ public final class MachPortEventController: MachPortEventPublisher {
9294

9395
private func callback(_ proxy: CGEventTapProxy, _ type: CGEventType,
9496
_ cgEvent: CGEvent) -> Unmanaged<CGEvent>? {
97+
if type == .tapDisabledByTimeout || type == .tapDisabledByUserInput {
98+
try? reload(mode: currentMode)
99+
}
100+
95101
let result: Unmanaged<CGEvent>? = Unmanaged.passUnretained(cgEvent)
96102
if cgEvent.getIntegerValueField(.eventSourceUserData) == signature {
97103
return result

0 commit comments

Comments
 (0)