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
113 lines (96 loc) · 3.52 KB
/
CGEventObserver.swift
File metadata and controls
113 lines (96 loc) · 3.52 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
110
111
112
113
import Cocoa
import Foundation
import Logger
public protocol CGEventObserverType {
@discardableResult
func activateIfPossible() -> Bool
func deactivate()
func createStream() -> AsyncStream<CGEvent>
var isEnabled: Bool { get }
}
public final class CGEventObserver: CGEventObserverType {
public var isEnabled: Bool { port != nil }
private var continuations: [UUID: AsyncStream<CGEvent>.Continuation] = [:]
private var port: CFMachPort?
private let eventsOfInterest: Set<CGEventType>
private let tapLocation: CGEventTapLocation = .cghidEventTap
private let tapPlacement: CGEventTapPlacement = .tailAppendEventTap
private let tapOptions: CGEventTapOptions = .defaultTap
deinit {
for continuation in continuations {
continuation.value.finish()
}
CFMachPortInvalidate(port)
}
public init(eventsOfInterest: Set<CGEventType>) {
self.eventsOfInterest = eventsOfInterest
}
public func createStream() -> AsyncStream<CGEvent> {
.init { continuation in
let id = UUID()
addContinuation(continuation, id: id)
continuation.onTermination = { [weak self] _ in
self?.removeContinuation(id: id)
}
}
}
private func addContinuation(_ continuation: AsyncStream<CGEvent>.Continuation, id: UUID) {
continuations[id] = continuation
}
private func removeContinuation(id: UUID) {
continuations[id] = nil
}
public func deactivate() {
guard let port else { return }
Logger.service.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,
continuationsPointer: UnsafeMutableRawPointer?
) -> Unmanaged<CGEvent>? {
guard AXIsProcessTrusted() else {
return .passRetained(event)
}
if eventType == .tapDisabledByTimeout || eventType == .tapDisabledByUserInput {
return .passRetained(event)
}
if let continuations = continuationsPointer?
.assumingMemoryBound(to: [UUID: AsyncStream<CGEvent>.Continuation].self)
{
for continuation in continuations.pointee {
continuation.value.yield(event)
}
}
return .passRetained(event)
}
let tapLocation = tapLocation
let tapPlacement = tapPlacement
let tapOptions = tapOptions
guard let port = withUnsafeMutablePointer(to: &continuations, { pointer in
CGEvent.tapCreate(
tap: tapLocation,
place: tapPlacement,
options: tapOptions,
eventsOfInterest: eoi,
callback: callback,
userInfo: pointer
)
}) else {
return false
}
self.port = port
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, port, 0)
CFRunLoopAddSource(RunLoop.main.getCFRunLoop(), runLoopSource, .commonModes)
Logger.service.info("CGEventObserver activated.")
return true
}
}