7 Commits
46 changed files with 2959 additions and 348 deletions

No files matched your search

+7 -2
View File
@@ -13,6 +13,8 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy, rustfmt
- name: Install X11 headers for the compatibility library
run: sudo apt-get update && sudo apt-get install -y --no-install-recommends libx11-dev pkg-config
- name: Rust checks
run: |
cargo fmt --check
@@ -22,6 +24,8 @@ jobs:
/tmp/frame-protocol
cc -std=c11 -O2 -Wall -Wextra -Werror tests/cursor-overlay.c -o /tmp/cursor-overlay
/tmp/cursor-overlay
- name: Build the lock-modifier compatibility library
run: ./compat/build.sh
- name: Build
run: cargo build --locked --release
@@ -50,13 +54,14 @@ jobs:
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
appstream binutils-mingw-w64-x86-64 desktop-file-utils \
gcc gcc-mingw-w64-x86-64 libpipewire-0.3-dev libwayland-dev pkg-config \
wine xvfb
gcc gcc-mingw-w64-x86-64 libpipewire-0.3-dev libwayland-dev \
libx11-dev libxtst-dev pkg-config wine xvfb
- name: Build and stage
run: |
cargo build --release --locked
./hook/build.sh
./capture/build.sh
./compat/build.sh
stage="$(mktemp -d)"
./packaging/stage.sh "$stage"
test -x "$stage/usr/bin/uur"
+3 -2
View File
@@ -39,7 +39,7 @@ jobs:
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
binutils-mingw-w64-x86-64 gcc libpipewire-0.3-dev libwayland-dev \
gcc-mingw-w64-x86-64 pkg-config zstd
gcc-mingw-w64-x86-64 libx11-dev pkg-config zstd
- name: Install nFPM
env:
NFPM_VERSION: 2.47.0
@@ -73,7 +73,7 @@ jobs:
- name: Install Arch build dependencies
run: |
pacman -Syu --noconfirm \
cargo rust gcc pkgconf pipewire wayland \
cargo rust gcc pkgconf pipewire wayland libx11 \
mingw-w64-gcc mingw-w64-binutils
- name: Build native pacman package with makepkg
run: |
@@ -84,6 +84,7 @@ jobs:
CARGO_BUILD_JOBS=2 cargo build --release --locked
./hook/build.sh
./capture/build.sh
./compat/build.sh
./packaging/build-arch-package.sh "$1/dist"
' _ "$GITHUB_WORKSPACE"
- uses: actions/upload-artifact@v4
Generated
+1 -1
View File
@@ -1487,7 +1487,7 @@ dependencies = [
[[package]]
name = "uur"
version = "0.1.6"
version = "0.1.7"
dependencies = [
"anyhow",
"ashpd",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "uur"
version = "0.1.6"
version = "0.1.7"
edition = "2021"
description = "Native Linux companion for NetEase UU Remote: X11/Wayland input and capture, packaged as a regular desktop app"
license = "MIT"
+5 -4
View File
@@ -69,7 +69,7 @@ recipes and release artifacts share one version and filesystem layout.
Install a downloaded release package with pacman:
```bash
sudo pacman -U ./dist/uur-0.1.6-1-x86_64.pkg.tar.zst
sudo pacman -U ./dist/uur-0.1.7-1-x86_64.pkg.tar.zst
```
### AUR
@@ -83,13 +83,13 @@ paru -S uur
### Debian and Ubuntu package family
```bash
sudo apt install ./uur_0.1.6_amd64.deb
sudo apt install ./uur_0.1.7_amd64.deb
```
### Fedora and compatible RPM systems
```bash
sudo dnf install ./uur-0.1.6-1.x86_64.rpm
sudo dnf install ./uur-0.1.7-1.x86_64.rpm
```
On systems with different RPM dependency names, install the dependencies
@@ -98,7 +98,7 @@ manually or use the portable archive.
### Portable archive
```bash
sudo tar --zstd -C / -xf ./uur-0.1.6-linux-x86_64.tar.zst
sudo tar --zstd -C / -xf ./uur-0.1.7-linux-x86_64.tar.zst
```
### Nix and NixOS
@@ -126,6 +126,7 @@ An `APKBUILD` is included under `packaging/alpine/`.
cargo build --release --locked
./hook/build.sh
./capture/build.sh
./compat/build.sh
./scripts/install-user.sh
```
+5 -4
View File
@@ -66,7 +66,7 @@ uur run
使用 pacman 安装下载的正式发布包:
```bash
sudo pacman -U ./dist/uur-0.1.6-1-x86_64.pkg.tar.zst
sudo pacman -U ./dist/uur-0.1.7-1-x86_64.pkg.tar.zst
```
### AUR
@@ -80,13 +80,13 @@ paru -S uur
### Debian、Ubuntu 及兼容发行版
```bash
sudo apt install ./uur_0.1.6_amd64.deb
sudo apt install ./uur_0.1.7_amd64.deb
```
### Fedora 及兼容 RPM 发行版
```bash
sudo dnf install ./uur-0.1.6-1.x86_64.rpm
sudo dnf install ./uur-0.1.7-1.x86_64.rpm
```
如果发行版的 RPM 依赖名称不同,请手动安装依赖或使用通用压缩包。
@@ -94,7 +94,7 @@ sudo dnf install ./uur-0.1.6-1.x86_64.rpm
### 通用压缩包
```bash
sudo tar --zstd -C / -xf ./uur-0.1.6-linux-x86_64.tar.zst
sudo tar --zstd -C / -xf ./uur-0.1.7-linux-x86_64.tar.zst
```
### Nix 与 NixOS
@@ -122,6 +122,7 @@ docker build -f packaging/alpine/Dockerfile .
cargo build --release --locked
./hook/build.sh
./capture/build.sh
./compat/build.sh
./scripts/install-user.sh
```
+514 -117
View File
@@ -2,10 +2,10 @@
/*
* uur-pw-capture — PipeWire ScreenCast consumer.
*
* Receives an inherited portal PipeWire fd (UUR_PW_FD) and the authorized
* video node id (UUR_PW_NODE), streams the monitor as BGRx and publishes
* frames into the private UUR_FRAME_PATH following the seqlock protocol in
* docs/capture-protocol.md.
* Receives an inherited portal PipeWire fd (UUR_PW_FD) and authorized monitor
* stream specifications (UUR_PW_STREAMS), composites their BGRx frames and
* publishes the virtual desktop into UUR_FRAME_PATH using the seqlock protocol
* in docs/capture-protocol.md.
*
* The shared file is created only after the stream format is negotiated,
* because its size depends on the negotiated stride. The uur PE hook maps
@@ -48,35 +48,76 @@ struct frame_header {
uint32_t stride;
uint32_t format;
_Atomic uint64_t total_frames;
uint8_t reserved[32];
uint32_t flags;
uint32_t monitor_count;
_Atomic uint64_t heartbeat_ms;
uint8_t reserved[16];
struct uurf_monitor_layout monitors[UURF_MAX_MONITORS];
};
_Static_assert(sizeof(struct frame_header) == UURF_HEADER_BYTES,
"frame protocol header size changed");
static uint32_t scale_floor(uint64_t value, uint32_t numerator,
uint32_t denominator);
static uint64_t scale_ceil(uint64_t value, uint32_t numerator,
uint32_t denominator);
struct frame_slot {
_Atomic uint64_t seq;
/* frame bytes follow */
};
struct capture {
struct pw_main_loop *loop;
struct capture;
struct capture_stream {
struct capture *capture;
struct pw_stream *stream;
struct spa_hook listener;
uint32_t node_id;
int32_t x;
int32_t y;
int32_t layout_x;
int32_t layout_y;
uint32_t logical_width;
uint32_t logical_height;
int provided_logical_size;
uint32_t width;
uint32_t height;
enum spa_video_format format;
uint8_t *pixels;
size_t pixels_size;
int dmabuf_usable;
int format_ready;
int has_frame;
int was_streaming;
uint64_t paused_ms;
uint64_t counter;
};
struct capture {
struct pw_main_loop *loop;
struct capture_stream streams[16];
uint32_t stream_count;
uint32_t width;
uint32_t height;
uint32_t stride;
enum spa_video_format format;
uint32_t flags;
uint64_t counter;
uint8_t *composite;
size_t composite_size;
int32_t layout_x;
int32_t layout_y;
uint32_t layout_width;
uint32_t layout_height;
uint32_t scale_num;
uint32_t scale_den;
int shm_fd;
uint8_t *shm;
size_t shm_size;
int dmabuf_usable;
int exit_status;
volatile sig_atomic_t stopping;
int was_streaming;
uint64_t started_ms;
uint64_t paused_ms;
char frame_path[PATH_MAX];
};
@@ -111,6 +152,34 @@ static uint8_t *slot_frame(struct frame_slot *slot)
return (uint8_t *)slot + sizeof(struct frame_slot);
}
static struct uurf_monitor_layout monitor_layout(const struct capture *c,
const struct capture_stream *s)
{
struct uurf_monitor_layout monitor = {0};
monitor.logical_x = s->layout_x;
monitor.logical_y = s->layout_y;
monitor.logical_width = s->logical_width;
monitor.logical_height = s->logical_height;
monitor.capture_width = s->width;
monitor.capture_height = s->height;
if (c->flags == 0) {
monitor.frame_width = c->width;
monitor.frame_height = c->height;
} else {
uint64_t left = (uint64_t)((int64_t)s->layout_x - c->layout_x);
uint64_t top = (uint64_t)((int64_t)s->layout_y - c->layout_y);
uint32_t x0 = scale_floor(left, c->scale_num, c->scale_den);
uint32_t y0 = scale_floor(top, c->scale_num, c->scale_den);
uint64_t x1 = scale_ceil(left + s->logical_width, c->scale_num, c->scale_den);
uint64_t y1 = scale_ceil(top + s->logical_height, c->scale_num, c->scale_den);
monitor.frame_x = x0;
monitor.frame_y = y0;
monitor.frame_width = x1 > x0 ? (uint32_t)(x1 - x0) : 1;
monitor.frame_height = y1 > y0 ? (uint32_t)(y1 - y0) : 1;
}
return monitor;
}
/* Publish a new inode for every geometry. A Wine reader may still map the
* previous inode: never truncate or resize memory another process is reading. */
static int shm_setup(struct capture *c)
@@ -157,6 +226,12 @@ static int shm_setup(struct capture *c)
header->stride = c->stride;
header->format = FRAME_FORMAT_BGRX;
atomic_init(&header->total_frames, 0);
atomic_init(&header->heartbeat_ms, monotonic_ms());
header->flags = c->flags;
header->monitor_count = c->stream_count;
for (uint32_t i = 0; i < c->stream_count; ++i) {
header->monitors[i] = monitor_layout(c, &c->streams[i]);
}
for (uint32_t i = 0; i < UURF_SLOT_COUNT; ++i) {
struct frame_slot *slot = (struct frame_slot *)(mapping +
UURF_HEADER_BYTES + i * slot_bytes);
@@ -184,10 +259,9 @@ static int shm_setup(struct capture *c)
return 0;
}
static void publish_frame(struct capture *c, const uint8_t *pixels,
uint32_t src_stride, uint32_t src_height)
static void publish_frame(struct capture *c)
{
if (c->shm == NULL)
if (c->shm == NULL || c->composite == NULL)
return;
c->counter += 1;
@@ -196,31 +270,7 @@ static void publish_frame(struct capture *c, const uint8_t *pixels,
memory_order_release); /* writing */
uint8_t *dst = slot_frame(slot);
uint32_t rows = src_height < c->height ? src_height : c->height;
for (uint32_t row = 0; row < rows; ++row) {
uint8_t *target = dst + (size_t)row * c->stride;
const uint8_t *source = pixels + (size_t)row * src_stride;
if (c->format == SPA_VIDEO_FORMAT_BGRx ||
c->format == SPA_VIDEO_FORMAT_BGRA) {
memcpy(target, source, c->stride);
} else if (c->format == SPA_VIDEO_FORMAT_RGBx ||
c->format == SPA_VIDEO_FORMAT_RGBA) {
for (uint32_t column = 0; column < c->width; ++column) {
target[column * 4 + 0] = source[column * 4 + 2];
target[column * 4 + 1] = source[column * 4 + 1];
target[column * 4 + 2] = source[column * 4 + 0];
target[column * 4 + 3] = 0;
}
} else {
int rgb = c->format == SPA_VIDEO_FORMAT_RGB;
for (uint32_t column = 0; column < c->width; ++column) {
target[column * 4 + 0] = source[column * 3 + (rgb ? 2 : 0)];
target[column * 4 + 1] = source[column * 3 + 1];
target[column * 4 + 2] = source[column * 3 + (rgb ? 0 : 2)];
target[column * 4 + 3] = 0;
}
}
}
memcpy(dst, c->composite, c->composite_size);
atomic_store_explicit(&slot->seq, 2 * c->counter, memory_order_release);
struct frame_header *header = (struct frame_header *)c->shm;
@@ -238,15 +288,253 @@ static int sync_dmabuf(int fd, uint64_t flags)
return result;
}
static int all_streams_ready(const struct capture *c)
{
for (uint32_t i = 0; i < c->stream_count; ++i) {
if (!c->streams[i].format_ready)
return 0;
}
return c->stream_count != 0;
}
static int all_streams_have_frames(const struct capture *c)
{
for (uint32_t i = 0; i < c->stream_count; ++i) {
if (!c->streams[i].has_frame)
return 0;
}
return c->stream_count != 0;
}
static int ratio_less(uint64_t left_num, uint64_t left_den,
uint64_t right_num, uint64_t right_den)
{
return left_num * right_den < right_num * left_den;
}
static uint32_t scale_floor(uint64_t value, uint32_t numerator,
uint32_t denominator)
{
return denominator == 0 ? 0 : (uint32_t)(value * numerator / denominator);
}
static uint64_t scale_ceil(uint64_t value, uint32_t numerator,
uint32_t denominator)
{
return denominator == 0 ? 0 :
(value * numerator + denominator - 1) / denominator;
}
static int configure_layout(struct capture *c)
{
uint32_t width, height, flags;
uint64_t width64, height64;
int missing_position = 0;
int64_t min_x = INT64_MAX, min_y = INT64_MAX;
int64_t max_x = INT64_MIN, max_y = INT64_MIN;
uint32_t scale_num = 0, scale_den = 1;
if (!all_streams_ready(c))
return 0;
for (uint32_t i = 0; i < c->stream_count; ++i) {
struct capture_stream *s = &c->streams[i];
if (s->logical_width == 0)
s->logical_width = s->width;
if (s->logical_height == 0)
s->logical_height = s->height;
if (s->x == INT32_MIN || s->y == INT32_MIN)
missing_position = 1;
}
if (c->stream_count == 1) {
struct capture_stream *s = &c->streams[0];
width64 = s->width;
height64 = s->height;
flags = 0;
s->layout_x = 0;
s->layout_y = 0;
c->layout_x = 0;
c->layout_y = 0;
c->layout_width = s->width;
c->layout_height = s->height;
scale_num = 1;
scale_den = 1;
} else {
int64_t next_x = 0;
for (uint32_t i = 0; i < c->stream_count; ++i) {
struct capture_stream *s = &c->streams[i];
if (missing_position && (next_x < INT32_MIN || next_x > INT32_MAX)) {
fprintf(stderr, "monitor layout position is out of range\n");
return -1;
}
s->layout_x = missing_position ? (int32_t)next_x : s->x;
s->layout_y = missing_position ? 0 : s->y;
next_x = (int64_t)s->layout_x + s->logical_width;
int64_t right = (int64_t)s->layout_x + s->logical_width;
int64_t bottom = (int64_t)s->layout_y + s->logical_height;
if (s->layout_x < min_x)
min_x = s->layout_x;
if (s->layout_y < min_y)
min_y = s->layout_y;
if (right > max_x)
max_x = right;
if (bottom > max_y)
max_y = bottom;
uint64_t ratio_num = s->width;
uint64_t ratio_den = s->logical_width;
if (ratio_less(s->height, s->logical_height, ratio_num, ratio_den)) {
ratio_num = s->height;
ratio_den = s->logical_height;
}
if (scale_num == 0 || ratio_less(ratio_num, ratio_den,
scale_num, scale_den)) {
scale_num = (uint32_t)ratio_num;
scale_den = (uint32_t)ratio_den;
}
}
if (min_x == INT64_MAX || min_y == INT64_MAX || max_x <= min_x ||
max_y <= min_y || scale_num == 0 || scale_den == 0 ||
min_x < INT32_MIN || min_x > INT32_MAX || min_y < INT32_MIN ||
min_y > INT32_MAX || max_x - min_x > UINT32_MAX ||
max_y - min_y > UINT32_MAX) {
fprintf(stderr, "invalid monitor stream layout\n");
return -1;
}
c->layout_x = (int32_t)min_x;
c->layout_y = (int32_t)min_y;
c->layout_width = (uint32_t)(max_x - min_x);
c->layout_height = (uint32_t)(max_y - min_y);
width64 = scale_ceil(c->layout_width, scale_num, scale_den);
height64 = scale_ceil(c->layout_height, scale_num, scale_den);
flags = UURF_FLAG_COMPOSITE;
}
if (width64 == 0 || height64 == 0 || width64 > 16384 || height64 > 16384 ||
width64 * 4 * height64 > 256u * 1024 * 1024) {
fprintf(stderr, "composited capture geometry is unsupported\n");
return -1;
}
width = (uint32_t)width64;
height = (uint32_t)height64;
int changed = c->width != width || c->height != height || c->flags != flags;
c->width = width;
c->height = height;
c->stride = width * 4;
c->flags = flags;
c->scale_num = scale_num;
c->scale_den = scale_den;
if (!changed && c->shm != NULL) {
const struct frame_header *previous = (const struct frame_header *)c->shm;
for (uint32_t i = 0; i < c->stream_count; ++i) {
struct uurf_monitor_layout current = monitor_layout(c, &c->streams[i]);
if (memcmp(&current, &previous->monitors[i], sizeof(current))) {
changed = 1;
break;
}
}
}
if (changed || c->composite == NULL || c->shm == NULL) {
size_t bytes = (size_t)c->stride * height;
uint8_t *fresh = realloc(c->composite, bytes);
if (fresh == NULL) {
fprintf(stderr, "could not allocate composited capture frame\n");
return -1;
}
c->composite = fresh;
c->composite_size = bytes;
memset(c->composite, 0, bytes);
if (shm_setup(c) < 0)
return -1;
}
return 1;
}
static void compose_frame(struct capture *c)
{
if (c->composite == NULL || c->width == 0 || c->height == 0)
return;
if (c->stream_count == 1) {
memcpy(c->composite, c->streams[0].pixels, c->composite_size);
return;
}
memset(c->composite, 0, c->composite_size);
for (uint32_t i = 0; i < c->stream_count; ++i) {
struct capture_stream *s = &c->streams[i];
uint64_t left_logical = (uint64_t)((int64_t)s->layout_x - c->layout_x);
uint64_t top_logical = (uint64_t)((int64_t)s->layout_y - c->layout_y);
uint32_t left = scale_floor(left_logical, c->scale_num, c->scale_den);
uint32_t top = scale_floor(top_logical, c->scale_num, c->scale_den);
uint32_t right = scale_ceil(left_logical + s->logical_width,
c->scale_num, c->scale_den);
uint32_t bottom = scale_ceil(top_logical + s->logical_height,
c->scale_num, c->scale_den);
uint32_t target_width = right > left ? right - left : 1;
uint32_t target_height = bottom > top ? bottom - top : 1;
if (left >= c->width || top >= c->height)
continue;
if (target_width > c->width - left)
target_width = c->width - left;
if (target_height > c->height - top)
target_height = c->height - top;
if (target_width == s->width && target_height == s->height) {
for (uint32_t row = 0; row < target_height; ++row) {
memcpy(c->composite + (size_t)(top + row) * c->stride + (size_t)left * 4,
s->pixels + (size_t)row * s->width * 4, (size_t)target_width * 4);
}
continue;
}
for (uint32_t row = 0; row < target_height; ++row) {
uint32_t source_y = (uint32_t)((uint64_t)row * s->height / target_height);
uint8_t *target = c->composite + (size_t)(top + row) * c->stride + (size_t)left * 4;
const uint8_t *source = s->pixels + (size_t)source_y * s->width * 4;
for (uint32_t column = 0; column < target_width; ++column) {
uint32_t source_x = (uint32_t)((uint64_t)column * s->width / target_width);
memcpy(target + (size_t)column * 4, source + (size_t)source_x * 4, 4);
}
}
}
}
static void normalize_pixels(struct capture_stream *s, const uint8_t *pixels,
uint32_t source_stride)
{
uint32_t source_bpp =
s->format == SPA_VIDEO_FORMAT_BGR || s->format == SPA_VIDEO_FORMAT_RGB ? 3u : 4u;
if (s->format == SPA_VIDEO_FORMAT_BGRx || s->format == SPA_VIDEO_FORMAT_BGRA) {
for (uint32_t row = 0; row < s->height; ++row) {
memcpy(s->pixels + (size_t)row * s->width * 4,
pixels + (size_t)row * source_stride, (size_t)s->width * 4);
}
return;
}
for (uint32_t row = 0; row < s->height; ++row) {
const uint8_t *source = pixels + (size_t)row * source_stride;
uint8_t *target = s->pixels + (size_t)row * s->width * 4;
for (uint32_t column = 0; column < s->width; ++column) {
int rgb = s->format == SPA_VIDEO_FORMAT_RGBx ||
s->format == SPA_VIDEO_FORMAT_RGBA ||
s->format == SPA_VIDEO_FORMAT_RGB;
target[column * 4] = source[column * source_bpp + (rgb ? 2 : 0)];
target[column * 4 + 1] = source[column * source_bpp + 1];
target[column * 4 + 2] = source[column * source_bpp + (rgb ? 0 : 2)];
target[column * 4 + 3] = 0;
}
}
}
static void on_process(void *userdata)
{
struct capture *c = userdata;
struct pw_buffer *buffer = pw_stream_dequeue_buffer(c->stream);
struct capture_stream *s = userdata;
struct capture *c = s->capture;
struct pw_buffer *buffer = pw_stream_dequeue_buffer(s->stream);
if (buffer == NULL)
return;
struct spa_buffer *buf = buffer->buffer;
if (buf->n_datas >= 1 && c->shm != NULL) {
if (buf->n_datas >= 1 && s->format_ready && s->pixels != NULL) {
struct spa_data *plane = &buf->datas[0];
uint8_t *pixels = NULL;
void *mapping = MAP_FAILED;
@@ -257,18 +545,17 @@ static void on_process(void *userdata)
if (plane->chunk == NULL || plane->chunk->stride < 0 ||
(plane->chunk->flags & (SPA_CHUNK_FLAG_CORRUPTED | SPA_CHUNK_FLAG_EMPTY)))
goto done;
uint32_t row_bytes = c->width *
(c->format == SPA_VIDEO_FORMAT_BGR || c->format == SPA_VIDEO_FORMAT_RGB ? 3u : 4u);
uint32_t source_bpp =
s->format == SPA_VIDEO_FORMAT_BGR || s->format == SPA_VIDEO_FORMAT_RGB ? 3u : 4u;
uint32_t row_bytes = s->width * source_bpp;
uint32_t stride = plane->chunk->stride > 0 ?
(uint32_t)plane->chunk->stride : row_bytes;
uint64_t span = (uint64_t)stride * (c->height - 1) + row_bytes;
uint64_t span = (uint64_t)stride * (s->height - 1) + row_bytes;
uint32_t data_offset = plane->chunk->offset;
if (stride < row_bytes || span > 256u * 1024 * 1024)
goto done;
if (dmabuf) {
// DMA-BUF maxsize/chunk size may be zero or placeholders. Query
// the fd and include BOTH the plane mapping and chunk offsets.
if (!c->dmabuf_usable || plane->fd < 0)
if (!s->dmabuf_usable || plane->fd < 0)
goto done;
off_t extent = lseek((int)plane->fd, 0, SEEK_END);
uint64_t end = (uint64_t)plane->mapoffset + data_offset + span;
@@ -302,10 +589,16 @@ static void on_process(void *userdata)
pixels = (uint8_t *)mapping + delta;
}
if (pixels != NULL) {
if (c->counter == 0)
fprintf(stderr, "capture buffer: %s, source stride %u, chunk offset %u\n",
dmabuf ? "linear DMA-BUF" : "shared memory", stride, data_offset);
publish_frame(c, pixels, stride, c->height);
if (s->counter == 0)
fprintf(stderr, "stream %u buffer: %s, source stride %u, chunk offset %u\n",
s->node_id, dmabuf ? "linear DMA-BUF" : "shared memory", stride, data_offset);
normalize_pixels(s, pixels, stride);
s->counter++;
s->has_frame = 1;
if (c->shm != NULL && all_streams_have_frames(c)) {
compose_frame(c);
publish_frame(c);
}
}
done:
if (synchronized)
@@ -314,13 +607,14 @@ done:
munmap(mapping, mapping_size);
}
pw_stream_queue_buffer(c->stream, buffer);
pw_stream_queue_buffer(s->stream, buffer);
}
static void on_param_changed(void *userdata, uint32_t id,
const struct spa_pod *param)
{
struct capture *c = userdata;
struct capture_stream *s = userdata;
struct capture *c = s->capture;
if (param == NULL || id != SPA_PARAM_Format)
return;
@@ -333,56 +627,89 @@ static void on_param_changed(void *userdata, uint32_t id,
info.format != SPA_VIDEO_FORMAT_RGBA &&
info.format != SPA_VIDEO_FORMAT_BGR &&
info.format != SPA_VIDEO_FORMAT_RGB) {
fprintf(stderr, "negotiated unexpected format %d\n", info.format);
fprintf(stderr, "stream %u negotiated unexpected format %d\n",
s->node_id, info.format);
return;
}
if (info.size.width == 0 || info.size.height == 0 ||
info.size.width > 16384 || info.size.height > 16384 ||
(uint64_t)info.size.width * 4 * info.size.height > 256u * 1024 * 1024) {
fprintf(stderr, "unsupported capture geometry\n");
fprintf(stderr, "stream %u has unsupported capture geometry\n", s->node_id);
c->exit_status = 1;
pw_main_loop_quit(c->loop);
return;
}
c->width = (uint32_t)info.size.width;
c->height = (uint32_t)info.size.height;
c->format = info.format;
c->stride = SPA_ROUND_UP_N(c->width * 4, 4);
int changed = !s->format_ready || s->width != info.size.width ||
s->height != info.size.height || s->format != info.format;
s->width = (uint32_t)info.size.width;
s->height = (uint32_t)info.size.height;
if (!s->provided_logical_size) {
s->logical_width = s->width;
s->logical_height = s->height;
}
s->format = info.format;
/* The DMA-BUF path mmaps the fd directly, which is only correct for a
* linear layout. A tiled modifier needs a real importer (GBM/EGL), so
* refuse to publish garbage rather than emit a corrupt frame. */
c->dmabuf_usable = spa_pod_find_prop(param, NULL, SPA_FORMAT_VIDEO_modifier) != NULL &&
s->dmabuf_usable = spa_pod_find_prop(param, NULL, SPA_FORMAT_VIDEO_modifier) != NULL &&
info.modifier == DRM_FORMAT_MOD_LINEAR;
fprintf(stderr, "negotiated: %ux%u stride %u modifier %#llx%s\n", c->width,
c->height, c->stride, (unsigned long long)info.modifier,
c->dmabuf_usable ? "" : " (DMA-BUF CPU mapping disabled)");
const struct frame_header *header = (const struct frame_header *)c->shm;
if (header == NULL || header->width != c->width || header->height != c->height) {
if (shm_setup(c) < 0) {
if (changed) {
size_t bytes = (size_t)s->width * s->height * 4;
size_t source_total = bytes;
for (uint32_t i = 0; i < c->stream_count; ++i) {
if (&c->streams[i] != s)
source_total += c->streams[i].pixels_size;
}
if (source_total > 512u * 1024 * 1024) {
fprintf(stderr, "monitor source buffers exceed the memory limit\n");
c->exit_status = 1;
pw_main_loop_quit(c->loop);
return;
}
uint8_t *fresh = realloc(s->pixels, bytes);
if (fresh == NULL) {
fprintf(stderr, "could not allocate stream %u frame buffer\n", s->node_id);
c->exit_status = 1;
pw_main_loop_quit(c->loop);
return;
}
s->pixels = fresh;
s->pixels_size = bytes;
memset(s->pixels, 0, bytes);
s->has_frame = 0;
}
s->format_ready = 1;
fprintf(stderr, "stream %u negotiated: %ux%u modifier %#llx%s\n", s->node_id,
s->width, s->height, (unsigned long long)info.modifier,
s->dmabuf_usable ? "" : " (DMA-BUF CPU mapping disabled)");
int layout = configure_layout(c);
if (layout < 0) {
c->exit_status = 1;
pw_main_loop_quit(c->loop);
} else if (layout > 0 && all_streams_have_frames(c)) {
compose_frame(c);
publish_frame(c);
}
}
static void on_state_changed(void *userdata, enum pw_stream_state old,
enum pw_stream_state state, const char *error)
{
struct capture *c = userdata;
fprintf(stderr, "stream state: %s%s%s\n",
struct capture_stream *s = userdata;
struct capture *c = s->capture;
fprintf(stderr, "stream %u state: %s%s%s\n", s->node_id,
pw_stream_state_as_string(state),
error != NULL && error[0] != '\0' ? " - " : "",
error != NULL ? error : "");
if (c->stopping)
return;
if (state == PW_STREAM_STATE_STREAMING) {
c->was_streaming = 1;
c->paused_ms = 0;
} else if (state == PW_STREAM_STATE_PAUSED && c->was_streaming) {
if (c->paused_ms == 0)
c->paused_ms = monotonic_ms();
s->was_streaming = 1;
s->paused_ms = 0;
} else if (state == PW_STREAM_STATE_PAUSED && s->was_streaming) {
if (s->paused_ms == 0)
s->paused_ms = monotonic_ms();
} else if (state == PW_STREAM_STATE_ERROR ||
(state == PW_STREAM_STATE_UNCONNECTED && old != state)) {
c->exit_status = 75;
@@ -395,14 +722,20 @@ static void on_watchdog(void *userdata, uint64_t expirations)
struct capture *c = userdata;
(void)expirations;
uint64_t now = monotonic_ms();
// A static desktop can legitimately produce no new frames while streaming.
// Only a prolonged PAUSED state or a stream with no FIRST frame is fatal.
if (!c->stopping &&
((c->paused_ms != 0 && now - c->paused_ms >= 10000) ||
(c->counter == 0 && now - c->started_ms >= 30000))) {
fprintf(stderr, "capture stream stopped producing usable frames; reconnecting\n");
c->exit_status = 75;
pw_main_loop_quit(c->loop);
if (c->shm != NULL)
atomic_store_explicit(&((struct frame_header *)c->shm)->heartbeat_ms,
now, memory_order_release);
for (uint32_t i = 0; i < c->stream_count; ++i) {
struct capture_stream *s = &c->streams[i];
if (!c->stopping &&
((s->paused_ms != 0 && now - s->paused_ms >= 10000) ||
(!s->has_frame && now - c->started_ms >= 30000))) {
fprintf(stderr, "stream %u stopped producing usable frames; reconnecting\n",
s->node_id);
c->exit_status = 75;
pw_main_loop_quit(c->loop);
return;
}
}
}
@@ -451,30 +784,75 @@ static const struct spa_pod *capture_format(struct spa_pod_builder *builder,
return spa_pod_builder_pop(builder, &frame);
}
static int parse_stream_specs(struct capture *c, const char *text)
{
char *copy = strdup(text);
if (copy == NULL)
return -1;
char *save = NULL;
char *item = strtok_r(copy, ";", &save);
while (item != NULL) {
unsigned node, width, height;
int x, y;
if (c->stream_count >= sizeof(c->streams) / sizeof(c->streams[0]) ||
sscanf(item, "%u,%d,%d,%u,%u", &node, &x, &y, &width, &height) != 5) {
free(copy);
return -1;
}
struct capture_stream *s = &c->streams[c->stream_count++];
s->node_id = node;
s->x = x;
s->y = y;
s->logical_width = width;
s->logical_height = height;
s->provided_logical_size = width != 0 && height != 0;
item = strtok_r(NULL, ";", &save);
}
free(copy);
return c->stream_count == 0 ? -1 : 0;
}
int main(void)
{
const char *fd_text = getenv("UUR_PW_FD");
const char *node_text = getenv("UUR_PW_NODE");
if (fd_text == NULL || node_text == NULL) {
fprintf(stderr,
"UUR_PW_FD and UUR_PW_NODE must be set by 'uur capture'\n");
return 2;
}
const char *spec_text = getenv("UUR_PW_STREAMS");
const char *legacy_node = getenv("UUR_PW_NODE");
const char *frame_path = getenv("UUR_FRAME_PATH");
if (frame_path == NULL || frame_path[0] != '/' ||
if (fd_text == NULL || frame_path == NULL || frame_path[0] != '/' ||
strlen(frame_path) >= PATH_MAX) {
fprintf(stderr, "UUR_FRAME_PATH must be a short absolute path\n");
fprintf(stderr, "UUR_PW_FD and an absolute UUR_FRAME_PATH are required\n");
return 2;
}
int pw_fd = atoi(fd_text);
uint32_t node_id = (uint32_t)strtoul(node_text, NULL, 10);
pw_init(NULL, NULL);
struct capture c;
memset(&c, 0, sizeof(c));
c.shm_fd = -1;
if (spec_text != NULL) {
if (parse_stream_specs(&c, spec_text) < 0) {
fprintf(stderr, "invalid UUR_PW_STREAMS specification\n");
return 2;
}
} else if (legacy_node != NULL) {
char *end = NULL;
errno = 0;
unsigned long node = strtoul(legacy_node, &end, 10);
if (errno != 0 || end == legacy_node || *end != '\0' || node > UINT32_MAX) {
fprintf(stderr, "invalid UUR_PW_NODE\n");
return 2;
}
c.stream_count = 1;
c.streams[0].node_id = (uint32_t)node;
c.streams[0].x = INT32_MIN;
c.streams[0].y = INT32_MIN;
} else {
fprintf(stderr, "UUR_PW_STREAMS must be set by uur\n");
return 2;
}
memcpy(c.frame_path, frame_path, strlen(frame_path) + 1);
fprintf(stderr, "capturing %u ScreenCast stream(s)\n", c.stream_count);
pw_init(NULL, NULL);
active_capture = &c;
signal(SIGINT, stop_capture);
signal(SIGTERM, stop_capture);
@@ -494,8 +872,7 @@ int main(void)
return 1;
}
/* Connect through the portal's private PipeWire instance: only the
* consented node is reachable on this descriptor. */
/* The portal fd only exposes streams covered by this ScreenCast consent. */
struct pw_core *core = pw_context_connect_fd(context, pw_fd, NULL, 0);
if (core == NULL) {
fprintf(stderr, "pw_context_connect_fd failed\n");
@@ -503,16 +880,7 @@ int main(void)
}
struct spa_hook core_listener;
pw_core_add_listener(core, &core_listener, &core_events, &c);
struct pw_properties *props = pw_properties_new(
PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY, "Capture",
PW_KEY_MEDIA_ROLE, "Screen", NULL);
c.stream = pw_stream_new(core, "uur-capture", props);
if (c.stream == NULL) {
fprintf(stderr, "pw_stream_new failed\n");
return 1;
}
pw_stream_add_listener(c.stream, &c.listener, &stream_events, &c);
struct spa_source *watchdog = NULL;
uint8_t pod_buffer[1024];
struct spa_pod_builder pod_builder =
@@ -522,28 +890,56 @@ int main(void)
params[1] = capture_format(&pod_builder, 0);
c.started_ms = monotonic_ms();
if (pw_stream_connect(c.stream, PW_DIRECTION_INPUT, node_id,
PW_STREAM_FLAG_MAP_BUFFERS |
PW_STREAM_FLAG_AUTOCONNECT,
params, 2) < 0) {
fprintf(stderr, "pw_stream_connect failed\n");
return 1;
for (uint32_t i = 0; i < c.stream_count; ++i) {
struct capture_stream *s = &c.streams[i];
s->capture = &c;
char name[64];
snprintf(name, sizeof(name), "uur-capture-%u", i + 1);
struct pw_properties *props = pw_properties_new(
PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY, "Capture",
PW_KEY_MEDIA_ROLE, "Screen", NULL);
s->stream = pw_stream_new(core, name, props);
if (s->stream == NULL) {
fprintf(stderr, "pw_stream_new failed for node %u\n", s->node_id);
c.exit_status = 1;
goto cleanup;
}
pw_stream_add_listener(s->stream, &s->listener, &stream_events, s);
if (pw_stream_connect(s->stream, PW_DIRECTION_INPUT, s->node_id,
PW_STREAM_FLAG_MAP_BUFFERS |
PW_STREAM_FLAG_AUTOCONNECT,
params, 2) < 0) {
fprintf(stderr, "pw_stream_connect failed for node %u\n", s->node_id);
c.exit_status = 1;
goto cleanup;
}
}
struct spa_source *watchdog = pw_loop_add_timer(pw_main_loop_get_loop(loop),
on_watchdog, &c);
watchdog = pw_loop_add_timer(pw_main_loop_get_loop(loop), on_watchdog, &c);
struct timespec interval = {.tv_sec = 1};
if (watchdog == NULL || pw_loop_update_timer(pw_main_loop_get_loop(loop),
watchdog, &interval, &interval, false) < 0) {
fprintf(stderr, "could not start capture watchdog\n");
return 1;
c.exit_status = 1;
goto cleanup;
}
pw_main_loop_run(loop);
c.stopping = 1;
active_capture = NULL;
pw_loop_destroy_source(pw_main_loop_get_loop(loop), watchdog);
pw_stream_destroy(c.stream);
cleanup:
c.stopping = 1;
active_capture = NULL;
if (watchdog != NULL)
pw_loop_destroy_source(pw_main_loop_get_loop(loop), watchdog);
for (uint32_t i = 0; i < c.stream_count; ++i) {
struct capture_stream *s = &c.streams[i];
if (s->stream != NULL) {
pw_stream_destroy(s->stream);
spa_hook_remove(&s->listener);
}
free(s->pixels);
}
spa_hook_remove(&core_listener);
pw_core_disconnect(core);
pw_context_destroy(context);
@@ -555,6 +951,7 @@ int main(void)
}
if (c.shm_fd >= 0)
close(c.shm_fd);
free(c.composite);
unlink(c.frame_path);
pw_deinit();
return c.exit_status;
+15 -1
View File
@@ -24,7 +24,9 @@
struct frame_header {
uint32_t magic, version, width, height, stride, format;
_Atomic uint64_t total_frames;
uint8_t reserved[32];
uint32_t flags, monitor_count;
uint8_t reserved[24];
struct uurf_monitor_layout monitors[UURF_MAX_MONITORS];
};
_Static_assert(sizeof(struct frame_header) == UURF_HEADER_BYTES, "frame header ABI");
@@ -136,6 +138,18 @@ static int setup_ring(struct capture *c)
header->width = c->width; header->height = c->height;
header->stride = c->width * 4; header->format = 1;
atomic_init(&header->total_frames, 0);
header->flags = 0;
header->monitor_count = 1;
header->monitors[0].logical_x = 0;
header->monitors[0].logical_y = 0;
header->monitors[0].logical_width = c->width;
header->monitors[0].logical_height = c->height;
header->monitors[0].frame_x = 0;
header->monitors[0].frame_y = 0;
header->monitors[0].frame_width = c->width;
header->monitors[0].frame_height = c->height;
header->monitors[0].capture_width = c->width;
header->monitors[0].capture_height = c->height;
for (uint32_t i = 0; i < UURF_SLOT_COUNT; ++i) {
_Atomic uint64_t *seq = (_Atomic uint64_t *)((uint8_t *)c->ring +
UURF_HEADER_BYTES + i * (sizeof(uint64_t) + bytes));
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
# Build the Wine lock-modifier shim (LD_PRELOAD, links libX11).
set -euo pipefail
root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd -P)
readonly root
out="$root/build/compat"
cc="${CC:-gcc}"
flags=(-std=c11 -O2 -Wall -Wextra -Werror -fPIC -pthread)
command -v "$cc" >/dev/null 2>&1 || { printf 'missing tool: %s\n' "$cc" >&2; exit 1; }
command -v pkg-config >/dev/null 2>&1 || { printf 'missing tool: pkg-config\n' >&2; exit 1; }
mkdir -p "$out"
"$cc" "${flags[@]}" -shared -o "$out/libuur-lock-mods.so" \
"$root/compat/uur-lock-mods.c" \
$(pkg-config --cflags --libs x11) -ldl
printf 'built: %s\n' "$out/libuur-lock-mods.so"
+193
View File
@@ -0,0 +1,193 @@
/* Process-local workaround for Wine/UU lock-key reconciliation. */
#define _GNU_SOURCE
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/keysym.h>
#include <dlfcn.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef Bool (*predicate_fn)(Display *, XEvent *, XPointer);
static pthread_once_t init_once = PTHREAD_ONCE_INIT;
static unsigned int requested_mask;
static int enabled;
static _Atomic unsigned long generation = 1;
static _Thread_local Display *cached_display;
static _Thread_local unsigned long cached_generation;
static _Thread_local unsigned int cached_mask;
#define EVENT_FUNCTIONS(X) \
X(int, XNextEvent, (Display *d, XEvent *e), (d, e), rc == 0) \
X(int, XPeekEvent, (Display *d, XEvent *e), (d, e), rc == 0) \
X(int, XWindowEvent, (Display *d, Window w, long m, XEvent *e), (d, w, m, e), rc == 0) \
X(Bool, XCheckWindowEvent, (Display *d, Window w, long m, XEvent *e), (d, w, m, e), rc != 0) \
X(int, XMaskEvent, (Display *d, long m, XEvent *e), (d, m, e), rc == 0) \
X(Bool, XCheckMaskEvent, (Display *d, long m, XEvent *e), (d, m, e), rc != 0) \
X(Bool, XCheckTypedEvent, (Display *d, int t, XEvent *e), (d, t, e), rc != 0) \
X(Bool, XCheckTypedWindowEvent, (Display *d, Window w, int t, XEvent *e), (d, w, t, e), rc != 0) \
X(int, XIfEvent, (Display *d, XEvent *e, predicate_fn p, XPointer a), (d, e, p, a), rc == 0) \
X(int, XPeekIfEvent, (Display *d, XEvent *e, predicate_fn p, XPointer a), (d, e, p, a), rc == 0) \
X(Bool, XCheckIfEvent, (Display *d, XEvent *e, predicate_fn p, XPointer a), (d, e, p, a), rc != 0)
#define DECLARE(result, name, args, call, success) static result (*real_##name) args;
EVENT_FUNCTIONS(DECLARE)
#undef DECLARE
static int (*real_XRefreshKeyboardMapping)(XMappingEvent *);
static Status (*real_XkbRefreshKeyboardMapping)(XkbMapNotifyEvent *);
static int (*real_XCloseDisplay)(Display *);
static void *symbol(const char *name)
{
void *address = dlsym(RTLD_NEXT, name);
if (address == NULL) {
fprintf(stderr, "uur keyboard compatibility: cannot resolve %s\n", name);
abort();
}
return address;
}
static int wine_process(void)
{
char executable[PATH_MAX];
ssize_t size = readlink("/proc/self/exe", executable, sizeof(executable) - 1);
if (size <= 0 || (size_t)size >= sizeof(executable) - 1)
return 0;
executable[size] = '\0';
char *deleted = strstr(executable, " (deleted)");
if (deleted != NULL)
*deleted = '\0';
char *name = strrchr(executable, '/');
name = name == NULL ? executable : name + 1;
if (*name == '.')
name++;
char *wrapped = strstr(name, "-wrapped");
if (wrapped != NULL && wrapped[8] == '\0')
*wrapped = '\0';
return !strcmp(name, "wine") || !strcmp(name, "wine64") ||
!strcmp(name, "wine-preloader") || !strcmp(name, "wine64-preloader") ||
!strcmp(name, "wineserver");
}
static void initialize(void)
{
#define RESOLVE(result, name, args, call, success) real_##name = symbol(#name);
EVENT_FUNCTIONS(RESOLVE)
#undef RESOLVE
real_XRefreshKeyboardMapping = symbol("XRefreshKeyboardMapping");
real_XkbRefreshKeyboardMapping = symbol("XkbRefreshKeyboardMapping");
real_XCloseDisplay = symbol("XCloseDisplay");
if (getenv("WINEPREFIX") == NULL || !wine_process())
return;
const char *mask = getenv("UUR_STRIP_LOCK_MODS");
requested_mask = 0xff;
if (mask != NULL && strcmp(mask, "auto")) {
char *end;
errno = 0;
unsigned long value = strtoul(mask, &end,
mask[0] == '0' && (mask[1] == 'x' || mask[1] == 'X') ? 16 : 10);
if (errno || end == mask || *end != '\0' || value > 0xff) {
fprintf(stderr, "uur keyboard compatibility: invalid modifier mask\n");
return;
}
requested_mask = (unsigned int)value;
}
enabled = requested_mask != 0;
}
/* A modifier shared with a shortcut/layout key is left alone. CapsLock and
* ShiftLock are never removed: their state affects character translation. */
static unsigned int locking_mask(Display *display)
{
unsigned long current = atomic_load_explicit(&generation, memory_order_relaxed);
if (cached_display == display && cached_generation == current)
return cached_mask;
unsigned int mask = 0;
XModifierKeymap *mapping = XGetModifierMapping(display);
if (mapping != NULL) {
for (int modifier = 3; modifier < 8; ++modifier) {
int locking = 0, other = 0;
for (int key = 0; key < mapping->max_keypermod; ++key) {
KeyCode code = mapping->modifiermap[modifier * mapping->max_keypermod + key];
if (code == 0)
continue;
int lock_key = 0;
for (int level = 0; level < 4; ++level) {
KeySym sym = XkbKeycodeToKeysym(display, code, 0, level);
if (sym == XK_Num_Lock || sym == XK_Scroll_Lock)
lock_key = 1;
else if (sym != NoSymbol)
other = 1;
}
if (lock_key)
locking = 1;
else
other = 1;
}
if (locking && !other)
mask |= 1u << modifier;
}
XFreeModifiermap(mapping);
}
cached_display = display;
cached_generation = current;
cached_mask = mask & requested_mask;
return cached_mask;
}
static void scrub(Display *display, XEvent *event)
{
if (!enabled || event == NULL)
return;
if (event->type == MappingNotify) {
atomic_fetch_add_explicit(&generation, 1, memory_order_relaxed);
return;
}
if (event->type != KeyPress && event->type != KeyRelease)
return;
/* NumLock changes keypad virtual keys in EVENT_event_to_vkey(). Leave
* those events intact rather than turning numeric input into navigation. */
KeySym key = XkbKeycodeToKeysym(display, event->xkey.keycode, 0, 0);
if (IsKeypadKey(key))
return;
event->xkey.state &= ~locking_mask(display);
}
#define IMPLEMENT(result, name, args, call, success) \
result name args { \
pthread_once(&init_once, initialize); \
result rc = real_##name call; \
if (success) scrub(d, e); \
return rc; \
}
EVENT_FUNCTIONS(IMPLEMENT)
#undef IMPLEMENT
int XRefreshKeyboardMapping(XMappingEvent *event)
{
pthread_once(&init_once, initialize);
int result = real_XRefreshKeyboardMapping(event);
atomic_fetch_add_explicit(&generation, 1, memory_order_relaxed);
return result;
}
Status XkbRefreshKeyboardMapping(XkbMapNotifyEvent *event)
{
pthread_once(&init_once, initialize);
Status result = real_XkbRefreshKeyboardMapping(event);
atomic_fetch_add_explicit(&generation, 1, memory_order_relaxed);
return result;
}
int XCloseDisplay(Display *display)
{
pthread_once(&init_once, initialize);
atomic_fetch_add_explicit(&generation, 1, memory_order_relaxed);
return real_XCloseDisplay(display);
}
+2 -2
View File
@@ -6,7 +6,7 @@ adapts stable Windows APIs at runtime; the Rust host owns policy, desktop
integration, and process lifetime.
```text
UU Remote / Wine
UU Remote / Wine (LD_PRELOAD: compat/libuur-lock-mods.so)
└─ PE ABI adapter (SendInput, GDI/DXGI capture entry points)
└─ authenticated loopback protocol + private frame transport
└─ Rust session supervisor
@@ -62,7 +62,7 @@ RAII cleanup terminates only those helper groups and the one managed Wine
prefix. There are no permanent daemons, global `pkill` calls, or required
systemd units.
Frame data lives in `$XDG_RUNTIME_DIR/uur/frames.v1` with mode 0600. The path is
Frame data lives in `$XDG_RUNTIME_DIR/uur/frames.v3` with mode 0600. The path is
passed through the adapter configuration, so users and concurrent sessions do
not share a global `/dev/shm` object.
+45 -15
View File
@@ -1,16 +1,18 @@
# Capture frame protocol
How frames travel from the compositor (via PipeWire) into the client's
capture calls. Two producers/consumers, one shared file, no locks.
capture calls. Multiple selected monitor streams are composed into one virtual
desktop frame in the shared transport; the Windows hook crops each monitor
request from that frame.
```text
xdg-desktop-portal ScreenCast
│ (portal-consented session; per-monitor)
│ (portal-consented session; one stream per selected monitor)
▼
uur-pw-capture (C) ── PipeWire stream, BGRx, SHM buffers
uur-pw-capture (C) ── PipeWire streams, BGRx, desktop composition
│ writes slots, seqlock per slot
▼
$XDG_RUNTIME_DIR/uur/frames.v1 (mode 0600, 64-byte header + 3 slots)
$XDG_RUNTIME_DIR/uur/frames.v3 (mode 0600, 704-byte header + 3 slots)
│ mapped read-only by the PE hook through its configured Wine Z: path
▼
uur-hook.dll ── intercepts static/dynamic gdi32!BitBlt/StretchBlt calls;
@@ -19,20 +21,31 @@ uur-hook.dll ── intercepts static/dynamic gdi32!BitBlt/StretchBlt calls;
## Shared file layout
All integers little-endian. Total size = 64 + 3 × (8 + stride × height).
All integers little-endian. Total size = 704 + 3 × (8 + stride × height).
| offset | type | field |
| --- | --- | --- |
| 0 | u32 | magic `0x46525555` ("UURF") |
| 4 | u32 | protocol version = 1 |
| 4 | u32 | protocol version = 3 |
| 8 | u32 | frame width (px) |
| 12 | u32 | frame height (px) |
| 16 | u32 | stride (bytes per row) |
| 20 | u32 | format: 1 = BGRx (32 bits per pixel) |
| 24 | u64 | total frames written (monotonic counter) |
| 32..64 | — | reserved, zero |
| 32 | u32 | flags: bit 0 means the frame is a virtual-desktop composite |
| 36 | u32 | monitor layout record count (1..16) |
| 40 | u64 | producer CLOCK_MONOTONIC heartbeat in milliseconds; zero if unsupported |
| 48..64 | — | reserved, zero |
Slot *i* (i = 0, 1, 2) at offset `64 + i × (8 + stride × height)`:
The fixed-size monitor table starts at offset 64 and contains 16 records of
40 bytes each. Unused records are zero. Each record contains signed logical
`x` and `y`, logical `width` and `height`, then unsigned `frame_x`, `frame_y`,
`frame_width` and `frame_height` describing that monitor's rectangle in the
composited frame, then `capture_width` and `capture_height` for the negotiated
source stream resolution. A single-stream capture has one record covering the
full frame.
Slot *i* (i = 0, 1, 2) at offset `704 + i × (8 + stride × height)`:
| offset | type | field |
| --- | --- | --- |
@@ -50,12 +63,27 @@ the managed prefix configuration. It is never a global shared-memory name.
## Geometry and session changes
An exact Wine monitor rectangle maps onto the entire selected frame when their
aspect ratios match. Pixel dimensions may differ under fractional scaling, where
Xwayland's render size can exceed the portal's native frame size. Partial regions
and whole-virtual-desktop requests retain the existing virtual-desktop mapping.
This does not establish monitor identity between Wine and the portal; selecting
a non-primary monitor still requires consistent client/input coordinates.
For one selected stream, an exact Wine monitor rectangle maps onto the entire
frame when their aspect ratios match. With multiple selected monitor streams,
the producer uses portal position/size metadata to compose the streams into a
virtual desktop frame; Wine monitor rectangles then crop their matching region.
If a portal omits layout metadata, streams are arranged left to right. Fractional
scaling is normalized to a shared frame scale; each stream is resampled into
its reported logical rectangle. The hook matches Wine monitor bounds to the
captured layout and crops in monitor-local coordinates, so differing monitor
resolutions and vertical offsets do not shift the top edge of a screen.
The capture service's virtual display API uses the composed frame rectangles
relative to the primary monitor, including a common pixel scale. UI and WebView
processes retain native display geometry. Raw source pixel sizes remain metadata
and are never combined with logical origins. Portals without stable output
identity require the selected monitor count to match Wine's monitor count for
per-monitor matching; otherwise the composite is used as a whole.
Multiple monitors are explicitly enabled with `uur display source monitor
--multiple`, followed by a new session and monitor selection in the portal.
Upgrading preserves existing grants. Choosing `uur display source monitor`
returns to a single selected source. Absolute input selects the consented node
containing the logical position, then sends that node's local coordinates.
Each transport inode has immutable geometry. A negotiated size change creates
a new mode-0600 file, initializes its header and slots, then atomically replaces
@@ -69,7 +97,9 @@ fd extent, readiness fence and CPU-access synchronization. Implicit modifiers
require a graphics importer and are not advertised by this CPU consumer.
A stream error, lost connection or ten seconds in PAUSED after streaming makes
the helper exit with status 75. The ScreenCast supervisor retries up to three
the helper exit with status 75. A ten-second expired producer heartbeat also
restarts a hung helper, independently of desktop changes. Both ScreenCast and
combined RemoteDesktop supervisors retry up to three
times using the saved restore token, with a one-second backoff. Thirty seconds
of healthy capture resets the retry budget. An unchanged desktop in STREAMING
does not trigger this watchdog. Recovery keeps the saved token and performs no
+7 -6
View File
@@ -12,13 +12,13 @@ needed for system-wide package installation.
paru -S uur
# Downloaded release package
sudo pacman -U ./uur-0.1.6-1-x86_64.pkg.tar.zst
sudo pacman -U ./uur-0.1.7-1-x86_64.pkg.tar.zst
```
## Debian, Ubuntu, and derivatives
```bash
sudo apt install ./uur_0.1.6_amd64.deb
sudo apt install ./uur_0.1.7_amd64.deb
```
The same release artifact covers Debian, Ubuntu, Linux Mint, Pop!_OS, KDE neon,
@@ -28,10 +28,10 @@ derivatives.
## Fedora, RHEL, and openSUSE families
```bash
sudo dnf install ./uur-0.1.6-1.x86_64.rpm
sudo dnf install ./uur-0.1.7-1.x86_64.rpm
# openSUSE
sudo zypper install ./uur-0.1.6-1.x86_64.rpm
sudo zypper install ./uur-0.1.7-1.x86_64.rpm
```
This format also covers Fedora derivatives and RPM systems whose repositories
@@ -42,7 +42,7 @@ provide the declared Wine, GLib, PipeWire, and portal dependencies.
The portable archive targets conventional glibc/FHS systems:
```bash
sudo tar --zstd -C / -xf ./uur-0.1.6-linux-x86_64.tar.zst
sudo tar --zstd -C / -xf ./uur-0.1.7-linux-x86_64.tar.zst
```
## Nix and NixOS
@@ -79,7 +79,7 @@ sudo apk add ~/packages/*/x86_64/uur-*.apk
Gentoo, Funtoo, Void Linux, Slackware, Solus, Clear Linux, ALT Linux, Exherbo,
Mageia, OpenMandriva, PCLinuxOS, Venom Linux, and other FHS/glibc systems can
install the portable release archive or build from source. Their native recipe
can call `packaging/stage.sh` after the three standard builds; the staged tree
can call `packaging/stage.sh` after the standard builds (`hook`, `capture`, `compat`); the staged tree
is shared with pacman, deb, rpm, APK, and the portable archive.
GNU Guix can wrap the same source build and staged tree. Flatpak and Snap need
@@ -91,6 +91,7 @@ manifests granting Wine execution, session D-Bus, PipeWire, and input access.
cargo build --release --locked
./hook/build.sh
./capture/build.sh
./compat/build.sh
./scripts/install-user.sh
```
+4 -4
View File
@@ -6,14 +6,14 @@
| Receive remote video | available | Official UU UI under Wine |
| Share X11 desktop | available | Wine X11 capture |
| Share Wayland desktop | available | ScreenCast/PipeWire or explicitly enabled native screencopy → GDI adapter |
| Mouse and keyboard | implemented | RemoteDesktop portal → uinput → XTest capability order |
| Mouse and keyboard | implemented | RemoteDesktop portal → uinput → XTest capability order; process-local NumLock/ScrollLock workaround for ordinary controller keys; keypad/case behavior remains native |
| X11 multi-monitor | Wine integration | Native Wine/XRandR display topology |
| Wayland monitor/window selection | available | Persistent portal source selection |
| Wayland virtual source | available when advertised | `uur display source virtual` → ScreenCast Virtual source |
| Independent Wayland multi-screen switching | in progress | Multiple PipeWire streams → UU display topology adapter |
| Wayland multi-screen capture | available | Multiple PipeWire streams → virtual desktop composite → per-monitor crops |
| Clipboard and phone IME text | planned | RemoteDesktop Clipboard portal, then X11 fallback |
| Speaker and microphone audio | available through Wine | PulseAudio, pipewire-pulse, or ALSA selected from live capabilities |
| System-audio loopback and remote mute | Wine integration | WASAPI → winepulse/winealsa |
| Speaker and microphone audio | Wine fallback capture | PulseAudio, pipewire-pulse, or ALSA; proprietary dual-channel audio driver unavailable |
| System-audio loopback | explicit default-input override | `uur audio source system` selects a real PulseAudio monitor for the fallback stream; remote mute needs separate validation |
| Native tray | implemented | StatusNotifierItem + DBusMenu, supervised with the session; no Wine XEmbed dependency |
| Terminal | available | ConPTY/mux ABI adapters → authenticated native Rust PTY/login shell |
| Quick Launch | implemented | Windows app inventory adapter → XDG desktop catalog → `gio launch` |
+58 -8
View File
@@ -46,7 +46,7 @@ paru -S uur
From a downloaded Arch package:
```bash
sudo pacman -U ./uur-0.1.6-1-x86_64.pkg.tar.zst
sudo pacman -U ./uur-0.1.7-1-x86_64.pkg.tar.zst
```
Install the portal backend supplied or recommended by the selected desktop.
@@ -60,7 +60,7 @@ Zorin OS, elementary OS, KDE neon, TUXEDO OS, Kali Linux, MX Linux, and other
compatible derivatives:
```bash
sudo apt install ./uur_0.1.6_amd64.deb
sudo apt install ./uur_0.1.7_amd64.deb
```
The package manager resolves Wine, PipeWire, and the portal frontend. Install
@@ -74,10 +74,10 @@ dependency names. Install it with the native frontend:
```bash
# Fedora and compatible systems
sudo dnf install ./uur-0.1.6-1.x86_64.rpm
sudo dnf install ./uur-0.1.7-1.x86_64.rpm
# openSUSE family
sudo zypper install ./uur-0.1.6-1.x86_64.rpm
sudo zypper install ./uur-0.1.7-1.x86_64.rpm
```
RHEL, CentOS Stream, Rocky Linux, AlmaLinux, Oracle Linux, Nobara, Ultramarine,
@@ -91,7 +91,7 @@ PCLinuxOS, and similar mutable/FHS systems can use the portable archive when
they provide compatible glibc, Wine, PipeWire, and portal libraries:
```bash
sudo tar --zstd -C / -xf ./uur-0.1.6-linux-x86_64.tar.zst
sudo tar --zstd -C / -xf ./uur-0.1.7-linux-x86_64.tar.zst
```
Alternatively, build and install for one user:
@@ -100,6 +100,7 @@ Alternatively, build and install for one user:
cargo build --release --locked
./hook/build.sh
./capture/build.sh
./compat/build.sh
./scripts/install-user.sh
```
@@ -132,6 +133,31 @@ Openbox, Fluxbox, IceWM, dwm, qtile, herbstluftwm, spectrwm, and FVWM.
Wine captures the X11 desktop directly and uur injects input with XTest. A
ScreenCast portal is not required for this path.
#### Lock modifier bits in the client's key events
Wine can inject a NumLock correction pair on each ordinary key when the
official client's low-level hook blocks lock-key synchronization. This affects
controller-side typing before uur's remote-input bridge receives any records.
The process-local `libuur-lock-mods.so` workaround discovers exclusive NumLock
and ScrollLock modifiers from each X11 display's actual keymap. With
`strip_lock_mods = "auto"` (the default), it filters those bits on ordinary key
events. Numeric masks further restrict the discovered set; `"0"` disables it.
It preserves Super, Ctrl, Alt, AltGr, Shift and CapsLock rather than assuming
that Mod2 and Mod4 are both locks, and refreshes its mapping after layout
changes. No X server, compositor or user lock setting is changed.
Keypad events keep their original state so NumLock-on digits do not become
navigation keys. CapsLock also keeps its case semantics. These events retain
Wine's native reconciliation behavior and can still trigger lock-key
corrections. Use `WINEDEBUG=+key,+keyboard uur run` and the
private session log to distinguish real key conversions from correction
pairs. `uur doctor` reports the local lock state and configured workaround.
After acquiring the session lock, a shim-enabled run restarts only the managed
Wine prefix so its service manager and service-spawned clients receive the
same compatibility environment. Other Wine prefixes remain running.
### Wayland session with ScreenCast and RemoteDesktop portals
Requirements: a working session D-Bus, PipeWire, the xdg-desktop-portal
@@ -179,19 +205,43 @@ the `pipewire-pulse` compatibility server. A native PipeWire session without
PulseAudio compatibility uses Wine's ALSA driver. `uur doctor` prints the live
choice; this is independent from PipeWire's Wayland video role.
UU's `nevaudio.sys` driver is unavailable under Wine. Its fallback can label
the default recording input as system sound, even when that input is a physical
microphone. To choose system sound explicitly with PulseAudio or pipewire-pulse:
```bash
uur audio sources
uur audio source system
uur stop
uur run
```
`system` resolves the default output's real monitor source before startup;
an unavailable selection stops startup with a diagnostic. The override applies
only to Wine's default capture endpoint in this managed prefix. An explicitly
selected device in UU keeps its own routing. Host audio defaults and other apps
are unchanged. `uur audio source SOURCE_NAME` selects another listed input;
`uur audio source default` follows the existing audio environment again.
The selected output is resolved again on the next session, so restart after
changing audio hardware. This provides one fallback capture stream; simultaneous
independent speaker and microphone channels still need the official driver.
### Displays and virtual sources
```bash
uur display status
uur display source monitor
uur display source monitor --multiple
uur display source window
uur display source virtual
```
The command clears the old restore token so the next Portal session can create
or select the requested source. X11 exposes its XRandR topology to Wine.
Wayland currently carries one selected stream; independent multi-stream
switching is the next display-topology adapter.
or select the requested source. With `monitor --multiple`, the chooser can select
multiple displays; uur composes those PipeWire streams into the virtual desktop
frame and crops each Wine monitor request from it. X11 exposes its XRandR
topology to Wine directly. Upgrades preserve existing restore tokens; multiple
monitor selection is an explicit choice and applies to the next session.
## First run and normal use
+4 -3
View File
@@ -3,11 +3,12 @@
## Frame sources
- X11 sessions expose the XRandR monitor topology directly to Wine.
- Wayland monitor and window sources use ScreenCast/PipeWire.
- Wayland monitor and window sources use ScreenCast/PipeWire. Monitor selection
supports multiple outputs with `uur display source monitor --multiple` and
composes their streams into the virtual desktop
frame consumed by the Wine capture adapter.
- `uur display source virtual` requests the portal's Virtual source type and
stores its restore token.
- A multi-stream compositor will combine or expose several PipeWire nodes to
UU's display topology for independent Wayland screen switching.
## Codec capabilities
+22 -2
View File
@@ -5,9 +5,29 @@
#include <stdint.h>
#define UURF_MAGIC 0x46525555u
#define UURF_VERSION 1u
#define UURF_VERSION 3u
#define UURF_SLOT_COUNT 3u
#define UURF_HEADER_BYTES 64u
#define UURF_BASE_HEADER_BYTES 64u
#define UURF_MAX_MONITORS 16u
#define UURF_MONITOR_BYTES 40u
#define UURF_HEADER_BYTES \
(UURF_BASE_HEADER_BYTES + UURF_MAX_MONITORS * UURF_MONITOR_BYTES)
#define UURF_FLAG_COMPOSITE 1u
struct uurf_monitor_layout {
int32_t logical_x;
int32_t logical_y;
uint32_t logical_width;
uint32_t logical_height;
uint32_t frame_x;
uint32_t frame_y;
uint32_t frame_width;
uint32_t frame_height;
uint32_t capture_width;
uint32_t capture_height;
};
_Static_assert(sizeof(struct uurf_monitor_layout) == UURF_MONITOR_BYTES,
"monitor layout ABI changed");
static inline size_t uurf_slot_offset(uint64_t sequence, size_t slot_bytes)
{
+5
View File
@@ -75,6 +75,11 @@ static int capture_round_trip(void)
put_u32(transport + 16, stride);
put_u32(transport + 20, 1);
put_u64(transport + 24, 1);
put_u32(transport + 36, 1);
put_u32(transport + UURF_BASE_HEADER_BYTES + 8, width);
put_u32(transport + UURF_BASE_HEADER_BYTES + 12, height);
put_u32(transport + UURF_BASE_HEADER_BYTES + 24, width);
put_u32(transport + UURF_BASE_HEADER_BYTES + 28, height);
unsigned char *slot = transport + uurf_slot_offset(1, slot_bytes);
put_u64(slot, 2);
for (size_t offset = 8; offset < slot_bytes; offset += 4) {
+857 -17
View File
@@ -27,6 +27,7 @@
#include <tlhelp32.h>
#include <dxgi.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
@@ -42,7 +43,35 @@
static UINT(WINAPI *original_send_input)(UINT, LPINPUT, int);
static HRESULT(WINAPI *original_create_dxgi_factory1)(REFIID, void **);
static FARPROC(WINAPI *original_get_proc_address)(HMODULE, LPCSTR);
static HMODULE hook_module;
static int(WINAPI *original_get_system_metrics)(int);
static BOOL(WINAPI *original_get_monitor_info_a)(HMONITOR, LPMONITORINFO);
static BOOL(WINAPI *original_get_monitor_info_w)(HMONITOR, LPMONITORINFO);
static BOOL(WINAPI *original_enum_display_settings_a)(LPCSTR, DWORD,
LPDEVMODEA);
static BOOL(WINAPI *original_enum_display_settings_w)(LPCWSTR, DWORD,
LPDEVMODEW);
static BOOL(WINAPI *original_enum_display_settings_ex_w)(LPCWSTR, DWORD,
LPDEVMODEW, DWORD);
static int(WINAPI *original_get_device_caps)(HDC, int);
static LONG(WINAPI *original_query_display_config)(UINT, UINT *,
DISPLAYCONFIG_PATH_INFO *,
UINT *,
DISPLAYCONFIG_MODE_INFO *,
DISPLAYCONFIG_TOPOLOGY_ID *);
UINT WINAPI hook_send_input(UINT count, LPINPUT inputs, int size);
static int WINAPI hook_get_system_metrics(int);
static BOOL WINAPI hook_get_monitor_info_a(HMONITOR, LPMONITORINFO);
static BOOL WINAPI hook_get_monitor_info_w(HMONITOR, LPMONITORINFO);
static BOOL WINAPI hook_enum_display_settings_a(LPCSTR, DWORD, LPDEVMODEA);
static BOOL WINAPI hook_enum_display_settings_w(LPCWSTR, DWORD, LPDEVMODEW);
static BOOL WINAPI hook_enum_display_settings_ex_w(LPCWSTR, DWORD, LPDEVMODEW,
DWORD);
static int WINAPI hook_get_device_caps(HDC, int);
static LONG WINAPI hook_query_display_config(UINT, UINT *,
DISPLAYCONFIG_PATH_INFO *, UINT *,
DISPLAYCONFIG_MODE_INFO *,
DISPLAYCONFIG_TOPOLOGY_ID *);
static volatile LONG dxgi_factories_patched = 0;
static volatile LONG dxgi_adapters_patched = 0;
@@ -50,6 +79,9 @@ static volatile LONG dxgi_outputs_patched = 0;
static volatile LONG dxgi_duplicate_calls = 0;
static volatile LONG dxgi_dup_acquires = 0;
static volatile LONG dxgi_dup_frames = 0;
static volatile LONG frame_crop_debug_count = 0;
static volatile LONG offorigin_crop_debug_count = 0;
static void hook_log(const char *text);
static HRESULT __attribute__((unused))(STDMETHODCALLTYPE *original_dxgi_acquire_next_frame)(
void *duplication, UINT timeout, void *frame_info, void **resource) = NULL;
@@ -234,6 +266,8 @@ static HANDLE frame_map_handle = NULL;
static uint32_t frame_width;
static uint32_t frame_height;
static uint32_t frame_stride;
static uint32_t frame_flags;
static uint32_t frame_monitor_count;
static uint64_t frame_slot_bytes;
static unsigned char *frame_snapshot;
static size_t frame_snapshot_capacity;
@@ -241,6 +275,7 @@ static size_t frame_map_size;
static BY_HANDLE_FILE_INFORMATION frame_identity;
static ULONGLONG frame_checked_at;
static SRWLOCK frame_lock = SRWLOCK_INIT;
static int capture_geometry_enabled;
static char *read_ini_value(const char *path, const char *key);
@@ -280,6 +315,8 @@ static void clear_frame_map(void)
frame_map = NULL;
frame_map_handle = NULL;
frame_map_size = 0;
frame_flags = 0;
frame_monitor_count = 0;
}
/* Producers publish immutable geometry via atomic replacement. Re-check the
@@ -347,10 +384,13 @@ static int ensure_frame_map(void)
frame_width = load_u32(header + 8);
frame_height = load_u32(header + 12);
frame_stride = load_u32(header + 16);
frame_flags = load_u32(header + 32);
frame_monitor_count = load_u32(header + 36);
frame_slot_bytes = 8ull + (uint64_t)frame_stride * frame_height;
uint64_t expected = UURF_HEADER_BYTES + UURF_SLOT_COUNT * frame_slot_bytes;
if (frame_width == 0 || frame_height == 0 ||
frame_width > 16384 || frame_height > 16384 ||
frame_monitor_count == 0 || frame_monitor_count > UURF_MAX_MONITORS ||
frame_stride != frame_width * 4 ||
(uint64_t)frame_stride * frame_height > 256ull * 1024 * 1024 ||
expected > (uint64_t)size.QuadPart) {
@@ -359,12 +399,515 @@ static int ensure_frame_map(void)
frame_map_handle = NULL;
return 0;
}
for (uint32_t i = 0; i < frame_monitor_count; ++i) {
const unsigned char *item = header + UURF_BASE_HEADER_BYTES + i * UURF_MONITOR_BYTES;
uint32_t x = load_u32(item + 16), y = load_u32(item + 20);
uint32_t w = load_u32(item + 24), h = load_u32(item + 28);
if (load_u32(item + 8) == 0 || load_u32(item + 12) == 0 ||
w == 0 || h == 0 || x >= frame_width || y >= frame_height ||
w > frame_width - x || h > frame_height - y) {
UnmapViewOfFile(view);
CloseHandle(frame_map_handle);
frame_map_handle = NULL;
return 0;
}
}
frame_map_size = (size_t)size.QuadPart;
frame_map = view;
frame_identity = identity;
return 1;
}
static int64_t abs_i64(int64_t value)
{
return value < 0 ? -value : value;
}
/* Compare relative extents, centers and aspect ratio, so a uniform XWayland
* scale cannot make a larger adjacent monitor look like the smaller one. */
static const unsigned char *find_frame_monitor(const unsigned char *header,
const RECT *target,
int screen_left,
int screen_top,
int screen_width,
int screen_height)
{
const unsigned char *best = NULL;
int64_t min_x = INT64_MAX, min_y = INT64_MAX;
int64_t max_x = INT64_MIN, max_y = INT64_MIN;
int64_t target_width = (int64_t)target->right - target->left;
int64_t target_height = (int64_t)target->bottom - target->top;
int64_t target_center_x = (int64_t)target->left + target->right;
int64_t target_center_y = (int64_t)target->top + target->bottom;
int64_t best_score = INT64_MAX;
if (screen_width <= 0 || screen_height <= 0 || target_width <= 0 ||
target_height <= 0 || frame_monitor_count == 0 ||
frame_monitor_count != (uint32_t)GetSystemMetrics(SM_CMONITORS))
return NULL;
for (uint32_t i = 0; i < frame_monitor_count; ++i) {
const unsigned char *item = header + UURF_BASE_HEADER_BYTES +
i * UURF_MONITOR_BYTES;
int64_t x = (int32_t)load_u32(item);
int64_t y = (int32_t)load_u32(item + 4);
int64_t width = load_u32(item + 8);
int64_t height = load_u32(item + 12);
if (x < min_x)
min_x = x;
if (y < min_y)
min_y = y;
if (x + width > max_x)
max_x = x + width;
if (y + height > max_y)
max_y = y + height;
}
if (max_x <= min_x || max_y <= min_y)
return NULL;
int64_t target_x = ((target_center_x - 2ll * screen_left) * 1000000) /
(2ll * screen_width);
int64_t target_y = ((target_center_y - 2ll * screen_top) * 1000000) /
(2ll * screen_height);
for (uint32_t i = 0; i < frame_monitor_count; ++i) {
const unsigned char *item = header + UURF_BASE_HEADER_BYTES +
i * UURF_MONITOR_BYTES;
int64_t x = (int32_t)load_u32(item);
int64_t y = (int32_t)load_u32(item + 4);
int64_t width = load_u32(item + 8);
int64_t height = load_u32(item + 12);
int64_t center_x = ((2 * x + width - 2 * min_x) * 1000000) /
(2 * (max_x - min_x));
int64_t center_y = ((2 * y + height - 2 * min_y) * 1000000) /
(2 * (max_y - min_y));
int64_t size_error =
abs_i64(target_width * 1000000 / screen_width -
width * 1000000 / (max_x - min_x)) +
abs_i64(target_height * 1000000 / screen_height -
height * 1000000 / (max_y - min_y));
int64_t aspect_left = target_width * height;
int64_t aspect_right = target_height * width;
int64_t aspect_error =
abs_i64(aspect_left - aspect_right) * 1000000 /
(aspect_left > aspect_right ? aspect_left : aspect_right);
int64_t position_error = abs_i64(target_x - center_x) +
abs_i64(target_y - center_y);
int64_t score = size_error * 2 + position_error * 4 + aspect_error;
if (score < best_score) {
best_score = score;
best = item;
}
}
return best;
}
typedef struct uur_frame_monitor {
int32_t logical_x;
int32_t logical_y;
uint32_t logical_width;
uint32_t logical_height;
uint32_t frame_x;
uint32_t frame_y;
uint32_t frame_width;
uint32_t frame_height;
uint32_t capture_width;
uint32_t capture_height;
} uur_frame_monitor;
/* Copy monitor metadata while holding the same lock used to remap the shared
* frame. The primary monitor remains at (0,0); other monitors use their
* captured stream's real pixel dimensions rather than XWayland's scaled
* monitor rectangles. */
static int copy_frame_monitors(const RECT *target, uur_frame_monitor *items,
uint32_t *count, uint32_t *primary_index,
uint32_t *target_index)
{
int result = 0;
if (!capture_geometry_enabled)
return 0;
AcquireSRWLockExclusive(&frame_lock);
if (!ensure_frame_map())
goto done;
if ((frame_flags & UURF_FLAG_COMPOSITE) == 0)
goto done;
const unsigned char *header = (const unsigned char *)frame_map;
int screen_left = GetSystemMetrics(SM_XVIRTUALSCREEN);
int screen_top = GetSystemMetrics(SM_YVIRTUALSCREEN);
int screen_width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
int screen_height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
if (screen_width < 1)
screen_width = (int)frame_width;
if (screen_height < 1)
screen_height = (int)frame_height;
POINT origin = {0, 0};
HMONITOR primary = MonitorFromPoint(origin, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO primary_info;
ZeroMemory(&primary_info, sizeof(primary_info));
primary_info.cbSize = sizeof(primary_info);
if (primary == NULL || !GetMonitorInfoA(primary, &primary_info))
goto done;
const unsigned char *primary_layout = find_frame_monitor(
header, &primary_info.rcMonitor, screen_left, screen_top,
screen_width, screen_height);
if (primary_layout == NULL)
goto done;
const unsigned char *selected_layout = target != NULL
? find_frame_monitor(header, target, screen_left, screen_top,
screen_width, screen_height)
: NULL;
uint32_t total = frame_monitor_count;
if (total == 0 || total > UURF_MAX_MONITORS)
goto done;
uint32_t primary_i =
(uint32_t)(primary_layout - (header + UURF_BASE_HEADER_BYTES)) /
UURF_MONITOR_BYTES;
uint32_t target_i = selected_layout != NULL
? (uint32_t)(selected_layout - (header + UURF_BASE_HEADER_BYTES)) /
UURF_MONITOR_BYTES
: UINT32_MAX;
for (uint32_t i = 0; i < total; ++i) {
const unsigned char *item = header + UURF_BASE_HEADER_BYTES +
i * UURF_MONITOR_BYTES;
items[i].logical_x = (int32_t)load_u32(item);
items[i].logical_y = (int32_t)load_u32(item + 4);
items[i].logical_width = load_u32(item + 8);
items[i].logical_height = load_u32(item + 12);
items[i].frame_x = load_u32(item + 16);
items[i].frame_y = load_u32(item + 20);
items[i].frame_width = load_u32(item + 24);
items[i].frame_height = load_u32(item + 28);
items[i].capture_width = load_u32(item + 32);
items[i].capture_height = load_u32(item + 36);
if (items[i].capture_width == 0 || items[i].capture_height == 0) {
items[i].capture_width = items[i].frame_width;
items[i].capture_height = items[i].frame_height;
}
}
if (primary_i >= total ||
(target_i != UINT32_MAX && target_i >= total))
goto done;
*count = total;
*primary_index = primary_i;
if (target_index != NULL)
*target_index = target_i;
result = 1;
done:
ReleaseSRWLockExclusive(&frame_lock);
return result;
}
static int frame_monitor_rect(const uur_frame_monitor *items,
uint32_t primary_index, uint32_t index,
RECT *rect)
{
if (items == NULL || rect == NULL || index >= UURF_MAX_MONITORS ||
primary_index >= UURF_MAX_MONITORS ||
items[index].frame_width == 0 || items[index].frame_height == 0)
return 0;
int64_t left = (int64_t)items[index].frame_x - items[primary_index].frame_x;
int64_t top = (int64_t)items[index].frame_y - items[primary_index].frame_y;
int64_t right = left + items[index].frame_width;
int64_t bottom = top + items[index].frame_height;
if (left < LONG_MIN || top < LONG_MIN || right > LONG_MAX ||
bottom > LONG_MAX)
return 0;
rect->left = (LONG)left;
rect->top = (LONG)top;
rect->right = (LONG)right;
rect->bottom = (LONG)bottom;
return 1;
}
static LONG scale_monitor_offset(LONG value, LONG old_size, LONG new_size)
{
if (old_size <= 0)
return 0;
return (LONG)((int64_t)value * new_size / old_size);
}
static void apply_monitor_layout(LPMONITORINFO info,
const uur_frame_monitor *items,
uint32_t primary_index, uint32_t index)
{
RECT adjusted;
if (info == NULL ||
!frame_monitor_rect(items, primary_index, index, &adjusted))
return;
RECT old_monitor = info->rcMonitor;
RECT old_work = info->rcWork;
LONG old_width = old_monitor.right - old_monitor.left;
LONG old_height = old_monitor.bottom - old_monitor.top;
LONG new_width = adjusted.right - adjusted.left;
LONG new_height = adjusted.bottom - adjusted.top;
info->rcMonitor = adjusted;
info->rcWork.left = adjusted.left +
scale_monitor_offset(old_work.left - old_monitor.left, old_width,
new_width);
info->rcWork.right = adjusted.left +
scale_monitor_offset(old_work.right - old_monitor.left, old_width,
new_width);
info->rcWork.top = adjusted.top +
scale_monitor_offset(old_work.top - old_monitor.top, old_height,
new_height);
info->rcWork.bottom = adjusted.top +
scale_monitor_offset(old_work.bottom - old_monitor.top, old_height,
new_height);
}
static int copy_layout_for_monitor_rect(const RECT *target,
uur_frame_monitor *items,
uint32_t *count,
uint32_t *primary_index,
uint32_t *target_index)
{
return copy_frame_monitors(target, items, count, primary_index,
target_index);
}
static void remap_devmode(DEVMODEW *mode)
{
if (mode == NULL || (mode->dmFields & (DM_POSITION | DM_PELSWIDTH |
DM_PELSHEIGHT)) !=
(DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT) ||
mode->dmPelsWidth == 0 || mode->dmPelsHeight == 0)
return;
RECT old_rect = {mode->dmPosition.x, mode->dmPosition.y,
mode->dmPosition.x + (LONG)mode->dmPelsWidth,
mode->dmPosition.y + (LONG)mode->dmPelsHeight};
uur_frame_monitor items[UURF_MAX_MONITORS];
uint32_t count = 0, primary_index = 0, target_index = UINT32_MAX;
if (!copy_layout_for_monitor_rect(&old_rect, items, &count,
&primary_index, &target_index) ||
target_index >= count)
return;
RECT adjusted;
if (!frame_monitor_rect(items, primary_index, target_index, &adjusted))
return;
mode->dmPosition.x = adjusted.left;
mode->dmPosition.y = adjusted.top;
mode->dmPelsWidth = items[target_index].frame_width;
mode->dmPelsHeight = items[target_index].frame_height;
mode->dmFields |= DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT;
}
static int WINAPI hook_get_system_metrics(int index)
{
int fallback = original_get_system_metrics != NULL
? original_get_system_metrics(index)
: GetSystemMetrics(index);
if (index != SM_CXSCREEN && index != SM_CYSCREEN &&
index != SM_XVIRTUALSCREEN && index != SM_YVIRTUALSCREEN &&
index != SM_CXVIRTUALSCREEN && index != SM_CYVIRTUALSCREEN)
return fallback;
uur_frame_monitor items[UURF_MAX_MONITORS];
uint32_t count = 0, primary_index = 0;
if (!copy_frame_monitors(NULL, items, &count, &primary_index, NULL))
return fallback;
if (index == SM_CXSCREEN)
return (int)items[primary_index].frame_width;
if (index == SM_CYSCREEN)
return (int)items[primary_index].frame_height;
int64_t min_x = INT64_MAX, min_y = INT64_MAX;
int64_t max_x = INT64_MIN, max_y = INT64_MIN;
for (uint32_t i = 0; i < count; ++i) {
RECT rect;
if (!frame_monitor_rect(items, primary_index, i, &rect))
continue;
if (rect.left < min_x)
min_x = rect.left;
if (rect.top < min_y)
min_y = rect.top;
if (rect.right > max_x)
max_x = rect.right;
if (rect.bottom > max_y)
max_y = rect.bottom;
}
if (min_x == INT64_MAX || min_y == INT64_MAX || max_x <= min_x ||
max_y <= min_y || max_x - min_x > INT_MAX ||
max_y - min_y > INT_MAX)
return fallback;
switch (index) {
case SM_XVIRTUALSCREEN:
return (int)min_x;
case SM_YVIRTUALSCREEN:
return (int)min_y;
case SM_CXVIRTUALSCREEN:
return (int)(max_x - min_x);
case SM_CYVIRTUALSCREEN:
return (int)(max_y - min_y);
default:
return fallback;
}
}
static BOOL WINAPI hook_get_monitor_info_a(HMONITOR monitor,
LPMONITORINFO info)
{
BOOL result = original_get_monitor_info_a != NULL
? original_get_monitor_info_a(monitor, info)
: GetMonitorInfoA(monitor, info);
if (!result || info == NULL || info->cbSize < sizeof(MONITORINFO))
return result;
uur_frame_monitor items[UURF_MAX_MONITORS];
uint32_t count = 0, primary_index = 0, target_index = UINT32_MAX;
if (copy_layout_for_monitor_rect(&info->rcMonitor, items, &count,
&primary_index, &target_index) &&
target_index < count)
apply_monitor_layout(info, items, primary_index, target_index);
return result;
}
static BOOL WINAPI hook_get_monitor_info_w(HMONITOR monitor,
LPMONITORINFO info)
{
BOOL result = original_get_monitor_info_w != NULL
? original_get_monitor_info_w(monitor, info)
: GetMonitorInfoW(monitor, info);
if (!result || info == NULL || info->cbSize < sizeof(MONITORINFO))
return result;
uur_frame_monitor items[UURF_MAX_MONITORS];
uint32_t count = 0, primary_index = 0, target_index = UINT32_MAX;
if (copy_layout_for_monitor_rect(&info->rcMonitor, items, &count,
&primary_index, &target_index) &&
target_index < count)
apply_monitor_layout(info, items, primary_index, target_index);
return result;
}
static BOOL WINAPI hook_enum_display_settings_w(LPCWSTR device, DWORD mode_num,
LPDEVMODEW mode)
{
BOOL result = original_enum_display_settings_w != NULL
? original_enum_display_settings_w(device, mode_num, mode)
: FALSE;
if (result && mode_num == ENUM_CURRENT_SETTINGS)
remap_devmode(mode);
return result;
}
static BOOL WINAPI hook_enum_display_settings_ex_w(LPCWSTR device,
DWORD mode_num,
LPDEVMODEW mode,
DWORD flags)
{
BOOL result = original_enum_display_settings_ex_w != NULL
? original_enum_display_settings_ex_w(device, mode_num,
mode, flags)
: FALSE;
if (result && mode_num == ENUM_CURRENT_SETTINGS)
remap_devmode(mode);
return result;
}
static BOOL WINAPI hook_enum_display_settings_a(LPCSTR device, DWORD mode_num,
LPDEVMODEA mode)
{
BOOL result = original_enum_display_settings_a != NULL
? original_enum_display_settings_a(device, mode_num,
mode)
: FALSE;
if (!result || mode_num != ENUM_CURRENT_SETTINGS || mode == NULL ||
(mode->dmFields & (DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT)) !=
(DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT) ||
mode->dmPelsWidth == 0 || mode->dmPelsHeight == 0)
return result;
RECT old_rect = {mode->dmPosition.x, mode->dmPosition.y,
mode->dmPosition.x + (LONG)mode->dmPelsWidth,
mode->dmPosition.y + (LONG)mode->dmPelsHeight};
uur_frame_monitor items[UURF_MAX_MONITORS];
uint32_t count = 0, primary_index = 0, target_index = UINT32_MAX;
if (!copy_layout_for_monitor_rect(&old_rect, items, &count,
&primary_index, &target_index) ||
target_index >= count)
return result;
RECT adjusted;
if (!frame_monitor_rect(items, primary_index, target_index, &adjusted))
return result;
mode->dmPosition.x = adjusted.left;
mode->dmPosition.y = adjusted.top;
mode->dmPelsWidth = items[target_index].frame_width;
mode->dmPelsHeight = items[target_index].frame_height;
mode->dmFields |= DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT;
return result;
}
static int WINAPI hook_get_device_caps(HDC dc, int index)
{
int result = original_get_device_caps != NULL
? original_get_device_caps(dc, index)
: GetDeviceCaps(dc, index);
if ((index != HORZRES && index != VERTRES) || dc == NULL ||
original_get_device_caps == NULL ||
original_get_device_caps(dc, TECHNOLOGY) != DT_RASDISPLAY)
return result;
POINT origin;
if (!GetDCOrgEx(dc, &origin))
return result;
int width = original_get_device_caps(dc, HORZRES);
int height = original_get_device_caps(dc, VERTRES);
if (width <= 0 || height <= 0)
return result;
RECT old_rect = {origin.x, origin.y, origin.x + width, origin.y + height};
uur_frame_monitor items[UURF_MAX_MONITORS];
uint32_t count = 0, primary_index = 0, target_index = UINT32_MAX;
if (!copy_layout_for_monitor_rect(&old_rect, items, &count,
&primary_index, &target_index) ||
target_index >= count)
return result;
return index == HORZRES ? (int)items[target_index].frame_width
: (int)items[target_index].frame_height;
}
static LONG WINAPI hook_query_display_config(
UINT flags, UINT *path_count, DISPLAYCONFIG_PATH_INFO *paths,
UINT *mode_count, DISPLAYCONFIG_MODE_INFO *modes,
DISPLAYCONFIG_TOPOLOGY_ID *topology)
{
LONG result = original_query_display_config != NULL
? original_query_display_config(flags, path_count, paths,
mode_count, modes,
topology)
: ERROR_NOT_SUPPORTED;
if (result != ERROR_SUCCESS || path_count == NULL || mode_count == NULL ||
paths == NULL || modes == NULL)
return result;
for (UINT i = 0; i < *path_count; ++i) {
UINT source_index = paths[i].sourceInfo.modeInfoIdx;
if (source_index >= *mode_count ||
modes[source_index].infoType != DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE)
continue;
DISPLAYCONFIG_SOURCE_MODE *source = &modes[source_index].sourceMode;
RECT old_rect = {source->position.x, source->position.y,
source->position.x + (LONG)source->width,
source->position.y + (LONG)source->height};
uur_frame_monitor items[UURF_MAX_MONITORS];
uint32_t count = 0, primary_index = 0, target_index = UINT32_MAX;
if (!copy_layout_for_monitor_rect(&old_rect, items, &count,
&primary_index, &target_index) ||
target_index >= count)
continue;
RECT adjusted;
if (!frame_monitor_rect(items, primary_index, target_index, &adjusted))
continue;
source->position.x = adjusted.left;
source->position.y = adjusted.top;
source->width = items[target_index].frame_width;
source->height = items[target_index].frame_height;
/* Hardware target timings retain their actual clock and scanout. */
}
return result;
}
static BOOL source_is_screen_dc(HDC source)
{
return GetObjectType(source) == OBJ_DC &&
@@ -492,13 +1035,10 @@ static BOOL feed_capture_frame_locked(HDC hdc_dest, int x, int y, int width,
screen_width = (int)fw;
if (screen_height < 1)
screen_height = (int)fh;
/* The frame holds exactly one monitor, but the client may ask
* for that monitor's whole rectangle. Wine reports the
* compositor's monitors in its own order, so the virtual desktop
* is only the right basis for a partial request; a complete
* monitor request must map onto the whole frame. Fractional
* scaling can give Xwayland and the portal different pixel
* sizes, so compare aspect ratios rather than resolutions. */
/* A single-stream frame represents one selected monitor, so a
* complete monitor request maps to the whole frame. Composite
* frames already follow the virtual desktop layout and must be
* cropped by the request's screen coordinates instead. */
int sx = 0;
int sy = 0;
int sw = 0;
@@ -512,9 +1052,13 @@ static BOOL feed_capture_frame_locked(HDC hdc_dest, int x, int y, int width,
? MonitorFromRect(&requested, MONITOR_DEFAULTTONULL)
: NULL;
MONITORINFO info;
ZeroMemory(&info, sizeof(info));
info.cbSize = sizeof(info);
BOOL whole_monitor = FALSE;
if (monitor != NULL && GetMonitorInfoA(monitor, &info)) {
BOOL have_monitor_info =
monitor != NULL && GetMonitorInfoA(monitor, &info);
if ((frame_flags & UURF_FLAG_COMPOSITE) == 0 &&
have_monitor_info) {
whole_monitor =
info.rcMonitor.left == requested.left &&
info.rcMonitor.top == requested.top &&
@@ -523,23 +1067,241 @@ static BOOL feed_capture_frame_locked(HDC hdc_dest, int x, int y, int width,
(int64_t)(info.rcMonitor.right - info.rcMonitor.left) * fh ==
(int64_t)(info.rcMonitor.bottom - info.rcMonitor.top) * fw;
}
if (whole_monitor) {
BOOL monitor_local_crop = FALSE;
const unsigned char *selected_layout = NULL;
const char *crop_mode = "screen";
if ((frame_flags & UURF_FLAG_COMPOSITE) != 0 &&
have_monitor_info && source_width > 0 && source_height > 0) {
int screen_left = GetSystemMetrics(SM_XVIRTUALSCREEN);
int screen_top = GetSystemMetrics(SM_YVIRTUALSCREEN);
RECT crop_monitor = info.rcMonitor;
const unsigned char *layout = NULL;
/* Projected rectangles must be checked first: a projected
* secondary monitor may lie inside Wine's old primary bounds. */
if (capture_geometry_enabled) {
POINT origin = {0, 0};
HMONITOR primary = MonitorFromPoint(origin, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO primary_info = {0};
primary_info.cbSize = sizeof(primary_info);
const unsigned char *primary_layout = NULL;
if (primary != NULL && GetMonitorInfoA(primary, &primary_info))
primary_layout = find_frame_monitor(header, &primary_info.rcMonitor,
screen_left, screen_top, screen_width, screen_height);
for (uint32_t i = 0; primary_layout != NULL && i < frame_monitor_count; ++i) {
const unsigned char *item = header + UURF_BASE_HEADER_BYTES + i * UURF_MONITOR_BYTES;
LONG left = (LONG)load_u32(item + 16) - (LONG)load_u32(primary_layout + 16);
LONG top = (LONG)load_u32(item + 20) - (LONG)load_u32(primary_layout + 20);
RECT projected = {left, top, left + (LONG)load_u32(item + 24),
top + (LONG)load_u32(item + 28)};
if (requested.left >= projected.left && requested.top >= projected.top &&
requested.right <= projected.right && requested.bottom <= projected.bottom) {
layout = item;
crop_monitor = projected;
crop_mode = "stream-local";
break;
}
}
}
if (layout == NULL)
layout = find_frame_monitor(header, &info.rcMonitor, screen_left, screen_top,
screen_width, screen_height);
if (layout != NULL) {
selected_layout = layout;
uint32_t frame_x = load_u32(layout + 16);
uint32_t frame_y = load_u32(layout + 20);
uint32_t frame_w = load_u32(layout + 24);
uint32_t frame_h = load_u32(layout + 28);
int64_t monitor_width = (int64_t)crop_monitor.right - crop_monitor.left;
int64_t monitor_height = (int64_t)crop_monitor.bottom - crop_monitor.top;
if (!monitor_local_crop &&
requested.left >= crop_monitor.left &&
requested.top >= crop_monitor.top &&
requested.right <= crop_monitor.right &&
requested.bottom <= crop_monitor.bottom &&
monitor_width > 0 && monitor_height > 0) {
int64_t local_x0 =
(int64_t)requested.left - crop_monitor.left;
int64_t local_y0 =
(int64_t)requested.top - crop_monitor.top;
int64_t local_x1 = local_x0 + source_width;
int64_t local_y1 = local_y0 + source_height;
int64_t sx0 =
frame_x + local_x0 * frame_w / monitor_width;
int64_t sy0 =
frame_y + local_y0 * frame_h / monitor_height;
int64_t sx1 = frame_x +
(local_x1 * frame_w + monitor_width - 1) /
monitor_width;
int64_t sy1 = frame_y +
(local_y1 * frame_h + monitor_height - 1) /
monitor_height;
if (frame_w > 0 && frame_h > 0 &&
frame_x <= fw && frame_y <= fh &&
frame_w <= fw - frame_x && frame_h <= fh - frame_y) {
if (sx0 < 0)
sx0 = 0;
if (sy0 < 0)
sy0 = 0;
if (sx1 > fw)
sx1 = fw;
if (sy1 > fh)
sy1 = fh;
if (sx1 > sx0 && sy1 > sy0) {
sx = (int)sx0;
sy = (int)sy0;
sw = (int)(sx1 - sx0);
sh = (int)(sy1 - sy0);
monitor_local_crop = TRUE;
if (crop_mode[0] == '\0' ||
strcmp(crop_mode, "screen") == 0)
crop_mode = "monitor-local";
}
}
}
}
}
if ((frame_flags & UURF_FLAG_COMPOSITE) != 0 &&
!monitor_local_crop) {
crop_mode = "composite";
int screen_left = GetSystemMetrics(SM_XVIRTUALSCREEN);
int screen_top = GetSystemMetrics(SM_YVIRTUALSCREEN);
int64_t sx0 = ((int64_t)source_x - screen_left) * fw / screen_width;
int64_t sy0 = ((int64_t)source_y - screen_top) * fh / screen_height;
/* Keep a nonempty source rectangle nonempty after scaling. */
int64_t sx1 = (((int64_t)source_x + source_width - screen_left) * fw + screen_width - 1) / screen_width;
int64_t sy1 = (((int64_t)source_y + source_height - screen_top) * fh + screen_height - 1) / screen_height;
if (sx0 < 0)
sx0 = 0;
if (sy0 < 0)
sy0 = 0;
if (sx1 > fw)
sx1 = fw;
if (sy1 > fh)
sy1 = fh;
if (sx1 <= sx0 || sy1 <= sy0)
return FALSE;
sx = (int)sx0;
sy = (int)sy0;
sw = (int)(sx1 - sx0);
sh = (int)(sy1 - sy0);
} else if ((frame_flags & UURF_FLAG_COMPOSITE) == 0 &&
whole_monitor) {
crop_mode = "whole-monitor";
sx = 0;
sy = 0;
sw = (int)fw;
sh = (int)fh;
} else {
sx = (int)((int64_t)source_x * fw / screen_width);
sy = (int)((int64_t)source_y * fh / screen_height);
sw = (int)((int64_t)source_width * fw / screen_width);
sh = (int)((int64_t)source_height * fh / screen_height);
} else if (!monitor_local_crop) {
int screen_left = GetSystemMetrics(SM_XVIRTUALSCREEN);
int screen_top = GetSystemMetrics(SM_YVIRTUALSCREEN);
int64_t sx0 = ((int64_t)source_x - screen_left) * fw /
screen_width;
int64_t sy0 = ((int64_t)source_y - screen_top) * fh /
screen_height;
int64_t sx1 = ((int64_t)source_x + source_width - screen_left) *
fw + screen_width - 1;
int64_t sy1 = ((int64_t)source_y + source_height - screen_top) *
fh + screen_height - 1;
sx1 /= screen_width;
sy1 /= screen_height;
if (sx0 < 0)
sx0 = 0;
if (sy0 < 0)
sy0 = 0;
if (sx1 > fw)
sx1 = fw;
if (sy1 > fh)
sy1 = fh;
if (sx1 <= sx0 || sy1 <= sy0)
return FALSE;
sx = (int)sx0;
sy = (int)sy0;
sw = (int)(sx1 - sx0);
sh = (int)(sy1 - sy0);
}
if (sw < 1)
sw = (int)fw;
if (sh < 1)
sh = (int)fh;
LONG debug_index = InterlockedIncrement(&frame_crop_debug_count);
LONG offorigin_index = 0;
if (have_monitor_info &&
(info.rcMonitor.left != 0 || info.rcMonitor.top != 0))
offorigin_index =
InterlockedIncrement(&offorigin_crop_debug_count);
if (debug_index <= 4 ||
(offorigin_index > 0 && offorigin_index <= 8)) {
char debug[768];
LONG monitor_left = have_monitor_info ? info.rcMonitor.left : 0;
LONG monitor_top = have_monitor_info ? info.rcMonitor.top : 0;
LONG monitor_right = have_monitor_info ? info.rcMonitor.right : 0;
LONG monitor_bottom = have_monitor_info ? info.rcMonitor.bottom : 0;
LONG layout_x = selected_layout != NULL
? (LONG)(int32_t)load_u32(selected_layout)
: 0;
LONG layout_y = selected_layout != NULL
? (LONG)(int32_t)load_u32(selected_layout + 4)
: 0;
uint32_t layout_w = selected_layout != NULL
? load_u32(selected_layout + 8)
: 0;
uint32_t layout_h = selected_layout != NULL
? load_u32(selected_layout + 12)
: 0;
uint32_t layout_fx = selected_layout != NULL
? load_u32(selected_layout + 16)
: 0;
uint32_t layout_fy = selected_layout != NULL
? load_u32(selected_layout + 20)
: 0;
uint32_t layout_fw = selected_layout != NULL
? load_u32(selected_layout + 24)
: 0;
uint32_t layout_fh = selected_layout != NULL
? load_u32(selected_layout + 28)
: 0;
uint32_t capture_w = selected_layout != NULL
? load_u32(selected_layout + 32)
: 0;
uint32_t capture_h = selected_layout != NULL
? load_u32(selected_layout + 36)
: 0;
sprintf(debug,
"crop #%ld offorigin-sample=%ld mode=%s flags=%lu frame=%lux%lu "
"src=%d,%d %dx%d dest=%d,%d %dx%d virtual=%ld,%ld %dx%d "
"monitor=%ld,%ld-%ld,%ld whole=%d layout=%ld,%ld "
"%lux%lu frame-at=%lu,%lu %lux%lu capture=%lux%lu "
"crop=%d,%d %dx%d",
debug_index, offorigin_index, crop_mode,
(unsigned long)frame_flags, (unsigned long)fw,
(unsigned long)fh, source_x,
source_y, source_width, source_height, x, y, width,
height,
(LONG)GetSystemMetrics(SM_XVIRTUALSCREEN),
(LONG)GetSystemMetrics(SM_YVIRTUALSCREEN),
screen_width, screen_height, monitor_left, monitor_top,
monitor_right, monitor_bottom, whole_monitor, layout_x,
layout_y, (unsigned long)layout_w,
(unsigned long)layout_h, (unsigned long)layout_fx,
(unsigned long)layout_fy, (unsigned long)layout_fw,
(unsigned long)layout_fh, (unsigned long)capture_w,
(unsigned long)capture_h, sx, sy, sw, sh);
hook_log(debug);
}
/* Wine's top-down DIB subrectangle path can offset a short
* monitor crop from the bottom of the composite. Compact the
* chosen rows in-place and draw a whole DIB at source (0,0).
* Destination rows precede source rows, so memmove also covers
* overlapping crops without another frame-sized allocation. */
for (int row = 0; row < sh; ++row)
memmove(frame_snapshot + (size_t)row * sw * 4,
frame_snapshot + (size_t)(sy + row) * fstride + (size_t)sx * 4,
(size_t)sw * 4);
bmp_header.biWidth = sw;
bmp_header.biHeight = -sh;
bmp_header.biSizeImage = (DWORD)((size_t)sw * sh * 4);
int result = StretchDIBits(
hdc_dest, x, y, width, height, sx, sy, sw, sh,
hdc_dest, x, y, width, height, 0, 0, sw, sh,
(const void *)frame_snapshot, (const BITMAPINFO *)&bmp_header,
DIB_RGB_COLORS, SRCCOPY);
ok = result != 0 && (DWORD)result != GDI_ERROR;
@@ -686,6 +1448,17 @@ static FARPROC WINAPI hook_get_proc_address(HMODULE module, LPCSTR name)
BOOL(WINAPI *stretch_blt)(HDC, int, int, int, int, HDC, int, int,
int, int, DWORD);
HRESULT(WINAPI *create_dxgi_factory1)(REFIID, void **);
int(WINAPI *get_system_metrics)(int);
int(WINAPI *get_device_caps)(HDC, int);
BOOL(WINAPI *get_monitor_info)(HMONITOR, LPMONITORINFO);
BOOL(WINAPI *enum_display_settings_a)(LPCSTR, DWORD, LPDEVMODEA);
BOOL(WINAPI *enum_display_settings_w)(LPCWSTR, DWORD, LPDEVMODEW);
BOOL(WINAPI *enum_display_settings_ex_w)(LPCWSTR, DWORD, LPDEVMODEW,
DWORD);
LONG(WINAPI *query_display_config)(UINT, UINT *,
DISPLAYCONFIG_PATH_INFO *, UINT *,
DISPLAYCONFIG_MODE_INFO *,
DISPLAYCONFIG_TOPOLOGY_ID *);
} replacement;
FARPROC resolved = original_get_proc_address(module, name);
if (name == NULL || ((uintptr_t)name >> 16) == 0)
@@ -706,6 +1479,39 @@ static FARPROC WINAPI hook_get_proc_address(HMODULE module, LPCSTR name)
replacement.create_dxgi_factory1 = hook_create_dxgi_factory1;
return replacement.generic;
}
if (lstrcmpiA(name, "GetSystemMetrics") == 0) {
replacement.get_system_metrics = hook_get_system_metrics;
return replacement.generic;
}
if (lstrcmpiA(name, "GetDeviceCaps") == 0) {
replacement.get_device_caps = hook_get_device_caps;
return replacement.generic;
}
if (lstrcmpiA(name, "GetMonitorInfoA") == 0) {
replacement.get_monitor_info = hook_get_monitor_info_a;
return replacement.generic;
}
if (lstrcmpiA(name, "GetMonitorInfoW") == 0) {
replacement.get_monitor_info = hook_get_monitor_info_w;
return replacement.generic;
}
if (lstrcmpiA(name, "EnumDisplaySettingsA") == 0) {
replacement.enum_display_settings_a = hook_enum_display_settings_a;
return replacement.generic;
}
if (lstrcmpiA(name, "EnumDisplaySettingsW") == 0) {
replacement.enum_display_settings_w = hook_enum_display_settings_w;
return replacement.generic;
}
if (lstrcmpiA(name, "EnumDisplaySettingsExW") == 0) {
replacement.enum_display_settings_ex_w =
hook_enum_display_settings_ex_w;
return replacement.generic;
}
if (lstrcmpiA(name, "QueryDisplayConfig") == 0) {
replacement.query_display_config = hook_query_display_config;
return replacement.generic;
}
return resolved;
}
@@ -916,7 +1722,7 @@ UINT WINAPI hook_send_input(UINT count, LPINPUT inputs, int size)
static int patch_module_iat(HMODULE base)
{
if (base == NULL)
if (base == NULL || base == hook_module)
return 0;
IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)base;
@@ -973,6 +1779,30 @@ static int patch_module_iat(HMODULE base)
} else if (lstrcmpiA(name->Name, "GetProcAddress") == 0) {
hook_fn = (void *)hook_get_proc_address;
original_slot = (void **)&original_get_proc_address;
} else if (lstrcmpiA(name->Name, "GetSystemMetrics") == 0) {
hook_fn = (void *)hook_get_system_metrics;
original_slot = (void **)&original_get_system_metrics;
} else if (lstrcmpiA(name->Name, "GetDeviceCaps") == 0) {
hook_fn = (void *)hook_get_device_caps;
original_slot = (void **)&original_get_device_caps;
} else if (lstrcmpiA(name->Name, "GetMonitorInfoA") == 0) {
hook_fn = (void *)hook_get_monitor_info_a;
original_slot = (void **)&original_get_monitor_info_a;
} else if (lstrcmpiA(name->Name, "GetMonitorInfoW") == 0) {
hook_fn = (void *)hook_get_monitor_info_w;
original_slot = (void **)&original_get_monitor_info_w;
} else if (lstrcmpiA(name->Name, "EnumDisplaySettingsA") == 0) {
hook_fn = (void *)hook_enum_display_settings_a;
original_slot = (void **)&original_enum_display_settings_a;
} else if (lstrcmpiA(name->Name, "EnumDisplaySettingsW") == 0) {
hook_fn = (void *)hook_enum_display_settings_w;
original_slot = (void **)&original_enum_display_settings_w;
} else if (lstrcmpiA(name->Name, "EnumDisplaySettingsExW") == 0) {
hook_fn = (void *)hook_enum_display_settings_ex_w;
original_slot = (void **)&original_enum_display_settings_ex_w;
} else if (lstrcmpiA(name->Name, "QueryDisplayConfig") == 0) {
hook_fn = (void *)hook_query_display_config;
original_slot = (void **)&original_query_display_config;
}
if (hook_fn == NULL)
continue;
@@ -1064,11 +1894,21 @@ void uur_hook_version(void) {}
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
{
(void)instance;
(void)reserved;
if (reason != DLL_PROCESS_ATTACH)
return TRUE;
hook_module = instance;
WCHAR executable[MAX_PATH];
if (GetModuleFileNameW(NULL, executable, MAX_PATH) > 0) {
const WCHAR *name = wcsrchr(executable, L'\\');
name = name != NULL ? name + 1 : executable;
capture_geometry_enabled = lstrcmpiW(name, L"GameViewerServer.exe") == 0 ||
lstrcmpiW(name, L"GameViewerService.exe") == 0;
}
WCHAR geometry_option[2];
if (GetEnvironmentVariableW(L"UUR_CAPTURE_GEOMETRY", geometry_option, 2) == 1)
capture_geometry_enabled = geometry_option[0] == L'1';
DisableThreadLibraryCalls(instance);
if (connect_bridge()) {
hook_log("attach: bridge connected");
+4 -2
View File
@@ -9,6 +9,7 @@
pipewire,
wayland,
wayland-scanner,
xorg,
pulseaudio,
curl,
glib,
@@ -38,14 +39,14 @@ rustPlatform.buildRustPackage {
bash
wayland-scanner
];
buildInputs = [ pipewire wayland ];
buildInputs = [ pipewire wayland xorg.libX11 xorg.libXext ];
CC = "${stdenv.cc}/bin/cc";
UUR_BUILD_SHELL = runtimeShell;
doCheck = false;
postPatch = ''
patchShebangs hook/build.sh capture/build.sh packaging/stage.sh
patchShebangs hook/build.sh capture/build.sh compat/build.sh packaging/stage.sh
'';
postBuild = ''
@@ -54,6 +55,7 @@ rustPlatform.buildRustPackage {
UU_MINGW_DLLTOOL=${windowsBinutils}/bin/${targetPrefix}dlltool \
./hook/build.sh
./capture/build.sh
./compat/build.sh
'';
installPhase = ''
+4 -3
View File
@@ -1,14 +1,14 @@
# Contributor: uur contributors
# Maintainer: uur contributors
pkgname=uur
pkgver=0.1.6
pkgver=0.1.7
pkgrel=0
pkgdesc="Native Linux companion for NetEase UU Remote"
url="https://github.com/panxuc/uur"
arch="x86_64"
license="MIT"
depends="bash wine curl glib wayland-libs-client pulseaudio-utils xdg-desktop-portal"
makedepends="cargo rust gcc musl-dev pipewire-dev wayland-dev pkgconf mingw-w64-gcc mingw-w64-binutils"
depends="bash wine curl glib wayland-libs-client libx11 pulseaudio-utils xdg-desktop-portal"
makedepends="cargo rust gcc musl-dev pipewire-dev wayland-dev libx11-dev pkgconf mingw-w64-gcc mingw-w64-binutils"
options="!check"
source="$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz"
builddir="$srcdir/$pkgname-$pkgver"
@@ -17,6 +17,7 @@ build() {
cargo build --release --locked
./hook/build.sh
./capture/build.sh
./compat/build.sh
}
check() {
+3 -2
View File
@@ -1,7 +1,7 @@
FROM alpine:edge AS build
RUN apk add --no-cache \
bash cargo rust gcc musl-dev pipewire-dev wayland-dev pkgconf \
bash cargo rust gcc musl-dev pipewire-dev wayland-dev libx11-dev pkgconf \
mingw-w64-gcc mingw-w64-binutils
WORKDIR /src
@@ -12,10 +12,11 @@ RUN --mount=type=cache,target=/root/.cargo/registry \
&& cargo build --release --locked \
&& ./hook/build.sh \
&& ./capture/build.sh \
&& ./compat/build.sh \
&& ./packaging/stage.sh /out
FROM alpine:edge
RUN apk add --no-cache \
bash ca-certificates curl glib pipewire-libs wayland-libs-client pulseaudio-utils wine xdg-desktop-portal
bash ca-certificates curl glib pipewire-libs wayland-libs-client libx11 pulseaudio-utils wine xdg-desktop-portal
COPY --from=build /out/ /
ENTRYPOINT ["/usr/bin/uur"]
+1 -1
View File
@@ -5,7 +5,7 @@ pkgdesc="Native Linux companion for NetEase UU Remote"
arch=('x86_64')
url="https://github.com/panxuc/uur"
license=('MIT')
depends=('glibc' 'gcc-libs' 'glib2' 'wine' 'curl' 'pipewire' 'wayland' 'xdg-desktop-portal')
depends=('glibc' 'gcc-libs' 'glib2' 'wine' 'curl' 'pipewire' 'wayland' 'libx11' 'libxext' 'xdg-desktop-portal')
optdepends=(
'xdg-desktop-portal-gnome: GNOME-compatible Wayland capture and input'
'xdg-desktop-portal-kde: Plasma Wayland capture and input'
+5 -2
View File
@@ -8,7 +8,7 @@
# `uur setup` (it is never redistributed by this package).
pkgname=uur
pkgver=0.1.6
pkgver=0.1.7
pkgrel=1
pkgdesc="Native Linux companion for NetEase UU Remote (X11/Wayland input and capture bridge)"
arch=('x86_64')
@@ -22,9 +22,11 @@ depends=(
'curl'
'pipewire'
'wayland'
'libx11'
'libxext'
'xdg-desktop-portal'
)
makedepends=('cargo' 'gcc' 'pkgconf' 'wayland' 'mingw-w64-gcc' 'mingw-w64-binutils')
makedepends=('cargo' 'gcc' 'pkgconf' 'wayland' 'libx11' 'mingw-w64-gcc' 'mingw-w64-binutils')
optdepends=(
'osslsigncode: Authenticode verification of official updater downloads'
'xdg-desktop-portal-gnome: GNOME, niri, and compatible Wayland capture/input'
@@ -50,6 +52,7 @@ build() {
cargo build --release --locked
./hook/build.sh
./capture/build.sh
./compat/build.sh
}
check() {
+7
View File
@@ -15,6 +15,13 @@ command -v makepkg >/dev/null 2>&1 || {
exit 2
}
cargo build --manifest-path "$project_root/Cargo.toml" --release --locked
"$project_root/hook/build.sh"
"$project_root/capture/build.sh"
if [[ -f "$project_root/compat/build.sh" ]]; then
"$project_root/compat/build.sh"
fi
sed -e "s|@UUR_VERSION@|$version|g" \
-e "s|@UUR_PROJECT_ROOT@|$project_root|g" \
"$project_root/packaging/arch/PKGBUILD.in" >"$build_dir/PKGBUILD"
+1
View File
@@ -15,6 +15,7 @@ find "$output" -maxdepth 1 -type f \
cargo build --manifest-path "$project_root/Cargo.toml" --release --locked
"$project_root/hook/build.sh"
"$project_root/capture/build.sh"
"$project_root/compat/build.sh"
"$project_root/packaging/stage.sh" "$stage"
tar --zstd -C "$stage" -cf "$output/uur-${version}-linux-x86_64.tar.zst" .
+9 -4
View File
@@ -25,6 +25,8 @@ overrides:
- curl
- libpipewire-0.3-0
- libwayland-client0
- libx11-6
- libxext6
- xdg-desktop-portal
recommends:
- xdg-desktop-portal-gnome | xdg-desktop-portal-kde | xdg-desktop-portal-wlr
@@ -36,12 +38,15 @@ overrides:
rpm:
depends:
- glibc
- libgcc
- glib2
- "libgcc_s.so.1()(64bit)"
- /usr/bin/gdbus
- /usr/bin/gsettings
- wine
- curl
- pipewire-libs
- wayland-libs
- "libpipewire-0.3.so.0()(64bit)"
- "libwayland-client.so.0()(64bit)"
- "libX11.so.6()(64bit)"
- "libXext.so.6()(64bit)"
- xdg-desktop-portal
recommends:
- ethtool
+2
View File
@@ -12,6 +12,8 @@ install -Dm0755 "$project_root/capture/uur-pw-capture" \
"$destination$prefix/lib/uur/uur-pw-capture"
install -Dm0755 "$project_root/capture/uur-wl-capture" \
"$destination$prefix/lib/uur/uur-wl-capture"
install -Dm0755 "$project_root/build/compat/libuur-lock-mods.so" \
"$destination$prefix/lib/uur/libuur-lock-mods.so"
install -Dm0755 "$project_root/packaging/post-install-message" \
"$destination$prefix/lib/uur/post-install-message"
for component in wevtapi.dll uur-hook.dll wtsapi32.dll winlogon.exe uur-terminal-proxy.exe uur-mux-proxy.exe uur-launch-proxy.exe; do
+1
View File
@@ -17,3 +17,4 @@ cc -std=c11 -O2 -Wall -Wextra -Werror \
"$temporary/cursor-overlay"
"$project_root/hook/build.sh"
"$project_root/capture/build.sh"
"$project_root/compat/build.sh"
+6
View File
@@ -29,11 +29,17 @@ done
printf 'missing capture helper — run: ./capture/build.sh\n' >&2
exit 1
}
[[ -f "$root/build/compat/libuur-lock-mods.so" ]] || {
printf 'missing lock-modifier shim — run: ./compat/build.sh\n' >&2
exit 1
}
install -Dm0755 "$root/target/release/uur" "$prefix/bin/uur"
for helper in uur-pw-capture uur-wl-capture; do
install -Dm0755 "$root/capture/$helper" "$prefix/lib/uur/$helper"
done
install -Dm0755 "$root/build/compat/libuur-lock-mods.so" \
"$prefix/lib/uur/libuur-lock-mods.so"
for dll in "${components[@]}"; do
install -Dm0644 "$root/build/hook/$dll" "$prefix/lib/uur/hook/$dll"
done
+111 -1
View File
@@ -4,6 +4,116 @@ use anyhow::{Context, Result};
use std::path::Path;
use std::process::{Command, Stdio};
fn pactl(arguments: &[&str]) -> Result<String> {
let output = Command::new("pactl")
.args(arguments)
.env("LC_ALL", "C")
.stdin(Stdio::null())
.output()
.context("querying PulseAudio; install pactl and enable PulseAudio or pipewire-pulse")?;
anyhow::ensure!(
output.status.success(),
"pactl failed: {}",
String::from_utf8_lossy(&output.stderr).trim()
);
Ok(String::from_utf8(output.stdout)?.trim().to_owned())
}
/// Resolve only a real, available source. Explicit loopback selection must
/// fail if unavailable rather than silently capturing a physical microphone.
pub fn capture_source(selection: Option<&str>) -> Result<Option<String>> {
let Some(selection) = selection else {
return Ok(None);
};
anyhow::ensure!(
!selection.is_empty() && !selection.chars().any(char::is_control),
"invalid audio source name"
);
let sources: Vec<serde_json::Value> =
serde_json::from_str(&pactl(&["--format=json", "list", "sources"])?)?;
let name = if selection == "system" {
let default = pactl(&["get-default-sink"])?;
let sinks: Vec<serde_json::Value> =
serde_json::from_str(&pactl(&["--format=json", "list", "sinks"])?)?;
sinks
.iter()
.find(|sink| sink["name"].as_str() == Some(&default))
.and_then(|sink| sink["monitor_source"].as_str())
.context("the default output has no system-audio monitor source")?
.to_owned()
} else {
selection.to_owned()
};
anyhow::ensure!(sources.iter().any(|source| source["name"].as_str() == Some(&name)),
"audio source {name:?} is unavailable; use `uur audio sources` to choose an available source");
Ok(Some(name))
}
pub fn print_sources() -> Result<()> {
let sources: Vec<serde_json::Value> =
serde_json::from_str(&pactl(&["--format=json", "list", "sources"])?)?;
println!("Default source: {}", pactl(&["get-default-source"])?);
for source in sources {
let name = source["name"]
.as_str()
.context("audio source has no name")?;
let description = source["description"].as_str().unwrap_or(name);
let monitor = source["monitor_source"].as_str().is_some()
|| source["monitor_of_sink"]
.as_u64()
.is_some_and(|index| index != u64::from(u32::MAX));
println!(
"{name}\t{}\t{description}",
if monitor {
"system monitor"
} else {
"capture input"
}
);
}
Ok(())
}
pub fn configure_source(selection: &str) -> Result<()> {
let choice = (selection != "default").then_some(selection);
let resolved = capture_source(choice)?;
crate::config::Config::update(|stored| {
stored.audio_capture_source = choice.map(ToOwned::to_owned)
})?;
println!(
"Wine default audio capture: {}; applies after `uur stop` and `uur run`",
resolved
.as_deref()
.unwrap_or("follow the existing audio environment")
);
if choice == Some("system") {
println!("The fallback capture input will carry system sound. Separate simultaneous microphone and speaker channels still require the official audio driver.");
}
Ok(())
}
pub(crate) fn apply_capture_env(
command: &mut Command,
config: &crate::config::Config,
) -> Result<()> {
if config.audio_capture_source.is_none() {
return Ok(());
}
// Each process resolves once, before starting the managed wineserver/SCM.
static SOURCE: std::sync::OnceLock<std::result::Result<Option<String>, String>> =
std::sync::OnceLock::new();
match SOURCE.get_or_init(|| {
capture_source(config.audio_capture_source.as_deref()).map_err(|e| format!("{e:#}"))
}) {
Ok(Some(source)) => {
command.env("PULSE_SOURCE", source);
}
Ok(None) => {}
Err(error) => anyhow::bail!("Wine audio capture unavailable: {error}"),
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Backend {
PipeWirePulse,
@@ -52,7 +162,7 @@ pub fn sync_wine(prefix: &Path) -> Result<Backend> {
Backend::None => None,
};
if let Some(driver) = driver {
let status = Command::new("wine")
let status = crate::wine::command(prefix)?
.env("WINEPREFIX", prefix)
.env("WINEDEBUG", "-all")
.args([
+27 -5
View File
@@ -156,25 +156,48 @@ pub fn serve() -> Result<()> {
let backend_handle = backend.clone();
let addr = format!("127.0.0.1:{}", config.bridge_port);
let listener = TcpListener::bind(&addr).with_context(|| format!("binding {addr}"))?;
listener.set_nonblocking(true)?;
println!("{}", t!("bridge.listening", port = config.bridge_port));
// The service process hooks itself too, so multiple authenticated
// connections coexist; each gets its own thread. Injection is
// serialized through the single backend.
for stream in listener.incoming() {
let stream = match stream {
Ok(s) => s,
Err(_) => continue,
loop {
backend.lock().unwrap().backend.check_health()?;
let stream = match listener.accept() {
Ok((stream, _)) => stream,
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
thread::sleep(std::time::Duration::from_millis(50));
continue;
}
Err(error)
if matches!(
error.kind(),
std::io::ErrorKind::Interrupted | std::io::ErrorKind::ConnectionAborted
) =>
{
continue
}
Err(error) => return Err(error).context("accepting hook connection"),
};
let config = config.clone();
let backend_handle = backend_handle.clone();
thread::spawn(move || {
let mut stream = stream;
if stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.is_err()
{
return;
}
match authenticate(&mut stream, &config) {
Ok(true) => {}
Ok(false) => return,
Err(_) => return,
}
if stream.set_read_timeout(None).is_err() {
return;
}
use input::InputBackend;
// Read sockets without holding the shared desktop input lock.
let mut connection = ConnectionInput {
@@ -199,7 +222,6 @@ pub fn serve() -> Result<()> {
let _ = connection.release_all();
});
}
Ok(())
}
fn authenticate(stream: &mut TcpStream, config: &Config) -> Result<bool> {
+137 -28
View File
@@ -1,9 +1,9 @@
//! Portal side of the capture path.
//!
//! Negotiates an xdg-desktop-portal ScreenCast session, obtains a private
//! PipeWire file descriptor and the video node id, then hands both to the
//! C capture helper (`uur-pw-capture`) which runs the PipeWire stream and
//! publishes frames into a private XDG runtime file (see
//! PipeWire file descriptor and the consented video nodes, then hands them to
//! the C capture helper (`uur-pw-capture`) which runs the PipeWire streams and
//! composites their frames into a private XDG runtime file (see
//! docs/capture-protocol.md).
//!
//! Consent: the first run shows the compositor's share dialog once. The
@@ -25,17 +25,28 @@ use std::time::{Duration, Instant};
use crate::config::Config;
const FRAME_PROTOCOL_VERSION: u32 = 3;
type PortalSession =
ashpd::desktop::Session<'static, ashpd::desktop::screencast::Screencast<'static>>;
struct PortalCapture {
fd: OwnedFd,
node: u32,
streams: Vec<CaptureStream>,
restore_token: Option<String>,
session: PortalSession,
closed: zbus::proxy::SignalStream<'static>,
}
#[derive(Clone, Debug)]
pub(crate) struct CaptureStream {
pub(crate) node: u32,
pub(crate) x: i32,
pub(crate) y: i32,
pub(crate) width: u32,
pub(crate) height: u32,
}
/// Run the capture path until interrupted.
pub fn run_standalone() -> Result<()> {
// Public capture uses the same lifecycle lock as run/setup so stop and
@@ -93,13 +104,13 @@ pub fn run() -> Result<()> {
};
let PortalCapture {
fd: pw_fd,
node,
streams,
session,
mut closed,
..
} = match opened {
Ok(opened) => opened,
Err(error) if !initial_session && failures < 3 => {
Err(error) if !initial_session && failures < 3 && !user_cancelled(&error) => {
failures += 1;
eprintln!("capture reconnect {failures}/3 failed: {error:#}");
tokio::select! {
@@ -110,13 +121,18 @@ pub fn run() -> Result<()> {
Err(error) => return Err(error),
};
initial_session = false;
println!("{}", t!("capture.session_ready", node = node));
let nodes = streams
.iter()
.map(|stream| stream.node.to_string())
.collect::<Vec<_>>()
.join(",");
println!("{}", t!("capture.session_ready", node = nodes));
match std::fs::remove_file(&frames) {
Ok(()) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => return Err(error).context("retiring the previous capture transport"),
}
let mut helper = CaptureHelper(spawn_helper(pw_fd, node)?);
let mut helper = CaptureHelper(spawn_capture_helper(pw_fd, &streams)?);
let mut healthy_since = None;
let mut portal_closed = false;
let mut reconfigure = false;
@@ -143,6 +159,10 @@ pub fn run() -> Result<()> {
if has_frame(&frames) && healthy_since.is_none() {
healthy_since = Some(Instant::now());
}
if helper_stalled(&frames) {
eprintln!("capture helper heartbeat expired; reconnecting");
break None;
}
if let Some(status) = helper.0.try_wait()
.context("waiting for capture helper")? {
break Some(status);
@@ -175,14 +195,16 @@ pub fn run() -> Result<()> {
"capture recovery failed repeatedly; restart uur run to share again"
);
}
let status = status.context("capture helper stopped without an exit status")?;
if !matches!(status.code(), Some(0 | 75) | None) {
if status
.as_ref()
.is_some_and(|status| !matches!(status.code(), Some(0 | 75) | None))
{
anyhow::bail!(
"{} ({status}); capture recovery stopped",
"{} ({status:?}); capture recovery stopped",
t!("capture.helper_failed")
);
}
eprintln!("capture stream interrupted ({status}); reconnecting {failures}/3");
eprintln!("capture stream interrupted ({status:?}); reconnecting {failures}/3");
}
tokio::select! {
_ = tokio::time::sleep(Duration::from_secs(1)) => {},
@@ -206,7 +228,7 @@ pub fn run() -> Result<()> {
})
}
async fn watch_display_changes() -> Option<zbus::proxy::SignalStream<'static>> {
pub(crate) async fn watch_display_changes() -> Option<zbus::proxy::SignalStream<'static>> {
let connection = zbus::Connection::session().await.ok()?;
let bus = zbus::fdo::DBusProxy::new(&connection).await.ok()?;
let name = "org.gnome.Mutter.DisplayConfig".try_into().ok()?;
@@ -225,7 +247,7 @@ async fn watch_display_changes() -> Option<zbus::proxy::SignalStream<'static>> {
proxy.receive_signal("MonitorsChanged").await.ok()
}
async fn next_display_change(changes: &mut Option<zbus::proxy::SignalStream<'static>>) {
pub(crate) async fn next_display_change(changes: &mut Option<zbus::proxy::SignalStream<'static>>) {
loop {
match changes.as_mut() {
Some(stream) => {
@@ -245,7 +267,7 @@ async fn wait_for_stop(stopping: &AtomicBool) {
}
}
struct CaptureHelper(Child);
pub(crate) struct CaptureHelper(pub(crate) Child);
impl Drop for CaptureHelper {
fn drop(&mut self) {
@@ -276,17 +298,48 @@ pub(crate) fn has_frame(path: &Path) -> bool {
}
let field = |offset| u32::from_le_bytes(header[offset..offset + 4].try_into().unwrap());
field(0) == 0x4652_5555
&& field(4) == 1
&& field(4) == FRAME_PROTOCOL_VERSION
&& field(8) > 0
&& field(12) > 0
&& field(16) >= field(8).saturating_mul(4)
&& field(20) == 1
&& (1..=16).contains(&field(36))
&& u64::from_le_bytes(header[24..32].try_into().unwrap()) > 0
}
/// The producer heartbeat advances even when the desktop is static. A zero
/// value belongs to a producer without heartbeat support, never a stale frame.
pub(crate) fn helper_stalled(path: &Path) -> bool {
let mut header = [0u8; 48];
let Ok(mut file) = std::fs::File::open(path) else {
return false;
};
if file.read_exact(&mut header).is_err()
|| u32::from_le_bytes(header[4..8].try_into().unwrap()) != FRAME_PROTOCOL_VERSION
{
return false;
}
let heartbeat = u64::from_le_bytes(header[40..48].try_into().unwrap());
let mut now: libc::timespec = unsafe { std::mem::zeroed() };
if heartbeat == 0 || unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut now) } != 0 {
return false;
}
let milliseconds = (now.tv_sec as u64).saturating_mul(1000) + now.tv_nsec as u64 / 1_000_000;
milliseconds.saturating_sub(heartbeat) > 10_000
}
/// Start the native PipeWire consumer with a portal-owned descriptor. The
/// descriptor remains private to the child and is never reopened by path.
pub(crate) fn spawn_helper(pw_fd: OwnedFd, node: u32) -> Result<Child> {
pub(crate) fn spawn_helper(pw_fd: OwnedFd, streams: &[CaptureStream]) -> Result<Child> {
spawn_capture_helper(pw_fd, streams)
}
fn spawn_capture_helper(pw_fd: OwnedFd, streams: &[CaptureStream]) -> Result<Child> {
anyhow::ensure!(!streams.is_empty(), "ScreenCast returned no streams");
anyhow::ensure!(
streams.len() <= 16,
"too many ScreenCast streams (maximum 16)"
);
// The helper inherits this descriptor: strip close-on-exec and keep the
// OwnedFd alive across the spawn (closing it would invalidate the number
// the child receives).
@@ -303,9 +356,19 @@ pub(crate) fn spawn_helper(pw_fd: OwnedFd, node: u32) -> Result<Child> {
let helper = find_helper("uur-pw-capture")?;
let mut command = Command::new(&helper);
let stream_specs = streams
.iter()
.map(|stream| {
format!(
"{},{},{},{},{}",
stream.node, stream.x, stream.y, stream.width, stream.height
)
})
.collect::<Vec<_>>()
.join(";");
command
.env("UUR_PW_FD", raw.to_string())
.env("UUR_PW_NODE", node.to_string())
.env("UUR_PW_STREAMS", stream_specs)
.env("UUR_FRAME_PATH", frame_path()?);
if let Ok(debug) = std::env::var("UUR_PW_DEBUG") {
command.env("PIPEWIRE_DEBUG", debug);
@@ -316,14 +379,14 @@ pub(crate) fn spawn_helper(pw_fd: OwnedFd, node: u32) -> Result<Child> {
Ok(child)
}
fn frame_path() -> Result<PathBuf> {
pub(crate) fn frame_path() -> Result<PathBuf> {
if let Some(path) = std::env::var_os("UUR_FRAME_PATH") {
return Ok(PathBuf::from(path));
}
Ok(crate::config::runtime_dir()?.join("frames.v1"))
Ok(crate::config::runtime_dir()?.join("frames.v3"))
}
/// Open the portal session and return (inherited fd number, node id).
/// Open the portal session and return its PipeWire streams and node ids.
/// First attempt reuses the stored restore token; if that fails (stale
/// token, monitor changed, compositor restart) fall back to one
/// interactive attempt and store the fresh token.
@@ -341,15 +404,33 @@ async fn portal_session(config: &mut Config, interactive: bool) -> Result<Portal
.into_iter()
.chain(interactive.then_some(None))
{
match negotiate(&screencast, source, token.as_deref()).await {
match negotiate(
&screencast,
source,
config.capture_multiple,
token.as_deref(),
)
.await
{
Ok(capture) => {
if capture.restore_token != saved_token {
config.capture_restore_token = capture.restore_token.clone();
config.store().context("storing the new restore token")?;
Config::update(|stored| {
if stored.capture_source == config.capture_source
&& stored.capture_multiple == config.capture_multiple
&& stored.capture_backend == config.capture_backend
{
stored.capture_restore_token = capture.restore_token.clone();
}
})
.context("storing the new restore token")?;
}
return Ok(capture);
}
Err(error) => {
if user_cancelled(&error) {
return Err(error);
}
last_error = Some(error);
}
}
@@ -359,6 +440,20 @@ async fn portal_session(config: &mut Config, interactive: bool) -> Result<Portal
}))
}
pub(crate) fn user_cancelled(error: &anyhow::Error) -> bool {
error.chain().any(|source| {
matches!(
source.downcast_ref::<ashpd::Error>(),
Some(ashpd::Error::Response(
ashpd::desktop::ResponseError::Cancelled
))
) || matches!(
source.downcast_ref::<ashpd::desktop::ResponseError>(),
Some(ashpd::desktop::ResponseError::Cancelled)
)
})
}
/// Choose a cursor mode from the flags the portal advertises.
///
/// `Embedded` is what the capture path wants (the hook composites the cursor
@@ -412,6 +507,7 @@ pub(crate) async fn preferred_cursor_mode(
async fn negotiate(
screencast: &ashpd::desktop::screencast::Screencast<'static>,
source: ashpd::desktop::screencast::SourceType,
multiple: bool,
restore_token: Option<&str>,
) -> Result<PortalCapture> {
use ashpd::desktop::PersistMode;
@@ -440,7 +536,7 @@ async fn negotiate(
&session,
cursor_mode,
source.into(),
false,
multiple && matches!(source, ashpd::desktop::screencast::SourceType::Monitor),
restore_token,
PersistMode::ExplicitlyRevoked,
)
@@ -449,9 +545,22 @@ async fn negotiate(
let response = screencast.start(&session, None).await?;
let streams_response = response.response()?;
let streams = streams_response.streams();
let stream = streams.first().context("no monitor selected")?;
let node = stream.pipe_wire_node_id();
let streams = streams_response
.streams()
.iter()
.map(|stream| {
let (x, y) = stream.position().unwrap_or((i32::MIN, i32::MIN));
let (width, height) = stream.size().unwrap_or((0, 0));
CaptureStream {
node: stream.pipe_wire_node_id(),
x,
y,
width: u32::try_from(width).unwrap_or(0),
height: u32::try_from(height).unwrap_or(0),
}
})
.collect::<Vec<_>>();
anyhow::ensure!(!streams.is_empty(), "no monitor selected");
let fresh_token = streams_response.restore_token().map(String::from);
let fd = screencast
@@ -460,7 +569,7 @@ async fn negotiate(
.context("opening the PipeWire remote")?;
Ok(PortalCapture {
fd,
node,
streams,
restore_token: fresh_token,
session,
closed,
+115 -13
View File
@@ -1,6 +1,10 @@
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::io::Write;
use std::os::fd::AsRawFd;
use std::os::unix::fs::OpenOptionsExt;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
/// Persistent user configuration. Everything lives under the XDG state and
/// config directories; the managed Wine prefix lives under data.
@@ -24,10 +28,23 @@ pub struct Config {
pub remote_desktop_restore_token: Option<String>,
/// Portal source type: monitor, window, or virtual.
pub capture_source: String,
/// Multiple consented monitors are opt-in; existing grants stay intact.
pub capture_multiple: bool,
/// "portal" uses system consent; "wayland" is explicitly enabled direct monitor capture.
pub capture_backend: String,
/// Named Wayland output for direct capture; a single-output session needs no name.
pub capture_output: Option<String>,
/// Override Wine's default capture source: "system" or a PulseAudio source
/// name. None follows the user's existing audio environment.
pub audio_capture_source: Option<String>,
/// Strip these X11 modifier bits from key events handed to the managed
/// client. Wine reconciles its asynchronous keyboard state against the
/// modifier bits on every key event and, when the client's low-level hook
/// swallows lock keys, forges a lock-key down/up pair per keystroke. The
/// "auto" discovers NumLock/ScrollLock from the actual X11 keymap.
/// Shortcut, layout, case and keypad modifiers are preserved. Numeric
/// masks restrict this discovered set; 0 disables the workaround.
pub strip_lock_mods: Option<String>,
}
impl Default for Config {
@@ -41,8 +58,11 @@ impl Default for Config {
capture_restore_token: None,
remote_desktop_restore_token: None,
capture_source: "monitor".into(),
capture_multiple: false,
capture_backend: "portal".into(),
capture_output: None,
audio_capture_source: None,
strip_lock_mods: Some("auto".into()),
}
}
}
@@ -116,24 +136,104 @@ pub fn runtime_dir() -> Result<PathBuf> {
impl Config {
pub fn load() -> Result<Self> {
let path = config_path()?;
if !path.exists() {
let config = Self::default();
config.store()?;
return Ok(config);
match std::fs::read_to_string(&path) {
Ok(raw) => toml::from_str(&raw).context("parsing config.toml"),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Self::update(|_| {}),
Err(error) => Err(error).with_context(|| format!("reading {}", path.display())),
}
let raw = std::fs::read_to_string(&path)
.with_context(|| format!("reading {}", path.display()))?;
toml::from_str(&raw).context("parsing config.toml")
}
pub fn store(&self) -> Result<()> {
let path = config_path()?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("creating {}", parent.display()))?;
let (path, _lock) = Self::write_lock()?;
self.store_locked(&path)
}
/// Serialize read/modify/write across the capture, input and frontend
/// processes. Background token saves must not overwrite other fields.
pub fn update(change: impl FnOnce(&mut Self)) -> Result<Self> {
let (path, _lock) = Self::write_lock()?;
let mut config = match std::fs::read_to_string(&path) {
Ok(raw) => toml::from_str(&raw).context("parsing config.toml")?,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Self::default(),
Err(error) => return Err(error).context("reading configuration for update"),
};
change(&mut config);
config.store_locked(&path)?;
Ok(config)
}
fn write_lock() -> Result<(PathBuf, std::fs::File)> {
let mut path = config_path()?;
// Do not replace a user-created symlink to declarative configuration.
if std::fs::symlink_metadata(&path).is_ok_and(|meta| meta.file_type().is_symlink()) {
path = std::fs::canonicalize(&path).context("resolving configuration symlink")?;
}
std::fs::write(&path, toml::to_string_pretty(self)?)
.with_context(|| format!("writing {}", path.display()))
let parent = path.parent().context("configuration has no parent")?;
std::fs::create_dir_all(parent)?;
let lock = std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.mode(0o600)
.open(path.with_extension("toml.lock"))?;
loop {
if unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX) } == 0 {
break;
}
let error = std::io::Error::last_os_error();
if error.kind() != std::io::ErrorKind::Interrupted {
return Err(error).context("locking configuration");
}
}
Ok((path, lock))
}
fn store_locked(&self, path: &std::path::Path) -> Result<()> {
static NEXT: AtomicU64 = AtomicU64::new(0);
let parent = path.parent().context("configuration has no parent")?;
let mut document = match std::fs::read_to_string(path) {
Ok(raw) => {
toml::from_str::<toml::Table>(&raw).context("parsing existing configuration")?
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => toml::Table::new(),
Err(error) => return Err(error).context("reading existing configuration"),
};
// Keep keys belonging to newer versions. JSON includes None fields,
// allowing explicitly cleared optional fields to remove old values.
for name in serde_json::to_value(self)?.as_object().unwrap().keys() {
document.remove(name);
}
document.extend(toml::Value::try_from(self)?.as_table().unwrap().clone());
let text = toml::to_string_pretty(&document)?;
for _ in 0..32 {
let temporary = parent.join(format!(
".uur-config-{}-{}.tmp",
std::process::id(),
NEXT.fetch_add(1, Ordering::Relaxed)
));
let mut file = match std::fs::OpenOptions::new()
.create_new(true)
.write(true)
.mode(0o600)
.open(&temporary)
{
Ok(file) => file,
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(error) => return Err(error).context("creating configuration temporary file"),
};
let result = (|| -> Result<()> {
file.write_all(text.as_bytes())?;
file.sync_all()?;
std::fs::rename(&temporary, path)?;
std::fs::File::open(parent)?.sync_all()?;
Ok(())
})();
if result.is_err() {
let _ = std::fs::remove_file(&temporary);
}
return result.with_context(|| format!("writing {}", path.display()));
}
anyhow::bail!("could not create a configuration temporary file")
}
}
@@ -164,5 +264,7 @@ input_backend = "auto"
assert!(config.remote_desktop_restore_token.is_none());
assert!(config.capture_restore_token.is_none());
assert_eq!(config.capture_source, "monitor");
assert!(!config.capture_multiple);
assert_eq!(config.strip_lock_mods.as_deref(), Some("auto"));
}
}
+37 -2
View File
@@ -247,6 +247,17 @@ pub fn report() -> Result<()> {
let audio = crate::audio::detect();
line("info", "audio backend", audio.label());
let audio_config = crate::config::Config::load()?;
match crate::audio::capture_source(audio_config.audio_capture_source.as_deref()) {
Ok(source) => line(
"info",
"Wine default audio capture",
source
.as_deref()
.unwrap_or("existing audio environment; may be a microphone"),
),
Err(error) => line("warn", "Wine default audio capture", &format!("{error:#}")),
}
let video = crate::video::detect();
line(
@@ -347,16 +358,40 @@ pub fn report() -> Result<()> {
let session_type = std::env::var("XDG_SESSION_TYPE").unwrap_or_default();
let on_wayland = session_type == "wayland" || std::env::var_os("WAYLAND_DISPLAY").is_some();
let configured = crate::config::Config::load()?;
if std::env::var_os("DISPLAY").is_some() {
match crate::input::xtest::lock_state() {
Ok(state) => line("info", "X11 lock state", &state),
Err(error) => line("WARN", "X11 lock state", &error.to_string()),
}
let policy = configured.strip_lock_mods.as_deref().unwrap_or("disabled");
let shim = crate::session::lock_mods_shim();
let disabled = matches!(policy, "disabled" | "0" | "0x0" | "0x00");
line(
if !disabled && shim.is_none() {
"WARN"
} else {
"info"
},
"Wine keyboard workaround",
&format!(
"{policy}; shim={}; case/keypad remain native",
shim.as_deref()
.map(|path| path.display().to_string())
.unwrap_or_else(|| "not installed".into())
),
);
}
line(
"info",
"capture backend",
&format!(
"{} ({})",
"{} ({}; multiple={})",
configured.capture_backend,
configured
.capture_output
.as_deref()
.unwrap_or("selected/default output")
.unwrap_or("selected/default output"),
configured.capture_multiple
),
);
let mut preferred = std::env::var("UUR_INPUT_BACKEND").unwrap_or(configured.input_backend);
+3
View File
@@ -7,6 +7,9 @@ use anyhow::Result;
/// A desktop input sink. Records arrive already normalized by `protocol`.
pub trait InputBackend: Send {
fn name(&self) -> &'static str;
fn check_health(&self) -> Result<()> {
Ok(())
}
fn key(&mut self, vkey: u16, down: bool) -> Result<()>;
fn button(&mut self, button: u16, down: bool) -> Result<()>;
+392 -44
View File
@@ -2,11 +2,13 @@ use anyhow::{anyhow, Context, Result};
use ashpd::desktop::remote_desktop::{Axis, DeviceType, KeyState, RemoteDesktop};
use ashpd::desktop::screencast::Screencast;
use ashpd::desktop::PersistMode;
use futures::StreamExt;
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{self, SyncSender};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::time::Instant;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use super::InputBackend;
@@ -28,15 +30,103 @@ struct Motion {
y: f64,
}
struct CaptureGuard(std::process::Child);
impl Drop for CaptureGuard {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
/// Select a consented stream in the same logical layout used by the compositor.
/// A point in a monitor gap is clamped to the closest selected monitor.
fn absolute_target(
streams: &[crate::capture::CaptureStream],
x: f64,
y: f64,
) -> Result<(u32, f64, f64)> {
anyhow::ensure!(
x.is_finite() && y.is_finite(),
"invalid absolute pointer position"
);
anyhow::ensure!(
!streams.is_empty() && streams.len() <= 16,
"invalid input stream count"
);
let mut streams = streams.to_vec();
if streams
.iter()
.any(|stream| stream.width == 0 || stream.height == 0)
{
use std::io::Read;
let mut header = [0u8; 704];
std::fs::File::open(crate::capture::frame_path()?)?.read_exact(&mut header)?;
let field = |offset| u32::from_le_bytes(header[offset..offset + 4].try_into().unwrap());
anyhow::ensure!(
field(0) == 0x4652_5555
&& field(4) == 3
&& field(36) as usize == streams.len()
&& u64::from_le_bytes(header[24..32].try_into().unwrap()) > 0,
"capture layout is not ready for pointer routing"
);
for (index, stream) in streams.iter_mut().enumerate() {
let offset = 64 + index * 40;
stream.x = field(offset) as i32;
stream.y = field(offset + 4) as i32;
stream.width = field(offset + 8);
stream.height = field(offset + 12);
}
}
anyhow::ensure!(
streams
.iter()
.all(|stream| stream.width > 0 && stream.height > 0),
"missing pointer stream dimensions"
);
if streams
.iter()
.any(|stream| stream.x == i32::MIN || stream.y == i32::MIN)
{
let mut next_x = 0i64;
for stream in &mut streams {
stream.x = i32::try_from(next_x).context("input stream layout is too wide")?;
stream.y = 0;
next_x += i64::from(stream.width);
}
}
let left = streams
.iter()
.map(|stream| i64::from(stream.x))
.min()
.unwrap();
let top = streams
.iter()
.map(|stream| i64::from(stream.y))
.min()
.unwrap();
let right = streams
.iter()
.map(|stream| i64::from(stream.x) + i64::from(stream.width))
.max()
.unwrap();
let bottom = streams
.iter()
.map(|stream| i64::from(stream.y) + i64::from(stream.height))
.max()
.unwrap();
let global_x = left as f64 + x.clamp(0.0, 65535.0) * (right - left - 1) as f64 / 65535.0;
let global_y = top as f64 + y.clamp(0.0, 65535.0) * (bottom - top - 1) as f64 / 65535.0;
let mut best = None;
let mut distance = f64::INFINITY;
for stream in &streams {
let local_x = (global_x - f64::from(stream.x)).clamp(0.0, f64::from(stream.width - 1));
let local_y = (global_y - f64::from(stream.y)).clamp(0.0, f64::from(stream.height - 1));
let dx = global_x - f64::from(stream.x) - local_x;
let dy = global_y - f64::from(stream.y) - local_y;
let current = dx * dx + dy * dy;
// The producer draws later streams last when rectangles overlap.
if current <= distance {
best = Some((stream.node, local_x, local_y));
distance = current;
}
}
best.context("no input stream matches the pointer position")
}
use crate::capture::CaptureHelper as CaptureGuard;
/// Wayland input through the freedesktop RemoteDesktop portal. The actual
/// backend can be GNOME, KDE, COSMIC, or any future implementation; uur never
/// branches on a desktop name.
@@ -46,18 +136,21 @@ pub struct PortalBackend {
motion_queued: Arc<AtomicBool>,
held_keys: HashSet<i32>,
held_buttons: HashSet<i32>,
failure: Arc<Mutex<Option<String>>>,
}
impl PortalBackend {
pub fn connect() -> Result<Self> {
let mut config = crate::config::Config::load()?;
let restore_token = config.remote_desktop_restore_token.clone();
let (sender, receiver) = unbounded_channel();
let (ready_sender, ready_receiver) = mpsc::sync_channel(1);
let pending_motion = Arc::new(Mutex::new(None));
let motion_queued = Arc::new(AtomicBool::new(false));
let worker_motion = pending_motion.clone();
let worker_queued = motion_queued.clone();
let failure = Arc::new(Mutex::new(None));
let worker_failure = failure.clone();
let worker_config = config.clone();
std::thread::Builder::new()
.name("uur-portal-input".into())
@@ -70,12 +163,15 @@ impl PortalBackend {
runtime.block_on(portal_worker(
receiver,
ready_sender.clone(),
restore_token,
worker_config,
worker_motion,
worker_queued,
worker_failure.clone(),
))
});
if let Err(error) = result {
*worker_failure.lock().unwrap() = Some(error.to_string());
eprintln!("portal input worker stopped: {error:#}");
let _ = ready_sender.send(Err(error.to_string()));
}
})
@@ -87,7 +183,15 @@ impl PortalBackend {
.map_err(|error| anyhow!(error))?;
if fresh_token != config.remote_desktop_restore_token {
config.remote_desktop_restore_token = fresh_token;
config.store()?;
crate::config::Config::update(|stored| {
if stored.capture_source == config.capture_source
&& stored.capture_multiple == config.capture_multiple
&& stored.capture_backend == config.capture_backend
{
stored.remote_desktop_restore_token =
config.remote_desktop_restore_token.clone();
}
})?;
}
Ok(Self {
@@ -96,31 +200,139 @@ impl PortalBackend {
motion_queued,
held_keys: HashSet::new(),
held_buttons: HashSet::new(),
failure,
})
}
fn submit(&self, event: impl FnOnce(Reply) -> Event) -> Result<()> {
if let Some(error) = self.failure.lock().unwrap().clone() {
anyhow::bail!("portal input session failed: {error}");
}
let (reply_sender, reply_receiver) = mpsc::sync_channel(1);
self.sender
.send(event(reply_sender))
.map_err(|_| anyhow!("portal input session closed"))?;
reply_receiver
.recv_timeout(Duration::from_secs(3))
.context("portal input request timed out")?
.map_err(|error| anyhow!(error))
let result = reply_receiver
// Allow the helper shutdown, reconnect delay and portal restore
// to finish while keeping a stalled session bounded.
.recv_timeout(Duration::from_secs(10))
.context("portal input request timed out")
.and_then(|result| result.map_err(|error| anyhow!(error)));
if let Err(error) = &result {
*self.failure.lock().unwrap() = Some(error.to_string());
}
result
}
}
async fn portal_worker(
mut receiver: UnboundedReceiver<Event>,
receiver: UnboundedReceiver<Event>,
ready: SyncSender<std::result::Result<Option<String>, String>>,
configured: crate::config::Config,
pending_motion: Arc<Mutex<Option<Motion>>>,
motion_queued: Arc<AtomicBool>,
failure: Arc<Mutex<Option<String>>>,
) -> Result<()> {
let mut worker = PortalWorker {
restore_token: configured.remote_desktop_restore_token.clone(),
configured,
receiver,
ready,
pending_motion,
motion_queued,
failure,
initial: true,
opened: false,
held_keys: HashSet::new(),
held_buttons: HashSet::new(),
};
let mut display_changes = crate::capture::watch_display_changes().await;
let mut failures = 0u8;
loop {
worker.opened = false;
match portal_session_worker(&mut worker, &mut display_changes).await {
Ok(SessionExit::Shutdown) => return Ok(()),
Ok(SessionExit::Reconnect { layout, healthy }) => {
if healthy {
failures = 0;
}
if !layout {
failures += 1;
}
anyhow::ensure!(failures <= 3, "portal capture recovery exhausted");
anyhow::ensure!(worker.restore_token.is_some(),
"portal capture changed without a restorable grant; restart uur run to share again");
eprintln!(
"restoring the consented RemoteDesktop session after {}",
if layout {
"a display change"
} else {
"a capture failure"
}
);
tokio::time::sleep(Duration::from_millis(if layout { 250 } else { 1000 })).await;
while tokio::time::timeout(
Duration::from_millis(100),
crate::capture::next_display_change(&mut display_changes),
)
.await
.is_ok()
{}
}
Err(error)
if !worker.initial
&& !worker.opened
&& failures < 3
&& worker.restore_token.is_some()
&& !crate::capture::user_cancelled(&error) =>
{
failures += 1;
eprintln!("portal restore {failures}/3 failed: {error:#}");
tokio::time::sleep(Duration::from_secs(1)).await;
}
Err(error) => return Err(error),
}
}
}
struct PortalWorker {
receiver: UnboundedReceiver<Event>,
ready: SyncSender<std::result::Result<Option<String>, String>>,
configured: crate::config::Config,
restore_token: Option<String>,
pending_motion: Arc<Mutex<Option<Motion>>>,
motion_queued: Arc<AtomicBool>,
) -> Result<()> {
failure: Arc<Mutex<Option<String>>>,
initial: bool,
opened: bool,
held_keys: HashSet<i32>,
held_buttons: HashSet<i32>,
}
enum SessionExit {
Shutdown,
Reconnect { layout: bool, healthy: bool },
}
async fn portal_session_worker(
worker: &mut PortalWorker,
display_changes: &mut Option<zbus::proxy::SignalStream<'static>>,
) -> Result<SessionExit> {
let PortalWorker {
receiver,
ready,
configured,
restore_token,
pending_motion,
motion_queued,
failure,
initial,
opened,
held_keys,
held_buttons,
} = worker;
let portal = RemoteDesktop::new().await?;
let screencast = Screencast::new().await?;
let configured = crate::config::Config::load()?;
let source = crate::capture::source_type(&configured.capture_source);
let available = portal.available_device_types().await?;
if !available.contains(DeviceType::Keyboard) || !available.contains(DeviceType::Pointer) {
@@ -128,6 +340,18 @@ async fn portal_worker(
}
let session = portal.create_session().await?;
let serialized = serde_json::to_value(&session)?;
let session_proxy = zbus::Proxy::new_owned(
portal.connection().clone(),
"org.freedesktop.portal.Desktop",
serialized
.as_str()
.context("invalid portal session path")?
.to_owned(),
"org.freedesktop.portal.Session",
)
.await?;
let mut closed = session_proxy.receive_signal("Closed").await?;
portal
.select_devices(
&session,
@@ -142,27 +366,110 @@ async fn portal_worker(
&session,
crate::capture::preferred_cursor_mode(&screencast).await?,
source.into(),
false,
configured.capture_multiple && configured.capture_source == "monitor",
None,
PersistMode::DoNot,
)
.await?
.response()?;
let selection = portal.start(&session, None).await?.response()?;
let stream = selection
*opened = true;
anyhow::ensure!(
selection.devices().contains(DeviceType::Keyboard)
&& selection.devices().contains(DeviceType::Pointer),
"RemoteDesktop grant did not include keyboard and pointer"
);
let selected_streams = selection
.streams()
.and_then(|streams| streams.first())
.context("RemoteDesktop portal returned no screen stream")?;
let stream_id = stream.pipe_wire_node_id();
let (logical_width, logical_height) = stream.size().unwrap_or((65535, 65535));
.context("RemoteDesktop portal returned no screen streams")?;
anyhow::ensure!(
!selected_streams.is_empty(),
"RemoteDesktop portal returned no screen streams"
);
let streams = selected_streams
.iter()
.map(|stream| {
let (x, y) = stream.position().unwrap_or((i32::MIN, i32::MIN));
let (width, height) = stream.size().unwrap_or((0, 0));
crate::capture::CaptureStream {
node: stream.pipe_wire_node_id(),
x,
y,
width: u32::try_from(width).unwrap_or(0),
height: u32::try_from(height).unwrap_or(0),
}
})
.collect::<Vec<_>>();
let pipewire = screencast.open_pipe_wire_remote(&session).await?;
let _capture = CaptureGuard(crate::capture::spawn_helper(pipewire, stream_id)?);
let mut capture = CaptureGuard(crate::capture::spawn_helper(pipewire, &streams)?);
let token = selection.restore_token().map(ToOwned::to_owned);
ready
.send(Ok(token))
.map_err(|_| anyhow!("portal setup receiver closed"))?;
while let Some(event) = receiver.recv().await {
*restore_token = token.clone();
if *initial {
ready
.send(Ok(token))
.map_err(|_| anyhow!("portal setup receiver closed"))?;
*initial = false;
} else {
crate::config::Config::update(|stored| {
if stored.capture_source == configured.capture_source
&& stored.capture_multiple == configured.capture_multiple
&& stored.capture_backend == configured.capture_backend
{
stored.remote_desktop_restore_token = token;
}
})?;
for key in held_keys.iter().copied() {
portal
.notify_keyboard_keycode(&session, key, KeyState::Pressed)
.await?;
}
for button in held_buttons.iter().copied() {
portal
.notify_pointer_button(&session, button, KeyState::Pressed)
.await?;
}
}
let mut healthy_since = None;
let mut health_tick = tokio::time::interval(Duration::from_millis(50));
let outcome: Result<SessionExit> = async {
loop {
if let Some(error) = failure.lock().unwrap().clone() {
anyhow::bail!("portal input session failed: {error}");
}
let event = tokio::select! {
biased;
_ = closed.next() => {
let layout = tokio::time::timeout(Duration::from_millis(250),
crate::capture::next_display_change(display_changes)).await.is_ok();
anyhow::ensure!(layout, "screen sharing was closed by the portal; restart uur run to share again");
break Ok(SessionExit::Reconnect { layout: true,
healthy: healthy_since.is_some_and(|since: Instant| since.elapsed() >= Duration::from_secs(30)) });
},
_ = crate::capture::next_display_change(display_changes) => {
break Ok(SessionExit::Reconnect { layout: true,
healthy: healthy_since.is_some_and(|since: Instant| since.elapsed() >= Duration::from_secs(30)) });
},
_ = health_tick.tick() => {
if crate::capture::helper_stalled(&crate::capture::frame_path()?) {
eprintln!("portal capture helper heartbeat expired; reconnecting");
break Ok(SessionExit::Reconnect { layout: false,
healthy: healthy_since.is_some_and(|since: Instant| since.elapsed() >= Duration::from_secs(30)) });
}
if crate::capture::has_frame(&crate::capture::frame_path()?) && healthy_since.is_none() {
healthy_since = Some(Instant::now());
}
if let Some(status) = capture.0.try_wait()? {
anyhow::ensure!(status.code() == Some(75), "portal capture helper exited ({status})");
break Ok(SessionExit::Reconnect { layout: false,
healthy: healthy_since.is_some_and(|since: Instant| since.elapsed() >= Duration::from_secs(30)) });
}
continue;
},
event = receiver.recv() => match event {
Some(event) => event,
None => break Ok(SessionExit::Shutdown),
},
};
match event {
Event::Key(code, down, reply) => {
let result = portal
@@ -176,6 +483,9 @@ async fn portal_worker(
},
)
.await;
if result.is_ok() {
if down { held_keys.insert(code); } else { held_keys.remove(&code); }
}
answer(reply, result)?;
}
Event::Button(button, down, reply) => {
@@ -190,25 +500,22 @@ async fn portal_worker(
},
)
.await;
if result.is_ok() {
if down { held_buttons.insert(button); } else { held_buttons.remove(&button); }
}
answer(reply, result)?;
}
Event::FlushMotion => loop {
Event::FlushMotion => {
motion_queued.store(false, Ordering::Release);
let motion = pending_motion.lock().unwrap().take();
let Some(motion) = motion else {
motion_queued.store(false, Ordering::Release);
let has_new_motion = pending_motion.lock().unwrap().is_some();
if has_new_motion
&& motion_queued
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
.is_ok()
{
continue;
}
break;
continue;
};
let result = if motion.absolute {
let logical_x = motion.x.clamp(0.0, 65535.0) * logical_width as f64 / 65535.0;
let logical_y = motion.y.clamp(0.0, 65535.0) * logical_height as f64 / 65535.0;
let (stream_id, logical_x, logical_y) = match absolute_target(&streams, motion.x, motion.y) {
Ok(target) => target,
Err(error) => { eprintln!("portal pointer layout not ready: {error}"); continue; }
};
portal
.notify_pointer_motion_absolute(&session, stream_id, logical_x, logical_y)
.await
@@ -229,10 +536,14 @@ async fn portal_worker(
.await,
)?;
}
Event::Shutdown => break,
Event::Shutdown => break Ok(SessionExit::Shutdown),
}
}
Ok(())
}.await;
drop(closed);
drop(capture);
let _ = tokio::time::timeout(Duration::from_secs(2), session.close()).await;
outcome
}
fn answer<T>(reply: Reply, result: std::result::Result<T, ashpd::Error>) -> Result<()> {
@@ -246,6 +557,14 @@ impl InputBackend for PortalBackend {
"portal"
}
fn check_health(&self) -> Result<()> {
if let Some(error) = self.failure.lock().unwrap().clone() {
anyhow::bail!("portal input worker failed: {error}");
}
anyhow::ensure!(!self.sender.is_closed(), "portal input worker stopped");
Ok(())
}
fn key(&mut self, vkey: u16, down: bool) -> Result<()> {
let code = super::uinput::vk_to_linux_key(vkey) as i32;
if code == 0 {
@@ -342,7 +661,7 @@ impl Drop for PortalBackend {
#[cfg(test)]
mod tests {
use super::{merge_motion, Motion};
use super::{absolute_target, merge_motion, Motion};
#[test]
fn absolute_motion_keeps_only_the_latest_position() {
@@ -358,6 +677,35 @@ mod tests {
};
merge_motion(&mut pending, latest);
assert_eq!(pending, Some(latest));
let streams = [
crate::capture::CaptureStream {
node: 41,
x: -1280,
y: 0,
width: 1280,
height: 720,
},
crate::capture::CaptureStream {
node: 42,
x: 0,
y: 0,
width: 1920,
height: 1080,
},
];
assert_eq!(absolute_target(&streams, 0.0, 0.0).unwrap(), (41, 0.0, 0.0));
assert_eq!(
absolute_target(&streams, 65535.0, 65535.0).unwrap(),
(42, 1919.0, 1079.0)
);
let target = absolute_target(&streams, 1280.0 * 65535.0 / 3199.0, 0.0).unwrap();
assert_eq!(target.0, 42);
assert!(target.1.abs() < 1e-6);
assert!(absolute_target(&streams, f64::NAN, 0.0).is_err());
let mut gap = streams;
gap[1].x = 200;
let target = absolute_target(&gap, 1280.0 * 65535.0 / 3399.0, 0.0).unwrap();
assert_eq!(target, (41, 1279.0, 0.0));
}
#[test]
+43
View File
@@ -15,6 +15,49 @@ const FAKE_MOTION: u8 = 6;
/// XTEST detail for motion: 0 = absolute, 1 = relative.
const MOTION_RELATIVE: u8 = 1;
/// Observe this display's effective lock state without changing any XKB state.
pub(crate) fn lock_state() -> Result<String> {
let (connection, screen) = x11rb::connect(None)?;
let setup = connection.setup();
let state = u16::from(
connection
.query_pointer(setup.roots[screen].root)?
.reply()?
.mask,
);
let modifiers = connection.get_modifier_mapping()?.reply()?;
let keys = connection
.get_keyboard_mapping(setup.min_keycode, setup.max_keycode - setup.min_keycode + 1)?
.reply()?;
let width = modifiers.keycodes.len() / 8;
let mut num = 0u16;
let mut scroll = 0u16;
for (index, code) in modifiers.keycodes.iter().copied().enumerate() {
if code < setup.min_keycode || width == 0 {
continue;
}
let offset = usize::from(code - setup.min_keycode) * usize::from(keys.keysyms_per_keycode);
let symbols = keys
.keysyms
.get(offset..offset + usize::from(keys.keysyms_per_keycode));
if let Some(symbols) = symbols {
if symbols.contains(&0xff7f) {
num |= 1 << (index / width);
}
if symbols.contains(&0xff14) {
scroll |= 1 << (index / width);
}
}
}
let flag = |mask| if state & mask != 0 { "on" } else { "off" };
Ok(format!(
"CapsLock={} NumLock={} ScrollLock={} (Num mask={num:#x}, Scroll mask={scroll:#x})",
flag(2),
flag(num),
flag(scroll)
))
}
/// X11 XTest injection. Works on every X11 session; on XWayland it reaches
/// XWayland-native windows only, which is why the portal backend is
/// preferred on Wayland-only sessions.
+1 -1
View File
@@ -66,7 +66,7 @@ pub fn prepare(prefix: &Path, proxy: &Path) -> Result<usize> {
std::fs::write(root.with_extension("keys"), keys.join("\n") + "\n")?;
let registry_path = prefix.join("drive_c/uur-linux-apps.reg");
write_utf16_registry(&registry_path, &registry)?;
let status = Command::new("wine")
let status = crate::wine::command(prefix)?
.env("WINEPREFIX", prefix)
.env("WINEDEBUG", "-all")
.args(["reg", "import"])
+55 -17
View File
@@ -89,6 +89,11 @@ enum Command {
#[command(subcommand)]
action: DisplayAction,
},
/// Select Wine's default microphone/system-audio capture source.
Audio {
#[command(subcommand)]
action: AudioAction,
},
/// Internal input bridge process. Not part of the public CLI.
#[command(hide = true, name = "__bridge")]
InternalBridge,
@@ -157,7 +162,12 @@ enum DisplayAction {
/// Show the configured source type.
Status,
/// Use a monitor, one window, or a portal-provided virtual display.
Source { source: DisplaySource },
Source {
source: DisplaySource,
/// Allow selecting several monitors in the next portal consent dialog.
#[arg(long)]
multiple: bool,
},
/// Choose portal capture, or explicitly enable direct Wayland monitor capture.
Backend {
backend: CaptureBackend,
@@ -169,6 +179,14 @@ enum DisplayAction {
Outputs,
}
#[derive(Subcommand)]
enum AudioAction {
/// List real capture inputs and system-audio monitor sources.
Sources,
/// Use system sound, a source name, or default to follow the existing environment.
Source { source: String },
}
#[derive(Clone, ValueEnum)]
enum CaptureBackend {
Portal,
@@ -207,6 +225,10 @@ fn main() -> Result<()> {
Command::Doctor => doctor::report(),
Command::Pointer => bridge::print_pointer(),
Command::Capture => capture::run_standalone(),
Command::Audio { action } => match action {
AudioAction::Sources => audio::print_sources(),
AudioAction::Source { source } => audio::configure_source(&source),
},
Command::Run => session::start(),
Command::Stop => session::stop(),
Command::Uninstall {
@@ -228,29 +250,32 @@ fn main() -> Result<()> {
if matches!(action, DisplayAction::Outputs) {
return capture::list_wayland_outputs();
}
let mut config = config::Config::load()?;
let config = config::Config::load()?;
match action {
DisplayAction::Status => {
println!("{}", t!("display.status", source = config.capture_source));
println!("capture backend: {}", config.capture_backend);
println!("multiple monitors: {}", config.capture_multiple);
if let Some(output) = &config.capture_output {
println!("output: {output}");
}
Ok(())
}
DisplayAction::Backend { backend, output } => {
config.capture_backend = match backend {
let backend = match backend {
CaptureBackend::Portal => "portal",
CaptureBackend::Wayland => "wayland",
}
.into();
config.capture_output = output;
if config.capture_backend == "wayland" {
config.capture_source = "monitor".into();
}
config.capture_restore_token = None;
config.remote_desktop_restore_token = None;
config.store()?;
};
let config = config::Config::update(|stored| {
stored.capture_backend = backend.into();
stored.capture_output = output;
if backend == "wayland" {
stored.capture_source = "monitor".into();
stored.capture_multiple = false;
}
stored.capture_restore_token = None;
stored.remote_desktop_restore_token = None;
})?;
println!(
"{}",
if config.capture_backend == "wayland" {
@@ -262,12 +287,25 @@ fn main() -> Result<()> {
Ok(())
}
DisplayAction::Outputs => unreachable!(),
DisplayAction::Source { source } => {
config.capture_source = source.as_str().to_string();
config.capture_restore_token = None;
config.remote_desktop_restore_token = None;
config.store()?;
DisplayAction::Source { source, multiple } => {
anyhow::ensure!(
!multiple || matches!(&source, DisplaySource::Monitor),
"--multiple is supported only for monitor sources"
);
anyhow::ensure!(
!multiple || config.capture_backend == "portal",
"multi-monitor selection requires the portal capture backend"
);
config::Config::update(|stored| {
stored.capture_source = source.as_str().to_string();
stored.capture_multiple = multiple;
stored.capture_restore_token = None;
stored.remote_desktop_restore_token = None;
})?;
println!("{}", t!("display.changed", source = source.as_str()));
println!(
"multiple monitors: {multiple}; selection applies on the next session"
);
Ok(())
}
}
+2 -2
View File
@@ -2,7 +2,7 @@
use anyhow::{Context, Result};
use std::path::Path;
use std::process::{Command, Stdio};
use std::process::Stdio;
pub fn sync(prefix: &Path) -> Result<Option<String>> {
let http = environment_proxy("HTTP_PROXY", "http_proxy");
@@ -60,7 +60,7 @@ fn proxy_endpoint(value: &str) -> Option<String> {
}
fn reg_add(prefix: &Path, value: &str, kind: &str, data: &str) -> Result<()> {
let status = Command::new("wine")
let status = crate::wine::command(prefix)?
.env("WINEPREFIX", prefix)
.env("WINEDEBUG", "-all")
.args([
+196 -21
View File
@@ -71,6 +71,93 @@ fn hook_source_dir() -> Result<PathBuf> {
hook_dir()
}
/// Path of the lock-modifier shim that is LD_PRELOADed into the managed Wine
/// processes. Layout mirrors the other native helpers (installed under
/// `<prefix>/lib/uur`, found next to the build tree during development).
pub(crate) fn lock_mods_shim() -> Option<PathBuf> {
let mut candidates = Vec::new();
if let Ok(exe) = std::env::current_exe() {
if let Some(dir) = exe.parent() {
candidates.push(dir.join("../lib/uur/libuur-lock-mods.so"));
candidates.push(dir.join("../../build/compat/libuur-lock-mods.so"));
candidates.push(dir.join("libuur-lock-mods.so"));
}
}
candidates.push(PathBuf::from("/usr/lib/uur/libuur-lock-mods.so"));
candidates.push(PathBuf::from("/usr/local/lib/uur/libuur-lock-mods.so"));
if let Ok(home) = std::env::var("HOME") {
candidates.push(PathBuf::from(&home).join(".local/lib/uur/libuur-lock-mods.so"));
}
if let Ok(directory) = std::env::current_dir() {
candidates.push(directory.join("build/compat/libuur-lock-mods.so"));
}
candidates.into_iter().find(|path| path.is_file())
}
/// Environment for the managed client: keep Wine from forging lock-key
/// events when X11 reports a locked modifier that the client's keyboard hook
/// swallows. See `compat/uur-lock-mods.c` for the full rationale.
///
/// This only affects the child process environment; the X server, the
/// compositor and the user's NumLock setting are never touched.
fn lock_mods_mask(config: &Config) -> Result<Option<String>> {
let Some(mask) = config.strip_lock_mods.as_deref().map(str::trim) else {
return Ok(None);
};
if mask == "auto" {
return Ok(Some(mask.to_string()));
}
let value = match mask.strip_prefix("0x").or_else(|| mask.strip_prefix("0X")) {
Some(hex) => u8::from_str_radix(hex, 16),
None => mask.parse::<u8>(),
}
.context("strip_lock_mods must be auto, 0, or an 8-bit decimal/hex mask")?;
Ok((value != 0).then(|| format!("0x{value:x}")))
}
pub(crate) fn apply_lock_mods_env(command: &mut Command, config: &Config) -> Result<()> {
apply_lock_mods_with_shim(
command,
config,
lock_mods_shim().as_deref(),
std::env::var_os("LD_PRELOAD").as_deref(),
)
}
fn apply_lock_mods_with_shim(
command: &mut Command,
config: &Config,
shim: Option<&Path>,
existing: Option<&std::ffi::OsStr>,
) -> Result<()> {
let Some(mask) = lock_mods_mask(config)? else {
return Ok(());
};
let Some(shim) = shim else {
eprintln!("lock-modifier shim not found; leaving Wine keyboard handling untouched");
return Ok(());
};
// The dynamic loader splits LD_PRELOAD on ':' and whitespace and offers no
// escaping, so a path containing either cannot be expressed there.
let Some(shim_text) = shim.to_str() else {
eprintln!("lock-modifier shim path must be valid UTF-8; skipping it");
return Ok(());
};
if shim_text.contains(':') || shim_text.chars().any(|ch| ch.is_ascii_whitespace()) {
eprintln!("lock-modifier shim path cannot be expressed in LD_PRELOAD; skipping it");
return Ok(());
}
let mut preload = shim.as_os_str().to_os_string();
if let Some(existing) = existing.filter(|value| !value.is_empty()) {
preload.push(":");
preload.push(existing);
}
command
.env("UUR_STRIP_LOCK_MODS", mask)
.env("LD_PRELOAD", preload);
Ok(())
}
static DEPLOY_TEMP_SEQUENCE: AtomicU64 = AtomicU64::new(0);
/// Replace a managed prefix file without inheriting the source's permissions.
@@ -213,8 +300,8 @@ fn deploy_terminal_proxy(install_dir: &Path) -> Result<PathBuf> {
/// bootstrapper ships inside the client's bin directory.
const WEBVIEW2_DLL_OVERRIDES: &str = "wtsapi32=b,wevtapi=b";
fn webview2_install_command(prefix: &Path, bootstrapper: &Path) -> Command {
let mut command = Command::new("wine");
fn webview2_install_command(prefix: &Path, bootstrapper: &Path) -> Result<Command> {
let mut command = crate::wine::command(prefix)?;
command
.env("WINEPREFIX", prefix)
.env("WINEDEBUG", wine_debug())
@@ -224,7 +311,7 @@ fn webview2_install_command(prefix: &Path, bootstrapper: &Path) -> Command {
.env("WINEDLLOVERRIDES", WEBVIEW2_DLL_OVERRIDES)
.arg(bootstrapper)
.args(["/silent", "/install"]);
command
Ok(command)
}
fn ensure_webview2(prefix: &Path, install_dir: &Path) -> Result<()> {
@@ -237,7 +324,7 @@ fn ensure_webview2(prefix: &Path, install_dir: &Path) -> Result<()> {
anyhow::bail!("{}", t!("session.webview_missing"));
}
println!("{}", t!("session.installing_webview"));
let status = webview2_install_command(prefix, &bootstrapper)
let status = webview2_install_command(prefix, &bootstrapper)?
.status()
.context("running the WebView2 bootstrapper")?;
if !status.success() || !crate::wine::webview2_installed(prefix) {
@@ -404,6 +491,7 @@ pub fn start() -> Result<()> {
let _session_lock = acquire_session_lock()?;
rotate_session_log();
let config = Config::load()?;
let _ = lock_mods_mask(&config)?;
let on_wayland = std::env::var_os("WAYLAND_DISPLAY").is_some()
|| std::env::var("XDG_SESSION_TYPE").is_ok_and(|session| session == "wayland");
let prefix = data_dir()?.join("wine");
@@ -413,6 +501,15 @@ pub fn start() -> Result<()> {
anyhow::bail!("{}", t!("setup.service_incomplete"));
}
let mut resources = SessionResources::new(prefix.clone());
let selected_audio = crate::audio::capture_source(config.audio_capture_source.as_deref())?;
if selected_audio.is_some()
|| (lock_mods_mask(&config)?.is_some() && lock_mods_shim().is_some())
{
// A server left by setup or an older client has the old environment.
// The session lock excludes a running uur session; restart only this
// managed prefix so its SCM-spawned clients inherit the same shim.
stop_wineserver(&prefix);
}
if let Some(dpi) = xft_dpi() {
match crate::wine::set_log_pixels(&prefix, dpi) {
Ok(()) => println!("{}", t!("session.wine_dpi", dpi = dpi)),
@@ -532,7 +629,7 @@ pub fn start() -> Result<()> {
// Windows process table without repeatedly initializing Wine utilities.
use std::os::unix::process::CommandExt;
let marker = hook_dir()?.join("winlogon.exe");
let mut monitor = Command::new("wine")
let mut monitor = crate::wine::command(&prefix)?
.env("WINEPREFIX", &prefix)
.env("WINEDEBUG", wine_debug())
.env("WINEDLLOVERRIDES", "wevtapi=b;wtsapi32=b")
@@ -561,7 +658,7 @@ pub fn start() -> Result<()> {
// waits forever.
if !service_running(&prefix) {
let _ = run_with_timeout(
Command::new("wine")
crate::wine::command(&prefix)?
.env("WINEPREFIX", &prefix)
.env("WINEDEBUG", wine_debug())
.env("WINEDLLOVERRIDES", "wevtapi=b;wtsapi32=b")
@@ -585,7 +682,8 @@ pub fn start() -> Result<()> {
println!("{}", t!("session.launching_client"));
let ui = install_dir.join("GameViewer.exe");
let launch = || -> Result<Child> {
Command::new("wine")
let mut command = crate::wine::command(&prefix)?;
command
.env("WINEPREFIX", &prefix)
.env("WINEDEBUG", wine_debug())
.current_dir(&install_dir)
@@ -595,9 +693,8 @@ pub fn start() -> Result<()> {
.env("UUR_BRIDGE_TOKEN", config.bridge_token.clone())
.stdout(Stdio::from(log.try_clone()?))
.stderr(Stdio::from(log.try_clone()?))
.arg(ui.to_string_lossy().as_ref())
.spawn()
.context("launching the managed client")
.arg(ui.to_string_lossy().as_ref());
command.spawn().context("launching the managed client")
};
// Start the client detached: it IS the session's lifetime anchor.
@@ -758,8 +855,11 @@ fn run_with_timeout(command: &mut Command, timeout: std::time::Duration) -> Opti
/// True when the Windows service reports STATE 4 (RUNNING) via `sc query`.
fn service_running(prefix: &Path) -> bool {
let Ok(mut command) = crate::wine::command(prefix) else {
return false;
};
let output = run_with_timeout(
Command::new("wine")
command
.env("WINEPREFIX", prefix)
.env("WINEDEBUG", wine_debug())
.env("WINEDLLOVERRIDES", "wevtapi=b;wtsapi32=b")
@@ -817,7 +917,8 @@ pub(crate) fn show_client() -> Result<()> {
if process_start_time(pid).ok() != Some(start) {
anyhow::bail!("managed session is not running");
}
let mut child = Command::new("wine")
let mut command = crate::wine::command(&prefix)?;
command
.env("WINEPREFIX", prefix)
.env("WINEDEBUG", wine_debug())
.env("WINEDLLOVERRIDES", "wevtapi=n,wtsapi32=n")
@@ -825,9 +926,8 @@ pub(crate) fn show_client() -> Result<()> {
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.arg(install.join("GameViewer.exe"))
.spawn()
.context("showing the managed client")?;
.arg(install.join("GameViewer.exe"));
let mut child = command.spawn().context("showing the managed client")?;
thread::spawn(move || {
let _ = child.wait();
});
@@ -879,7 +979,7 @@ fn process_start_time(pid: u32) -> Result<u64> {
}
fn frame_path() -> Result<PathBuf> {
Ok(crate::config::runtime_dir()?.join("frames.v1"))
Ok(crate::config::runtime_dir()?.join("frames.v3"))
}
fn unix_path_to_wine(path: &Path) -> String {
@@ -1037,9 +1137,16 @@ fn wine_process_matches(directory: &Path, prefix: &Path) -> bool {
let Ok(executable) = std::fs::read_link(directory.join("exe")) else {
return false;
};
let name = executable
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default();
let name = name.strip_suffix(" (deleted)").unwrap_or(name);
let name = name.strip_prefix('.').unwrap_or(name);
let name = name.strip_suffix("-wrapped").unwrap_or(name);
if !matches!(
executable.file_name().and_then(|name| name.to_str()),
Some("wine" | "wine64" | "wine-preloader" | "wine64-preloader" | "wineserver")
name,
"wine" | "wine64" | "wine-preloader" | "wine64-preloader" | "wineserver"
) {
return false;
}
@@ -1109,6 +1216,73 @@ mod tests {
}
}
fn command_env(command: &Command, key: &str) -> Option<String> {
command
.get_envs()
.find(|(name, _)| *name == OsStr::new(key))
.and_then(|(_, value)| value)
.map(|value| value.to_string_lossy().into_owned())
}
#[test]
fn lock_modifier_env_is_skipped_when_disabled() {
for mask in [None, Some("0".to_string()), Some("0x00".to_string())] {
let config = Config {
strip_lock_mods: mask,
..Config::default()
};
let mut command = Command::new("wine");
apply_lock_mods_with_shim(&mut command, &config, Some(Path::new("/shim.so")), None)
.unwrap();
assert!(command_env(&command, "LD_PRELOAD").is_none());
assert!(command_env(&command, "UUR_STRIP_LOCK_MODS").is_none());
}
}
#[test]
fn lock_modifier_env_sets_mask_and_preloads_shim() {
let config = Config {
strip_lock_mods: Some("0x50".into()),
..Config::default()
};
let mut command = Command::new("wine");
apply_lock_mods_with_shim(
&mut command,
&config,
Some(Path::new("/build/compat/libuur-lock-mods.so")),
Some(OsStr::new("/nonexistent/first.so")),
)
.unwrap();
assert_eq!(
command_env(&command, "UUR_STRIP_LOCK_MODS").as_deref(),
Some("0x50")
);
let preload = command_env(&command, "LD_PRELOAD").expect("preload set");
assert!(preload.ends_with("/build/compat/libuur-lock-mods.so:/nonexistent/first.so"));
}
#[test]
fn lock_modifier_env_reports_missing_shim_without_failing() {
let config = Config::default();
let mut command = Command::new("wine");
apply_lock_mods_with_shim(&mut command, &config, None, None).unwrap();
let mask = command_env(&command, "UUR_STRIP_LOCK_MODS");
let preload = command_env(&command, "LD_PRELOAD");
assert_eq!(mask.is_some(), preload.is_some());
assert!(mask.is_none());
for path in [
"/bad path/shim.so",
"/bad\npath/shim.so",
"/bad:path/shim.so",
] {
let mut command = Command::new("wine");
apply_lock_mods_with_shim(&mut command, &config, Some(Path::new(path)), None).unwrap();
assert!(command_env(&command, "LD_PRELOAD").is_none());
assert!(command_env(&command, "UUR_STRIP_LOCK_MODS").is_none());
}
}
#[test]
fn deploy_file_repairs_readonly_targets_and_is_repeatable() {
let directory = TestDirectory::create();
@@ -1178,7 +1352,8 @@ mod tests {
let command = webview2_install_command(
Path::new("/tmp/uur-wine-prefix"),
Path::new("/tmp/MicrosoftEdgeWebview2Setup.exe"),
);
)
.unwrap();
let overrides = command
.get_envs()
.find_map(|(name, value)| (name == OsStr::new("WINEDLLOVERRIDES")).then_some(value))
@@ -1202,8 +1377,8 @@ mod tests {
#[test]
fn runtime_path_converts_to_a_wine_z_path() {
assert_eq!(
unix_path_to_wine(Path::new("/run/user/1000/uur/frames.v1")),
r"Z:\run\user\1000\uur\frames.v1"
unix_path_to_wine(Path::new("/run/user/1000/uur/frames.v3")),
r"Z:\run\user\1000\uur\frames.v3"
);
}
}
+1
View File
@@ -254,6 +254,7 @@ impl Plan {
"lib/uur/user-install",
"lib/uur/uur-pw-capture",
"lib/uur/uur-wl-capture",
"lib/uur/libuur-lock-mods.so",
"lib/uur/hook/wevtapi.dll",
"lib/uur/hook/uur-hook.dll",
"lib/uur/hook/wtsapi32.dll",
+2 -1
View File
@@ -71,7 +71,8 @@ fn extract_version(filename: &str) -> Option<String> {
fn installed_version() -> Option<String> {
let prefix = crate::config::data_dir().ok()?.join("wine");
crate::wine::find_client_dir(&prefix)?;
let output = Command::new("wine")
let output = crate::wine::command(&prefix)
.ok()?
.env("WINEPREFIX", &prefix)
.env("WINEDEBUG", "-all")
.args([
+1 -1
View File
@@ -90,7 +90,7 @@ pub fn sync(prefix: &Path) -> Result<Option<Wallpaper>> {
};
let compatible = windows_compatible_path(&wallpaper.path)?;
let windows_path = format!("Z:{}", compatible.display()).replace('/', "\\");
let status = Command::new("wine")
let status = crate::wine::command(prefix)?
.env("WINEPREFIX", prefix)
.env("WINEDEBUG", "-all")
.args([
+25 -9
View File
@@ -9,6 +9,22 @@ const OFFICIAL_DOWNLOAD_URL: &str = "https://api.nrd.nie.163.com/api/v1/release/
const EULA_URL: &str = "https://uuyc.163.com/contact/20240402/40294_1146065.html";
const SETUP_DLL_OVERRIDES: &str = "wevtapi=b,wtsapi32=b";
/// Apply process-local compatibility to every managed-prefix Wine launch,
/// including the first utility that starts wineserver and its service manager.
pub(crate) fn command(prefix: &Path) -> Result<Command> {
let mut command = Command::new("wine");
command.env("WINEPREFIX", prefix);
let managed = crate::config::data_dir().is_ok_and(|data| data.join("wine") == prefix);
if managed {
let config = Config::load()?;
crate::audio::apply_capture_env(&mut command, &config)?;
if let Err(error) = crate::session::apply_lock_mods_env(&mut command, &config) {
eprintln!("Wine keyboard compatibility unavailable: {error}");
}
}
Ok(command)
}
struct SetupPrefix(PathBuf);
impl Drop for SetupPrefix {
@@ -90,7 +106,7 @@ pub fn setup(accept_eula: bool, repair: bool) -> Result<()> {
}
println!("{}", t!("setup.installing_client"));
let status = Command::new("wine")
let status = command(&prefix)?
.env("WINEPREFIX", &prefix)
.env("WINEDEBUG", wine_debug())
.env("WINEDLLOVERRIDES", SETUP_DLL_OVERRIDES)
@@ -174,7 +190,7 @@ pub fn client_service_registered(prefix: &Path) -> bool {
}
fn query_client_service(prefix: &Path) -> Result<bool> {
let output = Command::new("wine")
let output = command(prefix)?
.env("WINEPREFIX", prefix)
.env("WINEDEBUG", wine_debug())
.env("WINEDLLOVERRIDES", SETUP_DLL_OVERRIDES)
@@ -260,7 +276,7 @@ pub fn apply_registry_compat(prefix: &Path) -> Result<()> {
format!("HKCU\\{key}")
};
for (value, reg_type, data) in &values {
let status = Command::new("wine")
let status = command(prefix)?
.env("WINEPREFIX", prefix)
.env("WINEDEBUG", wine_debug())
.args([
@@ -280,7 +296,7 @@ pub fn apply_registry_compat(prefix: &Path) -> Result<()> {
}
pub fn set_dll_override(prefix: &Path, name: &str, mode: &str) -> Result<()> {
let status = Command::new("wine")
let status = command(prefix)?
.env("WINEPREFIX", prefix)
.env("WINEDEBUG", wine_debug())
.args([
@@ -307,7 +323,7 @@ pub fn set_dll_override(prefix: &Path, name: &str, mode: &str) -> Result<()> {
}
pub fn set_log_pixels(prefix: &Path, dpi: u32) -> Result<()> {
let status = log_pixels_command(prefix, dpi)
let status = log_pixels_command(prefix, dpi)?
.status()
.context("setting the Wine system DPI")?;
if !status.success() {
@@ -316,8 +332,8 @@ pub fn set_log_pixels(prefix: &Path, dpi: u32) -> Result<()> {
Ok(())
}
fn log_pixels_command(prefix: &Path, dpi: u32) -> Command {
let mut command = Command::new("wine");
fn log_pixels_command(prefix: &Path, dpi: u32) -> Result<Command> {
let mut command = command(prefix)?;
command
.env("WINEPREFIX", prefix)
.env("WINEDEBUG", wine_debug())
@@ -336,7 +352,7 @@ fn log_pixels_command(prefix: &Path, dpi: u32) -> Command {
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
command
Ok(command)
}
fn wine_debug() -> String {
@@ -832,7 +848,7 @@ mod tests {
#[test]
fn system_dpi_command_writes_winecfg_logpixels_value() {
let command = log_pixels_command(Path::new("/tmp/wine-prefix"), 192);
let command = log_pixels_command(Path::new("/tmp/wine-prefix"), 192).unwrap();
let args = command
.get_args()
.map(|arg| arg.to_string_lossy().into_owned())