Linux 二三事 - 解决 XWayland 应用剪切板在 Hyprland 无法粘贴到 Wayland 应用的问题

用户 Passthem 的头像
Passthem
2025 年 6 月 13 日 更新

X11 协议的程序有很复杂的剪切板逻辑,而要兼容 Wayland 下的剪切板,则难上加难。最近开始使用 Hyprland 事,剪切板无法使用。因此在这里需要寻找一个兼容的方式。

通过简单的网络搜索,找到了一个需要弄一个守护程序的解法:使用 clipnofity^1 来监听 x11 下的复制事件,然后用 xclip^2 处理剪切板。


依赖安装

遗憾的是,Fedora 包管理器并没有直接配布 clipnotify,我们要从源码自己编译。因此,我们安装一些依赖:

sudo dnf in libX11-devel libXfixes-devel

这个 libXfixes 找了我好久。。接着编译安装。

从 Github 下载代码并编译:

git clone https://github.com/cdown/clipnotify.git
cd clipnotify
make

安装到我的 bin 文件夹中:

sudo install -o root -g root -m 0755 ./clipnotify /usr/local/bin/clipnotify

这个操作会把 ./clipnotify 复制到 /usr/local/bin/clipnotify 中,并且设置基本的文件归属、文件权限等属性。

另一个依赖,xclip 可以直接从包管理器安装:

sudo dnf in xclip

下载别人的代码并修改

这里我们不重复造轮子,而是用别人的轮子:

git clone https://github.com/arabianq/wl-x11-clipsync.git

进去改几行代码,为了让 QQ 的图片工作起来更正常:

# L61: 将图片和 HTML 的匹配替换
    if 'text/uri-list' in targets:
        return 'text/uri-list'
    for t in targets:
        if t.startswith('image/'):
            return t
    if 'text/html' in targets:
        return 'text/html'
# L138: 将图片和 HTML 的匹配替换
    for t in targets:
        if t.startswith('image/'):
            return t
    if 'text/html' in targets:
        return 'text/html'

立即启动并开机自启动

先立即启动:

 nohup python clipsync.py > /dev/null &
[1] 645842
nohup: 忽略输入并将标准错误重定向到标准输出

然后,设置 Hyprland 的开机自启动:

exec-once = python /home/passthem/GitClone/wl-x11-clipsync/clipsync.py

[1] clipnotify: https://github.com/cdown/clipnotify
[2] xclip: https://github.com/astrand/xclip

评论区