Wayland初体验

Window Manager (Sway)

i3之于X11,亦即sway之于Wayland. Sway的设计目标就是如此,绝大部分兼容i3配置(drop-in replacement)。 几篇重要的参考,

  1. Useful add ons for sway
  2. i3 Migration Guide
  3. Sway - Alpinelinux Wiki

不过我用的是sway的一个fork,叫做swayfx,声称比原版有更好的视觉效果,例如窗口阴影。

Bar

Polybar只支持X11,wayland我选择了eww. 使用这个工具可以自定义很多小部件,包括但不限于状态栏。猛击这里获取我的配置。下图是目前的配置样子。

TroubleShooting

Electron应用默认走XWayland协议,看起来很模糊

并非所有应用都原生支持wayland,所以迁移会有阵痛期。虽然可以用XWayland临时打开使用X11协议的软件,但这种方式打开的软件会有明显的像素感,像是放大了一样不清晰。

XWayland programs will be blurry when scaling is more than 1.

# obsidian启用wayland支持
$ OBSIDIAN_USE_WAYLAND=1 obsidian -enable-features=UseOzonePlatform -ozone-platform=wayland
 
# vscode启用wayland支持
$ code --enable-features=UseOzonePlatform --ozone-platform=$XDG_SESSION_TYPE

或者设置下面这个环境变量,对electron应用都可以用。参考:https://github.com/microsoft/vscode/issues/207033#issuecomment-2104720712

ELECTRON_OZONE_PLATFORM_HINT=auto code/obsidian/...

环境变量有两种,一种是shell里面的,一般设置在.bashrc.zshrc中;另一种是systemd的,可以设置在user space下,不污染系统环境,具体参考man 5 environment.d.

Ranger默认的w3m预览图片失效

w3mimgdisplay不支持wayland1,因此ranger默认的图片预览方式失败。可以使用ueberzugpp过度一下。不过这种途径类似在指定位置打开临时窗口预览图片,好像并非真的在终端中渲染图片。

Xwayland窗口中fcitx5不可用

启动sway前设置如下环境变量,

# Do not set GTK_IM_MODULE, see:
#   https://fcitx-im.org/wiki/Using_Fcitx_5_on_Wayland#GTK_IM_MODULE
# export GTK_IM_MODULE=fcitx
#
export QT_IM_MODULE=fcitx
export XMODIFIERS=@im=fcitx

For Gtk 3, add following content to ~/.config/gtk-3.0/settings.ini

[Settings]
gtk-im-module=fcitx

X11 tools

  • xclip wl-clipboard, wlsnarf, wl-clipboard-rs, wayclip,我用了wl-clipboard-rs
  • redshift gammastep

wl-clipboard cheatsheet

# Copy a simple text message:
$ wl-copy Hello world!
 
# Copy the list of files in ~/Downloads:
$ ls ~/Downloads | wl-copy
 
# Copy an image:
$ wl-copy < ~/Pictures/photo.png
 
# Copy the previous command:
$ wl-copy "!!"
 
# Paste to a file:
$ wl-paste > clipboard.txt
 
# Sort clipboard contents:
$ wl-paste | sort | wl-copy
 
# Upload clipboard contents to a pastebin on each change:
$ wl-paste --watch nc paste.example.org 5555

see: https://github.com/swaywm/sway/wiki/i3-Migration-Guide

Chrome/Obs无法捕获屏幕

参考

  1. https://wiki.archlinux.org/title/XDG_Desktop_Portal#Backends
  2. https://wiki.alpinelinux.org/wiki/Sway#PipeWire_and_Screensharing

报错信息:

org.freedesktop.DBus.Error.UnknownMethod: No such interface “org.freedesktop.portal.Settings” on object at path /org/freedesktop/portal/desktop

大概率是xdg-desktop-portal的原因,确保安装下列包,

extra/xdg-desktop-portal 1.20.3-2 [已安装]
    Desktop integration portals for sandboxed apps
extra/xdg-desktop-portal-gtk 1.15.3-1 [已安装]
    A backend implementation for xdg-desktop-portal using GTK
extra/xdg-desktop-portal-wlr 0.8.1-1 [已安装]
    xdg-desktop-portal backend for wlroots

确保下面几个service正常运行,

systemctl --user status xdg-desktop-portal xdg-desktop-portal-wlr

捕获声音。如果使用pipewire,确保这几个service正常

systemctl --user status pipewire pipewire-pulse wireplumber

如果启动后无声音,pipewire无响应,journal报错显示

pipewire-pulse[1287]: mod.protocol-pulse: server 0x55a676877270: too many client application connections: 连接被拒绝

尝试重启(参考 https://askubuntu.com/a/1521255

systemctl --user restart pipewire pipewire-pulse wireplumber pipewire-pulse.socket

暂未解决obs无法捕获指定窗口

Flameshot截图时屏幕被放缩

flameshot软件界面字体看起来正常,但是进入截图界面,屏幕被放大,导致无法截取完整屏幕。问题形如:Bad behavior on wayland with fractional scaling.

问题的原因其实是在高分辨率屏幕上(HDPI),sway默认有放缩,可见

$ swaymsg -t get_outputs
Output eDP-1 'Lenovo Group Limited 0x8AB1 Unknown' (focused)
  Current mode: 3072x1920 @ 120.002 Hz
  Power: on
  Position: 0,0
  Scale factor: 2.000000
  Scale filter: nearest
  Subpixel hinting: unknown
  Transform: normal
  Workspace: 3:Web
  Max render time: off
  Adaptive sync: disabled
  Allow tearing: no
  Available modes:
    3072x1920 @ 60.000 Hz
    3072x1920 @ 120.002 Hz
    1920x1200 @ 60.000 Hz
    1920x1080 @ 60.000 Hz
    ...
    640x480 @ 60.000 Hz

这个放缩无法被QT app(flameshot)感知,所以在调用这类软件时需要设置相应的环境变量(如QT_SCALE_FACTOR),在sway放缩比为2时,对应

QT_SCALE_FACTOR=0.5 flameshot gui

当然,也可以动态根据放缩比设置2

bindsym $mod+Print exec env QT_SCREEN_SCALE_FACTORS="$(swaymsg -t get_outputs | jq 'map(select(.focused == true) | {type, focused, scale}) | .[0].scale | if . then . else 1 end | 1 / .')" flameshot gui

QT_SCREEN_SCALE_FACTORS环境变量在我这里无用,QT_SCALE_FACOTR有用。

References

Footnotes

  1. https://github.com/alacritty/alacritty/issues/3227

  2. https://github.com/flameshot-org/flameshot/issues/748#issuecomment-1854190573