|
| 1 | +import Cocoa |
| 2 | +import Foundation |
| 3 | + |
| 4 | +public protocol CGEventObserverType { |
| 5 | + @discardableResult |
| 6 | + func activateIfPossible() -> Bool |
| 7 | + func deactivate() |
| 8 | + var stream: AsyncStream<Void> { get } |
| 9 | + var isEnabled: Bool { get } |
| 10 | +} |
| 11 | + |
| 12 | +final class CGEventObserver: CGEventObserverType { |
| 13 | + let stream: AsyncStream<Void> |
| 14 | + var isEnabled: Bool { port != nil } |
| 15 | + |
| 16 | + private var continuation: AsyncStream<Void>.Continuation |
| 17 | + private var port: CFMachPort? |
| 18 | + private let eventsOfInterest: Set<CGEventType> = [.keyUp, .leftMouseUp, .mouseMoved] |
| 19 | + private let tapLocation: CGEventTapLocation = .cghidEventTap |
| 20 | + private let tapPlacement: CGEventTapPlacement = .tailAppendEventTap |
| 21 | + private let tapOptions: CGEventTapOptions = .listenOnly |
| 22 | + private var retryTask: Task<Void, Error>? |
| 23 | + |
| 24 | + deinit { |
| 25 | + continuation.finish() |
| 26 | + CFMachPortInvalidate(port) |
| 27 | + } |
| 28 | + |
| 29 | + init() { |
| 30 | + var continuation: AsyncStream<Void>.Continuation! |
| 31 | + stream = AsyncStream { c in |
| 32 | + continuation = c |
| 33 | + } |
| 34 | + self.continuation = continuation |
| 35 | + } |
| 36 | + |
| 37 | + public func deactivate() { |
| 38 | + retryTask?.cancel() |
| 39 | + retryTask = nil |
| 40 | + guard let port = port else { return } |
| 41 | + print("Deactivate") |
| 42 | + CFMachPortInvalidate(port) |
| 43 | + self.port = nil |
| 44 | + } |
| 45 | + |
| 46 | + @discardableResult |
| 47 | + public func activateIfPossible() -> Bool { |
| 48 | + guard AXIsProcessTrusted() else { return false } |
| 49 | + guard port == nil else { return true } |
| 50 | + |
| 51 | + let eoi = UInt64(eventsOfInterest.reduce(into: 0) { $0 |= 1 << $1.rawValue }) |
| 52 | + |
| 53 | + func callback( |
| 54 | + tapProxy _: CGEventTapProxy, |
| 55 | + eventType: CGEventType, |
| 56 | + event: CGEvent, |
| 57 | + continuationPointer: UnsafeMutableRawPointer? |
| 58 | + ) -> Unmanaged<CGEvent>? { |
| 59 | + guard AXIsProcessTrusted() else { |
| 60 | + return .passRetained(event) |
| 61 | + } |
| 62 | + |
| 63 | + if eventType == .tapDisabledByTimeout || eventType == .tapDisabledByUserInput { |
| 64 | + return .passRetained(event) |
| 65 | + } |
| 66 | + |
| 67 | + if let continuation = continuationPointer?.assumingMemoryBound(to: AsyncStream<Void>.Continuation.self) { |
| 68 | + continuation.pointee.yield(()) |
| 69 | + } |
| 70 | + |
| 71 | + return .passRetained(event) |
| 72 | + } |
| 73 | + |
| 74 | + let tapLocation = self.tapLocation |
| 75 | + let tapPlacement = self.tapPlacement |
| 76 | + let tapOptions = self.tapOptions |
| 77 | + |
| 78 | + guard let port = withUnsafeMutablePointer(to: &continuation, { pointer in |
| 79 | + CGEvent.tapCreate( |
| 80 | + tap: tapLocation, |
| 81 | + place: tapPlacement, |
| 82 | + options: tapOptions, |
| 83 | + eventsOfInterest: eoi, |
| 84 | + callback: callback, |
| 85 | + userInfo: pointer |
| 86 | + ) |
| 87 | + }) else { |
| 88 | + retryTask = Task { |
| 89 | + try? await Task.sleep(nanoseconds: 2_000_000_000) |
| 90 | + try Task.checkCancellation() |
| 91 | + activateIfPossible() |
| 92 | + } |
| 93 | + return false |
| 94 | + } |
| 95 | + self.port = port |
| 96 | + let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, port, 0) |
| 97 | + CFRunLoopAddSource(RunLoop.main.getCFRunLoop(), runLoopSource, .commonModes) |
| 98 | + return true |
| 99 | + } |
| 100 | +} |
0 commit comments