screen-relay 0.0.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/Makefile +165 -0
- package/README.md +112 -1
- package/THIRD-PARTY-NOTICES.md +75 -0
- package/dist/cli/client.d.ts +28 -0
- package/dist/cli/commands.d.ts +52 -0
- package/dist/cli/config.d.ts +32 -0
- package/dist/cli/format.d.ts +15 -0
- package/dist/cli/help.d.ts +5 -0
- package/dist/cli/host/common.d.ts +23 -0
- package/dist/cli/host/doctor.d.ts +2 -0
- package/dist/cli/host/packages.d.ts +13 -0
- package/dist/cli/host/session_commands.d.ts +8 -0
- package/dist/cli/host/setup.d.ts +22 -0
- package/dist/cli/host/state.d.ts +23 -0
- package/dist/cli/host/supervisor.d.ts +94 -0
- package/dist/cli/host/watcher.d.ts +4 -0
- package/dist/cli/host/windows.d.ts +82 -0
- package/dist/cli/main.d.ts +3 -0
- package/dist/cli/mode.d.ts +14 -0
- package/dist/cli/output.d.ts +24 -0
- package/dist/cli/parser.d.ts +6 -0
- package/dist/cli/relay.mjs +12631 -0
- package/dist/cli/skills.d.ts +21 -0
- package/dist/cli/theme.d.ts +14 -0
- package/dist/protocol/errors.d.ts +155 -0
- package/dist/protocol/index.d.ts +119 -0
- package/dist/protocol/index.js +418 -0
- package/dist/sdk/index.d.ts +78 -0
- package/dist/sdk/index.js +834 -0
- package/dist/sdk/transport.d.ts +20 -0
- package/dist/viewer/index.html +96 -0
- package/dist/viewer/viewer.js +1079 -0
- package/package.json +47 -7
- package/packages.tsv +38 -0
- package/skills/core/SKILL.md +171 -0
- package/skills/core/references/cli.md +119 -0
- package/skills/core/references/config.md +48 -0
- package/skills/core/references/macos.md +50 -0
- package/skills/core/references/sdk.md +69 -0
- package/skills/core/references/troubleshooting.md +33 -0
- package/skills/core/references/wayland.md +47 -0
- package/skills/screen-relay/SKILL.md +24 -0
- package/skills/vercel-sandbox/SKILL.md +68 -0
- package/src/engine/backends/linux/keycodes.hpp +35 -0
- package/src/engine/backends/linux/wayland/capture.cpp +298 -0
- package/src/engine/backends/linux/wayland/input.cpp +559 -0
- package/src/engine/backends/linux/wayland/keymap.cpp +118 -0
- package/src/engine/backends/linux/wayland/keymap.hpp +54 -0
- package/src/engine/backends/linux/wayland/protocols/virtual-keyboard-unstable-v1.xml +112 -0
- package/src/engine/backends/linux/wayland/protocols/wlr-screencopy-unstable-v1.xml +234 -0
- package/src/engine/backends/linux/wayland/protocols/wlr-virtual-pointer-unstable-v1.xml +152 -0
- package/src/engine/backends/linux/wayland/wayland.cpp +166 -0
- package/src/engine/backends/linux/wayland/wayland.hpp +60 -0
- package/src/engine/backends/linux/x11/capture.cpp +206 -0
- package/src/engine/backends/linux/x11/input.cpp +266 -0
- package/src/engine/backends/macos/capture.mm +367 -0
- package/src/engine/backends/macos/display.hpp +20 -0
- package/src/engine/backends/macos/display.mm +84 -0
- package/src/engine/backends/macos/input.mm +287 -0
- package/src/engine/backends/macos/keys.hpp +34 -0
- package/src/engine/backends/windows/capture.cpp +393 -0
- package/src/engine/backends/windows/input.cpp +274 -0
- package/src/engine/backends/windows/output.cpp +65 -0
- package/src/engine/backends/windows/output.hpp +26 -0
- package/src/engine/core/capture-bucket.hpp +40 -0
- package/src/engine/core/capture-source.hpp +59 -0
- package/src/engine/core/capture.cpp +565 -0
- package/src/engine/core/capture.hpp +58 -0
- package/src/engine/core/color.cpp +176 -0
- package/src/engine/core/color.hpp +16 -0
- package/src/engine/core/cpu-budget.hpp +196 -0
- package/src/engine/core/flow.hpp +317 -0
- package/src/engine/core/input.cpp +98 -0
- package/src/engine/core/input.hpp +115 -0
- package/src/engine/core/wait.cpp +125 -0
- package/src/engine/core/wait.hpp +41 -0
- package/src/engine/server/backend.cpp +65 -0
- package/src/engine/server/backend.hpp +18 -0
- package/src/engine/server/config.cpp +257 -0
- package/src/engine/server/config.hpp +24 -0
- package/src/engine/server/main.cpp +82 -0
- package/src/engine/server/metrics.cpp +47 -0
- package/src/engine/server/metrics.hpp +10 -0
- package/src/engine/server/screenshot.cpp +108 -0
- package/src/engine/server/screenshot.hpp +31 -0
- package/src/engine/server/server.cpp +535 -0
- package/src/engine/server/server.hpp +89 -0
- package/src/engine/server/wire.cpp +70 -0
- package/src/engine/server/wire.hpp +29 -0
- package/index.js +0 -2
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
#include "core/capture-source.hpp"
|
|
2
|
+
#include "backends/windows/output.hpp"
|
|
3
|
+
#include <d3d11.h>
|
|
4
|
+
#include <algorithm>
|
|
5
|
+
#include <atomic>
|
|
6
|
+
#include <condition_variable>
|
|
7
|
+
#include <chrono>
|
|
8
|
+
#include <cstring>
|
|
9
|
+
#include <memory>
|
|
10
|
+
#include <mutex>
|
|
11
|
+
#include <optional>
|
|
12
|
+
#include <stdexcept>
|
|
13
|
+
#include <string>
|
|
14
|
+
#include <thread>
|
|
15
|
+
#include <tuple>
|
|
16
|
+
#include <vector>
|
|
17
|
+
|
|
18
|
+
namespace relay {
|
|
19
|
+
namespace {
|
|
20
|
+
using Microsoft::WRL::ComPtr;
|
|
21
|
+
/// Damage rectangles a source holds before it treats the next capture as a full frame.
|
|
22
|
+
constexpr size_t max_pending_rects = 256;
|
|
23
|
+
/// Converts a duplication pointer shape to straight-alpha RGBA, box-filtered down to the stream's scale.
|
|
24
|
+
CursorImage cursorImage(const DXGI_OUTDUPL_POINTER_SHAPE_INFO& info, const std::vector<uint8_t>& data, int level) {
|
|
25
|
+
const bool mono = info.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME;
|
|
26
|
+
const int w = int(info.Width), h = int(mono ? info.Height / 2 : info.Height);
|
|
27
|
+
std::vector<uint8_t> full(size_t(w) * h * 4, 0);
|
|
28
|
+
for (int y = 0; y < h; ++y)
|
|
29
|
+
for (int x = 0; x < w; ++x) {
|
|
30
|
+
uint8_t* out = full.data() + (size_t(y) * w + x) * 4;
|
|
31
|
+
if (mono) {
|
|
32
|
+
const int mask = 0x80 >> (x % 8);
|
|
33
|
+
const bool andBit = data[size_t(y) * info.Pitch + x / 8] & mask;
|
|
34
|
+
const bool xorBit = data[size_t(y + h) * info.Pitch + x / 8] & mask;
|
|
35
|
+
if (andBit && !xorBit) continue;
|
|
36
|
+
const uint8_t value = xorBit ? 255 : 0;
|
|
37
|
+
out[0] = out[1] = out[2] = value; out[3] = 255;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const uint8_t* in = data.data() + size_t(y) * info.Pitch + size_t(x) * 4;
|
|
41
|
+
const bool masked = info.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR;
|
|
42
|
+
out[0] = in[2]; out[1] = in[1]; out[2] = in[0];
|
|
43
|
+
out[3] = masked ? (in[3] ? (in[0] | in[1] | in[2] ? 255 : 0) : 255) : in[3];
|
|
44
|
+
}
|
|
45
|
+
const int factor = 1 << level;
|
|
46
|
+
const int sw = std::min<int>(CursorImage::max_extent, std::max(1, w / factor));
|
|
47
|
+
const int sh = std::min<int>(CursorImage::max_extent, std::max(1, h / factor));
|
|
48
|
+
CursorImage image;
|
|
49
|
+
image.width = uint16_t(sw); image.height = uint16_t(sh);
|
|
50
|
+
image.xhot = uint16_t(std::min(sw - 1, int(info.HotSpot.x) / factor));
|
|
51
|
+
image.yhot = uint16_t(std::min(sh - 1, int(info.HotSpot.y) / factor));
|
|
52
|
+
image.rgba.assign(size_t(sw) * sh * 4, 0);
|
|
53
|
+
for (int y = 0; y < sh; ++y)
|
|
54
|
+
for (int x = 0; x < sw; ++x) {
|
|
55
|
+
unsigned sum[4] = {}, count = 0;
|
|
56
|
+
for (int dy = 0; dy < factor && y * factor + dy < h; ++dy)
|
|
57
|
+
for (int dx = 0; dx < factor && x * factor + dx < w; ++dx) {
|
|
58
|
+
const uint8_t* p = full.data() + (size_t(y * factor + dy) * w + x * factor + dx) * 4;
|
|
59
|
+
for (int c = 0; c < 3; ++c) sum[c] += p[c] * p[3];
|
|
60
|
+
sum[3] += p[3]; ++count;
|
|
61
|
+
}
|
|
62
|
+
uint8_t* out = image.rgba.data() + (size_t(y) * sw + x) * 4;
|
|
63
|
+
for (int c = 0; c < 3; ++c) out[c] = uint8_t(sum[3] ? sum[c] / sum[3] : 0);
|
|
64
|
+
out[3] = uint8_t(count ? sum[3] / count : 0);
|
|
65
|
+
}
|
|
66
|
+
return image;
|
|
67
|
+
}
|
|
68
|
+
class WindowsCapture;
|
|
69
|
+
/// Owns the one Desktop Duplication a process may hold for a display and fans its frames out to every capture source.
|
|
70
|
+
class Duplicator {
|
|
71
|
+
public:
|
|
72
|
+
windows::Output display;
|
|
73
|
+
int width = 0, height = 0;
|
|
74
|
+
|
|
75
|
+
static std::shared_ptr<Duplicator> shared() {
|
|
76
|
+
static std::mutex lock;
|
|
77
|
+
static std::weak_ptr<Duplicator> current;
|
|
78
|
+
std::lock_guard<std::mutex> guard(lock);
|
|
79
|
+
auto existing = current.lock();
|
|
80
|
+
if (!existing || existing->failed()) { existing = std::make_shared<Duplicator>(); current = existing; }
|
|
81
|
+
return existing;
|
|
82
|
+
}
|
|
83
|
+
bool failed() {
|
|
84
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
85
|
+
return !failure.empty();
|
|
86
|
+
}
|
|
87
|
+
Duplicator() : display(windows::findOutput()) {
|
|
88
|
+
level = display.level();
|
|
89
|
+
std::tie(width, height) = display.streamSize();
|
|
90
|
+
requireSurfaceSize(width, height);
|
|
91
|
+
windows::check(D3D11CreateDevice(display.adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr,
|
|
92
|
+
D3D11_CREATE_DEVICE_BGRA_SUPPORT, nullptr, 0, D3D11_SDK_VERSION,
|
|
93
|
+
&device, nullptr, &context), "creating the Direct3D device");
|
|
94
|
+
D3D11_TEXTURE2D_DESC desc{};
|
|
95
|
+
desc.Width = UINT(width); desc.Height = UINT(height); desc.MipLevels = 1; desc.ArraySize = 1;
|
|
96
|
+
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; desc.SampleDesc.Count = 1;
|
|
97
|
+
desc.Usage = D3D11_USAGE_STAGING; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
|
98
|
+
windows::check(device->CreateTexture2D(&desc, nullptr, &staging), "creating the readback texture");
|
|
99
|
+
if (level) {
|
|
100
|
+
desc.Width = UINT(display.width()); desc.Height = UINT(display.height()); desc.MipLevels = UINT(level + 1);
|
|
101
|
+
desc.Usage = D3D11_USAGE_DEFAULT; desc.CPUAccessFlags = 0;
|
|
102
|
+
desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
|
|
103
|
+
desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
|
|
104
|
+
windows::check(device->CreateTexture2D(&desc, nullptr, &mips), "creating the scaling texture");
|
|
105
|
+
windows::check(device->CreateShaderResourceView(mips.Get(), nullptr, &mipView), "creating the scaling view");
|
|
106
|
+
}
|
|
107
|
+
duplicate();
|
|
108
|
+
image.assign(size_t(width) * height * 4, 0);
|
|
109
|
+
worker = std::thread([this] { run(); });
|
|
110
|
+
}
|
|
111
|
+
~Duplicator() {
|
|
112
|
+
stopping = true;
|
|
113
|
+
if (worker.joinable()) worker.join();
|
|
114
|
+
}
|
|
115
|
+
void subscribe(WindowsCapture* source);
|
|
116
|
+
void unsubscribe(WindowsCapture* source);
|
|
117
|
+
/// Waits up to timeout for the first frame so a new source never returns an empty image.
|
|
118
|
+
void waitForFrame(std::chrono::milliseconds timeout) {
|
|
119
|
+
std::unique_lock<std::mutex> lock(mutex);
|
|
120
|
+
arrived.wait_for(lock, timeout, [this] { return haveFrame || !failure.empty(); });
|
|
121
|
+
}
|
|
122
|
+
/// Copies the latest image into target, entirely or only inside rects.
|
|
123
|
+
void copy(std::vector<uint8_t>& target, const std::vector<DamageRect>* rects) {
|
|
124
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
125
|
+
if (!failure.empty()) throw std::runtime_error(failure);
|
|
126
|
+
if (!rects) { target = image; return; }
|
|
127
|
+
for (const auto& rect : *rects)
|
|
128
|
+
for (int y = rect.y; y < rect.y + rect.height; ++y)
|
|
129
|
+
std::memcpy(target.data() + (size_t(y) * width + rect.x) * 4, image.data() + (size_t(y) * width + rect.x) * 4,
|
|
130
|
+
size_t(rect.width) * 4);
|
|
131
|
+
}
|
|
132
|
+
std::optional<CursorImage> currentCursor() {
|
|
133
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
134
|
+
if (!shape) return std::nullopt;
|
|
135
|
+
return visible ? *shape : hidden();
|
|
136
|
+
}
|
|
137
|
+
static CursorImage hidden() {
|
|
138
|
+
CursorImage image;
|
|
139
|
+
image.width = image.height = 1;
|
|
140
|
+
image.rgba.assign(4, 0);
|
|
141
|
+
return image;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private:
|
|
145
|
+
int level = 0;
|
|
146
|
+
ComPtr<ID3D11Device> device;
|
|
147
|
+
ComPtr<ID3D11DeviceContext> context;
|
|
148
|
+
ComPtr<IDXGIOutputDuplication> duplication;
|
|
149
|
+
ComPtr<ID3D11Texture2D> mips, staging;
|
|
150
|
+
ComPtr<ID3D11ShaderResourceView> mipView;
|
|
151
|
+
std::mutex mutex;
|
|
152
|
+
std::condition_variable arrived;
|
|
153
|
+
std::vector<uint8_t> image;
|
|
154
|
+
std::vector<WindowsCapture*> sources;
|
|
155
|
+
std::optional<CursorImage> shape;
|
|
156
|
+
bool visible = true, haveFrame = false;
|
|
157
|
+
std::string failure;
|
|
158
|
+
std::atomic<bool> stopping{false};
|
|
159
|
+
std::thread worker;
|
|
160
|
+
|
|
161
|
+
void duplicate() {
|
|
162
|
+
duplication.Reset();
|
|
163
|
+
const HRESULT result = display.output->DuplicateOutput(device.Get(), &duplication);
|
|
164
|
+
if (result == E_ACCESSDENIED || result == DXGI_ERROR_UNSUPPORTED || result == DXGI_ERROR_SESSION_DISCONNECTED)
|
|
165
|
+
throw std::runtime_error("Desktop Duplication is unavailable; start relay in the signed-in user's desktop session");
|
|
166
|
+
windows::check(result, "duplicating the display");
|
|
167
|
+
}
|
|
168
|
+
void run();
|
|
169
|
+
bool reconnect();
|
|
170
|
+
void fail(const std::string& message);
|
|
171
|
+
void pointer(const DXGI_OUTDUPL_FRAME_INFO& info, std::vector<uint8_t>& data);
|
|
172
|
+
bool dirtyRects(const DXGI_OUTDUPL_FRAME_INFO& info, std::vector<uint8_t>& metadata, std::vector<DamageRect>& damage);
|
|
173
|
+
void copyFrame(const ComPtr<IDXGIResource>& resource, const std::vector<DamageRect>& damage, bool full);
|
|
174
|
+
};
|
|
175
|
+
/// One consumer of the shared duplication, such as the stream encoder or a screenshot, with its own damage and image.
|
|
176
|
+
class WindowsCapture final : public CaptureSource {
|
|
177
|
+
public:
|
|
178
|
+
WindowsCapture() : duplicator(Duplicator::shared()) {
|
|
179
|
+
width = duplicator->width; height = duplicator->height;
|
|
180
|
+
ready = CreateEventW(nullptr, TRUE, TRUE, nullptr);
|
|
181
|
+
if (!ready) throw std::runtime_error("creating the capture event failed");
|
|
182
|
+
front.assign(size_t(width) * height * 4, 0);
|
|
183
|
+
duplicator->subscribe(this);
|
|
184
|
+
duplicator->waitForFrame(std::chrono::seconds(2));
|
|
185
|
+
}
|
|
186
|
+
~WindowsCapture() override {
|
|
187
|
+
duplicator->unsubscribe(this);
|
|
188
|
+
CloseHandle(ready);
|
|
189
|
+
}
|
|
190
|
+
WaitHandle fd() const override { return ready; }
|
|
191
|
+
std::pair<int, int> dimensions() const override { return {width, height}; }
|
|
192
|
+
PixelFormat pixelFormat() const override { return PixelFormat::BGRA; }
|
|
193
|
+
bool drainDamage() override {
|
|
194
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
195
|
+
ResetEvent(ready);
|
|
196
|
+
return pendingFull || !pending.empty();
|
|
197
|
+
}
|
|
198
|
+
FrameView capture() override { return captureChanged(true); }
|
|
199
|
+
FrameView captureChanged(bool full) override {
|
|
200
|
+
std::vector<DamageRect> rects;
|
|
201
|
+
{
|
|
202
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
203
|
+
lastFull = full || pendingFull;
|
|
204
|
+
rects.swap(pending);
|
|
205
|
+
pendingFull = false;
|
|
206
|
+
last = lastFull ? std::vector<DamageRect>{} : rects;
|
|
207
|
+
}
|
|
208
|
+
duplicator->copy(front, lastFull ? nullptr : &rects);
|
|
209
|
+
return {front.data(), width * 4, width, height, PixelFormat::BGRA};
|
|
210
|
+
}
|
|
211
|
+
bool changedRects(std::vector<DamageRect>& rects) override {
|
|
212
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
213
|
+
if (lastFull) return false;
|
|
214
|
+
rects = last;
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
bool cursorSupported() const override { return true; }
|
|
218
|
+
std::optional<CursorImage> cursor(bool force) override {
|
|
219
|
+
if (force) return duplicator->currentCursor();
|
|
220
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
221
|
+
auto next = std::move(cursorPending);
|
|
222
|
+
cursorPending.reset();
|
|
223
|
+
return next;
|
|
224
|
+
}
|
|
225
|
+
/// Called by the duplicator with the damage of each new frame.
|
|
226
|
+
void damaged(const std::vector<DamageRect>& rects, bool full) {
|
|
227
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
228
|
+
if (!full && !pendingFull) pending.insert(pending.end(), rects.begin(), rects.end());
|
|
229
|
+
if (full || pending.size() > max_pending_rects) { pendingFull = true; pending.clear(); }
|
|
230
|
+
SetEvent(ready);
|
|
231
|
+
}
|
|
232
|
+
/// Called by the duplicator when the pointer shape or visibility changes.
|
|
233
|
+
void cursorChanged(const CursorImage& image) {
|
|
234
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
235
|
+
cursorPending = image;
|
|
236
|
+
SetEvent(ready);
|
|
237
|
+
}
|
|
238
|
+
/// Called by the duplicator when capture fails; the pending full frame makes the next capture throw the error.
|
|
239
|
+
void failed() {
|
|
240
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
241
|
+
pendingFull = true;
|
|
242
|
+
pending.clear();
|
|
243
|
+
SetEvent(ready);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private:
|
|
247
|
+
std::shared_ptr<Duplicator> duplicator;
|
|
248
|
+
int width = 0, height = 0;
|
|
249
|
+
HANDLE ready = nullptr;
|
|
250
|
+
std::mutex mutex;
|
|
251
|
+
std::vector<uint8_t> front;
|
|
252
|
+
std::vector<DamageRect> pending, last;
|
|
253
|
+
bool pendingFull = true, lastFull = true;
|
|
254
|
+
std::optional<CursorImage> cursorPending;
|
|
255
|
+
};
|
|
256
|
+
void Duplicator::subscribe(WindowsCapture* source) {
|
|
257
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
258
|
+
sources.push_back(source);
|
|
259
|
+
}
|
|
260
|
+
void Duplicator::unsubscribe(WindowsCapture* source) {
|
|
261
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
262
|
+
sources.erase(std::remove(sources.begin(), sources.end(), source), sources.end());
|
|
263
|
+
}
|
|
264
|
+
void Duplicator::run() {
|
|
265
|
+
std::vector<uint8_t> metadata, shapeData;
|
|
266
|
+
while (!stopping) {
|
|
267
|
+
DXGI_OUTDUPL_FRAME_INFO info{};
|
|
268
|
+
ComPtr<IDXGIResource> resource;
|
|
269
|
+
const HRESULT result = duplication ? duplication->AcquireNextFrame(100, &info, &resource) : DXGI_ERROR_ACCESS_LOST;
|
|
270
|
+
if (result == DXGI_ERROR_WAIT_TIMEOUT) continue;
|
|
271
|
+
if (result == DXGI_ERROR_ACCESS_LOST || result == DXGI_ERROR_INVALID_CALL) {
|
|
272
|
+
if (!reconnect()) return;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
try {
|
|
276
|
+
windows::check(result, "capturing the display");
|
|
277
|
+
std::vector<DamageRect> damage;
|
|
278
|
+
bool full = false;
|
|
279
|
+
if (info.LastMouseUpdateTime.QuadPart) pointer(info, shapeData);
|
|
280
|
+
if (info.LastPresentTime.QuadPart) full = dirtyRects(info, metadata, damage);
|
|
281
|
+
if (full || !damage.empty()) copyFrame(resource, damage, full);
|
|
282
|
+
} catch (const std::exception& error) {
|
|
283
|
+
if (duplication && SUCCEEDED(result)) duplication->ReleaseFrame();
|
|
284
|
+
fail(error.what());
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
duplication->ReleaseFrame();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
bool Duplicator::reconnect() {
|
|
291
|
+
while (!stopping) {
|
|
292
|
+
try { duplicate(); break; } catch (const std::exception&) {}
|
|
293
|
+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
294
|
+
}
|
|
295
|
+
try {
|
|
296
|
+
const RECT now = windows::findOutput().bounds;
|
|
297
|
+
if (!EqualRect(&now, &display.bounds)) {
|
|
298
|
+
fail("The display changed from " + std::to_string(display.width()) + "x" + std::to_string(display.height()) + " to " +
|
|
299
|
+
std::to_string(now.right - now.left) + "x" + std::to_string(now.bottom - now.top) +
|
|
300
|
+
" or moved; restart relay to stream it");
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
} catch (const std::exception& error) {
|
|
304
|
+
fail(error.what());
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
308
|
+
for (auto* source : sources) source->damaged({}, true);
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
void Duplicator::fail(const std::string& message) {
|
|
312
|
+
duplication.Reset();
|
|
313
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
314
|
+
failure = message;
|
|
315
|
+
arrived.notify_all();
|
|
316
|
+
for (auto* source : sources) source->failed();
|
|
317
|
+
}
|
|
318
|
+
void Duplicator::pointer(const DXGI_OUTDUPL_FRAME_INFO& info, std::vector<uint8_t>& data) {
|
|
319
|
+
std::optional<CursorImage> changed;
|
|
320
|
+
if (info.PointerShapeBufferSize) {
|
|
321
|
+
data.resize(info.PointerShapeBufferSize);
|
|
322
|
+
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo{};
|
|
323
|
+
UINT required = 0;
|
|
324
|
+
windows::check(duplication->GetFramePointerShape(UINT(data.size()), data.data(), &required, &shapeInfo),
|
|
325
|
+
"reading the pointer shape");
|
|
326
|
+
changed = cursorImage(shapeInfo, data, level);
|
|
327
|
+
}
|
|
328
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
329
|
+
const bool nowVisible = info.PointerPosition.Visible;
|
|
330
|
+
if (changed) shape = changed;
|
|
331
|
+
if ((!changed && nowVisible == visible) || !shape) { visible = nowVisible; return; }
|
|
332
|
+
visible = nowVisible;
|
|
333
|
+
const CursorImage image = visible ? *shape : hidden();
|
|
334
|
+
for (auto* source : sources) source->cursorChanged(image);
|
|
335
|
+
}
|
|
336
|
+
bool Duplicator::dirtyRects(const DXGI_OUTDUPL_FRAME_INFO& info, std::vector<uint8_t>& metadata,
|
|
337
|
+
std::vector<DamageRect>& damage) {
|
|
338
|
+
if (!info.TotalMetadataBufferSize) return true;
|
|
339
|
+
metadata.resize(info.TotalMetadataBufferSize);
|
|
340
|
+
UINT used = 0;
|
|
341
|
+
windows::check(duplication->GetFrameMoveRects(UINT(metadata.size()), reinterpret_cast<DXGI_OUTDUPL_MOVE_RECT*>(metadata.data()), &used),
|
|
342
|
+
"reading moved regions");
|
|
343
|
+
std::vector<RECT> rects;
|
|
344
|
+
for (UINT i = 0; i < used / sizeof(DXGI_OUTDUPL_MOVE_RECT); ++i)
|
|
345
|
+
rects.push_back(reinterpret_cast<DXGI_OUTDUPL_MOVE_RECT*>(metadata.data())[i].DestinationRect);
|
|
346
|
+
UINT dirty = 0;
|
|
347
|
+
windows::check(duplication->GetFrameDirtyRects(UINT(metadata.size()), reinterpret_cast<RECT*>(metadata.data()), &dirty),
|
|
348
|
+
"reading changed regions");
|
|
349
|
+
for (UINT i = 0; i < dirty / sizeof(RECT); ++i) rects.push_back(reinterpret_cast<RECT*>(metadata.data())[i]);
|
|
350
|
+
const int round = (1 << level) - 1;
|
|
351
|
+
for (const auto& r : rects) {
|
|
352
|
+
const int x0 = std::clamp(int(r.left) >> level, 0, width), y0 = std::clamp(int(r.top) >> level, 0, height);
|
|
353
|
+
const int x1 = std::clamp((int(r.right) + round) >> level, 0, width);
|
|
354
|
+
const int y1 = std::clamp((int(r.bottom) + round) >> level, 0, height);
|
|
355
|
+
if (x1 > x0 && y1 > y0) damage.push_back({x0, y0, x1 - x0, y1 - y0});
|
|
356
|
+
}
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
void Duplicator::copyFrame(const ComPtr<IDXGIResource>& resource, const std::vector<DamageRect>& damage, bool full) {
|
|
360
|
+
ComPtr<ID3D11Texture2D> desktop;
|
|
361
|
+
windows::check(resource.As(&desktop), "reading the desktop image");
|
|
362
|
+
const D3D11_BOX box{0, 0, 0, UINT(width), UINT(height), 1};
|
|
363
|
+
if (level) {
|
|
364
|
+
context->CopySubresourceRegion(mips.Get(), 0, 0, 0, 0, desktop.Get(), 0, nullptr);
|
|
365
|
+
context->GenerateMips(mipView.Get());
|
|
366
|
+
context->CopySubresourceRegion(staging.Get(), 0, 0, 0, 0, mips.Get(), UINT(level), &box);
|
|
367
|
+
} else {
|
|
368
|
+
context->CopySubresourceRegion(staging.Get(), 0, 0, 0, 0, desktop.Get(), 0, &box);
|
|
369
|
+
}
|
|
370
|
+
D3D11_MAPPED_SUBRESOURCE mapped{};
|
|
371
|
+
windows::check(context->Map(staging.Get(), 0, D3D11_MAP_READ, 0, &mapped), "reading back the desktop image");
|
|
372
|
+
const auto* source = static_cast<const uint8_t*>(mapped.pData);
|
|
373
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
374
|
+
const bool whole = full || !haveFrame;
|
|
375
|
+
if (whole) {
|
|
376
|
+
for (int y = 0; y < height; ++y)
|
|
377
|
+
std::memcpy(image.data() + size_t(y) * width * 4, source + size_t(y) * mapped.RowPitch, size_t(width) * 4);
|
|
378
|
+
} else {
|
|
379
|
+
for (const auto& rect : damage)
|
|
380
|
+
for (int y = rect.y; y < rect.y + rect.height; ++y)
|
|
381
|
+
std::memcpy(image.data() + (size_t(y) * width + rect.x) * 4, source + size_t(y) * mapped.RowPitch + size_t(rect.x) * 4,
|
|
382
|
+
size_t(rect.width) * 4);
|
|
383
|
+
}
|
|
384
|
+
context->Unmap(staging.Get(), 0);
|
|
385
|
+
haveFrame = true;
|
|
386
|
+
arrived.notify_all();
|
|
387
|
+
for (auto* consumer : sources) consumer->damaged(damage, whole);
|
|
388
|
+
}
|
|
389
|
+
} // namespace
|
|
390
|
+
namespace windows {
|
|
391
|
+
std::unique_ptr<CaptureSource> makeCaptureSource() { return std::make_unique<WindowsCapture>(); }
|
|
392
|
+
}
|
|
393
|
+
} // namespace relay
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
#include "core/input.hpp"
|
|
2
|
+
#include "backends/linux/keycodes.hpp"
|
|
3
|
+
#include "backends/windows/output.hpp"
|
|
4
|
+
#include <algorithm>
|
|
5
|
+
#include <condition_variable>
|
|
6
|
+
#include <mutex>
|
|
7
|
+
#include <set>
|
|
8
|
+
#include <stdexcept>
|
|
9
|
+
#include <thread>
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
namespace relay {
|
|
13
|
+
namespace {
|
|
14
|
+
constexpr NativeKey extended = 0x100, virtualKey = 0x200;
|
|
15
|
+
/// Set-1 scan code, with the extended flag, for an evdev keycode; Pause and Num Lock use virtual keys.
|
|
16
|
+
NativeKey scanCode(int keycode) {
|
|
17
|
+
if (keycode == 69) return virtualKey | VK_NUMLOCK;
|
|
18
|
+
if (keycode == 119) return virtualKey | VK_PAUSE;
|
|
19
|
+
if (keycode > 0 && keycode <= 88) return NativeKey(keycode);
|
|
20
|
+
if (keycode >= 183 && keycode <= 193) return NativeKey(0x64 + keycode - 183);
|
|
21
|
+
switch (keycode) {
|
|
22
|
+
case 194: return 0x76;
|
|
23
|
+
case 96: return extended | 0x1c; case 97: return extended | 0x1d; case 98: return extended | 0x35;
|
|
24
|
+
case 99: return extended | 0x37; case 100: return extended | 0x38; case 102: return extended | 0x47;
|
|
25
|
+
case 103: return extended | 0x48; case 104: return extended | 0x49; case 105: return extended | 0x4b;
|
|
26
|
+
case 106: return extended | 0x4d; case 107: return extended | 0x4f; case 108: return extended | 0x50;
|
|
27
|
+
case 109: return extended | 0x51; case 110: return extended | 0x52; case 111: return extended | 0x53;
|
|
28
|
+
case 117: return 0x59; case 125: return extended | 0x5b; case 126: return extended | 0x5c;
|
|
29
|
+
case 139: return extended | 0x5d;
|
|
30
|
+
default: return 0;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const std::set<NativeKey> modifierKeys = {0x1d, extended | 0x1d, 0x2a, 0x36, 0x38, extended | 0x38,
|
|
34
|
+
extended | 0x5b, extended | 0x5c};
|
|
35
|
+
INPUT keyInput(NativeKey key, bool press) {
|
|
36
|
+
INPUT input{};
|
|
37
|
+
input.type = INPUT_KEYBOARD;
|
|
38
|
+
if (key & virtualKey) {
|
|
39
|
+
input.ki.wVk = WORD(key & 0xff);
|
|
40
|
+
} else {
|
|
41
|
+
input.ki.wScan = WORD(key & 0xff);
|
|
42
|
+
input.ki.dwFlags = KEYEVENTF_SCANCODE | ((key & extended) ? KEYEVENTF_EXTENDEDKEY : 0);
|
|
43
|
+
}
|
|
44
|
+
if (!press) input.ki.dwFlags |= KEYEVENTF_KEYUP;
|
|
45
|
+
return input;
|
|
46
|
+
}
|
|
47
|
+
/// Applies queued input with SendInput on one thread attached to the active input desktop.
|
|
48
|
+
class WindowsInput final : public InputBackend {
|
|
49
|
+
RECT bounds{};
|
|
50
|
+
int desktopWidth = 0, desktopHeight = 0;
|
|
51
|
+
uint16_t width = 0, height = 0;
|
|
52
|
+
std::set<NativeKey> held;
|
|
53
|
+
mutable std::mutex mutex;
|
|
54
|
+
std::condition_variable wake;
|
|
55
|
+
InputQueue queue;
|
|
56
|
+
bool stopping = false, rejected = false;
|
|
57
|
+
std::thread worker;
|
|
58
|
+
|
|
59
|
+
public:
|
|
60
|
+
WindowsInput() {
|
|
61
|
+
const auto display = windows::findOutput();
|
|
62
|
+
bounds = display.bounds;
|
|
63
|
+
desktopWidth = display.width();
|
|
64
|
+
desktopHeight = display.height();
|
|
65
|
+
const auto size = display.streamSize();
|
|
66
|
+
width = uint16_t(size.first);
|
|
67
|
+
height = uint16_t(size.second);
|
|
68
|
+
worker = std::thread([this] { run(); });
|
|
69
|
+
}
|
|
70
|
+
~WindowsInput() override {
|
|
71
|
+
{ std::lock_guard<std::mutex> lock(mutex); stopping = true; }
|
|
72
|
+
wake.notify_all();
|
|
73
|
+
if (worker.joinable()) worker.join();
|
|
74
|
+
}
|
|
75
|
+
std::pair<uint16_t, uint16_t> dimensions() const override { return {width, height}; }
|
|
76
|
+
const char* name() const override { return "windows"; }
|
|
77
|
+
std::optional<NativeKey> nativeKey(uint16_t usage) const override {
|
|
78
|
+
const NativeKey key = scanCode(hidKeycode(usage));
|
|
79
|
+
if (!key) return std::nullopt;
|
|
80
|
+
return key;
|
|
81
|
+
}
|
|
82
|
+
bool typeText(uint32_t codepoint) override {
|
|
83
|
+
if (codepoint == 0 || codepoint > 0x10ffff || (codepoint >= 0xd800 && codepoint <= 0xdfff)) return false;
|
|
84
|
+
InputJob job{InputJob::Kind::text};
|
|
85
|
+
job.codepoint = codepoint;
|
|
86
|
+
post(job);
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
void pressKey(NativeKey key) override { postKey(key, true); }
|
|
90
|
+
void releaseKey(NativeKey key) override { postKey(key, false); }
|
|
91
|
+
void pressButton(unsigned button) override { postButton(button, true); }
|
|
92
|
+
void releaseButton(unsigned button) override { postButton(button, false); }
|
|
93
|
+
void move(int x, int y) override {
|
|
94
|
+
InputJob job{InputJob::Kind::motion};
|
|
95
|
+
job.x = x; job.y = y;
|
|
96
|
+
post(job);
|
|
97
|
+
}
|
|
98
|
+
void scroll(unsigned axis, int notches) override {
|
|
99
|
+
InputJob job{InputJob::Kind::scroll};
|
|
100
|
+
job.number = axis; job.notches = notches;
|
|
101
|
+
post(job);
|
|
102
|
+
}
|
|
103
|
+
void resetLocks() override { post(InputJob{InputJob::Kind::locks}); }
|
|
104
|
+
void fence(std::shared_ptr<InputFence> mark) override {
|
|
105
|
+
InputJob job{InputJob::Kind::fence};
|
|
106
|
+
job.fence = std::move(mark);
|
|
107
|
+
post(job);
|
|
108
|
+
}
|
|
109
|
+
void cancel() override {
|
|
110
|
+
{ std::lock_guard<std::mutex> lock(mutex); queue.cancel(); }
|
|
111
|
+
wake.notify_one();
|
|
112
|
+
}
|
|
113
|
+
void flush() override { post(InputJob{InputJob::Kind::flush}); }
|
|
114
|
+
size_t pending() const override {
|
|
115
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
116
|
+
return queue.pending();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private:
|
|
120
|
+
void postKey(NativeKey key, bool press) {
|
|
121
|
+
InputJob job{InputJob::Kind::key};
|
|
122
|
+
job.code = key; job.press = press;
|
|
123
|
+
post(job);
|
|
124
|
+
}
|
|
125
|
+
void postButton(unsigned button, bool press) {
|
|
126
|
+
InputJob job{InputJob::Kind::button};
|
|
127
|
+
job.number = button; job.press = press;
|
|
128
|
+
post(job);
|
|
129
|
+
}
|
|
130
|
+
void post(InputJob job) {
|
|
131
|
+
{ std::lock_guard<std::mutex> lock(mutex); queue.post(std::move(job)); }
|
|
132
|
+
wake.notify_one();
|
|
133
|
+
}
|
|
134
|
+
void attach() {
|
|
135
|
+
if (HDESK desktop = OpenInputDesktop(0, FALSE, GENERIC_ALL)) {
|
|
136
|
+
SetThreadDesktop(desktop);
|
|
137
|
+
CloseDesktop(desktop);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
void run() {
|
|
141
|
+
attach();
|
|
142
|
+
std::unique_lock<std::mutex> lock(mutex);
|
|
143
|
+
for (;;) {
|
|
144
|
+
wake.wait(lock, [this] { return stopping || !queue.empty(); });
|
|
145
|
+
if (stopping) queue.cancel();
|
|
146
|
+
if (queue.empty()) break;
|
|
147
|
+
const InputJob job = queue.take();
|
|
148
|
+
lock.unlock();
|
|
149
|
+
apply(job);
|
|
150
|
+
lock.lock();
|
|
151
|
+
}
|
|
152
|
+
lock.unlock();
|
|
153
|
+
std::vector<INPUT> releases;
|
|
154
|
+
for (const auto key : held) releases.push_back(keyInput(key, false));
|
|
155
|
+
send(releases);
|
|
156
|
+
}
|
|
157
|
+
bool send(std::vector<INPUT>& inputs) {
|
|
158
|
+
if (inputs.empty()) return true;
|
|
159
|
+
if (SendInput(UINT(inputs.size()), inputs.data(), sizeof(INPUT)) == inputs.size()) return true;
|
|
160
|
+
attach();
|
|
161
|
+
return SendInput(UINT(inputs.size()), inputs.data(), sizeof(INPUT)) == inputs.size();
|
|
162
|
+
}
|
|
163
|
+
void apply(const InputJob& job) {
|
|
164
|
+
std::vector<INPUT> inputs;
|
|
165
|
+
switch (job.kind) {
|
|
166
|
+
case InputJob::Kind::key:
|
|
167
|
+
if (job.press) held.insert(job.code); else held.erase(job.code);
|
|
168
|
+
inputs.push_back(keyInput(job.code, job.press));
|
|
169
|
+
break;
|
|
170
|
+
case InputJob::Kind::text: type(job.codepoint, inputs); break;
|
|
171
|
+
case InputJob::Kind::button: inputs.push_back(button(job.number, job.press)); break;
|
|
172
|
+
case InputJob::Kind::motion: inputs.push_back(motion(job.x, job.y)); break;
|
|
173
|
+
case InputJob::Kind::scroll: {
|
|
174
|
+
INPUT input{};
|
|
175
|
+
input.type = INPUT_MOUSE;
|
|
176
|
+
input.mi.dwFlags = job.number == 0 ? MOUSEEVENTF_WHEEL : MOUSEEVENTF_HWHEEL;
|
|
177
|
+
input.mi.mouseData = DWORD(job.number == 0 ? -job.notches * WHEEL_DELTA : job.notches * WHEEL_DELTA);
|
|
178
|
+
inputs.push_back(input);
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
case InputJob::Kind::locks:
|
|
182
|
+
if (GetKeyState(VK_CAPITAL) & 1) {
|
|
183
|
+
inputs.push_back(keyInput(virtualKey | VK_CAPITAL, true));
|
|
184
|
+
inputs.push_back(keyInput(virtualKey | VK_CAPITAL, false));
|
|
185
|
+
}
|
|
186
|
+
break;
|
|
187
|
+
case InputJob::Kind::flush: break;
|
|
188
|
+
case InputJob::Kind::fence:
|
|
189
|
+
if (rejected && job.fence) job.fence->complete(false, job.dropped);
|
|
190
|
+
else job.complete();
|
|
191
|
+
rejected = false;
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (!send(inputs)) rejected = true;
|
|
195
|
+
}
|
|
196
|
+
void type(uint32_t codepoint, std::vector<INPUT>& inputs) {
|
|
197
|
+
if (codepoint == '\n' || codepoint == '\t') {
|
|
198
|
+
const NativeKey key = codepoint == '\n' ? 0x1c : 0x0f;
|
|
199
|
+
inputs.push_back(keyInput(key, true));
|
|
200
|
+
inputs.push_back(keyInput(key, false));
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
std::vector<NativeKey> lifted;
|
|
204
|
+
for (const auto key : held) if (modifierKeys.count(key)) lifted.push_back(key);
|
|
205
|
+
for (const auto key : lifted) inputs.push_back(keyInput(key, false));
|
|
206
|
+
if (layoutKey(codepoint, inputs)) {
|
|
207
|
+
for (auto it = lifted.rbegin(); it != lifted.rend(); ++it) inputs.push_back(keyInput(*it, true));
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
wchar_t units[2];
|
|
211
|
+
int count = 1;
|
|
212
|
+
if (codepoint >= 0x10000) {
|
|
213
|
+
units[0] = wchar_t(0xd800 + ((codepoint - 0x10000) >> 10));
|
|
214
|
+
units[1] = wchar_t(0xdc00 + ((codepoint - 0x10000) & 0x3ff));
|
|
215
|
+
count = 2;
|
|
216
|
+
} else {
|
|
217
|
+
units[0] = wchar_t(codepoint);
|
|
218
|
+
}
|
|
219
|
+
for (const bool press : {true, false})
|
|
220
|
+
for (int i = 0; i < count; ++i) {
|
|
221
|
+
INPUT input{};
|
|
222
|
+
input.type = INPUT_KEYBOARD;
|
|
223
|
+
input.ki.wScan = WORD(units[i]);
|
|
224
|
+
input.ki.dwFlags = KEYEVENTF_UNICODE | (press ? 0 : KEYEVENTF_KEYUP);
|
|
225
|
+
inputs.push_back(input);
|
|
226
|
+
}
|
|
227
|
+
for (auto it = lifted.rbegin(); it != lifted.rend(); ++it) inputs.push_back(keyInput(*it, true));
|
|
228
|
+
}
|
|
229
|
+
/// Types codepoint with its key on the foreground window's keyboard layout, if the layout has one.
|
|
230
|
+
bool layoutKey(uint32_t codepoint, std::vector<INPUT>& inputs) {
|
|
231
|
+
if (codepoint > 0xffff) return false;
|
|
232
|
+
const HKL layout = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), nullptr));
|
|
233
|
+
const SHORT found = VkKeyScanExW(wchar_t(codepoint), layout);
|
|
234
|
+
if (found == -1 || (HIBYTE(found) & ~7)) return false;
|
|
235
|
+
const UINT scan = MapVirtualKeyExW(LOBYTE(found), MAPVK_VK_TO_VSC_EX, layout);
|
|
236
|
+
if (!scan) return false;
|
|
237
|
+
const NativeKey key = (scan & 0xff) | ((scan & 0xff00) == 0xe000 ? extended : 0);
|
|
238
|
+
std::vector<NativeKey> shifts;
|
|
239
|
+
if (HIBYTE(found) & 1) shifts.push_back(0x2a);
|
|
240
|
+
if (HIBYTE(found) & 2) shifts.push_back(0x1d);
|
|
241
|
+
if (HIBYTE(found) & 4) shifts.push_back(0x38);
|
|
242
|
+
for (const auto shift : shifts) inputs.push_back(keyInput(shift, true));
|
|
243
|
+
inputs.push_back(keyInput(key, true));
|
|
244
|
+
inputs.push_back(keyInput(key, false));
|
|
245
|
+
for (auto it = shifts.rbegin(); it != shifts.rend(); ++it) inputs.push_back(keyInput(*it, false));
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
INPUT button(unsigned number, bool press) {
|
|
249
|
+
INPUT input{};
|
|
250
|
+
input.type = INPUT_MOUSE;
|
|
251
|
+
if (number == 1) input.mi.dwFlags = press ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
|
|
252
|
+
else if (number == 2) input.mi.dwFlags = press ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP;
|
|
253
|
+
else input.mi.dwFlags = press ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP;
|
|
254
|
+
return input;
|
|
255
|
+
}
|
|
256
|
+
INPUT motion(int x, int y) {
|
|
257
|
+
const int left = GetSystemMetrics(SM_XVIRTUALSCREEN), top = GetSystemMetrics(SM_YVIRTUALSCREEN);
|
|
258
|
+
const int spanX = std::max(1, GetSystemMetrics(SM_CXVIRTUALSCREEN) - 1);
|
|
259
|
+
const int spanY = std::max(1, GetSystemMetrics(SM_CYVIRTUALSCREEN) - 1);
|
|
260
|
+
INPUT input{};
|
|
261
|
+
input.type = INPUT_MOUSE;
|
|
262
|
+
const int64_t px = bounds.left + (int64_t(2 * x + 1) * desktopWidth) / (2 * width) - left;
|
|
263
|
+
const int64_t py = bounds.top + (int64_t(2 * y + 1) * desktopHeight) / (2 * height) - top;
|
|
264
|
+
input.mi.dx = LONG((px * 65535 + spanX / 2) / spanX);
|
|
265
|
+
input.mi.dy = LONG((py * 65535 + spanY / 2) / spanY);
|
|
266
|
+
input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK;
|
|
267
|
+
return input;
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
} // namespace
|
|
271
|
+
namespace windows {
|
|
272
|
+
std::unique_ptr<InputBackend> makeInputBackend() { return std::make_unique<WindowsInput>(); }
|
|
273
|
+
}
|
|
274
|
+
} // namespace relay
|