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.
Files changed (91) hide show
  1. package/LICENSE +201 -0
  2. package/Makefile +165 -0
  3. package/README.md +112 -1
  4. package/THIRD-PARTY-NOTICES.md +75 -0
  5. package/dist/cli/client.d.ts +28 -0
  6. package/dist/cli/commands.d.ts +52 -0
  7. package/dist/cli/config.d.ts +32 -0
  8. package/dist/cli/format.d.ts +15 -0
  9. package/dist/cli/help.d.ts +5 -0
  10. package/dist/cli/host/common.d.ts +23 -0
  11. package/dist/cli/host/doctor.d.ts +2 -0
  12. package/dist/cli/host/packages.d.ts +13 -0
  13. package/dist/cli/host/session_commands.d.ts +8 -0
  14. package/dist/cli/host/setup.d.ts +22 -0
  15. package/dist/cli/host/state.d.ts +23 -0
  16. package/dist/cli/host/supervisor.d.ts +94 -0
  17. package/dist/cli/host/watcher.d.ts +4 -0
  18. package/dist/cli/host/windows.d.ts +82 -0
  19. package/dist/cli/main.d.ts +3 -0
  20. package/dist/cli/mode.d.ts +14 -0
  21. package/dist/cli/output.d.ts +24 -0
  22. package/dist/cli/parser.d.ts +6 -0
  23. package/dist/cli/relay.mjs +12631 -0
  24. package/dist/cli/skills.d.ts +21 -0
  25. package/dist/cli/theme.d.ts +14 -0
  26. package/dist/protocol/errors.d.ts +155 -0
  27. package/dist/protocol/index.d.ts +119 -0
  28. package/dist/protocol/index.js +418 -0
  29. package/dist/sdk/index.d.ts +78 -0
  30. package/dist/sdk/index.js +834 -0
  31. package/dist/sdk/transport.d.ts +20 -0
  32. package/dist/viewer/index.html +96 -0
  33. package/dist/viewer/viewer.js +1079 -0
  34. package/package.json +47 -7
  35. package/packages.tsv +38 -0
  36. package/skills/core/SKILL.md +171 -0
  37. package/skills/core/references/cli.md +119 -0
  38. package/skills/core/references/config.md +48 -0
  39. package/skills/core/references/macos.md +50 -0
  40. package/skills/core/references/sdk.md +69 -0
  41. package/skills/core/references/troubleshooting.md +33 -0
  42. package/skills/core/references/wayland.md +47 -0
  43. package/skills/screen-relay/SKILL.md +24 -0
  44. package/skills/vercel-sandbox/SKILL.md +68 -0
  45. package/src/engine/backends/linux/keycodes.hpp +35 -0
  46. package/src/engine/backends/linux/wayland/capture.cpp +298 -0
  47. package/src/engine/backends/linux/wayland/input.cpp +559 -0
  48. package/src/engine/backends/linux/wayland/keymap.cpp +118 -0
  49. package/src/engine/backends/linux/wayland/keymap.hpp +54 -0
  50. package/src/engine/backends/linux/wayland/protocols/virtual-keyboard-unstable-v1.xml +112 -0
  51. package/src/engine/backends/linux/wayland/protocols/wlr-screencopy-unstable-v1.xml +234 -0
  52. package/src/engine/backends/linux/wayland/protocols/wlr-virtual-pointer-unstable-v1.xml +152 -0
  53. package/src/engine/backends/linux/wayland/wayland.cpp +166 -0
  54. package/src/engine/backends/linux/wayland/wayland.hpp +60 -0
  55. package/src/engine/backends/linux/x11/capture.cpp +206 -0
  56. package/src/engine/backends/linux/x11/input.cpp +266 -0
  57. package/src/engine/backends/macos/capture.mm +367 -0
  58. package/src/engine/backends/macos/display.hpp +20 -0
  59. package/src/engine/backends/macos/display.mm +84 -0
  60. package/src/engine/backends/macos/input.mm +287 -0
  61. package/src/engine/backends/macos/keys.hpp +34 -0
  62. package/src/engine/backends/windows/capture.cpp +393 -0
  63. package/src/engine/backends/windows/input.cpp +274 -0
  64. package/src/engine/backends/windows/output.cpp +65 -0
  65. package/src/engine/backends/windows/output.hpp +26 -0
  66. package/src/engine/core/capture-bucket.hpp +40 -0
  67. package/src/engine/core/capture-source.hpp +59 -0
  68. package/src/engine/core/capture.cpp +565 -0
  69. package/src/engine/core/capture.hpp +58 -0
  70. package/src/engine/core/color.cpp +176 -0
  71. package/src/engine/core/color.hpp +16 -0
  72. package/src/engine/core/cpu-budget.hpp +196 -0
  73. package/src/engine/core/flow.hpp +317 -0
  74. package/src/engine/core/input.cpp +98 -0
  75. package/src/engine/core/input.hpp +115 -0
  76. package/src/engine/core/wait.cpp +125 -0
  77. package/src/engine/core/wait.hpp +41 -0
  78. package/src/engine/server/backend.cpp +65 -0
  79. package/src/engine/server/backend.hpp +18 -0
  80. package/src/engine/server/config.cpp +257 -0
  81. package/src/engine/server/config.hpp +24 -0
  82. package/src/engine/server/main.cpp +82 -0
  83. package/src/engine/server/metrics.cpp +47 -0
  84. package/src/engine/server/metrics.hpp +10 -0
  85. package/src/engine/server/screenshot.cpp +108 -0
  86. package/src/engine/server/screenshot.hpp +31 -0
  87. package/src/engine/server/server.cpp +535 -0
  88. package/src/engine/server/server.hpp +89 -0
  89. package/src/engine/server/wire.cpp +70 -0
  90. package/src/engine/server/wire.hpp +29 -0
  91. package/index.js +0 -2
