Skip to content

Commit

Permalink
Use serial queue in EventTap
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Aug 23, 2024
1 parent 0f0b8b8 commit f12be76
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions Ice/Utilities/EventMonitors/EventTap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class EventTap {
/// or to disable the tap from within the callback.
struct Proxy {
private let tap: EventTap

private let pointer: CGEventTapProxy

/// The label associated with the event tap.
Expand Down Expand Up @@ -87,13 +86,11 @@ class EventTap {
}

private let runLoop = CFRunLoopGetCurrent()

private let mode: CFRunLoopMode = .commonModes

private let callback: (EventTap, CGEventTapProxy, CGEventType, CGEvent) -> Unmanaged<CGEvent>?
private let queue: DispatchQueue

private var machPort: CFMachPort?

private var source: CFRunLoopSource?

/// The label associated with the event tap.
Expand Down Expand Up @@ -128,6 +125,7 @@ class EventTap {
self.callback = { tap, pointer, type, event in
callback(Proxy(tap: tap, pointer: pointer), type, event).map(Unmanaged.passUnretained)
}
self.queue = DispatchQueue(label: label)
guard let machPort = Self.createTapMachPort(
location: location,
place: place,
Expand Down Expand Up @@ -159,7 +157,10 @@ class EventTap {
event: CGEvent
) -> Unmanaged<CGEvent>? {
let callback = eventTap.callback
return callback(eventTap, proxy, type, event)
let queue = eventTap.queue
return queue.sync {
callback(eventTap, proxy, type, event)
}
}

private static func createTapMachPort(
Expand Down Expand Up @@ -221,8 +222,11 @@ class EventTap {
/// Enables the event tap.
func enable() {
withUnwrappedComponents { runLoop, source, machPort in
CFRunLoopAddSource(runLoop, source, mode)
CGEvent.tapEnable(tap: machPort, enable: true)
let mode = self.mode
queue.async {
CFRunLoopAddSource(runLoop, source, mode)
CGEvent.tapEnable(tap: machPort, enable: true)
}
}
}

Expand All @@ -234,17 +238,22 @@ class EventTap {
guard let self else {
return
}
if isEnabled {
onTimeout()
queue.async {
if self.isEnabled {
onTimeout()
}
}
}
}

/// Disables the event tap.
func disable() {
withUnwrappedComponents { runLoop, source, machPort in
CFRunLoopRemoveSource(runLoop, source, mode)
CGEvent.tapEnable(tap: machPort, enable: false)
let mode = self.mode
queue.async {
CFRunLoopRemoveSource(runLoop, source, mode)
CGEvent.tapEnable(tap: machPort, enable: false)
}
}
}
}
Expand Down

0 comments on commit f12be76

Please sign in to comment.