Sway

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Sway and the translation is 83% complete.
Outdated translations are marked like this.
Other languages:

Sway(缩写自 SirCmpwn's Wayland compositor)是一个基于 wlroots 的开源 Wayland 混成器,为兼容 i3 窗口管理器而设计。

安装

USE 标志

USE flags for gui-wm/sway i3-compatible Wayland window manager

+drm Enable support for gui-libs/wlroots compiled with USE=drm
+filecaps Use Linux file capabilities to control privilege rather than set*id (this is orthogonal to USE=caps which uses capabilities at runtime e.g. libcap)
+libinput Enable support for gui-libs/wlroots compiled with USE=libinput
+session Enable support for gui-libs/wlroots compiled with USE=session
+swaybar Install 'swaybar': sway's status bar component
+swaynag Install 'swaynag': shows a message with buttons
X Enable support for X11 applications (XWayland)
tray Enable support for StatusNotifierItem tray specification
verify-sig Verify upstream signatures on distfiles
wallpapers Install sway's default wallpaper image
x11-backend Enable support for gui-libs/wlroots compiled with USE=x11-backend

Emerge

root #emerge --ask gui-wm/sway

配置

要查看所有可用的配置选项,请执行以下命令:

user $man 5 sway

配置文件

每个运行 Sway 的用户均可编辑默认配置文件,以便运行定制化的 Sway 会话。Gentoo 将此文件默认存放在 /etc/sway/config 位置:

user $mkdir -p ~/.config/sway/
user $cp /etc/sway/config ~/.config/sway/

终端模拟器

默认情况下,Sway 的配置文件使用 foot 终端模拟器(位于 gui-apps/foot 软件包中)。建议安装此终端模拟器,以便在 Sway 运行时能够使用终端:

root #emerge --ask gui-apps/foot

其他受欢迎的选择包括 x11-terms/alacrittyx11-terms/kitty。若将 KITTY_ENABLE_WAYLAND 环境变量设置为 1,后者即可原生支持 Wayland。

另一个非常轻量级的替代方案是 st,但它并非 Wayland 原生。

显示器配置

显示器选项可通过以下方式查询:

user $swaymsg -t get_outputs
Output DP-1 'HP Inc. HP X34 6CM2261GK2' (focused)
  Current mode: 3440x1440 @ 165.000 Hz
  Position: 0,0
  Scale factor: 1.000000
  Scale filter: nearest
  Subpixel hinting: unknown
  Transform: normal
  Workspace: 1
  Max render time: off
  Adaptive sync: disabled
  Available modes:
    3440x1440 @ 165.000 Hz
 
...
 
Output DP-2 'LG Electronics LG HDR QHD 110NTTQ0U193'
  Current mode: 2560x1440 @ 59.951 Hz
  Position: 3440,0
  Scale factor: 1.000000
  Scale filter: nearest
  Subpixel hinting: unknown
  Transform: normal
  Workspace: 2
  Max render time: off
  Adaptive sync: disabled
  Available modes:
    2560x1440 @ 59.951 Hz
    2560x1440 @ 74.971 Hz
 
...
 
Output DP-3 'Ancor Communications Inc VE247 E3LMQS103610'
  Current mode: 1920x1080 @ 60.000 Hz
  Position: 6000,0
  Scale factor: 1.000000
  Scale filter: nearest
  Subpixel hinting: unknown
  Transform: normal
  Workspace: 3
  Max render time: off
  Adaptive sync: disabled
  Available modes:
    1920x1080 @ 60.000 Hz

结果已缩短为仅包含所需的分辨率。如果默认位置配置不正确,可通过修改 ~/.config/sway/config 进行调整。保存文件后,可使用 $mod+Shift+C 重新加载配置。

文件 ~/.config/sway/config配置左侧显示器,其物理尺寸略大于主显示器
output DP-1 resolution 3440x1440@165hz pos 2560 350
文件 ~/.config/sway/config配置居中的主显示器
output DP-2 resolution 2560x1440@74.971hz pos 0 250
文件 ~/.config/sway/config配置竖直方向的备用显示器
output DP-3 resolution 1920x1080@60hz pos 6000 0 transform 270

输入设备

输入设备可通过以下方式查询:

user $swaymsg -t get_inputs
Input device: Logitech G502 HERO Gaming Mouse Keyboard
  Type: Mouse
  Identifier: 1133:49291:Logitech_G502_HERO_Gaming_Mouse_Keyboard
  Product ID: 49291
  Vendor ID: 1133
  Libinput Send Events: enabled
 
Input device: Logitech G502 HERO Gaming Mouse Keyboard
  Type: Keyboard
  Identifier: 1133:49291:Logitech_G502_HERO_Gaming_Mouse_Keyboard
  Product ID: 49291
  Vendor ID: 1133
  Active Keyboard Layout: English (US)
  Libinput Send Events: enabled
 
Input device: Logitech G502 HERO Gaming Mouse
  Type: Mouse
  Identifier: 1133:49291:Logitech_G502_HERO_Gaming_Mouse
  Product ID: 49291
  Vendor ID: 1133
  Libinput Send Events: enabled
文件 ~/.config/sway/config禁用鼠标加速,降低指针速度
input "1133:49291:Logitech_G502_HERO_Gaming_Mouse" {
    accel_profile "flat"
    pointer_accel -0.1
}
文件 ~/.config/sway/config启用触摸板“轻触即点击”功能
input type:touchpad {
    tap enabled
}
提示
单指轻触为左键点击,双指轻触为右键点击,三指轻触为中键点击。此设置可通过 tap_button_map 进行调整。

应用程序启动器

Sway 支持多种应用程序启动器。默认情况下,它会尝试使用 gui-apps/wmenu,这是一个 Wayland 原生启动器。

bemenu

dev-libs/bemenu 是一个受 dmenu 启发的动态菜单库和客户端程序。要配置 Sway 使用 bemenu:

root #emerge --ask dev-libs/bemenu
文件 ~/.config/sway/config配置 Sway 使用 bemenu
set $menu bemenu-run --no-exec | xargs -r swaymsg exec --
文件 ~/.config/sway/config配置 Sway 使用 bemenu,并设置为空提示符
set $menu bemenu-run --no-exec -p "" | xargs -r swaymsg exec --
提示
--line-height-H)可设置为 26,以便使 bemenu 的高度与 Waybar 匹配。