@@ -0,0 +1,65 @@
1
+ #include "backends/windows/output.hpp"
2
+ #include "core/capture-source.hpp"
3
+ #include <cstdio>
4
+ #include <mutex>
5
+ #include <stdexcept>
6
+
7
+ namespace relay {
8
+ namespace windows {
9
+ namespace {
10
+ std::string chosen;
11
+ std::string narrow(const wchar_t* text) {
12
+ std::string out;
13
+ for (; *text; ++text) out += *text < 128 ? char(*text) : '?';
14
+ return out;
15
+ }
16
+ }
17
+ int Output::level() const {
18
+ int level = 0;
19
+ while ((width() >> level) > max_frame_width || (height() >> level) > max_frame_height) ++level;
20
+ return level;
21
+ }
22
+ void check(HRESULT result, const char* action) {
23
+ if (SUCCEEDED(result)) return;
24
+ char code[16];
25
+ std::snprintf(code, sizeof(code), "0x%08lx", static_cast<unsigned long>(result));
26
+ throw std::runtime_error(std::string(action) + " failed (" + code + ")");
27
+ }
28
+ Output findOutput() {
29
+ Microsoft::WRL::ComPtr<IDXGIFactory1> factory;
30
+ check(CreateDXGIFactory1(IID_PPV_ARGS(&factory)), "creating the DXGI factory");
31
+ std::string names;
32
+ Microsoft::WRL::ComPtr<IDXGIAdapter1> adapter;
33
+ for (UINT a = 0; SUCCEEDED(factory->EnumAdapters1(a, &adapter)); ++a) {
34
+ Microsoft::WRL::ComPtr<IDXGIOutput> candidate;
35
+ for (UINT o = 0; SUCCEEDED(adapter->EnumOutputs(o, &candidate)); ++o) {
36
+ DXGI_OUTPUT_DESC desc;
37
+ if (FAILED(candidate->GetDesc(&desc)) || !desc.AttachedToDesktop) continue;
38
+ const std::string name = narrow(desc.DeviceName);
39
+ names += (names.empty() ? "" : ", ") + name;
40
+ const bool primary = desc.DesktopCoordinates.left == 0 && desc.DesktopCoordinates.top == 0;
41
+ if (chosen.empty() ? !primary : name != chosen) continue;
42
+ Output found;
43
+ found.adapter = adapter;
44
+ check(candidate.As(&found.output), "opening the display for duplication");
45
+ found.bounds = desc.DesktopCoordinates;
46
+ return found;
47
+ }
48
+ }
49
+ if (names.empty())
50
+ throw std::runtime_error("No display is attached to this session; start relay in the signed-in user's desktop session");
51
+ throw std::runtime_error(chosen.empty() ? "No primary display is attached to the desktop"
52
+ : "Display " + chosen + " not found; attached displays: " + names);
53
+ }
54
+ bool initializeNativeBackend(const std::string& output) {
55
+ static std::once_flag once;
56
+ static bool initialized = false;
57
+ std::call_once(once, [&] {
58
+ chosen = output;
59
+ initialized = SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) ||
60
+ GetLastError() == ERROR_ACCESS_DENIED;
61
+ });
62
+ return initialized;
63
+ }
64
+ }
65
+ }
@@ -0,0 +1,26 @@
1
+ #pragma once
2
+ #include <string>
3
+ #include <utility>
4
+ #include <windows.h>
5
+ #include <dxgi1_2.h>
6
+ #include <wrl/client.h>
7
+
8
+ namespace relay {
9
+ namespace windows {
10
+ struct Output {
11
+ Microsoft::WRL::ComPtr<IDXGIAdapter1> adapter;
12
+ Microsoft::WRL::ComPtr<IDXGIOutput1> output;
13
+ RECT bounds{};
14
+ int width() const { return int(bounds.right - bounds.left); }
15
+ int height() const { return int(bounds.bottom - bounds.top); }
16
+ /// Mip level that halves the display until it fits the stream's maximum frame size.
17
+ int level() const;
18
+ /// Even stream size for this display after scaling by level().
19
+ std::pair<int, int> streamSize() const { return {(width() >> level()) & ~1, (height() >> level()) & ~1}; }
20
+ };
21
+ /// Finds the display chosen with --output, such as \\.\DISPLAY2, or the primary display when none was chosen.
22
+ Output findOutput();
23
+ /// Throws with the HRESULT and a readable action when a Windows call fails.
24
+ void check(HRESULT result, const char* action);
25
+ }
26
+ }
@@ -0,0 +1,40 @@
1
+ #pragma once
2
+
3
+ #include <algorithm>
4
+ #include <cstdint>
5
+
6
+ namespace relay {
7
+ /** Token bucket pacing captures at the stream rate while allowing a burst of two frames. */
8
+ struct CaptureBucket {
9
+ static constexpr int burst = 2;
10
+ int64_t interval_ns = 1, credit_ns = 0, updated_ns = 0;
11
+
12
+ /** Starts full at `now_ns`, refilling one token per `frame_interval_ns`. */
13
+ void reset(int64_t now_ns, int64_t frame_interval_ns) {
14
+ interval_ns = std::max<int64_t>(1, frame_interval_ns);
15
+ credit_ns = int64_t(burst) * interval_ns;
16
+ updated_ns = now_ns;
17
+ }
18
+ void refill(int64_t now_ns) {
19
+ if (now_ns <= updated_ns) return;
20
+ credit_ns = std::min(credit_ns + (now_ns - updated_ns), int64_t(burst) * interval_ns);
21
+ updated_ns = now_ns;
22
+ }
23
+ /** Refills to `now_ns` and reports whether a capture token is available without taking it. */
24
+ bool ready(int64_t now_ns) {
25
+ refill(now_ns);
26
+ return credit_ns >= interval_ns;
27
+ }
28
+ /** Refills to `now_ns` and charges one token for an encoded frame. */
29
+ void spend(int64_t now_ns) {
30
+ refill(now_ns);
31
+ credit_ns = std::max<int64_t>(0, credit_ns - interval_ns);
32
+ }
33
+ /** Nanoseconds from `now_ns` until one token is available, without changing the bucket. */
34
+ int64_t wait_ns(int64_t now_ns) const {
35
+ const int64_t elapsed = now_ns > updated_ns ? now_ns - updated_ns : 0;
36
+ const int64_t credit = std::min(credit_ns + elapsed, int64_t(burst) * interval_ns);
37
+ return credit >= interval_ns ? 0 : interval_ns - credit;
38
+ }
39
+ };
40
+ } // namespace relay
@@ -0,0 +1,59 @@
1
+ #pragma once
2
+ #include <cstdint>
3
+ #include <functional>
4
+ #include <memory>
5
+ #include <optional>
6
+ #include <stdexcept>
7
+ #include <string>
8
+ #include <utility>
9
+ #include <vector>
10
+ #include "wait.hpp"
11
+
12
+ namespace relay {
13
+ constexpr int min_frame_width = 320, max_frame_width = 16384;
14
+ constexpr int min_frame_height = 240, max_frame_height = 16384;
15
+ /** Throws unless width and height are even and inside the supported root size limits. */
16
+ inline void requireSurfaceSize(int width, int height) {
17
+ if (width < min_frame_width || width > max_frame_width || height < min_frame_height ||
18
+ height > max_frame_height || (width & 1) || (height & 1))
19
+ throw std::runtime_error("Unsupported root size " + std::to_string(width) + "x" + std::to_string(height) +
20
+ ": width must be even and " + std::to_string(min_frame_width) + " to " +
21
+ std::to_string(max_frame_width) + ", height even and " +
22
+ std::to_string(min_frame_height) + " to " + std::to_string(max_frame_height));
23
+ }
24
+ enum class PixelFormat { BGRA, ARGB };
25
+ // CPU-accessible pixels remain valid until the next capture or source destruction.
26
+ // GPU zero-copy would require a separate encoder interface.
27
+ struct FrameView {
28
+ const uint8_t* data;
29
+ int stride, width, height;
30
+ PixelFormat format;
31
+ int x = 0, y = 0;
32
+ };
33
+ struct DamageRect { int x, y, width, height; };
34
+ struct CursorImage {
35
+ static constexpr unsigned max_extent = 256;
36
+ uint16_t width = 0, height = 0, xhot = 0, yhot = 0;
37
+ // Straight-alpha RGBA. Empty dimensions explicitly restore the local arrow.
38
+ std::vector<uint8_t> rgba;
39
+ };
40
+ class CaptureSource {
41
+ public:
42
+ virtual ~CaptureSource() = default;
43
+ /** Becomes ready when the source may have new damage; see WaitHandle. */
44
+ virtual WaitHandle fd() const = 0;
45
+ /** Pixel size of every full frame this source captures. */
46
+ virtual std::pair<int, int> dimensions() const = 0;
47
+ virtual bool drainDamage() = 0;
48
+ virtual PixelFormat pixelFormat() const = 0;
49
+ virtual FrameView capture() = 0;
50
+ /** Captures only the damage region when full is false; a view may still cover the whole frame. */
51
+ virtual FrameView captureChanged(bool /*full*/) { return capture(); }
52
+ /** Fills the pixel rectangles changed since the previous capture(); false means unknown, treat the whole frame as changed. */
53
+ virtual bool changedRects(std::vector<DamageRect>& /*rects*/) { return false; }
54
+ virtual bool cursorSupported() const { return false; }
55
+ // Called on the source owner thread after draining events, even when video is paused.
56
+ virtual std::optional<CursorImage> cursor(bool /*force*/) { return std::nullopt; }
57
+ };
58
+ using CaptureSourceFactory = std::function<std::unique_ptr<CaptureSource>()>;
59
+ }