forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCGEventObserver.swift
More file actions
109 lines (95 loc) · 3.32 KB
/
CGEventObserver.swift
File metadata and controls
109 lines (95 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import Cocoa
import Foundation
import os.log
public protocol CGEventObserverType {
@discardableResult
func activateIfPossible() -> Bool
func deactivate()
var stream: AsyncStream<CGEventType> { get }
var isEnabled: Bool { get }
}
final class CGEventObserver: CGEventObserverType {
let stream: AsyncStream<CGEventType>
var isEnabled: Bool { port != nil }
private var continuation: AsyncStream<CGEventType>.Continuation
private var port: CFMachPort?
private let eventsOfInterest: Set<CGEventType> = [
.keyUp,
.keyDown,
.rightMouseDown,
.leftMouseDown,
]
private let tapLocation: CGEventTapLocation = .cghidEventTap
private let tapPlacement: CGEventTapPlacement = .tailAppendEventTap
private let tapOptions: CGEventTapOptions = .listenOnly
private var retryTask: Task<Void, Error>?
deinit {
continuation.finish()
CFMachPortInvalidate(port)
}
init() {
var continuation: AsyncStream<CGEventType>.Continuation!
stream = AsyncStream { c in
continuation = c
}
self.continuation = continuation
}
public func deactivate() {
retryTask?.cancel()
retryTask = nil
guard let port else { return }
os_log(.info, "CGEventObserver deactivated.")
CFMachPortInvalidate(port)
self.port = nil
}
@discardableResult
public func activateIfPossible() -> Bool {
guard AXIsProcessTrusted() else { return false }
guard port == nil else { return true }
let eoi = UInt64(eventsOfInterest.reduce(into: 0) { $0 |= 1 << $1.rawValue })
func callback(
tapProxy _: CGEventTapProxy,
eventType: CGEventType,
event: CGEvent,
continuationPointer: UnsafeMutableRawPointer?
) -> Unmanaged<CGEvent>? {
guard AXIsProcessTrusted() else {
return .passRetained(event)
}
if eventType == .tapDisabledByTimeout || eventType == .tapDisabledByUserInput {
return .passRetained(event)
}
if let continuation = continuationPointer?
.assumingMemoryBound(to: AsyncStream<CGEventType>.Continuation.self)
{
continuation.pointee.yield(eventType)
}
return .passRetained(event)
}
let tapLocation = tapLocation
let tapPlacement = tapPlacement
let tapOptions = tapOptions
guard let port = withUnsafeMutablePointer(to: &continuation, { pointer in
CGEvent.tapCreate(
tap: tapLocation,
place: tapPlacement,
options: tapOptions,
eventsOfInterest: eoi,
callback: callback,
userInfo: pointer
)
}) else {
retryTask = Task {
try? await Task.sleep(nanoseconds: 2_000_000_000)
try Task.checkCancellation()
activateIfPossible()
}
return false
}
self.port = port
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, port, 0)
CFRunLoopAddSource(RunLoop.main.getCFRunLoop(), runLoopSource, .commonModes)
os_log(.info, "CGEventObserver activated.")
return true
}
}