wmenu

默认情况下,Sway 会尝试使用 gui-apps/wmenu。它可通过以下方式安装:

root #emerge --ask gui-apps/wmenu

要配置 Sway 仅使用 wmenu:

文件 ~/.config/sway/config配置 Sway 使用 wmenu
set $menu wmenu-run | xargs swaymsg exec --
文件 ~/.config/sway/config配置 Sway 使用 wmenu 且不显示提示符
set $menu wmenu-run -p "" | xargs swaymsg exec --

状态栏

除了 Sway 自带的状态栏外,Waybar 可作为高度可定制的状态栏,在 Sway 上使用:

root #emerge --ask gui-apps/waybar

在 Sway 中启用 Waybar 有两种简单的方式:将 Waybar 作为状态栏(bar)子命令执行,或作为常规命令执行。这两种方式在 Sway 启动时(以及 Sway 退出后重新启动时)行为相同,但它们在 Sway 重新加载时有所不同。若将 Waybar(或其他状态栏)作为状态栏子命令执行,在 Sway 重新加载时会重新启动状态栏。若将 Waybar 作为常规命令执行,在 Sway 重新加载时不会重新启动状态栏;使用 exec_always 代替 exec 并不能解决此问题,它只会导致在 Sway 每次重新加载时生成更多状态栏。

用户若希望通过重新加载 Sway 来快速测试状态栏配置,应使用状态栏子命令的方式。

文件 ~/.config/sway/config通过状态栏子命令启用 Waybar
bar {
        # 执行 Waybar;在 Sway 重新加载时,Waybar 将重新启动。
        status_command waybar
 
        # 隐藏 Sway 的内置状态栏。
        mode invisible
}
文件 ~/.config/sway/config通过常规命令启用 Waybar
# 执行 Waybar;在 Sway 重新加载时,Waybar 不会重新启动。
exec waybar

亮度

调节背光亮度有多种方式可选,甚至可以通过向 /sys/class/backlight/<设备>/brightness 写入数据来实现。

acpilight

或者,sys-power/acpilight 也可以通过与 xbacklight 兼容的命令实现相同的亮度调节:

文件 ~/.config/sway/config设置支持屏幕亮度调节的键盘快捷键
bindsym XF86MonBrightnessDown exec xbacklight -dec 2
bindsym XF86MonBrightnessUp exec xbacklight -inc 4

