Compare commits

..
2 Commits
Author SHA1 Message Date
VallierDevandClaude Opus 5.5 89d209e7df Release v0.1.9
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 19:59:31 +08:00
VallierDevandClaude Opus 5.5 b5cc5ed736 Release input at desktop session boundaries; pipelined native Wayland capture
Squashed from the Codex v0.1.9 branch (reviewed):
- Clear stale input when a desktop session starts or ends and at service
  start/stop: X11 releases every pressed key/button on the display (a stuck
  XTest button blocked all clicks after a VM reboot), uinput resets its own
  devices, Wayland/portal sweep the keys this host can emit. A per-desktop
  lease stops late callbacks of an old connection from touching a new one;
  file/terminal/port-mapping peers never release desktop input.
- Native Wayland (--features wayland): double-buffered screencopy pipeline,
  viewer frame rate drives the capture rate, opt-in dma-buf (UU_WAYLAND_DMABUF=1,
  linear only, falls back to SHM). VM-measured only.
- Internal QA records (docs/validation, contrib/tests) removed from the public
  tree and ignored; they stay in the private project.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 19:45:12 +08:00
19 changed files with 1191 additions and 79 deletions

No files matched your search

+5
View File
@@ -1,3 +1,8 @@
target/
*.log
.DS_Store
# Internal QA records and handoffs stay private (kept under the parent project analysis/)
/docs/validation/
/docs/ACTIVE_HANDOFF.md
/contrib/tests/
+2 -3
View File
@@ -284,8 +284,7 @@ User-bus headless setup and the official Mac checklist: [virtual desktop accepta
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
Read the [KMS limitations, permission risk and revocation steps](docs/validation/kms.md) before use.
This capability is broad: it applies to the whole network-facing process and can expose greeter/lock screens that portal consent would hide. Use it only on a disposable machine with a reviewed binary; inspect with `getcap`, revoke with `sudo setcap -r <path>`. A VM greeter capture was solid black, so lock/greeter screens are not supported.
The VM GDM greeter capture was solid black (failed visual acceptance); lock/greeter support and Intel/AMD hardware remain unverified. Use uinput for input; clipboard
is unavailable without a desktop session. No default release behavior changes.
For headless desktop offers, [official error evidence remains incomplete](docs/validation/headless-error-evidence.md);
the existing offer behavior is retained.
No official "no desktop" error code was found for headless desktop offers, so the existing offer behavior is retained.
Generated
+1 -1
View File
@@ -2649,7 +2649,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uu-remote-linux"
version = "0.1.8"
version = "0.1.9"
dependencies = [
"base64 0.22.1",
"bytes",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "uu-remote-linux"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
authors = ["uu-remote-linux contributors"]
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
+17 -4
View File
@@ -49,6 +49,12 @@
| Reconnects | Every tool window has its own connection; they do not disturb each other |
| Headless servers | Goes online without a monitor with terminal, file transfer and port mapping; an Xvfb virtual desktop gives a full remote desktop (see below) |
Desktop connection boundaries and host startup clear stale input; delayed events
from an old desktop cannot affect its replacement. X11 clears pressed state on
the authenticated display (including physical keys whose origin it cannot
distinguish); uinput clears only this host's virtual devices, and Portal only its
authorized session. Independent file/terminal tool peers do not reset input.
## Supported Linux desktops
| Desktop | Display protocol | Status | Build |
@@ -64,10 +70,18 @@ Apart from GNOME on X11, these desktops have been tested in virtual machines so
real hardware, please [open an issue](../../issues) with your distribution, desktop, GPU and the relevant lines from
`journalctl --user -u uuremote-native.service`.
Wayland limits: single screen, fixed resolution. The native Hyprland backend captures via shm screencopy, so the frame rate is bounded by the compositor (about 16–22 fps measured at 2560x1600) and the viewer fps option has no effect. The portal backend does not support screen wake or
Wayland limits: single screen, fixed resolution. The native Hyprland backend now pipelines two shm screencopy buffers and passes the viewer's requested fps to both capture pacing and the encoder. Actual fps remains limited by the compositor, rendering and readback (the old serial path measured about 16–22 fps at 2560x1600; the pipeline is VM-measured only, not a hardware guarantee). The portal backend does not support screen wake or
dynamic resolution yet; the clipboard syncs while the viewer's remote window has focus. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
permission; the permission is remembered once granted.
Native Wayland experiments: `UU_WAYLAND_PIPELINE=0` disables pipelining for comparison.
`UU_WAYLAND_DMABUF=1` explicitly tries dma-buf → GBM CPU mapping (off by default;
requires runtime libgbm; `UU_WAYLAND_DMABUF_DEVICE` overrides `/dev/dri/renderD128`).
Only single-plane 8-bit RGB with an advertised linear modifier is supported;
failure falls back to SHM. The virgl VM verified fallback only: successful import,
mapping and hardware acceleration remain unverified. This is not zero-copy VAAPI;
`UU_VIDEO_ENCODER=vaapi` still uploads CPU frames through FFmpeg.
## Not implemented / not supported
| Feature | Status |
@@ -322,8 +336,7 @@ User-bus headless setup and the official Mac checklist: [virtual desktop accepta
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
Read the [KMS limitations, permission risk and revocation steps](docs/validation/kms.md) before use.
This capability is broad: it applies to the whole network-facing process and can expose greeter/lock screens that portal consent would hide. Use it only on a disposable machine with a reviewed binary; inspect with `getcap`, revoke with `sudo setcap -r <path>`. A VM greeter capture was solid black, so lock/greeter screens are not supported.
The VM GDM greeter capture was solid black (failed visual acceptance); lock/greeter support and Intel/AMD hardware remain unverified. Use uinput for input; clipboard
is unavailable without a desktop session. No default release behavior changes.
For headless desktop offers, [official error evidence remains incomplete](docs/validation/headless-error-evidence.md);
the existing offer behavior is retained.
No official "no desktop" error code was found for headless desktop offers, so the existing offer behavior is retained.
+10 -4
View File
@@ -45,6 +45,10 @@
| 多次重连 | 每个工具窗口独立连接,互不影响 |
| 无头服务器 | 没有显示器也能上线,提供终端、文件传输和端口映射;也可以用 Xvfb 虚拟桌面提供完整远程桌面(见下文) |
桌面连接建立、结束及服务启动时会清理遗留输入状态;旧连接的迟到事件不能影响新的桌面连接。
X11 清理当前已认证显示上的按下状态(包括无法区分来源的实际按键);uinput 仅清理本服务虚拟设备,
Portal 仅作用于本服务获授权的会话。文件传输、终端等独立工具连接不触发桌面松键。
## 支持的 Linux 桌面
| 桌面环境 | 显示协议 | 状态 | 编译方式 |
@@ -59,7 +63,9 @@
除 GNOME(X11)外,其余桌面目前都是在虚拟机里测试的。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端通过 shm screencopy 截屏,帧率受合成器限制(2560x1600 实测约 16~22 帧),控制端的帧率选项不生效;portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
原生 Wayland 实验选项:`UU_WAYLAND_PIPELINE=0` 可关闭流水线做对照。`UU_WAYLAND_DMABUF=1` 显式尝试 dma-buf→GBM CPU 映射(默认关闭,需要运行时 libgbm;可用 `UU_WAYLAND_DMABUF_DEVICE` 指定 render node,默认 `/dev/dri/renderD128`)。仅支持显式线性修饰符、单平面 8-bit RGB,失败回退 SHM;virgl VM 中只验证了回退,成功导入/映射和硬件加速均未验证。这不是零拷贝 VAAPI 路径;选择 `UU_VIDEO_ENCODER=vaapi` 时仍由 FFmpeg 上传 CPU 帧。
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
## 尚未实现 / 不支持
@@ -289,13 +295,13 @@ VAAPI 可选包:Intel 新显卡用 `intel-media-driver`,AMD 用 `libva-mesa-
`vainfo`。KMS 依赖 `libdrm`。虚拟 Xfce 依赖 `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
滚动 Arch 的 FFmpeg 可能超出 `inproc-av` 支持的 6.x;请使用默认进程方案或独立的 FFmpeg 6 前缀,
不要降级系统 FFmpeg。当前 Arch/Hyprland VM 验证结果见[开发验证记录](docs/validation/v017-results.md)。
不要降级系统 FFmpeg。
### 实验性 KMS 截屏
编译 `--features kms` 后显式设置 `UU_DESKTOP_BACKEND=kms`;`UU_KMS_DEVICE` 默认
`/dev/dri/card0`。需要 libdrm、card 节点访问权及主动授予的 `cap_sys_admin+ep`。
程序不会自动启用或授权;此 capability 权限很大,先阅读[限制、安全风险及撤销步骤](docs/validation/kms.md)。
程序不会自动启用或授权;此 capability 权限很大,此 capability 权限很大:它作用于整个联网的服务进程,也能看到登录界面/锁屏等原本受授权保护的画面;只在可丢弃的机器上、对审查过的二进制使用,用 `getcap` 检查,`sudo setcap -r <路径>` 撤销。目前在虚拟机里登录界面截图全黑,不支持锁屏/登录界面。
当前仅为线性帧缓冲原型:VM 的 GDM 截图仍是黑屏,**没有通过登录界面视觉验收**,不承诺锁屏可用。
键鼠需 uinput;没有桌面会话时不提供剪贴板。
无头桌面错误码的[官方证据仍不足](docs/validation/headless-error-evidence.md),因此保持原有 offer 行为。
无头模式下桌面连接没有找到官方的「无桌面」错误码,因此保持原有 offer 行为。
+1 -2
View File
@@ -72,7 +72,7 @@ fn main() {
let mut build = cc::Build::new();
build.flag_if_supported("-Wno-unused-parameter"); // protocol listener signatures
build.file("c_engine/wayland_engine.c").include(&out);
for name in ["wlr-screencopy-unstable-v1", "wlr-virtual-pointer-unstable-v1", "virtual-keyboard-unstable-v1", "wlr-output-power-management-unstable-v1"] {
for name in ["wlr-screencopy-unstable-v1", "wlr-virtual-pointer-unstable-v1", "virtual-keyboard-unstable-v1", "wlr-output-power-management-unstable-v1", "linux-dmabuf-unstable-v1"] {
let xml = format!("c_engine/wayland-protocols/{name}.xml");
println!("cargo:rerun-if-changed={xml}");
for (mode, suffix) in [("client-header", "client-protocol.h"), ("private-code", "protocol.c")] {
@@ -90,4 +90,3 @@ fn main() {
}
}
}
@@ -0,0 +1,589 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="linux_dmabuf_unstable_v1">
<copyright>
Copyright © 2014, 2015 Collabora, Ltd.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
</copyright>
<interface name="zwp_linux_dmabuf_v1" version="5">
<description summary="factory for creating dmabuf-based wl_buffers">
Following the interfaces from:
https://www.khronos.org/registry/egl/extensions/EXT/EGL_EXT_image_dma_buf_import.txt
https://www.khronos.org/registry/EGL/extensions/EXT/EGL_EXT_image_dma_buf_import_modifiers.txt
and the Linux DRM sub-system's AddFb2 ioctl.
This interface offers ways to create generic dmabuf-based wl_buffers.
Clients can use the get_surface_feedback request to get dmabuf feedback
for a particular surface. If the client wants to retrieve feedback not
tied to a surface, they can use the get_default_feedback request.
The following are required from clients:
- Clients must ensure that either all data in the dma-buf is
coherent for all subsequent read access or that coherency is
correctly handled by the underlying kernel-side dma-buf
implementation.
- Don't make any more attachments after sending the buffer to the
compositor. Making more attachments later increases the risk of
the compositor not being able to use (re-import) an existing
dmabuf-based wl_buffer.
The underlying graphics stack must ensure the following:
- The dmabuf file descriptors relayed to the server will stay valid
for the whole lifetime of the wl_buffer. This means the server may
at any time use those fds to import the dmabuf into any kernel
sub-system that might accept it.
However, when the underlying graphics stack fails to deliver the
promise, because of e.g. a device hot-unplug which raises internal
errors, after the wl_buffer has been successfully created the
compositor must not raise protocol errors to the client when dmabuf
import later fails.
To create a wl_buffer from one or more dmabufs, a client creates a
zwp_linux_dmabuf_params_v1 object with a zwp_linux_dmabuf_v1.create_params
request. All planes required by the intended format are added with
the 'add' request. Finally, a 'create' or 'create_immed' request is
issued, which has the following outcome depending on the import success.
The 'create' request,
- on success, triggers a 'created' event which provides the final
wl_buffer to the client.
- on failure, triggers a 'failed' event to convey that the server
cannot use the dmabufs received from the client.
For the 'create_immed' request,
- on success, the server immediately imports the added dmabufs to
create a wl_buffer. No event is sent from the server in this case.
- on failure, the server can choose to either:
- terminate the client by raising a fatal error.
- mark the wl_buffer as failed, and send a 'failed' event to the
client. If the client uses a failed wl_buffer as an argument to any
request, the behaviour is compositor implementation-defined.
For all DRM formats and unless specified in another protocol extension,
pre-multiplied alpha is used for pixel values.
Unless specified otherwise in another protocol extension, implicit
synchronization is used. In other words, compositors and clients must
wait and signal fences implicitly passed via the DMA-BUF's reservation
mechanism.
Disclaimer: This protocol extension has been marked stable. This copy is
no longer used and only retained for backwards compatibility. The
canonical version can be found in the stable/ directory.
</description>
<request name="destroy" type="destructor">
<description summary="unbind the factory">
Objects created through this interface, especially wl_buffers, will
remain valid.
</description>
</request>
<request name="create_params">
<description summary="create a temporary object for buffer parameters">
This temporary object is used to collect multiple dmabuf handles into
a single batch to create a wl_buffer. It can only be used once and
should be destroyed after a 'created' or 'failed' event has been
received.
</description>
<arg name="params_id" type="new_id" interface="zwp_linux_buffer_params_v1"
summary="the new temporary"/>
</request>
<event name="format">
<description summary="supported buffer format">
This event advertises one buffer format that the server supports.
All the supported formats are advertised once when the client
binds to this interface. A roundtrip after binding guarantees
that the client has received all supported formats.
For the definition of the format codes, see the
zwp_linux_buffer_params_v1::create request.
Starting version 4, the format event is deprecated and must not be
sent by compositors. Instead, use get_default_feedback or
get_surface_feedback.
</description>
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
</event>
<event name="modifier" since="3">
<description summary="supported buffer format modifier">
This event advertises the formats that the server supports, along with
the modifiers supported for each format. All the supported modifiers
for all the supported formats are advertised once when the client
binds to this interface. A roundtrip after binding guarantees that
the client has received all supported format-modifier pairs.
For legacy support, DRM_FORMAT_MOD_INVALID (that is, modifier_hi ==
0x00ffffff and modifier_lo == 0xffffffff) is allowed in this event.
It indicates that the server can support the format with an implicit
modifier. When a plane has DRM_FORMAT_MOD_INVALID as its modifier, it
is as if no explicit modifier is specified. The effective modifier
will be derived from the dmabuf.
A compositor that sends valid modifiers and DRM_FORMAT_MOD_INVALID for
a given format supports both explicit modifiers and implicit modifiers.
For the definition of the format and modifier codes, see the
zwp_linux_buffer_params_v1::create and zwp_linux_buffer_params_v1::add
requests.
Starting version 4, the modifier event is deprecated and must not be
sent by compositors. Instead, use get_default_feedback or
get_surface_feedback.
</description>
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
<arg name="modifier_hi" type="uint"
summary="high 32 bits of layout modifier"/>
<arg name="modifier_lo" type="uint"
summary="low 32 bits of layout modifier"/>
</event>
<!-- Version 4 additions -->
<request name="get_default_feedback" since="4">
<description summary="get default feedback">
This request creates a new wp_linux_dmabuf_feedback object not bound
to a particular surface. This object will deliver feedback about dmabuf
parameters to use if the client doesn't support per-surface feedback
(see get_surface_feedback).
</description>
<arg name="id" type="new_id" interface="zwp_linux_dmabuf_feedback_v1"/>
</request>
<request name="get_surface_feedback" since="4">
<description summary="get feedback for a surface">
This request creates a new wp_linux_dmabuf_feedback object for the
specified wl_surface. This object will deliver feedback about dmabuf
parameters to use for buffers attached to this surface.
If the surface is destroyed before the wp_linux_dmabuf_feedback object,
the feedback object becomes inert.
</description>
<arg name="id" type="new_id" interface="zwp_linux_dmabuf_feedback_v1"/>
<arg name="surface" type="object" interface="wl_surface"/>
</request>
</interface>
<interface name="zwp_linux_buffer_params_v1" version="5">
<description summary="parameters for creating a dmabuf-based wl_buffer">
This temporary object is a collection of dmabufs and other
parameters that together form a single logical buffer. The temporary
object may eventually create one wl_buffer unless cancelled by
destroying it before requesting 'create'.
Single-planar formats only require one dmabuf, however
multi-planar formats may require more than one dmabuf. For all
formats, an 'add' request must be called once per plane (even if the
underlying dmabuf fd is identical).
You must use consecutive plane indices ('plane_idx' argument for 'add')
from zero to the number of planes used by the drm_fourcc format code.
All planes required by the format must be given exactly once, but can
be given in any order. Each plane index can be set only once.
</description>
<enum name="error">
<entry name="already_used" value="0"
summary="the dmabuf_batch object has already been used to create a wl_buffer"/>
<entry name="plane_idx" value="1"
summary="plane index out of bounds"/>
<entry name="plane_set" value="2"
summary="the plane index was already set"/>
<entry name="incomplete" value="3"
summary="missing or too many planes to create a buffer"/>
<entry name="invalid_format" value="4"
summary="format not supported"/>
<entry name="invalid_dimensions" value="5"
summary="invalid width or height"/>
<entry name="out_of_bounds" value="6"
summary="offset + stride * height goes out of dmabuf bounds"/>
<entry name="invalid_wl_buffer" value="7"
summary="invalid wl_buffer resulted from importing dmabufs via
the create_immed request on given buffer_params"/>
</enum>
<request name="destroy" type="destructor">
<description summary="delete this object, used or not">
Cleans up the temporary data sent to the server for dmabuf-based
wl_buffer creation.
</description>
</request>
<request name="add">
<description summary="add a dmabuf to the temporary set">
This request adds one dmabuf to the set in this
zwp_linux_buffer_params_v1.
The 64-bit unsigned value combined from modifier_hi and modifier_lo
is the dmabuf layout modifier. DRM AddFB2 ioctl calls this the
fb modifier, which is defined in drm_mode.h of Linux UAPI.
This is an opaque token. Drivers use this token to express tiling,
compression, etc. driver-specific modifications to the base format
defined by the DRM fourcc code.
Starting from version 4, the invalid_format protocol error is sent if
the format + modifier pair was not advertised as supported.
Starting from version 5, the invalid_format protocol error is sent if
all planes don't use the same modifier.
This request raises the PLANE_IDX error if plane_idx is too large.
The error PLANE_SET is raised if attempting to set a plane that
was already set.
</description>
<arg name="fd" type="fd" summary="dmabuf fd"/>
<arg name="plane_idx" type="uint" summary="plane index"/>
<arg name="offset" type="uint" summary="offset in bytes"/>
<arg name="stride" type="uint" summary="stride in bytes"/>
<arg name="modifier_hi" type="uint"
summary="high 32 bits of layout modifier"/>
<arg name="modifier_lo" type="uint"
summary="low 32 bits of layout modifier"/>
</request>
<enum name="flags" bitfield="true">
<entry name="y_invert" value="1" summary="contents are y-inverted"/>
<entry name="interlaced" value="2" summary="content is interlaced"/>
<entry name="bottom_first" value="4" summary="bottom field first"/>
</enum>
<request name="create">
<description summary="create a wl_buffer from the given dmabufs">
This asks for creation of a wl_buffer from the added dmabuf
buffers. The wl_buffer is not created immediately but returned via
the 'created' event if the dmabuf sharing succeeds. The sharing
may fail at runtime for reasons a client cannot predict, in
which case the 'failed' event is triggered.
The 'format' argument is a DRM_FORMAT code, as defined by the
libdrm's drm_fourcc.h. The Linux kernel's DRM sub-system is the
authoritative source on how the format codes should work.
The 'flags' is a bitfield of the flags defined in enum "flags".
'y_invert' means the that the image needs to be y-flipped.
Flag 'interlaced' means that the frame in the buffer is not
progressive as usual, but interlaced. An interlaced buffer as
supported here must always contain both top and bottom fields.
The top field always begins on the first pixel row. The temporal
ordering between the two fields is top field first, unless
'bottom_first' is specified. It is undefined whether 'bottom_first'
is ignored if 'interlaced' is not set.
This protocol does not convey any information about field rate,
duration, or timing, other than the relative ordering between the
two fields in one buffer. A compositor may have to estimate the
intended field rate from the incoming buffer rate. It is undefined
whether the time of receiving wl_surface.commit with a new buffer
attached, applying the wl_surface state, wl_surface.frame callback
trigger, presentation, or any other point in the compositor cycle
is used to measure the frame or field times. There is no support
for detecting missed or late frames/fields/buffers either, and
there is no support whatsoever for cooperating with interlaced
compositor output.
The composited image quality resulting from the use of interlaced
buffers is explicitly undefined. A compositor may use elaborate
hardware features or software to deinterlace and create progressive
output frames from a sequence of interlaced input buffers, or it
may produce substandard image quality. However, compositors that
cannot guarantee reasonable image quality in all cases are recommended
to just reject all interlaced buffers.
Any argument errors, including non-positive width or height,
mismatch between the number of planes and the format, bad
format, bad offset or stride, may be indicated by fatal protocol
errors: INCOMPLETE, INVALID_FORMAT, INVALID_DIMENSIONS,
OUT_OF_BOUNDS.
Dmabuf import errors in the server that are not obvious client
bugs are returned via the 'failed' event as non-fatal. This
allows attempting dmabuf sharing and falling back in the client
if it fails.
This request can be sent only once in the object's lifetime, after
which the only legal request is destroy. This object should be
destroyed after issuing a 'create' request. Attempting to use this
object after issuing 'create' raises ALREADY_USED protocol error.
It is not mandatory to issue 'create'. If a client wants to
cancel the buffer creation, it can just destroy this object.
</description>
<arg name="width" type="int" summary="base plane width in pixels"/>
<arg name="height" type="int" summary="base plane height in pixels"/>
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
<arg name="flags" type="uint" enum="flags" summary="see enum flags"/>
</request>
<event name="created">
<description summary="buffer creation succeeded">
This event indicates that the attempted buffer creation was
successful. It provides the new wl_buffer referencing the dmabuf(s).
Upon receiving this event, the client should destroy the
zwp_linux_buffer_params_v1 object.
</description>
<arg name="buffer" type="new_id" interface="wl_buffer"
summary="the newly created wl_buffer"/>
</event>
<event name="failed">
<description summary="buffer creation failed">
This event indicates that the attempted buffer creation has
failed. It usually means that one of the dmabuf constraints
has not been fulfilled.
Upon receiving this event, the client should destroy the
zwp_linux_buffer_params_v1 object.
</description>
</event>
<request name="create_immed" since="2">
<description summary="immediately create a wl_buffer from the given
dmabufs">
This asks for immediate creation of a wl_buffer by importing the
added dmabufs.
In case of import success, no event is sent from the server, and the
wl_buffer is ready to be used by the client.
Upon import failure, either of the following may happen, as seen fit
by the implementation:
- the client is terminated with one of the following fatal protocol
errors:
- INCOMPLETE, INVALID_FORMAT, INVALID_DIMENSIONS, OUT_OF_BOUNDS,
in case of argument errors such as mismatch between the number
of planes and the format, bad format, non-positive width or
height, or bad offset or stride.
- INVALID_WL_BUFFER, in case the cause for failure is unknown or
platform specific.
- the server creates an invalid wl_buffer, marks it as failed and
sends a 'failed' event to the client. The result of using this
invalid wl_buffer as an argument in any request by the client is
defined by the compositor implementation.
This takes the same arguments as a 'create' request, and obeys the
same restrictions.
</description>
<arg name="buffer_id" type="new_id" interface="wl_buffer"
summary="id for the newly created wl_buffer"/>
<arg name="width" type="int" summary="base plane width in pixels"/>
<arg name="height" type="int" summary="base plane height in pixels"/>
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
<arg name="flags" type="uint" enum="flags" summary="see enum flags"/>
</request>
</interface>
<interface name="zwp_linux_dmabuf_feedback_v1" version="5">
<description summary="dmabuf feedback">
This object advertises dmabuf parameters feedback. This includes the
preferred devices and the supported formats/modifiers.
The parameters are sent once when this object is created and whenever they
change. The done event is always sent once after all parameters have been
sent. When a single parameter changes, all parameters are re-sent by the
compositor.
Compositors can re-send the parameters when the current client buffer
allocations are sub-optimal. Compositors should not re-send the
parameters if re-allocating the buffers would not result in a more optimal
configuration. In particular, compositors should avoid sending the exact
same parameters multiple times in a row.
The tranche_target_device and tranche_formats events are grouped by
tranches of preference. For each tranche, a tranche_target_device, one
tranche_flags and one or more tranche_formats events are sent, followed
by a tranche_done event finishing the list. The tranches are sent in
descending order of preference. All formats and modifiers in the same
tranche have the same preference.
To send parameters, the compositor sends one main_device event, tranches
(each consisting of one tranche_target_device event, one tranche_flags
event, tranche_formats events and then a tranche_done event), then one
done event.
</description>
<request name="destroy" type="destructor">
<description summary="destroy the feedback object">
Using this request a client can tell the server that it is not going to
use the wp_linux_dmabuf_feedback object anymore.
</description>
</request>
<event name="done">
<description summary="all feedback has been sent">
This event is sent after all parameters of a wp_linux_dmabuf_feedback
object have been sent.
This allows changes to the wp_linux_dmabuf_feedback parameters to be
seen as atomic, even if they happen via multiple events.
</description>
</event>
<event name="format_table">
<description summary="format and modifier table">
This event provides a file descriptor which can be memory-mapped to
access the format and modifier table.
The table contains a tightly packed array of consecutive format +
modifier pairs. Each pair is 16 bytes wide. It contains a format as a
32-bit unsigned integer, followed by 4 bytes of unused padding, and a
modifier as a 64-bit unsigned integer. The native endianness is used.
The client must map the file descriptor in read-only private mode.
Compositors are not allowed to mutate the table file contents once this
event has been sent. Instead, compositors must create a new, separate
table file and re-send feedback parameters. Compositors are allowed to
store duplicate format + modifier pairs in the table.
</description>
<arg name="fd" type="fd" summary="table file descriptor"/>
<arg name="size" type="uint" summary="table size, in bytes"/>
</event>
<event name="main_device">
<description summary="preferred main device">
This event advertises the main device that the server prefers to use
when direct scan-out to the target device isn't possible. The
advertised main device may be different for each
wp_linux_dmabuf_feedback object, and may change over time.
There is exactly one main device. The compositor must send at least
one preference tranche with tranche_target_device equal to main_device.
Clients need to create buffers that the main device can import and
read from, otherwise creating the dmabuf wl_buffer will fail (see the
wp_linux_buffer_params.create and create_immed requests for details).
The main device will also likely be kept active by the compositor,
so clients can use it instead of waking up another device for power
savings.
In general the device is a DRM node. The DRM node type (primary vs.
render) is unspecified. Clients must not rely on the compositor sending
a particular node type. Clients cannot check two devices for equality
by comparing the dev_t value.
If explicit modifiers are not supported and the client performs buffer
allocations on a different device than the main device, then the client
must force the buffer to have a linear layout.
</description>
<arg name="device" type="array" summary="device dev_t value"/>
</event>
<event name="tranche_done">
<description summary="a preference tranche has been sent">
This event splits tranche_target_device and tranche_formats events in
preference tranches. It is sent after a set of tranche_target_device
and tranche_formats events; it represents the end of a tranche. The
next tranche will have a lower preference.
</description>
</event>
<event name="tranche_target_device">
<description summary="target device">
This event advertises the target device that the server prefers to use
for a buffer created given this tranche. The advertised target device
may be different for each preference tranche, and may change over time.
There is exactly one target device per tranche.
The target device may be a scan-out device, for example if the
compositor prefers to directly scan-out a buffer created given this
tranche. The target device may be a rendering device, for example if
the compositor prefers to texture from said buffer.
The client can use this hint to allocate the buffer in a way that makes
it accessible from the target device, ideally directly. The buffer must
still be accessible from the main device, either through direct import
or through a potentially more expensive fallback path. If the buffer
can't be directly imported from the main device then clients must be
prepared for the compositor changing the tranche priority or making
wl_buffer creation fail (see the wp_linux_buffer_params.create and
create_immed requests for details).
If the device is a DRM node, the DRM node type (primary vs. render) is
unspecified. Clients must not rely on the compositor sending a
particular node type. Clients cannot check two devices for equality by
comparing the dev_t value.
This event is tied to a preference tranche, see the tranche_done event.
</description>
<arg name="device" type="array" summary="device dev_t value"/>
</event>
<event name="tranche_formats">
<description summary="supported buffer format modifier">
This event advertises the format + modifier combinations that the
compositor supports.
It carries an array of indices, each referring to a format + modifier
pair in the last received format table (see the format_table event).
Each index is a 16-bit unsigned integer in native endianness.
For legacy support, DRM_FORMAT_MOD_INVALID is an allowed modifier.
It indicates that the server can support the format with an implicit
modifier. When a buffer has DRM_FORMAT_MOD_INVALID as its modifier, it
is as if no explicit modifier is specified. The effective modifier
will be derived from the dmabuf.
A compositor that sends valid modifiers and DRM_FORMAT_MOD_INVALID for
a given format supports both explicit modifiers and implicit modifiers.
Compositors must not send duplicate format + modifier pairs within the
same tranche or across two different tranches with the same target
device and flags.
This event is tied to a preference tranche, see the tranche_done event.
For the definition of the format and modifier codes, see the
wp_linux_buffer_params.create request.
</description>
<arg name="indices" type="array" summary="array of 16-bit indexes"/>
</event>
<enum name="tranche_flags" bitfield="true">
<entry name="scanout" value="1" summary="direct scan-out tranche"/>
</enum>
<event name="tranche_flags">
<description summary="tranche flags">
This event sets tranche-specific flags.
The scanout flag is a hint that direct scan-out may be attempted by the
compositor on the target device if the client appropriately allocates a
buffer. How to allocate a buffer that can be scanned out on the target
device is implementation-defined.
This event is tied to a preference tranche, see the tranche_done event.
</description>
<arg name="flags" type="uint" enum="tranche_flags" summary="tranche flags"/>
</event>
</interface>
</protocol>
+236 -36
View File
@@ -11,6 +11,9 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <dlfcn.h>
#include "linux-dmabuf-unstable-v1-client-protocol.h"
#include "wlr-screencopy-unstable-v1-client-protocol.h"
#include "wlr-virtual-pointer-unstable-v1-client-protocol.h"
#include "virtual-keyboard-unstable-v1-client-protocol.h"
@@ -21,6 +24,28 @@ static struct wl_display *display;
static struct wl_shm *shm;
static struct wl_seat *seat;
static struct zwlr_screencopy_manager_v1 *copy_manager;
static struct zwp_linux_dmabuf_v1 *dmabuf;
static int dmabuf_requested,dmabuf_disabled,dmabuf_reported;
static unsigned dmabuf_linear_formats;
/* GBM is loaded only for explicit opt-in; default builds need no GBM headers. */
struct gbm_device;
struct gbm_bo;
static struct {
void *library;
int fd;
struct gbm_device *device;
struct gbm_device *(*create_device)(int);
void (*device_destroy)(struct gbm_device*);
struct gbm_bo *(*bo_create)(struct gbm_device*,uint32_t,uint32_t,uint32_t,uint32_t);
void (*bo_destroy)(struct gbm_bo*);
int (*bo_get_fd)(struct gbm_bo*);
int (*bo_get_plane_count)(struct gbm_bo*);
uint32_t (*bo_get_stride)(struct gbm_bo*);
uint32_t (*bo_get_offset)(struct gbm_bo*,int);
uint64_t (*bo_get_modifier)(struct gbm_bo*);
void *(*bo_map)(struct gbm_bo*,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t*,void**);
void (*bo_unmap)(struct gbm_bo*,void*);
} gbm={.fd=-1};
static struct zwlr_virtual_pointer_manager_v1 *pointer_manager;
static struct zwp_virtual_keyboard_manager_v1 *keyboard_manager;
static struct zwlr_virtual_pointer_v1 *pointer;
@@ -35,11 +60,24 @@ static unsigned char held_keys[768];
struct output { struct wl_output *handle; char name[128]; int transform,scale; uint32_t width,height; };
static struct output outputs[32];
static unsigned output_count, selected;
static uint32_t width,height,stride,format,flags;
static int done,failed,initialized;
static struct wl_buffer *buffer;
static void *pixels;
static size_t pixel_length;
static uint32_t width,height;
static int initialized;
/* Independent slots let the compositor fill the next frame during delivery. */
struct frame {
struct zwlr_screencopy_frame_v1 *handle;
struct wl_buffer *buffer;
void *pixels;
size_t length;
uint32_t width,height,stride,format,flags;
int done,failed,submitted;
struct zwp_linux_buffer_params_v1 *params;
struct gbm_bo *bo;
int bo_fd,dmabuf_used;
void *map_data;
uint32_t dma_format,dma_width,dma_height;
};
static struct frame frames[2];
static unsigned pending;
static uint32_t timestamp(void) {
struct timespec t; clock_gettime(CLOCK_MONOTONIC,&t);
@@ -82,6 +120,20 @@ static void output_scale(void*d,struct wl_output*o,int32_t scale) { if(scale>0 &
static void output_name(void*d,struct wl_output*o,const char*name) { snprintf(((struct output*)d)->name,128,"%s",name); }
static void output_description(void*d,struct wl_output*o,const char*desc) {}
static const struct wl_output_listener output_listener={output_geometry,output_mode,output_done,output_scale,output_name,output_description};
static unsigned dma_format_bit(uint32_t format) {
switch(format) {
case 0x34325258: return 1; /* DRM_FORMAT_XRGB8888 */
case 0x34325241: return 2; /* DRM_FORMAT_ARGB8888 */
case 0x34324258: return 4; /* DRM_FORMAT_XBGR8888 */
case 0x34324241: return 8; /* DRM_FORMAT_ABGR8888 */
default: return 0;
}
}
static void dma_format(void*d,struct zwp_linux_dmabuf_v1*b,uint32_t fmt) {}
static void dma_modifier(void*d,struct zwp_linux_dmabuf_v1*b,uint32_t fmt,uint32_t hi,uint32_t lo) {
if(!hi && !lo) dmabuf_linear_formats|=dma_format_bit(fmt);
}
static const struct zwp_linux_dmabuf_v1_listener dma_listener={dma_format,dma_modifier};
static void global(void*d,struct wl_registry*r,uint32_t id,const char*interface,uint32_t version) {
if (!strcmp(interface,"wl_shm")) shm=wl_registry_bind(r,id,&wl_shm_interface,1);
else if (!strcmp(interface,"wl_seat") && !seat) seat=wl_registry_bind(r,id,&wl_seat_interface,1);
@@ -90,6 +142,9 @@ static void global(void*d,struct wl_registry*r,uint32_t id,const char*interface,
o->scale=1;
o->handle=wl_registry_bind(r,id,&wl_output_interface,version<4?version:4);
wl_output_add_listener(o->handle,&output_listener,o);
} else if (!strcmp(interface,"zwp_linux_dmabuf_v1") && dmabuf_requested && version>=3) {
dmabuf=wl_registry_bind(r,id,&zwp_linux_dmabuf_v1_interface,3);
zwp_linux_dmabuf_v1_add_listener(dmabuf,&dma_listener,NULL);
} else if (!strcmp(interface,"zwlr_screencopy_manager_v1")) copy_manager=wl_registry_bind(r,id,&zwlr_screencopy_manager_v1_interface,version<3?version:3);
else if (!strcmp(interface,"zwlr_virtual_pointer_manager_v1") && version>=2) pointer_manager=wl_registry_bind(r,id,&zwlr_virtual_pointer_manager_v1_interface,2);
else if (!strcmp(interface,"zwp_virtual_keyboard_manager_v1")) keyboard_manager=wl_registry_bind(r,id,&zwp_virtual_keyboard_manager_v1_interface,1);
@@ -100,63 +155,160 @@ static const struct wl_registry_listener registry_listener={global,global_remove
static void power_mode(void*d,struct zwlr_output_power_v1*p,uint32_t mode) {}
static void power_failure(void*d,struct zwlr_output_power_v1*p) { power_failed=1; }
static const struct zwlr_output_power_v1_listener power_listener={power_mode,power_failure};
static void dma_disable(const char *reason) {
/* reason is always a fixed diagnostic tag, never driver/user text. */
if(!dmabuf_disabled) fprintf(stderr,"Wayland dma-buf unavailable; falling back to SHM (%s)\n",reason);
dmabuf_disabled=1;
}
static void dma_init(void) {
if(!dmabuf_requested) return;
if(!dmabuf) {dma_disable("protocol"); return;}
if(!dmabuf_linear_formats) {dma_disable("linear-format"); return;}
gbm.library=dlopen("libgbm.so.1",RTLD_NOW|RTLD_LOCAL);
if(!gbm.library) {dma_disable("gbm-library"); return;}
#define GBM_LOAD(field,name) do { *(void**)(&gbm.field)=dlsym(gbm.library,name); if(!gbm.field) {dma_disable("gbm-symbol"); return;} } while(0)
GBM_LOAD(create_device,"gbm_create_device"); GBM_LOAD(device_destroy,"gbm_device_destroy");
GBM_LOAD(bo_create,"gbm_bo_create"); GBM_LOAD(bo_destroy,"gbm_bo_destroy");
GBM_LOAD(bo_get_fd,"gbm_bo_get_fd"); GBM_LOAD(bo_get_plane_count,"gbm_bo_get_plane_count");
GBM_LOAD(bo_get_stride,"gbm_bo_get_stride"); GBM_LOAD(bo_get_offset,"gbm_bo_get_offset");
GBM_LOAD(bo_get_modifier,"gbm_bo_get_modifier"); GBM_LOAD(bo_map,"gbm_bo_map"); GBM_LOAD(bo_unmap,"gbm_bo_unmap");
#undef GBM_LOAD
const char *device=getenv("UU_WAYLAND_DMABUF_DEVICE");
gbm.fd=open(device && *device ? device:"/dev/dri/renderD128",O_RDWR|O_CLOEXEC);
if(gbm.fd<0 || !(gbm.device=gbm.create_device(gbm.fd))) dma_disable("render-device");
}
static void frame_clear(struct frame *frame) {
if(frame->handle) zwlr_screencopy_frame_v1_destroy(frame->handle);
if(frame->params) zwp_linux_buffer_params_v1_destroy(frame->params);
if(frame->buffer) wl_buffer_destroy(frame->buffer);
if(frame->dmabuf_used && frame->pixels) gbm.bo_unmap(frame->bo,frame->map_data);
else if(frame->pixels) munmap(frame->pixels,frame->length);
if(frame->bo) { if(frame->bo_fd>=0)close(frame->bo_fd); gbm.bo_destroy(frame->bo); }
memset(frame,0,sizeof(*frame));
}
static void frame_fail(struct frame *frame) { frame->failed=frame->done=frame->submitted=1; }
static void copy_buffer(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h,uint32_t s) {
format=fmt; width=w; height=h; stride=s;
struct frame *frame=d;
frame->format=fmt; frame->width=w; frame->height=h; frame->stride=s;
if (w==0 || h==0 || w>16384 || h>16384 || s<w*4 || s>131072 || (uint64_t)s*h>256*1024*1024 ||
(fmt!=WL_SHM_FORMAT_XRGB8888 && fmt!=WL_SHM_FORMAT_ARGB8888 &&
fmt!=WL_SHM_FORMAT_XBGR8888 && fmt!=WL_SHM_FORMAT_ABGR8888)) { failed=done=1; return; }
fmt!=WL_SHM_FORMAT_XBGR8888 && fmt!=WL_SHM_FORMAT_ABGR8888)) { frame_fail(frame); return; }
size_t length=(size_t)s*h;
if (buffer) { wl_buffer_destroy(buffer); buffer=NULL; }
if (pixels) { munmap(pixels,pixel_length); pixels=NULL; }
int fd=memfd_create("uuremote-wayland-frame",MFD_CLOEXEC);
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); failed=done=1; return; }
pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if (pixels==MAP_FAILED) { pixels=NULL; close(fd); failed=done=1; return; }
pixel_length=length;
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); frame_fail(frame); return; }
frame->pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if (frame->pixels==MAP_FAILED) { frame->pixels=NULL; close(fd); frame_fail(frame); return; }
frame->length=length;
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
frame->buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
wl_shm_pool_destroy(pool); close(fd);
/* copy is valid immediately for SHM, including frame versions <3. */
if (zwlr_screencopy_frame_v1_get_version(f)<3) zwlr_screencopy_frame_v1_copy(f,buffer);
if (zwlr_screencopy_frame_v1_get_version(f)<3) {
zwlr_screencopy_frame_v1_copy(f,frame->buffer); frame->submitted=1;
}
}
static void copy_flags(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t value) { flags=value; }
static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) { done=1; }
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { failed=done=1; }
static void copy_flags(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t value) { ((struct frame*)d)->flags=value; }
static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) {
struct frame *frame=d;
if(frame->dmabuf_used) {
/* GBM READ mapping synchronizes readback, possibly via a staging buffer. */
uint32_t stride=0;
frame->pixels=gbm.bo_map(frame->bo,0,0,frame->width,frame->height,1,&stride,&frame->map_data);
if(frame->pixels==MAP_FAILED) frame->pixels=NULL;
if(!frame->pixels || stride<frame->width*4 || stride>131072 || (uint64_t)stride*frame->height>256*1024*1024) {
dma_disable("cpu-map"); frame_fail(frame); return;
}
frame->stride=stride;
if(!dmabuf_reported) {fprintf(stderr,"Wayland dma-buf CPU mapping active (not zero-copy)\n"); dmabuf_reported=1;}
}
frame->done=1;
}
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { frame_fail(d); }
static void copy_damage(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t x,uint32_t y,uint32_t w,uint32_t h) {}
static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h) {}
static void copy_buffer_done(void*d,struct zwlr_screencopy_frame_v1*f) { if(buffer && !failed) zwlr_screencopy_frame_v1_copy(f,buffer); else failed=done=1; }
static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h) {
struct frame *frame=d;
if(w && h && w<=16384 && h<=16384 && (uint64_t)w*h*4<=256*1024*1024 && dma_format_bit(fmt)) {
frame->dma_format=fmt; frame->dma_width=w; frame->dma_height=h;
}
}
static void submit_shm(struct frame *frame) {
if(frame->buffer && !frame->failed) {
zwlr_screencopy_frame_v1_copy(frame->handle,frame->buffer); frame->submitted=1;
} else frame_fail(frame);
}
static void dma_created(void*d,struct zwp_linux_buffer_params_v1*params,struct wl_buffer*buffer) {
struct frame *frame=d;
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
wl_buffer_destroy(frame->buffer);
munmap(frame->pixels,frame->length); frame->pixels=NULL; frame->length=0;
frame->buffer=buffer; frame->dmabuf_used=1;
frame->width=frame->dma_width; frame->height=frame->dma_height;
frame->format=frame->dma_format==0x34325258 ? WL_SHM_FORMAT_XRGB8888:
frame->dma_format==0x34325241 ? WL_SHM_FORMAT_ARGB8888:frame->dma_format;
zwlr_screencopy_frame_v1_copy(frame->handle,buffer); frame->submitted=1;
}
static void dma_failed(void*d,struct zwp_linux_buffer_params_v1*params) {
struct frame *frame=d;
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
dma_disable("import"); submit_shm(frame);
}
static const struct zwp_linux_buffer_params_v1_listener dma_params_listener={dma_created,dma_failed};
static int dma_start(struct frame *frame) {
if(!dmabuf_requested || dmabuf_disabled || !gbm.device || frame->failed || !frame->buffer ||
!(dmabuf_linear_formats&dma_format_bit(frame->dma_format))) return 0;
/* Single-plane linear RGB only; unsupported imports are asynchronous failures,
never create_immed protocol errors. No privilege or device probing changes. */
frame->bo=gbm.bo_create(gbm.device,frame->dma_width,frame->dma_height,frame->dma_format,(1u<<2)|(1u<<4));
frame->bo_fd=-1;
if(!frame->bo) {dma_disable("allocation"); return 0;}
uint32_t stride=gbm.bo_get_stride(frame->bo);
if(gbm.bo_get_plane_count(frame->bo)!=1 || gbm.bo_get_modifier(frame->bo)!=0 ||
stride<frame->dma_width*4 || stride>131072 || (uint64_t)stride*frame->dma_height>256*1024*1024 ||
(frame->bo_fd=gbm.bo_get_fd(frame->bo))<0) {dma_disable("layout-or-export"); return 0;}
frame->params=zwp_linux_dmabuf_v1_create_params(dmabuf);
zwp_linux_buffer_params_v1_add_listener(frame->params,&dma_params_listener,frame);
zwp_linux_buffer_params_v1_add(frame->params,frame->bo_fd,0,gbm.bo_get_offset(frame->bo,0),stride,0,0);
zwp_linux_buffer_params_v1_create(frame->params,frame->dma_width,frame->dma_height,frame->dma_format,0);
return 1;
}
static void copy_buffer_done(void*d,struct zwlr_screencopy_frame_v1*f) {
struct frame *frame=d;
if(!dma_start(frame)) submit_shm(frame);
}
static const struct zwlr_screencopy_frame_v1_listener copy_listener={copy_buffer,copy_flags,copy_ready,copy_failed,copy_damage,copy_dmabuf,copy_buffer_done};
static int capture(void) {
done=failed=0; flags=0;
if(buffer) { wl_buffer_destroy(buffer); buffer=NULL; }
static void request_frame(struct frame *frame) {
/* Initial milestone: video excludes cursor, viewer draws its own pointer. */
struct zwlr_screencopy_frame_v1 *f=zwlr_screencopy_manager_v1_capture_output(copy_manager,0,outputs[selected].handle);
zwlr_screencopy_frame_v1_add_listener(f,&copy_listener,NULL);
int result=dispatch_until(&done);
zwlr_screencopy_frame_v1_destroy(f);
return result==0 && !failed && pixels ? 0:-1;
frame->handle=zwlr_screencopy_manager_v1_capture_output(copy_manager,0,outputs[selected].handle);
zwlr_screencopy_frame_v1_add_listener(frame->handle,&copy_listener,frame);
}
static void reset(void) {
if (key_state) xkb_state_unref(key_state);
if (keymap) xkb_keymap_unref(keymap);
if (xkb) xkb_context_unref(xkb);
if (pixels) munmap(pixels,pixel_length);
for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
if(gbm.device) gbm.device_destroy(gbm.device);
if(gbm.fd>=0) close(gbm.fd);
if(gbm.library) dlclose(gbm.library);
memset(&gbm,0,sizeof(gbm)); gbm.fd=-1;
/* Disconnect drops every proxy; failure never leaves an initialized backend. */
if (display) wl_display_disconnect(display);
display=NULL; shm=NULL; seat=NULL; copy_manager=NULL; pointer_manager=NULL; keyboard_manager=NULL;
pointer=NULL; keyboard=NULL; xkb=NULL; keymap=NULL; key_state=NULL; buffer=NULL; pixels=NULL;
pointer=NULL; keyboard=NULL; xkb=NULL; keymap=NULL; key_state=NULL;
power_manager=NULL; power=NULL; power_failed=0;
pixel_length=0; output_count=0; initialized=0; memset(outputs,0,sizeof(outputs));
dmabuf=NULL; dmabuf_requested=dmabuf_disabled=dmabuf_reported=0; dmabuf_linear_formats=0;
pending=0; output_count=0; initialized=0; memset(outputs,0,sizeof(outputs));
memset(held_keys,0,sizeof(held_keys));
}
int uu_wl_init(int32_t *w,int32_t *h) {
pthread_mutex_lock(&lock);
if(initialized) { *w=width; *h=height; pthread_mutex_unlock(&lock); return 0; }
int result=-1;
const char *dma=getenv("UU_WAYLAND_DMABUF"); dmabuf_requested=dma && !strcmp(dma,"1");
display=wl_display_connect(NULL); if (!display) goto end;
struct wl_registry *registry=wl_display_get_registry(display);
wl_registry_add_listener(registry,&registry_listener,NULL);
if(sync_display()!=0 || sync_display()!=0) goto end;
dma_init();
if(!shm || !seat || !copy_manager || !pointer_manager || !keyboard_manager || !output_count) goto end;
const char *name=getenv("UU_WAYLAND_OUTPUT"); selected=0;
if(name && *name) {
@@ -203,18 +355,41 @@ int uu_wl_wake(void) {
}
int uu_wl_capture(uint8_t *out,size_t length) {
pthread_mutex_lock(&lock);
int result=-1;
if(initialized && capture()==0 && length==(size_t)width*height*4) {
int result=-1,retried=0;
retry:;
struct frame *frame=&frames[pending];
if(!initialized || length!=(size_t)width*height*4) goto end;
if(!frame->handle) request_frame(frame);
if(dispatch_until(&frame->done)!=0) goto end;
if(frame->failed && frame->dmabuf_used && !retried) {
/* Mapping/copy failure retries once on SHM; connection errors do not. */
dma_disable("copy"); retried=1;
for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
pending=0; goto retry;
}
if(frame->failed || !frame->pixels || frame->width!=width || frame->height!=height) goto end;
zwlr_screencopy_frame_v1_destroy(frame->handle); frame->handle=NULL;
const char *pipeline=getenv("UU_WAYLAND_PIPELINE");
if(!pipeline || strcmp(pipeline,"0")) {
struct frame *next=&frames[1-pending];
request_frame(next);
/* Submit the next GPU copy before copying/delivering these CPU pixels. */
if(dispatch_until(&next->submitted)!=0 || wl_display_flush(display)<0) goto end;
}
{
for(uint32_t y=0;y<height;y++) {
uint32_t source=(flags&ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT)?height-y-1:y;
uint32_t source=(frame->flags&ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT)?height-y-1:y;
uint8_t *row=out+(size_t)y*width*4;
memcpy(row,(uint8_t*)pixels+(size_t)source*stride,width*4);
if(format==WL_SHM_FORMAT_XBGR8888 || format==WL_SHM_FORMAT_ABGR8888) {
memcpy(row,(uint8_t*)frame->pixels+(size_t)source*frame->stride,width*4);
if(frame->format==WL_SHM_FORMAT_XBGR8888 || frame->format==WL_SHM_FORMAT_ABGR8888) {
for(uint32_t x=0;x<width;x++) { uint8_t red=row[x*4]; row[x*4]=row[x*4+2]; row[x*4+2]=red; }
}
}
result=0;
}
frame_clear(frame); pending=1-pending;
end:
if(result!=0) for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
pthread_mutex_unlock(&lock); return result;
}
int uu_wl_move(int32_t x,int32_t y,int relative) {
@@ -275,3 +450,28 @@ int uu_wl_key(uint64_t symbol,int pressed) {
}
pthread_mutex_unlock(&lock); return result;
}
int uu_wl_release_all(void) {
pthread_mutex_lock(&lock);
int result=-1;
if(initialized) {
/* Sweep this client's virtual keyboard directly: a keysym lookup can
fail for unmapped OEM/keypad symbols before reaching held modifiers. */
for(xkb_keycode_t code=xkb_keymap_min_keycode(keymap);code<=xkb_keymap_max_keycode(keymap);code++) {
if(code<8 || code>=sizeof(held_keys) || !xkb_keymap_key_get_name(keymap,code)) continue;
zwp_virtual_keyboard_v1_key(keyboard,timestamp(),code-8,WL_KEYBOARD_KEY_STATE_RELEASED);
if(held_keys[code]) xkb_state_update_key(key_state,code,XKB_KEY_UP);
}
memset(held_keys,0,sizeof(held_keys));
zwp_virtual_keyboard_v1_modifiers(keyboard,
xkb_state_serialize_mods(key_state,XKB_STATE_MODS_DEPRESSED),
xkb_state_serialize_mods(key_state,XKB_STATE_MODS_LATCHED),
xkb_state_serialize_mods(key_state,XKB_STATE_MODS_LOCKED),
xkb_state_serialize_layout(key_state,XKB_STATE_LAYOUT_EFFECTIVE));
for(unsigned code=0x110;code<=0x114;code++)
zwlr_virtual_pointer_v1_button(pointer,timestamp(),code,WL_POINTER_BUTTON_STATE_RELEASED);
zwlr_virtual_pointer_v1_frame(pointer);
result=wl_display_flush(display)<0?-1:0;
}
pthread_mutex_unlock(&lock);return result;
}
+24
View File
@@ -195,6 +195,30 @@ int x11_key_event(int keycode, int is_press) {
return 0;
}
/* Session boundaries must also recover XTEST state left by a previous process.
* Only query/release on this authenticated display; never log key state. */
int x11_release_all(void) {
if (!g_display) return -1;
XSync(g_display, False);
char keys[32];
XQueryKeymap(g_display, keys);
for (int code = 8; code < 256; code++) {
if (((unsigned char)keys[code >> 3] | g_injected_down[code >> 3]) & (1u << (code & 7)))
XTestFakeKeyEvent(g_display, code, False, CurrentTime);
}
/* Includes side buttons; wheel buttons are stateless and need no release. */
unsigned char mapping[256];
int buttons = XGetPointerMapping(g_display, mapping, sizeof(mapping));
for (int button = 1; button <= buttons && button <= 9; button++) {
if (button <= 3 || button >= 8) XTestFakeButtonEvent(g_display, button, False, CurrentTime);
}
if (g_restore_repeat) XAutoRepeatOn(g_display);
memset(g_injected_down, 0, sizeof(g_injected_down));
g_injected_count = g_restore_repeat = 0;
XSync(g_display, False);
return 0;
}
/* ---- Terminal-aware paste (Mac viewer Cmd+V arrives as Ctrl+V) ------------------------------
* Linux terminals paste with Ctrl+Shift+V; Ctrl+V quotes the next key (a paste then shows as
* "^[[200~..."). Reports whether the focused window is a terminal emulator. Uses its own
+3 -13
View File
@@ -16,18 +16,8 @@ GUI session. Its D-Bus and systemd activation environment is shared by all that
X11 access succeeds, so the host does not race the X socket.
4. `DISPLAY=:99 XAUTHORITY="$XDG_RUNTIME_DIR/uu-virtual-99.Xauthority" xdpyinfo` must work.
`gnome-terminal` launched over the user bus must open on `:99`. Check its window via
`xdotool search --class gnome-terminal-server`; do not log terminal text.
`xdotool search --class '[Gg]nome.[Tt]erminal|gnome-terminal-server'` (window
classes vary by GNOME Terminal version; the server process name is not always
its window class). Do not log window titles or terminal text.
5. Restart/stop the desktop unit: its Xvfb, desktop children and authority file must be
removed. A stale pre-existing display is rejected. The TCP X11 listener is disabled.
## Official Mac client checklist (human acceptance)
- [ ] Select the isolated VM device; confirm its identity before connecting.
- [ ] After VM boot, connect without a physical/graphical login; Xfce is visible.
- [ ] Open GNOME Terminal through its application launcher; it appears on the same `:99` desktop.
- [ ] Move/click/drag, scroll, type a synthetic test marker and verify key releases (no repeats).
- [ ] Verify both clipboard directions using synthetic text; keep the remote window focused.
- [ ] Toggle 静音运行 during playback: remote sound remains audible; VM physical output is silent.
- [ ] Disconnect/reconnect; confirm desktop and applications remain usable.
- [ ] End the last session: audio default/modules restore; auto-lock follows the saved setting.
- [ ] Repeat after reboot with no GUI login. Do not enter production credentials in test fixtures.
+1 -1
View File
@@ -16,7 +16,7 @@ VAAPI 硬件编码、虚拟声卡、无人登录的虚拟桌面和 Arch/Omarchy
- **Arch Linux / Omarchy**:README 增加 pacman 依赖和 Omarchy(Hyprland)说明。✅ 虚拟机里 Hyprland 截屏、uinput 键鼠、剪贴板通过。
⚠️ 尚未在真实 Omarchy 上用官方控制端验收;相对鼠标位移的精确像素没有验证。
- **KMS 截屏(实验原型,默认不编译)**:`--features kms` + `UU_DESKTOP_BACKEND=kms`,需要 `cap_sys_admin`。
❌ 虚拟机里登录界面截图全黑,视觉验收未通过;**不支持**锁屏/登录界面,发布包里也不包含这个功能。说明见 `docs/validation/kms.md`。
❌ 虚拟机里登录界面截图全黑,视觉验收未通过;**不支持**锁屏/登录界面,发布包里也不包含这个功能。
### 修复 / Fixed
- 虚拟声卡恢复时,如果原默认声卡已经不存在(比如 USB 声卡被拔掉),不再留下虚拟输出作为默认。
+25
View File
@@ -0,0 +1,25 @@
修复连接断开或重启后鼠标/按键卡住,Hyprland 截屏改为流水线。/ Fixes stuck mouse buttons/keys after disconnects or restarts; pipelined Hyprland capture.
### 修复 / Fixed
- **鼠标键或按键卡在按下状态,导致之后点击全部无效**:如果远程连接在按住鼠标或按键时断开,或者服务重启,按下状态可能残留,
之后的点击和输入都会出错。现在桌面连接建立、断开以及服务启动/退出时,会自动松开所有按住的键和鼠标键
(X11 松开显示器上所有按下的键;uinput 重置本服务的虚拟设备;Wayland/portal 松开本服务能发送的所有键)。
旧连接迟到的事件不会影响新连接;文件传输、终端、端口映射这些工具连接不会触发松键。
✅ X11 实机(官方 Mac 控制端,按住鼠标时关闭远控窗口后重连)和虚拟机验收通过;portal 只做了编译和单元测试。
Input is released at desktop session boundaries and at service start/stop. Verified on X11 hardware; portal unit-tested only.
注意:X11 下会同时松开本机键盘上正在按住的键。/ On X11 this also releases keys held on the local keyboard.
### 改进 / Improved
- **Hyprland 原生后端(`--features wayland`)**:截屏改为双缓冲流水线,控制端选择的帧率会同步到截屏和编码
(#9)。可选 `UU_WAYLAND_DMABUF=1` 尝试 dma-buf 映射(默认关闭,只支持线性缓冲,失败回退 shm);`UU_WAYLAND_PIPELINE=0` 关闭流水线。
⚠️ 只在虚拟机里测量过,实机帧率提升尚未验证,实际帧率仍受合成器限制。发布包(X11 版)不含此功能。
Pipelined screencopy and viewer-selected fps for the native Wayland backend (VM-measured only).
- **AMD VAAPI**:#9 的报告者在 AMD Phoenix 上实测 v0.1.7 的 `UU_VIDEO_ENCODER=vaapi` 有效,消除了高分辨率下的顿挫感。
AMD VAAPI confirmed by a user on AMD Phoenix.
- 无人登录的虚拟桌面(`contrib/virtual-desktop`)在虚拟机里用官方 Mac 控制端验收了画面、键鼠、剪贴板(虚拟机→Mac)、
静音运行的声音和会话结束后的恢复。/ Linger virtual desktop accepted in a VM with the official Mac viewer.
### 下载 / Download
`uu-remote-linux-v0.1.9-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+146 -5
View File
@@ -8,6 +8,7 @@ extern "C" {
fn x11_pointer_state(x:*mut libc::c_int,y:*mut libc::c_int,mask:*mut libc::c_uint)->libc::c_int;
fn x11_key_symbol(symbol: libc::c_ulong, is_press: libc::c_int) -> libc::c_int;
fn x11_key_event(keycode: libc::c_int, is_press: libc::c_int) -> libc::c_int;
fn x11_release_all() -> libc::c_int;
fn x11_terminal_shortcut_state() -> libc::c_int;
}
#[cfg(not(target_os = "linux"))]
@@ -26,6 +27,9 @@ unsafe fn x11_key_symbol(_: libc::c_ulong, _: libc::c_int) -> libc::c_int { 0 }
#[cfg(not(target_os = "linux"))]
unsafe fn x11_mouse_relative(_: libc::c_int, _: libc::c_int) -> libc::c_int {0}
#[cfg(not(target_os="linux"))]
unsafe fn x11_release_all()->i32 {0}
#[derive(Default)]
struct PendingPointer {
absolute: Option<(i32,i32)>,
@@ -40,7 +44,7 @@ struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collecti
#[derive(Default)]
struct Shortcuts { modifiers: u8, shifted: std::collections::BTreeSet<i32> }
pub struct InputInjector { shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
pub struct InputInjector { serial:std::sync::Mutex<()>, owner:std::sync::Mutex<DesktopOwner>, shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
impl InputInjector {
pub fn new() -> Self {
@@ -51,7 +55,7 @@ impl InputInjector {
Err(e) => { log::warn!("uinput unavailable ({}); using the desktop input path. See README for the udev rule.", e.kind()); None }
}
} else { None };
Self { shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, screen: std::sync::Mutex::new((0,0)), portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
Self { serial:std::sync::Mutex::new(()), owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, screen: std::sync::Mutex::new((0,0)), portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
}
pub fn bind_portal(&self,session:Option<std::sync::Arc<crate::portal::PortalSession>>) {
@@ -59,6 +63,7 @@ impl InputInjector {
*self.portal.lock().unwrap()=session;
}
pub fn mouse_move(&self, x: i32, y: i32) {
let _serial=self.serial.lock().unwrap();
self.inject_move(x,y,false);
}
@@ -93,6 +98,10 @@ impl InputInjector {
}
pub fn discard_pointer(&self) { *self.pending.lock().unwrap() = PendingPointer::default(); }
pub fn flush_pointer(&self) {
let _serial=self.serial.lock().unwrap();
self.flush_pointer_locked();
}
fn flush_pointer_locked(&self) {
// Hold the queue lock through injection so an older timer tick cannot
// overwrite a newer position flushed before a button transition.
let mut pending = self.pending.lock().unwrap();
@@ -118,7 +127,8 @@ impl InputInjector {
}
}
pub fn mouse_button(&self, button: i32, is_press: bool) {
self.flush_pointer();
let _serial=self.serial.lock().unwrap();
self.flush_pointer_locked();
let result = self.inject_button(button,is_press);
if result == 0 {
let mut held = self.held.lock().unwrap();
@@ -158,6 +168,7 @@ impl InputInjector {
}
pub fn windows_key_event(&self, key: i32, is_press: bool) {
let _serial=self.serial.lock().unwrap();
if self.terminal_shift(key, is_press) {
const SHIFT_L: u64 = 0xffe1;
if is_press {
@@ -187,11 +198,29 @@ impl InputInjector {
}
pub fn release_held(&self) {
let _serial=self.serial.lock().unwrap();
self.discard_pointer();
let mut held=self.held.lock().unwrap();
for button in &held.buttons {self.inject_button(*button,false);}
for key in &held.keys {self.inject_key(*key,false);}
if let Some(uinput)=&self.uinput {
Self::uinput_result(uinput.release_all());
} else if self.backend==crate::desktop_backend::DesktopBackend::X11 {
unsafe {x11_release_all();}
} else if self.backend==crate::desktop_backend::DesktopBackend::Wayland && self.portal.lock().unwrap().is_none() {
crate::wayland::release_all();
} else {
// Portal/Wayland cannot query global state. Sweep every key/button
// that this host can emit through the currently bound session.
let keys:std::collections::BTreeSet<_>=(0..=255).filter_map(crate::input_mapping::windows_vk_to_keysym)
.chain(held.keys.iter().copied()).collect();
// A dead Portal bus can time out per call. Stop on the first
// failure rather than blocking teardown for hundreds of calls;
// the next connected session retries the complete sweep.
let buttons_ok=[1,2,3,8,9].into_iter().all(|button|self.inject_button(button,false)==0);
let keys_ok=buttons_ok && keys.into_iter().all(|key|self.inject_key(key,false)==0);
if !keys_ok {log::warn!("Desktop input reset incomplete: backend unavailable");}
}
*held=HeldInput::default();
*self.shortcuts.lock().unwrap()=Shortcuts::default();
}
// Per-injector budgets cover missing bindings, locked sessions, stale handles
// and D-Bus errors. Never include keys, coordinates, text or portal tokens.
@@ -260,6 +289,7 @@ impl InputInjector {
else { unsafe { x11_key_symbol(symbol as libc::c_ulong,i32::from(pressed)) } }
}
pub fn key_event(&self, keycode: i32, is_press: bool) {
let _serial=self.serial.lock().unwrap();
if self.backend!=crate::desktop_backend::DesktopBackend::X11 { return; }
unsafe { x11_key_event(keycode, if is_press { 1 } else { 0 }) };
}
@@ -333,3 +363,114 @@ mod scroll_tests {
assert_eq!(input.pending.lock().unwrap().scroll,(-5,-5));
}
}
/// Only the newest desktop lease may inject or release input. Tool peers never
/// obtain a lease; delayed callbacks from an old desktop cannot clear a new one.
#[derive(Default)]
struct DesktopOwner { next:u64, current:u64, active:bool, stopped:bool }
impl DesktopOwner {
fn replace(&mut self)->u64 {self.next+=1;self.current=self.next;self.active=false;self.current}
fn owns(&self,id:u64)->bool {id!=0 && self.current==id}
}
pub struct DesktopInputSession {input:std::sync::Arc<InputInjector>,id:u64}
impl InputInjector {
pub fn shutdown(&self) {
let mut owner=self.owner.lock().unwrap();
owner.current=0;owner.active=false;owner.stopped=true;
self.release_held();
}
pub fn desktop_session(self:&std::sync::Arc<Self>)->std::sync::Arc<DesktopInputSession> {
let mut owner=self.owner.lock().unwrap();
let id=if owner.stopped {0} else {self.release_held();owner.replace()};
std::sync::Arc::new(DesktopInputSession {input:self.clone(),id})
}
}
impl DesktopInputSession {
pub fn connected(&self) {
let mut owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && !owner.active {self.input.release_held();owner.active=true;}
}
pub fn disconnected(&self) {
let mut owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && owner.active {owner.active=false;self.input.release_held();}
}
pub fn with_input(&self, action:impl FnOnce()) {
let owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && owner.active {action();}
}
}
impl Drop for DesktopInputSession {fn drop(&mut self) {self.disconnected();}}
#[cfg(test)]
mod lifecycle_tests {
use super::*;
use std::sync::{Arc,atomic::{AtomicUsize,Ordering}};
#[test]
fn cleanup_discards_motion_shortcuts_and_untracked_input_ledger() {
let input=Arc::new(InputInjector::new());
input.held.lock().unwrap().keys.insert(0xffe1);
input.held.lock().unwrap().buttons.insert(1);
input.shortcuts.lock().unwrap().modifiers=2;
input.shortcuts.lock().unwrap().shifted.insert(0x56);
input.mouse_relative(32,0);
input.mouse_scroll(0,1);
input.release_held();
assert!(input.held.lock().unwrap().keys.is_empty());
assert!(input.held.lock().unwrap().buttons.is_empty());
assert_eq!(input.pending.lock().unwrap().relative,(0,0));
assert_eq!(input.pending.lock().unwrap().scroll,(0,0));
assert_eq!(input.shortcuts.lock().unwrap().modifiers,0);
assert!(input.shortcuts.lock().unwrap().shifted.is_empty());
input.release_held(); // idempotent even with an empty ledger
}
#[test]
fn disconnected_or_replaced_desktop_cannot_inject_or_clear_new_input() {
let input=Arc::new(InputInjector::new());
let count=AtomicUsize::new(0);
let old=input.desktop_session();
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
assert_eq!(count.load(Ordering::Relaxed),0);
old.connected();
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
old.disconnected();
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
assert_eq!(count.load(Ordering::Relaxed),1);
input.held.lock().unwrap().buttons.insert(2);
old.disconnected(); // repeated close states must not release later local activity
assert!(input.held.lock().unwrap().buttons.contains(&2));
old.connected(); // same peer can reconnect
assert!(input.held.lock().unwrap().buttons.is_empty());
let new=input.desktop_session();
new.connected();
input.held.lock().unwrap().buttons.insert(1);
old.disconnected();old.connected();drop(old);
assert!(input.held.lock().unwrap().buttons.contains(&1));
new.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
assert_eq!(count.load(Ordering::Relaxed),2);
drop(new);
assert!(input.held.lock().unwrap().buttons.is_empty());
let last=input.desktop_session();last.connected();
input.shutdown();last.connected();
last.with_input(||panic!("shutdown cannot be undone by a stale callback"));
let later=input.desktop_session();later.connected();
later.with_input(||panic!("shutdown must reject newly arriving offers too"));
}
#[test]
fn input_gate_serializes_disconnect_against_inflight_event() {
let input=Arc::new(InputInjector::new());
let session=input.desktop_session();session.connected();
let (started,ready)=std::sync::mpsc::channel();
let (finish,resume)=std::sync::mpsc::channel();
let worker=session.clone();
let event=std::thread::spawn(move ||worker.with_input(|| {
started.send(()).unwrap();resume.recv().unwrap();
worker.input.held.lock().unwrap().buttons.insert(1);
}));
ready.recv().unwrap();
let ending=session.clone();
let teardown=std::thread::spawn(move ||ending.disconnected());
finish.send(()).unwrap();event.join().unwrap();teardown.join().unwrap();
assert!(input.held.lock().unwrap().buttons.is_empty());
session.with_input(||panic!("late event after disconnect"));
}
}
+22 -1
View File
@@ -182,9 +182,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
input.mouse_button(1,true); input.mouse_button(1,false);
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
let measure=std::env::var("UU_INPUT_MEASURE").is_ok_and(|v|v=="1");
let cursor=|| -> Result<serde_json::Value,Box<dyn std::error::Error>> {
let output=std::process::Command::new("hyprctl").args(["-j","cursorpos"]).output()?;
if !output.status.success() { return Err("Cursor measurement unavailable".into()); }
Ok(serde_json::from_slice(&output.stdout)?)
};
let before=if measure {Some(cursor()?)} else {None};
input.mouse_relative(32,0);
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
if let Some(before)=before {
println!("Relative pointer measurement: {}",serde_json::json!({"before":before,"after":cursor()?,"requested_delta":[32,0]}));
}
for key in b"UUWAYLAND" { input.windows_key_event(i32::from(*key),true); input.windows_key_event(i32::from(*key),false); }
if std::env::var("UU_INPUT_SYMBOLS").is_ok_and(|v|v=="1") {
// Fixed synthetic symbols exercise the same VK+modifier path as CONTROL.
for (key,shift) in [(0xbd,true),(0xbb,true),(0xbd,false)] {
if shift {input.windows_key_event(0x10,true);}
input.windows_key_event(key,true); input.windows_key_event(key,false);
if shift {input.windows_key_event(0x10,false);}
}
}
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
worker.abort(); input.release_held();
println!("Desktop test events sent; verify the GTK acceptance report");
@@ -253,13 +271,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
ScreenCapture::unavailable(error)
}));
let input = Arc::new(InputInjector::new());
input.bind_portal(capture.portal.clone());
input.release_held(); // recover stale input before accepting any peer
let _pointer_worker = input.start_pointer_worker();
#[cfg(target_os="linux")]
{
let shutdown_input=input.clone();
tokio::spawn(async move {
if let Ok(mut term)=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
term.recv().await; virtual_audio::disable(); shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0);
tokio::select! { _=term.recv()=>{}, _=tokio::signal::ctrl_c()=>{} }
shutdown_input.shutdown(); virtual_audio::disable(); terminal::shutdown_all(); std::process::exit(0);
}
});
}
+45
View File
@@ -121,6 +121,16 @@ mod imp {
pub fn key(&self, code: u16, pressed: bool) -> std::io::Result<()> {
self.keyboard.lock().unwrap().emit(&[(EV_KEY, code, i32::from(pressed))])
}
pub fn release_all(&self) -> std::io::Result<()> {
// Release every supported code on our own devices, even if the
// userspace ledger missed a press. Never touch physical devices.
let keys:Vec<_>=(1..=248).map(|code|(EV_KEY,code,0))
.chain([BTN_LEFT,BTN_RIGHT,BTN_MIDDLE].into_iter().map(|code|(EV_KEY,code,0))).collect();
let buttons:Vec<_>=[BTN_LEFT,BTN_RIGHT,BTN_MIDDLE,BTN_SIDE,BTN_EXTRA].into_iter().map(|code|(EV_KEY,code,0)).collect();
let keyboard=self.keyboard.lock().unwrap().emit(&keys);
let pointer=self.pointer.lock().unwrap().emit(&buttons);
keyboard.and(pointer)
}
pub fn move_absolute(&self, x: i32, y: i32) -> std::io::Result<()> {
self.pointer.lock().unwrap().emit(&[(EV_ABS, ABS_X, x.clamp(0, ABS_MAX)), (EV_ABS, ABS_Y, y.clamp(0, ABS_MAX))])
}
@@ -138,6 +148,40 @@ mod imp {
self.pointer.lock().unwrap().emit(&events)
}
}
#[cfg(test)]
mod reset_acceptance {
use super::*;
fn snapshot(device:&Device)->[u8;96] {
let mut name=[0u8;80];
let request=0x8000_0000u64 | (80<<16) | (b'U' as u64)<<8 | 44;
assert!(unsafe {libc::ioctl(device.0.as_raw_fd(),request as libc::c_ulong,name.as_mut_ptr())}>=0);
let end=name.iter().position(|v|*v==0).unwrap();
let directory=std::path::Path::new("/sys/devices/virtual/input").join(std::str::from_utf8(&name[..end]).unwrap());
let event=std::fs::read_dir(directory).unwrap().filter_map(Result::ok)
.find(|e|e.file_name().to_string_lossy().starts_with("event")).unwrap();
let file=File::open(std::path::Path::new("/dev/input").join(event.file_name())).unwrap();
let mut state=[0u8;96];
let request=0x8000_0000u64 | (96<<16) | (b'E' as u64)<<8 | 0x18;
assert!(unsafe {libc::ioctl(file.as_raw_fd(),request as libc::c_ulong,state.as_mut_ptr())}>=0);
state
}
#[test]
#[ignore="requires isolated QEMU VM uinput and evdev permissions"]
fn kernel_devices_release_even_without_an_inputinjector_ledger() {
assert_eq!(std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap().trim(),"uu-hyprland-test");
assert_eq!(std::process::Command::new("systemd-detect-virt").output().unwrap().stdout,b"qemu\n");
let devices=Devices::open().unwrap();
std::thread::sleep(std::time::Duration::from_millis(500));
devices.key(42,true).unwrap();devices.key(30,true).unwrap();
devices.button(BTN_LEFT,true).unwrap();
assert!(snapshot(&devices.keyboard.lock().unwrap()).iter().any(|v|*v!=0));
assert!(snapshot(&devices.pointer.lock().unwrap()).iter().any(|v|*v!=0));
devices.release_all().unwrap();
assert!(snapshot(&devices.keyboard.lock().unwrap()).iter().all(|v|*v==0));
assert!(snapshot(&devices.pointer.lock().unwrap()).iter().all(|v|*v==0));
}
}
}
#[cfg(not(target_os = "linux"))]
@@ -151,6 +195,7 @@ mod imp {
pub struct Devices;
impl Devices {
pub fn open() -> std::io::Result<Self> { Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "uinput requires Linux")) }
pub fn release_all(&self) -> std::io::Result<()> { Ok(()) }
pub fn key(&self, _: u16, _: bool) -> std::io::Result<()> { Ok(()) }
pub fn move_absolute(&self, _: i32, _: i32) -> std::io::Result<()> { Ok(()) }
pub fn move_relative(&self, _: i32, _: i32) -> std::io::Result<()> { Ok(()) }
+19 -5
View File
@@ -202,7 +202,7 @@ fn rate_args(settings: &VideoSettings, encoder: Encoder) -> (String, String) {
async fn read_encoder_with(
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,encoder:Encoder,sequence:Arc<std::sync::atomic::AtomicU64>,
mut settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
)->Result<(),String>{
let kms=display.starts_with("kms:");
let wayland=kms || display.starts_with("wayland:");
@@ -226,8 +226,6 @@ async fn read_encoder_with(
return read_inprocess(config, latest, sequence, idr).await;
}
let size = format!("{}x{}", size.0, size.1);
// The separate wayland-capture process produces frames at its own fixed rate.
if wayland && portal.is_none() { settings.fps = FPS as u32; }
let fps = settings.fps.to_string();
// Keep the IDR interval at 2 s whatever the frame rate.
let gop = gop_frames(settings.fps, false).to_string();
@@ -323,7 +321,7 @@ async fn read_encoder_with(
let executable=std::path::PathBuf::from("/proc/self/exe");
#[cfg(not(target_os="linux"))]
let executable=std::env::current_exe().map_err(|e|format!("Capture executable: {e}"))?;
let mut capture=Command::new(executable).arg("wayland-capture")
let mut capture=wayland_capture_command(&executable,settings.fps)
.kill_on_drop(true).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit())
.spawn().map_err(|e|format!("Start Wayland capture: {e}"))?;
let mut source=capture.stdout.take().ok_or("Capture stdout missing")?;
@@ -359,6 +357,12 @@ async fn read_encoder_with(
}
}
fn wayland_capture_command(executable:&std::path::Path,fps:u32)->Command {
let mut command=Command::new(executable);
command.arg("wayland-capture").env("UU_WAYLAND_FPS",fps.to_string());
command
}
fn transport_closed(pc: &RTCPeerConnection) -> bool {
matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
|| pc.sctp().state() == webrtc::sctp_transport::sctp_transport_state::RTCSctpTransportState::Closed
@@ -373,7 +377,9 @@ pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal
};
let (tx,mut rx)=mpsc::channel(4);
let sequence=Arc::new(std::sync::atomic::AtomicU64::new(0));
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,VideoSettings::default(),sequence,Arc::new(AtomicBool::new(false)))));
let fps=crate::wayland::capture_fps(std::env::var("UU_PROBE_FPS").ok().as_deref())?;
let settings=VideoSettings {fps,..VideoSettings::default()};
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,settings,sequence,Arc::new(AtomicBool::new(false)))));
let mut count=0;
let mut bytes=0;
// UU_PROBE_FRAMES lengthens the sample for baseline measurements; default is 5 frames.
@@ -463,6 +469,14 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
#[cfg(test)]
mod tests {
#[test]
fn native_wayland_child_uses_requested_capture_rate() {
for fps in [5,30,60,90,144] {
let command=super::wayland_capture_command(std::path::Path::new("uuremote-server"),fps);
let value=fps.to_string();
assert!(command.as_std().get_envs().any(|(k,v)|k=="UU_WAYLAND_FPS" && v==Some(std::ffi::OsStr::new(&value))));
}
}
use super::*;
#[test]
fn x264_buffer_fits_keyframes() {
+27 -1
View File
@@ -7,6 +7,13 @@ extern "C" {
fn uu_wl_button(button: i32, pressed: i32) -> i32;
fn uu_wl_key(symbol: u64, pressed: i32) -> i32;
fn uu_wl_wake() -> i32;
fn uu_wl_release_all() -> i32;
}
pub fn release_all()->i32 {
#[cfg(all(target_os="linux",feature="wayland"))]
{return unsafe {uu_wl_release_all()};}
#[cfg(not(all(target_os="linux",feature="wayland")))]
-1
}
pub fn init() -> Result<(u32,u32),String> {
#[cfg(all(target_os="linux", feature="wayland"))]
@@ -58,7 +65,8 @@ pub fn capture_stdout() -> Result<(),String> {
let (w,h)=init()?;
let mut frame=vec![0u8;(w as usize)*(h as usize)*4];
let mut stdout=std::io::stdout().lock();
let interval=std::time::Duration::from_nanos(1_000_000_000/crate::video::FPS);
let fps=capture_fps(std::env::var("UU_WAYLAND_FPS").ok().as_deref())?;
let interval=std::time::Duration::from_nanos(1_000_000_000/u64::from(fps));
loop {
let start=std::time::Instant::now();
capture(&mut frame)?;
@@ -67,6 +75,24 @@ pub fn capture_stdout() -> Result<(),String> {
}
}
pub(crate) fn capture_fps(value:Option<&str>)->Result<u32,String> {
match value {
None=>Ok(crate::video::FPS as u32),
Some(value)=>value.parse::<u32>().ok().filter(|fps|(5..=144).contains(fps))
.ok_or_else(||"UU_WAYLAND_FPS must be between 5 and 144".to_owned()),
}
}
#[cfg(test)]
mod tests {
#[test]
fn capture_rate_accepts_viewer_range_and_rejects_invalid_values() {
assert_eq!(super::capture_fps(None),Ok(60));
for fps in [5,15,30,60,90,144] {assert_eq!(super::capture_fps(Some(&fps.to_string())),Ok(fps));}
for value in ["0","4","145","-1","NaN",""] {assert!(super::capture_fps(Some(value)).is_err());}
}
}
/// Use only in an isolated acceptance desktop; emits fixed test events, no logs.
pub fn input_probe()->Result<(),String> {
let (w,h)=init()?;
+17 -2
View File
@@ -86,6 +86,7 @@ impl WebRtcSession {
.map_err(|e| format!("Create PeerConnection failed: {}", e))?,
);
let input_session=desktop.then(||input.desktop_session());
let context = Arc::new(Mutex::new(SessionContext::default()));
crate::network_diagnostics::start(Arc::downgrade(&pc));
@@ -143,7 +144,7 @@ impl WebRtcSession {
}));
let connected_context = context.clone();
let connection_input=input.clone();
let connection_input=input_session.clone();
let connected_signal = signaling_client.clone();
let notified = Arc::new(std::sync::atomic::AtomicBool::new(false));
pc.on_peer_connection_state_change(Box::new(move |state| {
@@ -153,7 +154,10 @@ impl WebRtcSession {
let notified = notified.clone();
Box::pin(async move {
log::info!("PeerConnection State changed: {:?}", state);
if matches!(state,RTCPeerConnectionState::Closed|RTCPeerConnectionState::Failed|RTCPeerConnectionState::Disconnected) {input.release_held();}
if let Some(session)=input.as_ref() {
if state==RTCPeerConnectionState::Connected {session.connected();}
if matches!(state,RTCPeerConnectionState::Closed|RTCPeerConnectionState::Failed|RTCPeerConnectionState::Disconnected) {session.disconnected();}
}
if state == RTCPeerConnectionState::Connected && !notified.swap(true, std::sync::atomic::Ordering::AcqRel) {
tokio::spawn(async move {
let ctx = ctx.lock().await.clone();
@@ -327,6 +331,7 @@ impl WebRtcSession {
log::info!("New DataChannel received: {}", label);
let input_inst = input_clone.clone();
let channel_input_session=input_session.clone();
let screen = control_capture.clone();
let out_sequence = control_sequence.clone();
let shared_clipboard_ready = text_clipboard_ready.clone();
@@ -348,6 +353,13 @@ impl WebRtcSession {
let clipboard_assembly = Arc::new(std::sync::Mutex::new(crate::clipboard_protocol::Assembly::default()));
let rich_state=Arc::new(std::sync::Mutex::new(crate::clipboard_rich::State::default()));
let clipboard_enabled = shared_clipboard_ready.clone();
if label=="CONTROL_DATA_CHANNEL" {
let closing=channel_input_session.clone();
d.on_close(Box::new(move || {
let closing=closing.clone();
Box::pin(async move {if let Some(session)=closing {session.disconnected();}})
}));
}
// Ordered outbound queue for protocol replies produced outside this callback.
let (outbound, mut outbound_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
{
@@ -498,6 +510,7 @@ impl WebRtcSession {
d.on_message(Box::new(move |msg: DataChannelMessage| {
let channel_name = lbl.clone();
let input_worker = inp.clone();
let message_input_session=channel_input_session.clone();
let terminal_worker = terminal_conn.clone();
let rpc_out = outbound.clone();
let reply_chan = d_reply.clone();
@@ -760,6 +773,7 @@ impl WebRtcSession {
if RECEIPTS[slot].fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 16 {
log::info!("CONTROL input received: kind={}", kind);
}
if let Some(session)=message_input_session.as_ref() {session.with_input(|| {
match &event {
ControlInput::MouseAbsolute {x,y} => input_worker.mouse_absolute_normalized(*x,*y,input_size),
ControlInput::MouseRelative {dx,dy} => input_worker.mouse_relative(*dx,*dy),
@@ -770,6 +784,7 @@ impl WebRtcSession {
ControlInput::MouseClick {button} => {input_worker.wire_mouse_button(*button,true);input_worker.wire_mouse_button(*button,false);},
_ => {}
}
});}
},
_ => {}