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,559 @@
|
|
|
1
|
+
#include "backends/linux/keycodes.hpp"
|
|
2
|
+
#include "backends/linux/wayland/keymap.hpp"
|
|
3
|
+
#include "backends/linux/wayland/wayland.hpp"
|
|
4
|
+
#include "core/input.hpp"
|
|
5
|
+
#include <linux/input-event-codes.h>
|
|
6
|
+
#include <poll.h>
|
|
7
|
+
#include <sys/mman.h>
|
|
8
|
+
#include <sys/stat.h>
|
|
9
|
+
#include <unistd.h>
|
|
10
|
+
#include <xkbcommon/xkbcommon.h>
|
|
11
|
+
#include <algorithm>
|
|
12
|
+
#include <cerrno>
|
|
13
|
+
#include <chrono>
|
|
14
|
+
#include <condition_variable>
|
|
15
|
+
#include <cstdio>
|
|
16
|
+
#include <cstring>
|
|
17
|
+
#include <exception>
|
|
18
|
+
#include <limits>
|
|
19
|
+
#include <memory>
|
|
20
|
+
#include <mutex>
|
|
21
|
+
#include <set>
|
|
22
|
+
#include <stdexcept>
|
|
23
|
+
#include <string>
|
|
24
|
+
#include <thread>
|
|
25
|
+
#include <utility>
|
|
26
|
+
#include <vector>
|
|
27
|
+
|
|
28
|
+
namespace relay::wayland {
|
|
29
|
+
namespace {
|
|
30
|
+
using Seat = std::unique_ptr<wl_seat, decltype(&wl_seat_destroy)>;
|
|
31
|
+
using Keyboard = std::unique_ptr<wl_keyboard, decltype(&wl_keyboard_destroy)>;
|
|
32
|
+
using Registry = std::unique_ptr<wl_registry, decltype(&wl_registry_destroy)>;
|
|
33
|
+
using VirtualKeyboard = std::unique_ptr<zwp_virtual_keyboard_v1, decltype(&zwp_virtual_keyboard_v1_destroy)>;
|
|
34
|
+
using VirtualPointer = std::unique_ptr<zwlr_virtual_pointer_v1, decltype(&zwlr_virtual_pointer_v1_destroy)>;
|
|
35
|
+
|
|
36
|
+
class Mapping {
|
|
37
|
+
public:
|
|
38
|
+
Mapping(int fd, size_t size) : size_(size), data_(::mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0)) {
|
|
39
|
+
if (data_ == MAP_FAILED) throw std::runtime_error("Cannot map the Wayland keyboard keymap");
|
|
40
|
+
}
|
|
41
|
+
~Mapping() { ::munmap(data_, size_); }
|
|
42
|
+
Mapping(const Mapping&) = delete;
|
|
43
|
+
Mapping& operator=(const Mapping&) = delete;
|
|
44
|
+
const char* data() const { return static_cast<const char*>(data_); }
|
|
45
|
+
|
|
46
|
+
private:
|
|
47
|
+
size_t size_;
|
|
48
|
+
void* data_;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
struct SeatWatch {
|
|
52
|
+
bool hasKeyboard = false;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
struct SeatRegistry {
|
|
56
|
+
Seat seat{nullptr, wl_seat_destroy};
|
|
57
|
+
SeatWatch* watch = nullptr;
|
|
58
|
+
bool failed = false;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
struct KeymapCapture {
|
|
62
|
+
std::string text;
|
|
63
|
+
std::exception_ptr error;
|
|
64
|
+
bool received = false;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
void seatCapabilities(void* data, wl_seat*, uint32_t capabilities) {
|
|
68
|
+
static_cast<SeatWatch*>(data)->hasKeyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
void seatName(void*, wl_seat*, const char*) {}
|
|
72
|
+
|
|
73
|
+
const wl_seat_listener seatListener = {seatCapabilities, seatName};
|
|
74
|
+
|
|
75
|
+
void registryGlobal(void* data, wl_registry* registry, uint32_t name, const char* interface, uint32_t version) {
|
|
76
|
+
auto* state = static_cast<SeatRegistry*>(data);
|
|
77
|
+
if (state->seat || std::strcmp(interface, wl_seat_interface.name) != 0) return;
|
|
78
|
+
state->seat.reset(static_cast<wl_seat*>(wl_registry_bind(registry, name, &wl_seat_interface,
|
|
79
|
+
std::min(version, 1u))));
|
|
80
|
+
if (!state->seat || wl_seat_add_listener(state->seat.get(), &seatListener, state->watch) < 0)
|
|
81
|
+
state->failed = true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
void registryGlobalRemove(void*, wl_registry*, uint32_t) {}
|
|
85
|
+
|
|
86
|
+
const wl_registry_listener registryListener = {registryGlobal, registryGlobalRemove};
|
|
87
|
+
|
|
88
|
+
void keyboardKeymap(void* data, wl_keyboard*, uint32_t format, int32_t fd, uint32_t size) {
|
|
89
|
+
auto* capture = static_cast<KeymapCapture*>(data);
|
|
90
|
+
FileDescriptor keymapFd(fd);
|
|
91
|
+
try {
|
|
92
|
+
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 || fd < 0 || !size)
|
|
93
|
+
throw std::runtime_error("Wayland seat did not provide an XKB v1 keyboard keymap");
|
|
94
|
+
struct stat status {};
|
|
95
|
+
if (::fstat(keymapFd.get(), &status) < 0 || status.st_size < 0 || uint64_t(status.st_size) < size)
|
|
96
|
+
throw std::runtime_error("Wayland keyboard keymap fd is shorter than its advertised size");
|
|
97
|
+
Mapping mapping(keymapFd.get(), size);
|
|
98
|
+
const char* end = static_cast<const char*>(std::memchr(mapping.data(), '\0', size));
|
|
99
|
+
capture->text.assign(mapping.data(), end ? size_t(end - mapping.data()) : size_t(size));
|
|
100
|
+
if (capture->text.empty()) throw std::runtime_error("Wayland seat provided an empty keyboard keymap");
|
|
101
|
+
capture->received = true;
|
|
102
|
+
} catch (...) {
|
|
103
|
+
capture->error = std::current_exception();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
void keyboardEnter(void*, wl_keyboard*, uint32_t, wl_surface*, wl_array*) {}
|
|
108
|
+
void keyboardLeave(void*, wl_keyboard*, uint32_t, wl_surface*) {}
|
|
109
|
+
void keyboardKey(void*, wl_keyboard*, uint32_t, uint32_t, uint32_t, uint32_t) {}
|
|
110
|
+
void keyboardModifiers(void*, wl_keyboard*, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t) {}
|
|
111
|
+
void keyboardRepeatInfo(void*, wl_keyboard*, int32_t, int32_t) {}
|
|
112
|
+
|
|
113
|
+
const wl_keyboard_listener keyboardListener = {
|
|
114
|
+
keyboardKeymap, keyboardEnter, keyboardLeave, keyboardKey, keyboardModifiers, keyboardRepeatInfo
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
uint32_t linuxButton(unsigned button) {
|
|
118
|
+
switch (button) {
|
|
119
|
+
case 1: return BTN_LEFT;
|
|
120
|
+
case 2: return BTN_MIDDLE;
|
|
121
|
+
case 3: return BTN_RIGHT;
|
|
122
|
+
default: throw std::runtime_error("Unsupported Wayland pointer button");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
std::set<uint32_t> findModifierKeys(xkb_keymap* keymap) {
|
|
127
|
+
std::set<uint32_t> result;
|
|
128
|
+
const auto modifiers = xkb_keymap_num_mods(keymap);
|
|
129
|
+
for (xkb_keycode_t key = xkb_keymap_min_keycode(keymap); key <= xkb_keymap_max_keycode(keymap); ++key) {
|
|
130
|
+
if (key < 8) continue;
|
|
131
|
+
XkbState state(xkb_state_new(keymap));
|
|
132
|
+
if (!state) throw std::runtime_error("Cannot create XKB state to identify modifiers");
|
|
133
|
+
bool modifier = false;
|
|
134
|
+
for (const auto direction : {XKB_KEY_DOWN, XKB_KEY_UP}) {
|
|
135
|
+
xkb_state_update_key(state.get(), key, direction);
|
|
136
|
+
for (xkb_mod_index_t index = 0; index < modifiers; ++index)
|
|
137
|
+
if (xkb_state_mod_index_is_active(state.get(), index, XKB_STATE_MODS_EFFECTIVE) > 0) {
|
|
138
|
+
modifier = true;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
if (modifier) break;
|
|
142
|
+
}
|
|
143
|
+
if (modifier) result.insert(key - 8);
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
bool flushDisplay(wl_display* display) {
|
|
149
|
+
for (;;) {
|
|
150
|
+
if (wl_display_flush(display) >= 0) return true;
|
|
151
|
+
if (errno == EINTR) continue;
|
|
152
|
+
if (errno != EAGAIN && errno != EWOULDBLOCK) return false;
|
|
153
|
+
const int fd = wl_display_get_fd(display);
|
|
154
|
+
if (fd < 0) return false;
|
|
155
|
+
pollfd writable{fd, POLLOUT, 0};
|
|
156
|
+
int ready;
|
|
157
|
+
do { ready = ::poll(&writable, 1, 5000); } while (ready < 0 && errno == EINTR);
|
|
158
|
+
if (ready <= 0 || (writable.revents & (POLLERR | POLLHUP | POLLNVAL))) return false;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
class WaylandInput final : public InputBackend {
|
|
164
|
+
public:
|
|
165
|
+
WaylandInput() {
|
|
166
|
+
worker_ = std::thread([this] { run(); });
|
|
167
|
+
std::unique_lock<std::mutex> lock(mutex_);
|
|
168
|
+
wake_.wait(lock, [this] { return startupDone_; });
|
|
169
|
+
const auto error = startupError_;
|
|
170
|
+
lock.unlock();
|
|
171
|
+
if (error) {
|
|
172
|
+
if (worker_.joinable()) worker_.join();
|
|
173
|
+
std::rethrow_exception(error);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
~WaylandInput() override {
|
|
178
|
+
{
|
|
179
|
+
std::lock_guard<std::mutex> lock(mutex_);
|
|
180
|
+
stopping_ = true;
|
|
181
|
+
}
|
|
182
|
+
wake_.notify_all();
|
|
183
|
+
if (worker_.joinable()) worker_.join();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
std::optional<NativeKey> nativeKey(uint16_t usage) const override {
|
|
187
|
+
const int keycode = hidKeycode(usage);
|
|
188
|
+
if (!keycode) return std::nullopt;
|
|
189
|
+
return NativeKey(keycode);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
void pressKey(NativeKey key) override {
|
|
193
|
+
InputJob job{InputJob::Kind::key};
|
|
194
|
+
job.code = key;
|
|
195
|
+
job.press = true;
|
|
196
|
+
post(std::move(job));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
void releaseKey(NativeKey key) override {
|
|
200
|
+
InputJob job{InputJob::Kind::key};
|
|
201
|
+
job.code = key;
|
|
202
|
+
post(std::move(job));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
bool typeText(uint32_t codepoint) override {
|
|
206
|
+
{
|
|
207
|
+
std::lock_guard<std::mutex> lock(mutex_);
|
|
208
|
+
if (stopping_ || workerFailed_ || !xkb_utf32_to_keysym(codepoint)) return false;
|
|
209
|
+
InputJob job{InputJob::Kind::text};
|
|
210
|
+
job.codepoint = codepoint;
|
|
211
|
+
queue_.post(std::move(job));
|
|
212
|
+
}
|
|
213
|
+
wake_.notify_one();
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
void pressButton(unsigned button) override {
|
|
218
|
+
InputJob job{InputJob::Kind::button};
|
|
219
|
+
job.number = button;
|
|
220
|
+
job.press = true;
|
|
221
|
+
post(std::move(job));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
void releaseButton(unsigned button) override {
|
|
225
|
+
InputJob job{InputJob::Kind::button};
|
|
226
|
+
job.number = button;
|
|
227
|
+
post(std::move(job));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
void move(int x, int y) override {
|
|
231
|
+
InputJob job{InputJob::Kind::motion};
|
|
232
|
+
job.x = x;
|
|
233
|
+
job.y = y;
|
|
234
|
+
post(std::move(job));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
void scroll(unsigned axis, int notches) override {
|
|
238
|
+
InputJob job{InputJob::Kind::scroll};
|
|
239
|
+
job.number = axis;
|
|
240
|
+
job.notches = notches;
|
|
241
|
+
post(std::move(job));
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
void resetLocks() override { post(InputJob{InputJob::Kind::locks}); }
|
|
245
|
+
|
|
246
|
+
void fence(std::shared_ptr<InputFence> mark) override {
|
|
247
|
+
InputJob job{InputJob::Kind::fence};
|
|
248
|
+
job.fence = std::move(mark);
|
|
249
|
+
post(std::move(job));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
size_t pending() const override {
|
|
253
|
+
std::lock_guard<std::mutex> lock(mutex_);
|
|
254
|
+
return queue_.pending();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
void cancel() override {
|
|
258
|
+
{
|
|
259
|
+
std::lock_guard<std::mutex> lock(mutex_);
|
|
260
|
+
queue_.cancel();
|
|
261
|
+
}
|
|
262
|
+
wake_.notify_one();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
void flush() override { post(InputJob{InputJob::Kind::flush}); }
|
|
266
|
+
|
|
267
|
+
std::pair<uint16_t, uint16_t> dimensions() const override { return {width_, height_}; }
|
|
268
|
+
const char* name() const override { return "wayland"; }
|
|
269
|
+
|
|
270
|
+
private:
|
|
271
|
+
struct Masks {
|
|
272
|
+
xkb_mod_mask_t latched = 0;
|
|
273
|
+
xkb_mod_mask_t locked = 0;
|
|
274
|
+
xkb_layout_index_t latchedLayout = 0;
|
|
275
|
+
xkb_layout_index_t lockedLayout = 0;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
std::unique_ptr<Connection> connection_;
|
|
279
|
+
Seat seat_{nullptr, wl_seat_destroy};
|
|
280
|
+
VirtualKeyboard keyboard_{nullptr, zwp_virtual_keyboard_v1_destroy};
|
|
281
|
+
VirtualPointer pointer_{nullptr, zwlr_virtual_pointer_v1_destroy};
|
|
282
|
+
XkbContext xkbContext_;
|
|
283
|
+
XkbState xkbState_;
|
|
284
|
+
std::unique_ptr<Keymap> keymap_;
|
|
285
|
+
SeatWatch seatWatch_;
|
|
286
|
+
std::set<uint32_t> held_;
|
|
287
|
+
std::set<uint32_t> modifierKeys_;
|
|
288
|
+
uint16_t width_ = 0;
|
|
289
|
+
uint16_t height_ = 0;
|
|
290
|
+
mutable std::mutex mutex_;
|
|
291
|
+
std::condition_variable wake_;
|
|
292
|
+
InputQueue queue_;
|
|
293
|
+
bool stopping_ = false;
|
|
294
|
+
bool workerFailed_ = false;
|
|
295
|
+
bool startupDone_ = false;
|
|
296
|
+
std::exception_ptr startupError_;
|
|
297
|
+
std::thread worker_;
|
|
298
|
+
|
|
299
|
+
void post(InputJob job) {
|
|
300
|
+
{
|
|
301
|
+
std::lock_guard<std::mutex> lock(mutex_);
|
|
302
|
+
if (stopping_) return;
|
|
303
|
+
queue_.post(std::move(job));
|
|
304
|
+
}
|
|
305
|
+
wake_.notify_one();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
void initialize() {
|
|
309
|
+
connection_ = std::make_unique<Connection>();
|
|
310
|
+
if (!connection_->virtualKeyboardManager)
|
|
311
|
+
throw std::runtime_error("Wayland input requires zwp_virtual_keyboard_manager_v1");
|
|
312
|
+
if (!connection_->virtualPointerManager)
|
|
313
|
+
throw std::runtime_error("Wayland input requires zwlr_virtual_pointer_manager_v1 version 2");
|
|
314
|
+
if (connection_->width < 1 || connection_->height < 1 ||
|
|
315
|
+
connection_->width > std::numeric_limits<uint16_t>::max() ||
|
|
316
|
+
connection_->height > std::numeric_limits<uint16_t>::max())
|
|
317
|
+
throw std::runtime_error("Selected Wayland output has invalid dimensions");
|
|
318
|
+
width_ = uint16_t(connection_->width);
|
|
319
|
+
height_ = uint16_t(connection_->height);
|
|
320
|
+
bindSeat();
|
|
321
|
+
const std::string base = seatWatch_.hasKeyboard ? currentSeatKeymap() : defaultKeymap();
|
|
322
|
+
keymap_ = std::make_unique<Keymap>(base);
|
|
323
|
+
xkbContext_.reset(xkb_context_new(XKB_CONTEXT_NO_FLAGS));
|
|
324
|
+
if (!xkbContext_) throw std::runtime_error("libxkbcommon context unavailable");
|
|
325
|
+
keyboard_.reset(zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(
|
|
326
|
+
connection_->virtualKeyboardManager, seat_.get()));
|
|
327
|
+
if (!keyboard_) throw std::runtime_error("Cannot create Wayland virtual keyboard");
|
|
328
|
+
pointer_.reset(zwlr_virtual_pointer_manager_v1_create_virtual_pointer_with_output(
|
|
329
|
+
connection_->virtualPointerManager, seat_.get(), connection_->output));
|
|
330
|
+
if (!pointer_) throw std::runtime_error("Cannot create output-bound Wayland virtual pointer");
|
|
331
|
+
uploadKeymap(keymap_->text());
|
|
332
|
+
if (!flushDisplay(connection_->display) || wl_display_roundtrip(connection_->display) < 0)
|
|
333
|
+
throw std::runtime_error("Wayland input device registration failed");
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
void bindSeat() {
|
|
337
|
+
Registry registry(wl_display_get_registry(connection_->display), wl_registry_destroy);
|
|
338
|
+
if (!registry) throw std::runtime_error("Could not get Wayland input registry");
|
|
339
|
+
SeatRegistry state;
|
|
340
|
+
state.watch = &seatWatch_;
|
|
341
|
+
if (wl_registry_add_listener(registry.get(), ®istryListener, &state) < 0)
|
|
342
|
+
throw std::runtime_error("Could not listen to Wayland input registry");
|
|
343
|
+
if (wl_display_roundtrip(connection_->display) < 0)
|
|
344
|
+
throw std::runtime_error("Wayland input registry roundtrip failed");
|
|
345
|
+
if (state.failed) throw std::runtime_error("Could not listen to Wayland seat capabilities");
|
|
346
|
+
if (wl_display_roundtrip(connection_->display) < 0)
|
|
347
|
+
throw std::runtime_error("Wayland seat capability roundtrip failed");
|
|
348
|
+
if (state.failed) throw std::runtime_error("Could not listen to Wayland seat capabilities");
|
|
349
|
+
if (!state.seat) throw std::runtime_error("No Wayland seat available for input");
|
|
350
|
+
seat_ = std::move(state.seat);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
std::string currentSeatKeymap() {
|
|
354
|
+
Keyboard source(wl_seat_get_keyboard(seat_.get()), wl_keyboard_destroy);
|
|
355
|
+
if (!source) throw std::runtime_error("Could not create wl_keyboard to read the seat keymap");
|
|
356
|
+
KeymapCapture capture;
|
|
357
|
+
if (wl_keyboard_add_listener(source.get(), &keyboardListener, &capture) < 0)
|
|
358
|
+
throw std::runtime_error("Could not listen to the Wayland seat keymap");
|
|
359
|
+
if (wl_display_roundtrip(connection_->display) < 0)
|
|
360
|
+
throw std::runtime_error("Wayland keyboard keymap roundtrip failed");
|
|
361
|
+
if (capture.error) std::rethrow_exception(capture.error);
|
|
362
|
+
if (!capture.received) throw std::runtime_error("Wayland seat did not send a keyboard keymap");
|
|
363
|
+
return std::move(capture.text);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
void uploadKeymap(const std::string& text) {
|
|
367
|
+
XkbKeymap nextKeymap(xkb_keymap_new_from_string(xkbContext_.get(), text.c_str(),
|
|
368
|
+
XKB_KEYMAP_FORMAT_TEXT_V1,
|
|
369
|
+
XKB_KEYMAP_COMPILE_NO_FLAGS));
|
|
370
|
+
if (!nextKeymap) throw std::runtime_error("libxkbcommon cannot compile the uploaded keymap");
|
|
371
|
+
XkbState nextState(xkb_state_new(nextKeymap.get()));
|
|
372
|
+
if (!nextState) throw std::runtime_error("libxkbcommon cannot create state for the uploaded keymap");
|
|
373
|
+
Masks previous;
|
|
374
|
+
const bool hadState = bool(xkbState_);
|
|
375
|
+
if (hadState) {
|
|
376
|
+
previous.latched = xkb_state_serialize_mods(xkbState_.get(), XKB_STATE_MODS_LATCHED);
|
|
377
|
+
previous.locked = xkb_state_serialize_mods(xkbState_.get(), XKB_STATE_MODS_LOCKED);
|
|
378
|
+
previous.latchedLayout = xkb_state_serialize_layout(xkbState_.get(), XKB_STATE_LAYOUT_LATCHED);
|
|
379
|
+
previous.lockedLayout = xkb_state_serialize_layout(xkbState_.get(), XKB_STATE_LAYOUT_LOCKED);
|
|
380
|
+
}
|
|
381
|
+
for (const auto key : held_) xkb_state_update_key(nextState.get(), key + 8, XKB_KEY_DOWN);
|
|
382
|
+
if (hadState) {
|
|
383
|
+
xkb_state_update_mask(nextState.get(),
|
|
384
|
+
xkb_state_serialize_mods(nextState.get(), XKB_STATE_MODS_DEPRESSED),
|
|
385
|
+
previous.latched, previous.locked,
|
|
386
|
+
xkb_state_serialize_layout(nextState.get(), XKB_STATE_LAYOUT_DEPRESSED),
|
|
387
|
+
previous.latchedLayout, previous.lockedLayout);
|
|
388
|
+
} else {
|
|
389
|
+
modifierKeys_ = findModifierKeys(nextKeymap.get());
|
|
390
|
+
}
|
|
391
|
+
std::string contents = text;
|
|
392
|
+
contents.push_back('\0');
|
|
393
|
+
if (contents.size() > std::numeric_limits<uint32_t>::max())
|
|
394
|
+
throw std::runtime_error("Wayland keyboard keymap is too large");
|
|
395
|
+
auto fd = createMemfd("relay-keymap");
|
|
396
|
+
if (fd.get() < 0) throw std::runtime_error("Cannot create Wayland keyboard keymap memfd");
|
|
397
|
+
size_t written = 0;
|
|
398
|
+
while (written < contents.size()) {
|
|
399
|
+
const ssize_t count = ::write(fd.get(), contents.data() + written, contents.size() - written);
|
|
400
|
+
if (count < 0 && errno == EINTR) continue;
|
|
401
|
+
if (count <= 0) throw std::runtime_error("Cannot write Wayland keyboard keymap memfd");
|
|
402
|
+
written += size_t(count);
|
|
403
|
+
}
|
|
404
|
+
zwp_virtual_keyboard_v1_keymap(keyboard_.get(), WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
|
|
405
|
+
fd.get(), uint32_t(contents.size()));
|
|
406
|
+
xkbState_ = std::move(nextState);
|
|
407
|
+
sendModifiers();
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
void sendModifiers() {
|
|
411
|
+
zwp_virtual_keyboard_v1_modifiers(
|
|
412
|
+
keyboard_.get(),
|
|
413
|
+
uint32_t(xkb_state_serialize_mods(xkbState_.get(), XKB_STATE_MODS_DEPRESSED)),
|
|
414
|
+
uint32_t(xkb_state_serialize_mods(xkbState_.get(), XKB_STATE_MODS_LATCHED)),
|
|
415
|
+
uint32_t(xkb_state_serialize_mods(xkbState_.get(), XKB_STATE_MODS_LOCKED)),
|
|
416
|
+
uint32_t(xkb_state_serialize_layout(xkbState_.get(), XKB_STATE_LAYOUT_EFFECTIVE)));
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
void emitKey(uint32_t key, bool press) {
|
|
420
|
+
zwp_virtual_keyboard_v1_key(keyboard_.get(), timestamp(), key,
|
|
421
|
+
press ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED);
|
|
422
|
+
xkb_state_update_key(xkbState_.get(), key + 8, press ? XKB_KEY_DOWN : XKB_KEY_UP);
|
|
423
|
+
sendModifiers();
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
void type(uint32_t key) {
|
|
427
|
+
std::vector<uint32_t> lifted;
|
|
428
|
+
for (const auto held : held_)
|
|
429
|
+
if (held == key || modifierKeys_.count(held)) lifted.push_back(held);
|
|
430
|
+
for (const auto modifier : lifted) emitKey(modifier, false);
|
|
431
|
+
emitKey(key, true);
|
|
432
|
+
emitKey(key, false);
|
|
433
|
+
for (auto modifier = lifted.rbegin(); modifier != lifted.rend(); ++modifier) emitKey(*modifier, true);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
static uint32_t timestamp() {
|
|
437
|
+
const auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
438
|
+
std::chrono::steady_clock::now().time_since_epoch()).count();
|
|
439
|
+
return uint32_t(milliseconds);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
bool apply(const InputJob& job, bool previouslyFailed) {
|
|
443
|
+
switch (job.kind) {
|
|
444
|
+
case InputJob::Kind::key:
|
|
445
|
+
if (job.press) held_.insert(job.code); else held_.erase(job.code);
|
|
446
|
+
emitKey(job.code, job.press);
|
|
447
|
+
break;
|
|
448
|
+
case InputJob::Kind::text: {
|
|
449
|
+
bool changed = false;
|
|
450
|
+
const uint32_t key = keymap_->assign(job.codepoint, changed);
|
|
451
|
+
if (!key) throw std::runtime_error("Wayland keymap could not assign the queued codepoint");
|
|
452
|
+
if (changed) uploadKeymap(keymap_->text());
|
|
453
|
+
type(key);
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
case InputJob::Kind::button:
|
|
457
|
+
zwlr_virtual_pointer_v1_button(pointer_.get(), timestamp(), linuxButton(job.number),
|
|
458
|
+
job.press ? WL_POINTER_BUTTON_STATE_PRESSED : WL_POINTER_BUTTON_STATE_RELEASED);
|
|
459
|
+
zwlr_virtual_pointer_v1_frame(pointer_.get());
|
|
460
|
+
break;
|
|
461
|
+
case InputJob::Kind::motion:
|
|
462
|
+
zwlr_virtual_pointer_v1_motion_absolute(pointer_.get(), timestamp(), uint32_t(job.x), uint32_t(job.y),
|
|
463
|
+
width_, height_);
|
|
464
|
+
zwlr_virtual_pointer_v1_frame(pointer_.get());
|
|
465
|
+
break;
|
|
466
|
+
case InputJob::Kind::scroll:
|
|
467
|
+
zwlr_virtual_pointer_v1_axis_source(pointer_.get(), WL_POINTER_AXIS_SOURCE_WHEEL);
|
|
468
|
+
zwlr_virtual_pointer_v1_axis_discrete(
|
|
469
|
+
pointer_.get(), timestamp(), job.number == 0 ? WL_POINTER_AXIS_VERTICAL_SCROLL : WL_POINTER_AXIS_HORIZONTAL_SCROLL,
|
|
470
|
+
wl_fixed_from_int(job.notches * 15), job.notches);
|
|
471
|
+
zwlr_virtual_pointer_v1_frame(pointer_.get());
|
|
472
|
+
break;
|
|
473
|
+
case InputJob::Kind::locks:
|
|
474
|
+
xkb_state_update_mask(xkbState_.get(),
|
|
475
|
+
xkb_state_serialize_mods(xkbState_.get(), XKB_STATE_MODS_DEPRESSED),
|
|
476
|
+
xkb_state_serialize_mods(xkbState_.get(), XKB_STATE_MODS_LATCHED), 0,
|
|
477
|
+
xkb_state_serialize_layout(xkbState_.get(), XKB_STATE_LAYOUT_DEPRESSED),
|
|
478
|
+
xkb_state_serialize_layout(xkbState_.get(), XKB_STATE_LAYOUT_LATCHED),
|
|
479
|
+
xkb_state_serialize_layout(xkbState_.get(), XKB_STATE_LAYOUT_LOCKED));
|
|
480
|
+
sendModifiers();
|
|
481
|
+
break;
|
|
482
|
+
case InputJob::Kind::flush:
|
|
483
|
+
return flushDisplay(connection_->display);
|
|
484
|
+
case InputJob::Kind::fence:
|
|
485
|
+
if (job.fence) {
|
|
486
|
+
const bool reached = !previouslyFailed && wl_display_roundtrip(connection_->display) >= 0;
|
|
487
|
+
job.fence->complete(reached && job.dropped == 0, job.dropped);
|
|
488
|
+
return reached;
|
|
489
|
+
}
|
|
490
|
+
return !previouslyFailed && wl_display_roundtrip(connection_->display) >= 0;
|
|
491
|
+
}
|
|
492
|
+
return true;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
void cleanup() {
|
|
496
|
+
pointer_.reset();
|
|
497
|
+
keyboard_.reset();
|
|
498
|
+
seat_.reset();
|
|
499
|
+
xkbState_.reset();
|
|
500
|
+
xkbContext_.reset();
|
|
501
|
+
connection_.reset();
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
void run() {
|
|
505
|
+
std::exception_ptr error;
|
|
506
|
+
try {
|
|
507
|
+
initialize();
|
|
508
|
+
} catch (...) {
|
|
509
|
+
error = std::current_exception();
|
|
510
|
+
cleanup();
|
|
511
|
+
}
|
|
512
|
+
{
|
|
513
|
+
std::lock_guard<std::mutex> lock(mutex_);
|
|
514
|
+
startupError_ = error;
|
|
515
|
+
startupDone_ = true;
|
|
516
|
+
if (error) stopping_ = true;
|
|
517
|
+
}
|
|
518
|
+
wake_.notify_all();
|
|
519
|
+
if (error) return;
|
|
520
|
+
bool failed = false;
|
|
521
|
+
bool logged = false;
|
|
522
|
+
std::unique_lock<std::mutex> lock(mutex_);
|
|
523
|
+
for (;;) {
|
|
524
|
+
wake_.wait(lock, [this] { return stopping_ || !queue_.empty(); });
|
|
525
|
+
if (stopping_) {
|
|
526
|
+
queue_.cancel();
|
|
527
|
+
}
|
|
528
|
+
if (queue_.empty()) break;
|
|
529
|
+
const InputJob job = queue_.take();
|
|
530
|
+
lock.unlock();
|
|
531
|
+
bool success = false;
|
|
532
|
+
try {
|
|
533
|
+
success = apply(job, failed);
|
|
534
|
+
} catch (const std::exception& failure) {
|
|
535
|
+
if (!logged) std::fprintf(stderr, "input worker stopped: %s\n", failure.what());
|
|
536
|
+
logged = true;
|
|
537
|
+
} catch (...) {
|
|
538
|
+
if (!logged) std::fprintf(stderr, "input worker stopped: unknown error\n");
|
|
539
|
+
logged = true;
|
|
540
|
+
}
|
|
541
|
+
lock.lock();
|
|
542
|
+
if (!success) {
|
|
543
|
+
failed = true;
|
|
544
|
+
workerFailed_ = true;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
lock.unlock();
|
|
548
|
+
const std::vector<uint32_t> held(held_.begin(), held_.end());
|
|
549
|
+
for (const auto key : held) emitKey(key, false);
|
|
550
|
+
held_.clear();
|
|
551
|
+
if (connection_) flushDisplay(connection_->display);
|
|
552
|
+
cleanup();
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
std::unique_ptr<InputBackend> makeInputBackend() {
|
|
557
|
+
return std::make_unique<WaylandInput>();
|
|
558
|
+
}
|
|
559
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
#include "backends/linux/wayland/keymap.hpp"
|
|
2
|
+
#include "backends/linux/wayland/wayland.hpp"
|
|
3
|
+
#include <cstdlib>
|
|
4
|
+
#include <cstring>
|
|
5
|
+
#include <stdexcept>
|
|
6
|
+
|
|
7
|
+
namespace relay::wayland {
|
|
8
|
+
namespace {
|
|
9
|
+
/** XKB text v1 carries names of at most four characters; the serializer renames longer ones, so a spare key needs a short name. */
|
|
10
|
+
bool shortName(const char* name) { return name && std::strlen(name) <= 4; }
|
|
11
|
+
/** Whether any layout gives keycode a level, so the base keymap already spends that keycode. */
|
|
12
|
+
bool hasSymbols(xkb_keymap* keymap, xkb_keycode_t keycode) {
|
|
13
|
+
const xkb_layout_index_t layouts = xkb_keymap_num_layouts_for_key(keymap, keycode);
|
|
14
|
+
for (xkb_layout_index_t layout = 0; layout < layouts; ++layout)
|
|
15
|
+
if (xkb_keymap_num_levels_for_key(keymap, keycode, layout) > 0) return true;
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
/** Offset of the closing brace of the keymap's symbols section, where spare key definitions are written. */
|
|
19
|
+
size_t symbolsEnd(const std::string& text) {
|
|
20
|
+
const size_t section = text.find("xkb_symbols");
|
|
21
|
+
require(section != std::string::npos, "keymap text has no symbols section");
|
|
22
|
+
const size_t open = text.find('{', section);
|
|
23
|
+
require(open != std::string::npos, "keymap text has a malformed symbols section");
|
|
24
|
+
size_t depth = 0;
|
|
25
|
+
for (size_t index = open; index < text.size(); ++index) {
|
|
26
|
+
if (text[index] == '{') ++depth;
|
|
27
|
+
else if (text[index] == '}' && --depth == 0) return index;
|
|
28
|
+
}
|
|
29
|
+
throw std::runtime_error("keymap text has a malformed symbols section");
|
|
30
|
+
}
|
|
31
|
+
std::string keysymName(uint32_t symbol) {
|
|
32
|
+
char name[64] = {};
|
|
33
|
+
xkb_keysym_get_name(xkb_keysym_t(symbol), name, sizeof name);
|
|
34
|
+
return name;
|
|
35
|
+
}
|
|
36
|
+
/** Maps the newline and tab codepoints to the Return and Tab keys, so applications see the named keys. */
|
|
37
|
+
xkb_keysym_t textKeysym(uint32_t codepoint) {
|
|
38
|
+
if (codepoint == '\n') return XKB_KEY_Return;
|
|
39
|
+
if (codepoint == '\t') return XKB_KEY_Tab;
|
|
40
|
+
return xkb_utf32_to_keysym(codepoint);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
std::string defaultKeymap() {
|
|
45
|
+
XkbContext context(xkb_context_new(XKB_CONTEXT_NO_FLAGS));
|
|
46
|
+
require(bool(context), "libxkbcommon context unavailable");
|
|
47
|
+
const xkb_rule_names names{};
|
|
48
|
+
XkbKeymap keymap(xkb_keymap_new_from_names(context.get(), &names, XKB_KEYMAP_COMPILE_NO_FLAGS));
|
|
49
|
+
require(bool(keymap), "libxkbcommon cannot compile the default keymap; check the XKB_DEFAULT_* variables");
|
|
50
|
+
char* text = xkb_keymap_get_as_string(keymap.get(), XKB_KEYMAP_FORMAT_TEXT_V1);
|
|
51
|
+
std::string result = text ? text : "";
|
|
52
|
+
std::free(text);
|
|
53
|
+
if (result.empty()) throw std::runtime_error("libxkbcommon cannot serialize the default keymap");
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
Keymap::Keymap(const std::string& base) {
|
|
58
|
+
XkbContext context(xkb_context_new(XKB_CONTEXT_NO_FLAGS));
|
|
59
|
+
require(bool(context), "libxkbcommon context unavailable");
|
|
60
|
+
XkbKeymap keymap(xkb_keymap_new_from_string(context.get(), base.c_str(), XKB_KEYMAP_FORMAT_TEXT_V1,
|
|
61
|
+
XKB_KEYMAP_COMPILE_NO_FLAGS));
|
|
62
|
+
require(bool(keymap), "libxkbcommon cannot compile the keymap text");
|
|
63
|
+
char* text = xkb_keymap_get_as_string(keymap.get(), XKB_KEYMAP_FORMAT_TEXT_V1);
|
|
64
|
+
require(text, "libxkbcommon cannot serialize the keymap");
|
|
65
|
+
original = text;
|
|
66
|
+
std::free(text);
|
|
67
|
+
for (uint32_t keycode = 9; keycode <= 255; ++keycode) {
|
|
68
|
+
const xkb_keysym_t* symbols = nullptr;
|
|
69
|
+
if (xkb_keymap_key_get_syms_by_level(keymap.get(), keycode, 0, 0, &symbols) != 1) continue;
|
|
70
|
+
direct.emplace(symbols[0], keycode - 8);
|
|
71
|
+
}
|
|
72
|
+
for (uint32_t keycode = 9; keycode <= 255 && slots.size() < spareSlots; ++keycode) {
|
|
73
|
+
if (hasSymbols(keymap.get(), keycode)) continue;
|
|
74
|
+
const char* name = xkb_keymap_key_get_name(keymap.get(), keycode);
|
|
75
|
+
if (!shortName(name)) continue;
|
|
76
|
+
slots.push_back(Slot{keycode - 8, name, 0, 0, 0});
|
|
77
|
+
}
|
|
78
|
+
insertAt = symbolsEnd(original);
|
|
79
|
+
current = original;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
uint32_t Keymap::assign(uint32_t codepoint, bool& changed) {
|
|
83
|
+
changed = false;
|
|
84
|
+
const xkb_keysym_t symbol = textKeysym(codepoint);
|
|
85
|
+
if (!symbol) return 0;
|
|
86
|
+
const auto existing = direct.find(symbol);
|
|
87
|
+
if (existing != direct.end()) return existing->second;
|
|
88
|
+
for (auto& slot : slots)
|
|
89
|
+
if (slot.codepoint == codepoint) {
|
|
90
|
+
slot.used = ++typed;
|
|
91
|
+
return slot.keycode;
|
|
92
|
+
}
|
|
93
|
+
Slot* slot = nullptr;
|
|
94
|
+
for (auto& candidate : slots)
|
|
95
|
+
if (!candidate.codepoint) { slot = &candidate; break; }
|
|
96
|
+
if (!slot)
|
|
97
|
+
for (auto& candidate : slots)
|
|
98
|
+
if (!slot || candidate.used < slot->used) slot = &candidate;
|
|
99
|
+
if (!slot) return 0;
|
|
100
|
+
slot->codepoint = codepoint;
|
|
101
|
+
slot->symbol = symbol;
|
|
102
|
+
slot->used = ++typed;
|
|
103
|
+
changed = true;
|
|
104
|
+
refresh();
|
|
105
|
+
return slot->keycode;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const std::string& Keymap::text() const { return current; }
|
|
109
|
+
|
|
110
|
+
void Keymap::refresh() {
|
|
111
|
+
current = original.substr(0, insertAt);
|
|
112
|
+
for (const auto& slot : slots) {
|
|
113
|
+
if (!slot.codepoint) continue;
|
|
114
|
+
current += "\t\tkey <" + slot.name + "> { [ " + keysymName(slot.symbol) + " ] };\n";
|
|
115
|
+
}
|
|
116
|
+
current += original.substr(insertAt);
|
|
117
|
+
}
|
|
118
|
+
}
|