brightnessctl

app-misc/brightnessctl 位于 ::guru 中,可用于简单调整背光:

文件 ~/.config/sway/config添加 brightnessctl 的按键绑定
bindsym XF86MonBrightnessDown exec brightnessctl set 5%-
bindsym XF86MonBrightnessUp exec brightnessctl set 5%+

light

警告
dev-libs/lightGitHub 页面目前(2023-09)声明:“此项目自 2023 年 3 月 8 日起被视为被弃置的。强烈建议在其他开发者接手之前避免使用。”。该软件包目前仍保留在 Gentoo ebuild 仓库中,但用户应考虑开发者提供的信息。

dev-libs/light 可用于调节背光和亮度。以下是一个示例配置:

文件 ~/.config/sway/config设置支持屏幕亮度调节的键盘快捷键
bindsym XF86MonBrightnessDown exec light -U 2
bindsym XF86MonBrightnessUp exec light -A 4

ddcutil

对于外接显示器,可使用 app-misc/ddcutil 通过 I2C 控制亮度:

文件 ~/.config/sway/config添加 ddcutil 的按键绑定
bindsym XF86MonBrightnessDown exec ddcutil setvcp 10 - 5
bindsym XF86MonBrightnessUp exec ddcutil setvcp 10 + 5

通知

gui-apps/mako 可用作通知守护进程。

文件 ~/.config/sway/config在 Sway 启动时启动 mako 守护进程
exec mako

音量

XF86AudioRaiseVolumeXF86AudioLowerVolume 键码通常存在,用于调节系统音量。这些键码必须根据音频后端进行绑定和设置。

PipeWire

若正在使用 PipeWire,可使用以下配置(通过 WirePlumber)调节音量:

文件 ~/.config/sway/config设置键盘快捷键以调节 PipeWire 的音量
bindsym XF86AudioMute exec wpctl set-mute @DEFAULT_SINK@ toggle
# 提示:建议添加 `--limit 1.0` 以避免音量超过 100%
bindsym XF86AudioRaiseVolume exec wpctl set-volume @DEFAULT_SINK@ 5%+
bindsym XF86AudioLowerVolume exec wpctl set-volume @DEFAULT_SINK@ 5%-
bindsym XF86AudioMicMute exec wpctl set-mute @DEFAULT_SOURCE@ toggle

PulseAudio

若正在使用 PulseAudio,可使用以下配置调节音量:

文件 ~/.config/sway/config设置键盘快捷键以调节 PulseAudio 的音量
bindsym XF86AudioMute exec pactl set-sink-mute @DEFAULT_SINK@ toggle
bindsym XF86AudioRaiseVolume exec pactl set-sink-volume @DEFAULT_SINK@ +5%
bindsym XF86AudioLowerVolume exec pactl set-sink-volume @DEFAULT_SINK@ -5%
bindsym XF86AudioMicMute exec pactl set-source-mute @DEFAULT_SOURCE@ toggle

ALSA

若正在使用 ALSA,可使用以下配置调节音量:

文件 ~/.config/sway/config设置键盘快捷键以调节 ALSA 的音量
bindsym XF86AudioRaiseVolume exec amixer -Mq set Speaker 5%+
bindsym XF86AudioLowerVolume exec amixer -Mq set Speaker 5%-

sndio

若正在使用 media-sound/sndio,可使用以下配置调节音量:

文件 ~/.config/sway/config设置键盘快捷键以调节 sndio 的音量
bindsym XF86AudioRaiseVolume exec sndioctl -f snd/default output.level=+0.05
bindsym XF86AudioLowerVolume exec sndioctl -f snd/default output.level=-0.05

屏幕截图

简单方式:使用 slurpshot

slurpshot 是一个用于简化屏幕截图的脚本。它仅使用原生 Wayland 应用程序,并支持仅选择特定窗口,还可以在不保存的情况下预览和打印截图。首先安装依赖:

root #emerge --ask gui-apps/grim gui-apps/slurp app-misc/jq dev-libs/bemenu

将 slurpshot 脚本放在你的 PATH 环境变量中的某个路径里,例如 ~/bin,为其添加可执行的权限,并设置一个按键绑定:

文件 ~/.config/sway/config设置支持 slurpshot 的键盘快捷键
#
# Screen capture
#
bindsym Print exec slurpshot

