From 98af6fce285435a06f80ef603395d754168002b7 Mon Sep 17 00:00:00 2001 From: falcon-jin <48880836+falcon-jin@users.noreply.github.com> Date: Mon, 9 Nov 2026 23:26:07 +0800 Subject: [PATCH] feat(capture): support consented multi-monitor desktops with recovery (#32) --- capture/uur-pw-capture.c | 631 ++++++++++++++++++----- capture/uur-wl-capture.c | 16 +- docs/architecture.md | 2 +- docs/capture-protocol.md | 60 ++- docs/feature-parity.md | 2 +- docs/platform-support.md | 9 +- docs/video-backends.md | 7 +- hook/frame-protocol.h | 24 +- hook/selftest.c | 5 + hook/uur-hook.c | 874 +++++++++++++++++++++++++++++++- packaging/build-arch-package.sh | 7 + src/bridge.rs | 32 +- src/capture.rs | 165 +++++- src/config.rs | 114 ++++- src/doctor.rs | 5 +- src/input/mod.rs | 3 + src/input/portal.rs | 429 ++++++++++++++-- src/main.rs | 55 +- src/session.rs | 6 +- 19 files changed, 2175 insertions(+), 271 deletions(-) diff --git a/capture/uur-pw-capture.c b/capture/uur-pw-capture.c index c8a4a49..8006b7a 100644 --- a/capture/uur-pw-capture.c +++ b/capture/uur-pw-capture.c @@ -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(¤t, &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; diff --git a/capture/uur-wl-capture.c b/capture/uur-wl-capture.c index 6f6b760..4d6dd8c 100644 --- a/capture/uur-wl-capture.c +++ b/capture/uur-wl-capture.c @@ -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)); diff --git a/docs/architecture.md b/docs/architecture.md index 0c0139f..08a491d 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -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. diff --git a/docs/capture-protocol.md b/docs/capture-protocol.md index 16e8ef5..d57d5ae 100644 --- a/docs/capture-protocol.md +++ b/docs/capture-protocol.md @@ -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 diff --git a/docs/feature-parity.md b/docs/feature-parity.md index 1ca7ec1..78a8559 100644 --- a/docs/feature-parity.md +++ b/docs/feature-parity.md @@ -10,7 +10,7 @@ | 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 | diff --git a/docs/platform-support.md b/docs/platform-support.md index 79fe2d2..5d8685c 100644 --- a/docs/platform-support.md +++ b/docs/platform-support.md @@ -210,14 +210,17 @@ choice; this is independent from PipeWire's Wayland video role. ```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 diff --git a/docs/video-backends.md b/docs/video-backends.md index 9079354..58b90b9 100644 --- a/docs/video-backends.md +++ b/docs/video-backends.md @@ -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 diff --git a/hook/frame-protocol.h b/hook/frame-protocol.h index 60e7cc6..5bab58b 100644 --- a/hook/frame-protocol.h +++ b/hook/frame-protocol.h @@ -5,9 +5,29 @@ #include #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) { diff --git a/hook/selftest.c b/hook/selftest.c index cef8f95..17c8d7c 100644 --- a/hook/selftest.c +++ b/hook/selftest.c @@ -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) { diff --git a/hook/uur-hook.c b/hook/uur-hook.c index 1d4a570..d4d9dfe 100644 --- a/hook/uur-hook.c +++ b/hook/uur-hook.c @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -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"); diff --git a/packaging/build-arch-package.sh b/packaging/build-arch-package.sh index 51d23bf..0de8336 100755 --- a/packaging/build-arch-package.sh +++ b/packaging/build-arch-package.sh @@ -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" diff --git a/src/bridge.rs b/src/bridge.rs index 905a72a..187ed50 100644 --- a/src/bridge.rs +++ b/src/bridge.rs @@ -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 { diff --git a/src/capture.rs b/src/capture.rs index 38968ff..6d23a56 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -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, restore_token: Option, 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::>() + .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> { +pub(crate) async fn watch_display_changes() -> Option> { 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> { proxy.receive_signal("MonitorsChanged").await.ok() } -async fn next_display_change(changes: &mut Option>) { +pub(crate) async fn next_display_change(changes: &mut Option>) { 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 { +pub(crate) fn spawn_helper(pw_fd: OwnedFd, streams: &[CaptureStream]) -> Result { + spawn_capture_helper(pw_fd, streams) +} + +fn spawn_capture_helper(pw_fd: OwnedFd, streams: &[CaptureStream]) -> Result { + 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 { 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::>() + .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 { Ok(child) } -fn frame_path() -> Result { +pub(crate) fn frame_path() -> Result { 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 { 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 bool { + error.chain().any(|source| { + matches!( + source.downcast_ref::(), + Some(ashpd::Error::Response( + ashpd::desktop::ResponseError::Cancelled + )) + ) || matches!( + source.downcast_ref::(), + 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 { 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::>(); + 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, diff --git a/src/config.rs b/src/config.rs index 9fee0cd..6f11bef 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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,6 +28,8 @@ pub struct Config { pub remote_desktop_restore_token: Option, /// 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. @@ -49,6 +55,7 @@ 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, strip_lock_mods: Some("auto".into()), @@ -125,24 +132,104 @@ pub fn runtime_dir() -> Result { impl Config { pub fn load() -> Result { 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 { + 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::(&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") } } @@ -173,5 +260,6 @@ 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); } } diff --git a/src/doctor.rs b/src/doctor.rs index e59ef0b..1bf15c0 100644 --- a/src/doctor.rs +++ b/src/doctor.rs @@ -374,12 +374,13 @@ pub fn report() -> Result<()> { "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); diff --git a/src/input/mod.rs b/src/input/mod.rs index 7a2aa33..1f0f03e 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -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<()>; diff --git a/src/input/portal.rs b/src/input/portal.rs index a704e5c..5071950 100644 --- a/src/input/portal.rs +++ b/src/input/portal.rs @@ -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, held_keys: HashSet, held_buttons: HashSet, + failure: Arc>>, } impl PortalBackend { pub fn connect() -> Result { 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,6 +200,7 @@ impl PortalBackend { motion_queued, held_keys: HashSet::new(), held_buttons: HashSet::new(), + failure, }) } @@ -104,23 +209,125 @@ impl PortalBackend { self.sender .send(event(reply_sender)) .map_err(|_| anyhow!("portal input session closed"))?; - reply_receiver + let result = reply_receiver .recv_timeout(Duration::from_secs(3)) - .context("portal input request timed out")? - .map_err(|error| anyhow!(error)) + .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, + receiver: UnboundedReceiver, ready: SyncSender, String>>, + configured: crate::config::Config, + pending_motion: Arc>>, + motion_queued: Arc, + failure: Arc>>, +) -> 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, + ready: SyncSender, String>>, + configured: crate::config::Config, restore_token: Option, pending_motion: Arc>>, motion_queued: Arc, -) -> Result<()> { + failure: Arc>>, + initial: bool, + opened: bool, + held_keys: HashSet, + held_buttons: HashSet, +} + +enum SessionExit { + Shutdown, + Reconnect { layout: bool, healthy: bool }, +} + +async fn portal_session_worker( + worker: &mut PortalWorker, + display_changes: &mut Option>, +) -> Result { + 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 +335,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 +361,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::>(); 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 = 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 +478,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 +495,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 +531,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(reply: Reply, result: std::result::Result) -> Result<()> { @@ -246,6 +552,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 +656,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 +672,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] diff --git a/src/main.rs b/src/main.rs index 6621b45..ab5fed8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -157,7 +157,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, @@ -228,29 +233,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 +270,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(()) } } diff --git a/src/session.rs b/src/session.rs index 57b91aa..18f611b 100644 --- a/src/session.rs +++ b/src/session.rs @@ -973,7 +973,7 @@ fn process_start_time(pid: u32) -> Result { } fn frame_path() -> Result { - Ok(crate::config::runtime_dir()?.join("frames.v1")) + Ok(crate::config::runtime_dir()?.join("frames.v3")) } fn unix_path_to_wine(path: &Path) -> String { @@ -1370,8 +1370,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" ); } }