-
Notifications
You must be signed in to change notification settings - Fork 43
/
Remote.swift
140 lines (113 loc) · 4.75 KB
/
Remote.swift
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Remote.swift
// visionOS
//
// Created by Saagar Jha on 10/9/23.
//
import AppleConnect
import CoreMedia
struct Remote: macOSInterface {
let connection: Multiplexer
let local: Local
var name: String!
init(connection: Connection) {
local = Local()
self.connection = Multiplexer(connection: connection, localInterface: local)
local.remote = self
}
mutating func handshake() async throws -> Bool {
let handshake = try await _handshake(parameters: .init(version: Messages.version))
guard handshake.version == Messages.version else {
return false
}
name = handshake.name
return true
}
func _handshake(parameters: M.VisionOSHandshake.Request) async throws -> M.VisionOSHandshake.Reply {
try await M.VisionOSHandshake.send(parameters, through: connection)
}
var windows: [Window] {
get async throws {
try await _windows(parameters: .init()).windows
}
}
func _windows(parameters: M.Windows.Request) async throws -> M.Windows.Reply {
try await M.Windows.send(parameters, through: connection)
}
func windowPreview(for windowID: Window.ID) async throws -> Frame? {
try await _windowPreview(parameters: .init(windowID: windowID))
}
func _windowPreview(parameters: M.WindowPreview.Request) async throws -> M.WindowPreview.Reply {
try await M.WindowPreview.send(parameters, through: connection)
}
func startCasting(for windowID: Window.ID) async throws -> AsyncStream<Frame> {
let (stream, continuation) = AsyncStream.makeStream(of: Frame.self, bufferingPolicy: .bufferingNewest(1))
local.streams[windowID] = continuation
continuation.onTermination = { _ in
Task {
try await _stopCasting(parameters: .init(windowID: windowID))
}
}
_ = try await _startCasting(parameters: .init(windowID: windowID))
return stream
}
func _startCasting(parameters: M.StartCasting.Request) async throws -> M.StartCasting.Reply {
try await M.StartCasting.send(parameters, through: connection)
}
func _stopCasting(parameters: M.StopCasting.Request) async throws -> M.StopCasting.Reply {
try await M.StopCasting.send(parameters, through: connection)
}
func _windowMask(parameters: M.WindowMask.Request) async throws -> M.WindowMask.Reply {
try await M.WindowMask.send(parameters, through: connection)
}
func children(of windowID: Window.ID) async throws -> AsyncStream<[Window.ID]> {
let (stream, continuation) = AsyncStream.makeStream(of: [Window.ID].self)
local.children[windowID] = continuation
continuation.onTermination = { _ in
Task {
try await _stopWatchingForChildWindows(parameters: .init(windowID: windowID))
}
}
_ = try await _startWatchingForChildWindows(parameters: .init(windowID: windowID))
return stream
}
func appIcon(for windowID: Window.ID, size: CGSize) async throws -> Data {
try await _appIcon(parameters: .init(windowID: windowID, size: size)).image
}
func _startWatchingForChildWindows(parameters: M.StartWatchingForChildWindows.Request) async throws -> M.StartWatchingForChildWindows.Reply {
try await M.StartWatchingForChildWindows.send(parameters, through: connection)
}
func _stopWatchingForChildWindows(parameters: M.StopWatchingForChildWindows.Request) async throws -> M.StopWatchingForChildWindows.Reply {
try await M.StopWatchingForChildWindows.send(parameters, through: connection)
}
func _mouseMoved(parameters: M.MouseMoved.Request) async throws -> M.MouseMoved.Reply {
try await M.MouseMoved.send(parameters, through: connection)
}
func _clicked(parameters: M.Clicked.Request) async throws -> M.Clicked.Reply {
try await M.Clicked.send(parameters, through: connection)
}
func _scrollBegan(parameters: M.ScrollBegan.Request) async throws -> M.ScrollBegan.Reply {
try await M.ScrollBegan.send(parameters, through: connection)
}
func _scrollChanged(parameters: M.ScrollChanged.Request) async throws -> M.ScrollChanged.Reply {
try await M.ScrollChanged.send(parameters, through: connection)
}
func _scrollEnded(parameters: M.ScrollEnded.Request) async throws -> M.ScrollEnded.Reply {
try await M.ScrollEnded.send(parameters, through: connection)
}
func _dragBegan(parameters: M.DragBegan.Request) async throws -> M.DragBegan.Reply {
try await M.DragBegan.send(parameters, through: connection)
}
func _dragChanged(parameters: M.DragChanged.Request) async throws -> M.DragChanged.Reply {
try await M.DragChanged.send(parameters, through: connection)
}
func _dragEnded(parameters: M.DragEnded.Request) async throws -> M.DragEnded.Reply {
try await M.DragEnded.send(parameters, through: connection)
}
func _typed(parameters: M.Typed.Request) async throws -> M.Typed.Reply {
try await M.Typed.send(parameters, through: connection)
}
func _appIcon(parameters: M.AppIcon.Request) async throws -> M.AppIcon.Reply {
try await M.AppIcon.send(parameters, through: connection)
}
}