手动方式

要添加对屏幕截图的支持,请使用 grim 工具(位于 gui-apps/grim 包中)。grim 定义为 Grab Images 的缩写。该工具专为 Wayland 协议的特定需求量身定制。要安装 grim,请使用以下命令:

root #emerge --ask gui-apps/grim

为了添加对确定所选屏幕区域边界的支持,需要位于 gui-apps/slurp 包中的 slurp 工具与 grim 工具搭配使用。要安装 slurp,请使用以下命令:

root #emerge --ask gui-apps/slurp

要添加对剪贴板的支持,可使用 wl-clipboard,它位于 gui-apps/wl-clipboard。要安装 wl-clipboard,请使用以下命令:

root #emerge --ask gui-apps/wl-clipboard

接下来,编辑配置文件以添加对键盘快捷键的支持,用于执行屏幕截图操作:

文件 ~/.config/sway/config设置支持屏幕截图的键盘快捷键
#
# Screen capture
#
set $ps1 Print
set $ps2 Control+Print
set $ps3 Alt+Print
set $ps4 Alt+Control+Print
set $psf $(xdg-user-dir PICTURES)/ps_$(date +"%Y%m%d%H%M%S").png
 
bindsym $ps1 exec grim - | wl-copy
bindsym $ps2 exec grim -g "$(slurp)" - | wl-copy
bindsym $ps3 exec grim $psf
bindsym $ps4 exec grim -g "$(slurp)" $psf

请注意,按下 PrintCtrl + Print 键组后,会在 wl-copy 缓存中创建屏幕截图。这使得您可以直接从剪贴板粘贴图像,而无需保存到磁盘文件中。

对于 Alt + PrintAlt + Ctrl + Print 键盘快捷键,采用自动将图像文件保存到 Pictures 用户目录的方式。

类似剪裁工具的行为

当按下 $mod + Shift + s 时,以下设置将屏幕的指定区域复制到剪贴板:

文件 ~/.config/sway/configSimilar function to the snipping tool
bindsym $mod+Shift+s exec grim -g "$(slurp)" - | wl-copy
附注
-g 指示 grim 依指定区域剪裁,而 $(slurp) 获取该区域。屏幕截图输出到 stdoutwl-copy 将文件从那里复制到剪贴板。

设置随机壁纸

可以从文件夹中拉取并设置随机壁纸:[1]

文件 ~/.config/sway/config从文件夹中设置随机壁纸
set $wallpapers_path $HOME/Pictures/Wallpapers
output * bg $(find $wallpapers_path -type f | shuf -n 1) fill

Swaylock

gui-apps/swaylock 可用于锁定当前会话。

root #emerge --ask gui-apps/swaylock
文件 ~/.config/sway/config当按下 $mod + l 时锁定会话
bindsym $mod+l exec swaylock --ignore-empty-password --show-failed-attempts --color 1e1e1e
文件 ~/.config/sway/config使用颜色,查看 man swaylock 以获取更多信息
bindsym $mod+l exec swaylock --ignore-empty-password --show-failed-attempts \
    --color 1e1e1e --inside-color cccccc --ring-color ffffff \
    --inside-clear-color 11a8cd --ring-clear-color 29b8db \
    --inside-ver-color 2472c8 --ring-ver-color 3b8eea \
    --inside-wrong-color cd3131 --ring-wrong-color f14c4c
警告
如果账户被锁定,Swaylock 不会显示提示,而是直接运行失败。切换到另一个 TTY 并尝试登录可以帮助诊断此问题。

Swayidle

gui-apps/swayidle 可在系统空闲一段时间后执行一个命令,通常用于锁定屏幕和/或关闭屏幕。

root #emerge --ask gui-apps/swayidle
文件 ~/.config/sway/config在空闲 15 分钟后关闭所有显示器
exec swayidle -w \
  timeout 900 'swaymsg "output * power off"' \
  resume 'swaymsg "output * power on"'

HiDPI

要调整 Sway 在高分辨率显示器(4K 及以上)上的渲染效果,必须先获取需要调整的显示器名称。在 Sway 会话运行后,执行以下命令:

user $swaymsg -t get_outputs
附注
要使 swaymsg 命令在系统中可用,必须启用 swaymsg USE 标志。

在 Sway 配置文件中的 output 语句将接受一个 scale 参数,用于调整高分辨率显示器的缩放比例。

Xresources

文件 ~/.config/sway/config在 Sway 重新加载时重新加载 ~/.Xresources
exec_always test -f ~/.Xresources && xrdb -merge ~/.Xresources

GTK 配置

深色模式

GTK4

GTK4 dark mode can be enabled by setting:

文件 ~/.config/gtk-4.0/settings.iniEnable gtk4 dark mode
[Settings]
gtk-application-prefer-dark-theme = true
GTK3

GTK3 深色模式可通过以下设置启用:

文件 ~/.config/gtk-3.0/config.ini启用 GTK3 深色模式
[Settings]
gtk-application-prefer-dark-theme = true
GTK2

GTK2 没有深色模式切换功能,必须手动选择深色主题:

文件 ~/.gtkrc-2.0启用 GTK2 深色模式
gtk-theme-name = "Adwaita-dark"

GTK3 主题和字体

目前,设置 GTK 字体和主题应通过编辑 Sway 的配置文件来完成(参见 Sway 的维基):

文件 ~/.config/sway/config设置 GTK 应用程序的字体和主题
set $gnome-schema org.gnome.desktop.interface
exec_always {
    gsettings set $gnome-schema gtk-theme 'theme name'
    gsettings set $gnome-schema icon-theme 'icon theme name'
    gsettings set $gnome-schema cursor-theme 'cursor theme name'
    gsettings set $gnome-schema font-name 'Sans 10'
}

如果在设置某些应用程序(包括 Sway)的鼠标光标时遇到问题,以下配置可能有所帮助:

文件 ~/.config/sway/config设置光标主题
seat seat0 xcursor_theme custom_cursor_theme custom_cursor_size

请替换 custom_cursor_themecustom_cursor_sizeAdwaita24 在所有 Linux 发行版中基本上都是默认设置。

自动浮动窗口

默认情况下,Sway 会以平铺模式打开新窗口。以下配置片段可使许多应悬浮的常见窗口实现悬浮效果:

文件 ~/.config/sway/config更合理的浮动窗口[2]
for_window [window_role = "pop-up"] floating enable
for_window [window_role = "bubble"] floating enable
for_window [window_role = "dialog"] floating enable
for_window [window_type = "dialog"] floating enable
for_window [window_role = "task_dialog"] floating enable
for_window [window_type = "menu"] floating enable
for_window [app_id = "floating"] floating enable
for_window [app_id = "floating_update"] floating enable, resize set width 1000px height 600px
for_window [class = "(?i)pinentry"] floating enable
for_window [title = "Administrator privileges required"] floating enable

Firefox 优化

文件 ~/.config/sway/config使相关的 Firefox 窗口悬浮[3]
for_window [title = "About Mozilla Firefox"] floating enable
for_window [window_role = "About"] floating enable
for_window [app_id="firefox" title="Library"] floating enable, border pixel 1, sticky enable
文件 ~/.config/sway/config移除 Firefox 的共享指示器窗口[4]
for_window [title = "Firefox - Sharing Indicator"] kill
for_window [title = "Firefox — Sharing Indicator"] kill

Steam 优化

文件 ~/.config/sway/config使 Steam 对话窗口悬浮[5]
for_window [class="^Steam$" title="^Friends$"] floating enable
for_window [class="^Steam$" title="Steam - News"] floating enable
for_window [class="^Steam$" title=".* - Chat"] floating enable
for_window [class="^Steam$" title="^Settings$"] floating enable
for_window [class="^Steam$" title=".* - event started"] floating enable
for_window [class="^Steam$" title=".* CD key"] floating enable
for_window [class="^Steam$" title="^Steam - Self Updater$"] floating enable
for_window [class="^Steam$" title="^Screenshot Uploader$"] floating enable
for_window [class="^Steam$" title="^Steam Guard - Computer Authorization Required$"] floating enable
for_window [title="^Steam Keyboard$"] floating enable

服务

OpenRC

在基于 OpenRC 的系统上,必须将 elogind 添加到 boot 运行级别:

root #rc-update add elogind boot

切换键盘布局

Sway 使用 gui-libs/wlroots 并支持 x11-libs/libxkbcommon 键盘布局。可在文件 ~/.config/sway/config 中配置键盘布局,或使用以下命令动态切换:

user $swaymsg

临时更改键盘布局

要切换当前会话的键盘布局,请使用以下命令:

user $swaymsg input type:keyboard xkb_layout "fr"

"fr" 替换为所需的布局代码(例如,"us""de""es")。此更改立刻生效,但当 Sway 重新启动时,键盘布局将重置。

要使键盘布局在不同会话中保持不变,请在 Sway 配置文件中添加以下内容:

文件 ~/.config/sway/config更改键盘布局
input type:keyboard {
    xkb_layout "fr"
}

使用按键绑定切换键盘布局

要定义多个键盘布局,并启用键盘快捷键,以便在其间切换。请将以下内容添加到 Sway 配置中:

文件 ~/.config/sway/config启用键盘快捷键,以便在多个键盘布局之间切换
input type:keyboard {
    xkb_layout "us,fr"
    xkb_options "grp:alt_shift_toggle"
}

此配置设置了美国和法国键盘布局,并允许使用快捷键 Alt+Shift 在它们之间切换。您可通过修改 xkb_options 以自定义切换快捷键。

可通过运行以下命令查看完整选项列表:

root #cat /usr/share/x11/xkb/rules/base.lst | less

使用方法

启动 Sway

提示
尽管没有官方支持,某些 显示管理器 仍可与 Sway 配合使用。[6]
重要
通过 dbus 启动 Sway 需要设置 XDG_RUNTIME_DIR 环境变量。若使用 elogindsystemd,它们将自动设置此环境变量。省略 dbus-run-session 可能导致运行时错误

在 TTY 登录时自动启动 Sway

要在首次登录 TTY 时启动 Sway:

文件 ~/.bashrc登录第一个 TTY 后启动 Sway
if [ -z "${WAYLAND_DISPLAY}" ] && [ "${XDG_VTNR}" -eq 1 ]; then
    dbus-run-session sway
fi
在 tty1 上自动登录

To enable automatic login on tty1, --skip-login and --login-options can be added to tty1's agetty instance defined in /etc/inittab:

文件 /etc/inittabAutomatically login as Larry on tty1
c1:12345:respawn:/sbin/agetty --noclear 38400 tty1 linux --skip-login --login-options larry

手动启动 Sway

user $dbus-run-session sway

从脚本中启动 Sway

This method uses a script to forcibly take over a virtual terminal and launch Sway in it. The typical use case is to launch Sway automatically on boot.

附注
Sway is not supposed to be launched this way, the script may need some tuning to work.
文件 /usr/sbin/sway_launcherSway Launcher
#!/bin/sh
 
# Launch sway with a specific user, from a specific Virtual Terminal (vt)
# Two arguments are expected: a username (e.g., larry) and the id of a free vt (e.g., 7)
 
# prepare the tty for the user. vtX uses /dev/ttyX
chown "$1" "/dev/tty${2}"
chmod 600 "/dev/tty${2}"
 
# setup a clean environment for the user, take over the target vt, then launch sway
su --login --command "openvt --switch --console ${2} -- sway >\${HOME}/.sway_autolauncher.log 2>&1" "$1"
# this script returns immediately

This script has a few limitations:

  • XDG_RUNTIME_DIR is expected to be defined and valid, see the section above.
  • Without the --switch option for openvt, sway will freeze when trying to switch to a different VT (Ctrl+Alt+Fn), whether this is a bug or not is unknown.
  • The VT is not cleared when Sway exits, clear it by calling deallocvt.
  • Similarly the TTY's owner and mode are not changed back to their default values when Sway exits.

Launching this script on boot can be done with the local service:

文件 /etc/local.d/sway.startLaunch Sway on boot
#!/bin/sh
sway_launcher larry 7

Starting Sway without elogind or systemd

Systems that are configured with neither systemd nor elogind will need to create a shell script (or use some other means) to set the XDG_RUNTIME_DIR variable.

The environment variable can be defined in the usual configuration files. For example, if Larry the cow (Larry) sets the XDG_RUNTIME_DIR variable in his shell's configuration file and he has chosen that the directory will be in /tmp:

文件 /home/larry/.bash_profileSet the XDG_RUNTIME_DIR variable
#!/bin/sh
if test -z "${XDG_RUNTIME_DIR}"; then
  export XDG_RUNTIME_DIR=/tmp/"${UID}"-runtime-dir
    if ! test -d "${XDG_RUNTIME_DIR}"; then
        mkdir "${XDG_RUNTIME_DIR}"
        chmod 0700 "${XDG_RUNTIME_DIR}"
    fi
fi

With the XDG_RUNTIME_DIR defined, sway can be launched as usual:

user $dbus-run-session sway

If issues are encountered, check Sway issues on GitHub before contacting the Sway community on IRC (#sway (webchat)) or opening a new Gentoo bug.

Movement

All key combinations will be defined in the ~/.config/sway/config configuration file.

The Super key is defined as the $mod value by default. On most keyboards this will be the Windows key.

Sway has a Vi-like interface. h (left), j (down), k (up), and l (right) can be used for movement, in addition to the arrow keys.

Focus can be moved with mod+direction key, windows can be moved with mod+shift+direction key:

文件 ~/.config/sway/configDefault movement definitions
bindsym $mod+Left focus left
bindsym $mod+Down focus down
bindsym $mod+Up focus up
bindsym $mod+Right focus right
 
bindsym $mod+Shift+Left move left
bindsym $mod+Shift+Down move down
bindsym $mod+Shift+Up move up
bindsym $mod+Shift+Right move right

See man 5 sway-input for more information.

Useful binds

文件 ~/.config/sway/configCycle between workspaces with mod+control+left arrow and mod+control+right arrow
bindsym $mod+control+Right workspace next
bindsym $mod+control+Left workspace prev

Layouts

By default, Sway uses a tiling layout. Layout modes can be switched with the following default binds:

  • mod+b - Horizontal split
  • mod+v - Vertical split
  • mod+s - Stacking
  • mod+w - Tabbed
  • mod+e - Toggle split
  • mod+shift+space - Toggle floating
文件 ~/.config/sway/configDefault layout definitions
bindsym $mod+b splith
bindsym $mod+v splitv
 
bindsym $mod+s layout stacking
bindsym $mod+w layout tabbed
bindsym $mod+e layout toggle split
 
bindsym $mod+Shift+space floating toggle

终端

The default key combination to open a terminal emulator is $mod+Enter.

Foot Server

Foot is a minimal Wayland terminal emulator that can be configured to run as a server, reducing resource usage.

文件 ~/.config/sway/configStart the Foot server with Sway
exec foot -s
文件 ~/.config/sway/configSet the default terminal to be a foot client
set $term footclient
附注
If starting the foot server with Sway, it may not start fast enough to also auto-start a service, exec swaymsg "workspace 2; exec $term {command}" may be slow enough to mitigate this

添加功能

Sway 是为扩展而设计的,添加新功能非常简单:

文件 ~/.config/sway/config按下 Ctrl+Shift+Esc 时启动 htop
bindsym control+shift+escape exec $term htop

如果使用 foot,可以使用 -a 设置 app_id,使其自动浮动:

文件 ~/.config/sway/config按下 Ctrl+Shift+Esc 时启动 htop 浮动窗口
bindsym control+shift+escape exec $term -a 'flying-foot' htop
for_window [app_id="flying-foot"] floating enable

在不存在的工作区中左右移动

Sway can switch to the left (prev) or right (next) workspace as long as there exists a workspace to switch to in that direction; this includes moving containers to those workspaces:

文件 ~/.config/sway/config
set $super mod4
bindsym $super+alt+left  workspace prev
bindsym $super+alt+right workspace next
bindsym $super+control+alt+left  move container to workspace prev, workspace prev
bindsym $super+control+alt+right move container to workspace next, workspace next

To be able to switch to non-existing workspaces, we can create a script to tell Sway to switch to a specific workspace:

文件 ~/.config/sway/config
set $super             mod4
set $num_of_workspaces 10
bindsym $super+alt+left          exec swaymsg -pt get_workspaces | gawk -f ~/.config/sway/workspace.gawk -v move_type="left"  -v num_of_workspaces=$num_of_workspaces
bindsym $super+alt+right         exec swaymsg -pt get_workspaces | gawk -f ~/.config/sway/workspace.gawk -v move_type="right" -v num_of_workspaces=$num_of_workspaces
bindsym $super+control+alt+left  exec swaymsg -pt get_workspaces | gawk -f ~/.config/sway/workspace.gawk -v move_type="container_left"  -v num_of_workspaces=$num_of_workspaces
bindsym $super+control+alt+right exec swaymsg -pt get_workspaces | gawk -f ~/.config/sway/workspace.gawk -v move_type="container_right" -v num_of_workspaces=$num_of_workspaces
文件 ~/.config/sway/workspace.gawk
#!/bin/gawk -f
 
$3 == "(focused)" {
	switch(move_type) {
	case "left":
		if ($2 == 1)
			$2=num_of_workspaces+1
		system("sway workspace "$2-1)
		exit
	
	case "right":
		if ($2 == num_of_workspaces)
			$2=0
		system("sway workspace "$2+1)
		exit
	
	case "container_left":
		if ($2 == 1)
			$2=num_of_workspaces+1
		system("sway move container to workspace "$2-1", workspace "$2-1)
		exit
	
	case "container_right":
		if ($2 == num_of_workspaces)
			$2=0
		system("sway move container to workspace "$2+1", workspace "$2+1)
		exit
	}
}

字体大小调整

文件 ~/.config/sway/config
# 对于状态栏的字体大小调整
bar {
    font Fira Sans SemiBold 14
}
# 全局字体大小调整
font Fira Sans SemiBold 13

要获取 Sway 当前使用的字体,请执行以下命令:

user $swaymsg -t get_config | grep font

对于 foot 终端的字体大小调整:

文件 ~/.config/foot/foot.ini
[main]
font=Mono:style=Regular:size=14

故障排除

屏幕共享无法正常工作

请确保您的 Sway 配置未将输出设置为 render_bit_depth 10,因为屏幕共享仅支持 render_bit_depth 8

请确保已安装包 gui-libs/xdg-desktop-portal-wlr。默认情况下,该程序本应由 D-Bus 自动启动。但由于其运行需要 Sway 导出(export)的环境变量,而 D-Bus 会话在 Sway 启动前已启动,因此该程序无法正常运行。为解决此问题,请在 Sway 配置文件的开头添加以下行,以更新 D-Bus 环境:

文件 ~/.config/sway/config
exec --no-startup-id dbus-update-activation-environment --all

另请参阅此链接以查看 PipeWire 是否在正常工作。确保已设置环境变量 XDG_CURRENT_DESKTOP=sway。如果同时使用 systemd 和 WirePlumber,您可能需要启用 wireplumber.service,只启用 wireplumber.socket 可能不够。

无法连接到用户总线

[swaybar/tray/tray.c:42] Failed to connect to user bus: No such file or directory

警告:未加载任何图标主题

[swaybar/tray/icon.c:348] Warning: no icon themes loaded

Sway 正在寻找 x11-themes/hicolor-icon-theme,请安装此软件包。

没有后端能够打开 seat

[ERROR] [wlr] [libseat] [libseat/libseat.c:78] No backend was able to open a seat

请确保您已正确完成了使用方法中的所有操作。

您需要安装一个 seat 管理守护进程,例如 sys-auth/seatdsys-auth/elogind,并启用相应的服务。同时请检查是否需要设置 XDG_RUNTIME_DIR。如果使用的是 elogind 软件包,它应该会自动设置 XDG_RUNTIME_DIR 变量,无需进一步配置。而使用 seatd 软件包时则并非如此,您需要自行设置该变量,请参阅在不使用 elogind 或 systemd 的情况下启动 Sway 获取操作说明。此外,请检查用户是否需要加入 seat 用户组。

重要
如果使用的是 sys-auth/seatd,则要求设置 server USE 标志

应用程序忘记登录信息

某些应用程序(例如 net-misc/nextcloud-client)使用密钥服务代理来保存登录凭据。如果应用程序在每次运行时都要求输入账户凭据,可能是因为密钥服务代理配置不正确。

首先,安装 gnome-base/gnome-keyring

root #emerge --ask gnome-base/gnome-keyring

然后,启用 gnome-keyring USE 标志。

文件 /etc/portage/package.use
# Sway 密钥服务代理
*/* gnome-keyring

更新系统以应用新的 USE 标志。

root #emerge -avuDN @world

要在登录 Sway 会话时运行并解锁代理的存储空间,请编辑以下两个文件。

文件 ~/.config/sway/config
exec dbus-update-activation-environment --all
exec gnome-keyring-daemon --start --components=secrets
exec export $(gnome-keyring-daemon)
文件 /etc/pam.d/login
auth      optional  pam_gnome_keyring.so
password  optional  pam_gnome_keyring.so
session   optional  pam_gnome_keyring.so auto_start

参见

参考