Skip to content

The only downloader you need. 下载器的集大成者。

feat(platform): X11 下的窗口也在 KWin 上显示阴影

XiaoYouChR238d2b93db67
diff --git a/app/platform/shadow.py b/app/platform/shadow.py
index c68f57f5..19e0e1a1 100644
--- a/app/platform/shadow.py
+++ b/app/platform/shadow.py
@@ -11,7 +11,11 @@ from ctypes import (
addressof,
c_char_p,
c_int,
+ c_int16,
c_int32,
+ c_uint,
+ c_uint8,
+ c_uint16,
c_uint32,
c_void_p,
)
@@ -41,13 +45,29 @@ class Tile:
pixels: bytes # 预乘 ARGB8888
+@dataclass(frozen=True)
+class Tiles:
+ left: Tile
+ topLeft: Tile
+ top: Tile
+ topRight: Tile
+ right: Tile
+ bottomRight: Tile
+ bottom: Tile
+ bottomLeft: Tile
+
+
def registerWindow(window: QWindow) -> None:
"""让合成器给无边框窗口画阴影,隐藏再显示后自动重挂;合成器不支持时什么也不做。"""
- if not QGuiApplication.platformName().startswith("wayland"):
- return
- kwinShadow = createKwinShadow()
- if kwinShadow is not None:
- WindowShadow(window, kwinShadow)
+ platform = QGuiApplication.platformName()
+ if platform.startswith("wayland"):
+ kwinShadow = createKwinShadow()
+ if kwinShadow is not None:
+ WindowShadow(window, kwinShadow)
+ elif platform == "xcb":
+ x11Shadow = createX11Shadow()
+ if x11Shadow is not None:
+ x11Shadow.set(window)
class WindowShadow(QObject):
@@ -72,8 +92,7 @@ class WindowShadow(QObject):
return False
-def buildTiles() -> list[Tile]:
- """按 attach 请求的顺序:左、左上、上、右上、右、右下、下、左下。"""
+def buildTiles() -> Tiles:
left, top, right, bottom = PADDING
def cdf(z: float) -> float:
@@ -114,16 +133,16 @@ def buildTiles() -> list[Tile]:
leftColumns, rightColumns, middle = range(-left, INSET), range(-INSET, right), range(1)
topRows, bottomRows = range(-top, INSET), range(-INSET, bottom)
- return [
- build(range(-left, 0), "left", middle, "middle"),
- build(leftColumns, "left", topRows, "top"),
- build(middle, "middle", range(-top, 0), "top"),
- build(rightColumns, "right", topRows, "top"),
- build(range(right), "right", middle, "middle"),
- build(rightColumns, "right", bottomRows, "bottom"),
- build(middle, "middle", range(bottom), "bottom"),
- build(leftColumns, "left", bottomRows, "bottom"),
- ]
+ return Tiles(
+ left=build(range(-left, 0), "left", middle, "middle"),
+ topLeft=build(leftColumns, "left", topRows, "top"),
+ top=build(middle, "middle", range(-top, 0), "top"),
+ topRight=build(rightColumns, "right", topRows, "top"),
+ right=build(range(right), "right", middle, "middle"),
+ bottomRight=build(rightColumns, "right", bottomRows, "bottom"),
+ bottom=build(middle, "middle", range(bottom), "bottom"),
+ bottomLeft=build(leftColumns, "left", bottomRows, "bottom"),
+ )
# libwayland-client 的 C 结构,PySide6 没有 Wayland 绑定
@@ -304,7 +323,10 @@ def createKwinShadow() -> KwinShadow | None:
shm, _ = bind(b"wl_shm", interface("wl_shm_interface"), 1)
wayland.wl_proxy_destroy(registry)
- tiles = buildTiles()
+ built = buildTiles()
+ # attach 请求的顺序
+ tiles = (built.left, built.topLeft, built.top, built.topRight,
+ built.right, built.bottomRight, built.bottom, built.bottomLeft)
fd = os.memfd_create("ghost-downloader-shadow")
try:
for tile in tiles:
@@ -321,3 +343,114 @@ def createKwinShadow() -> KwinShadow | None:
marshal(wayland, pool, 1, [], flags=WL_MARSHAL_FLAG_DESTROY)
wayland.wl_display_flush(display)
return KwinShadow(wayland, display, surfaceByWindow, protocol, manager, managerVersion, buffers)
+
+
+# libxcb 的 C 结构,只声明用到的字段
+class XcbScreenIterator(Structure):
+ _fields_ = [("data", POINTER(c_uint32)), ("rem", c_int), ("index", c_int)] # data 首字段是 root
+
+
+class XcbInternAtomReply(Structure):
+ _fields_ = [("response_type", c_uint8), ("pad0", c_uint8), ("sequence", c_uint16),
+ ("length", c_uint32), ("atom", c_uint32)]
+
+
+XCB_ATOM_ATOM, XCB_ATOM_CARDINAL = 4, 6
+XCB_PROP_MODE_REPLACE, XCB_IMAGE_FORMAT_Z_PIXMAP = 0, 2
+
+
+class X11Shadow:
+ """_KDE_NET_WM_SHADOW:把贴图 pixmap 写进窗口属性,由 KWin 画在窗口外。"""
+
+ def __init__(self, xcb: CDLL, connection: int, atom: int, data):
+ self._xcb = xcb
+ self._connection = connection
+ self._atom = atom
+ self._data = data
+
+ def set(self, window: QWindow) -> None:
+ # X 窗口 id 在隐藏和显示之间不变,属性设一次即可
+ self._xcb.xcb_change_property(self._connection, XCB_PROP_MODE_REPLACE, window.winId(), self._atom,
+ XCB_ATOM_CARDINAL, 32, len(self._data), self._data)
+ self._xcb.xcb_flush(self._connection)
+
+
+@cache
+def createX11Shadow() -> X11Shadow | None:
+ try:
+ xcb = CDLL("libxcb.so.1")
+ except OSError as e:
+ logger.warning("无法加载窗口阴影所需的 libxcb: {}", e)
+ return None
+ free = CDLL(None).free
+ free.argtypes = [c_void_p]
+
+ # cookie 是只含一个 unsigned int 的结构体,按值传递时与 c_uint 一致
+ xcb.xcb_get_setup.restype = c_void_p
+ xcb.xcb_get_setup.argtypes = [c_void_p]
+ xcb.xcb_setup_roots_iterator.restype = XcbScreenIterator
+ xcb.xcb_setup_roots_iterator.argtypes = [c_void_p]
+ xcb.xcb_intern_atom.restype = c_uint
+ xcb.xcb_intern_atom.argtypes = [c_void_p, c_uint8, c_uint16, c_char_p]
+ xcb.xcb_intern_atom_reply.restype = POINTER(XcbInternAtomReply)
+ xcb.xcb_intern_atom_reply.argtypes = [c_void_p, c_uint, c_void_p]
+ xcb.xcb_get_property.restype = c_uint
+ xcb.xcb_get_property.argtypes = [c_void_p, c_uint8, c_uint32, c_uint32, c_uint32, c_uint32, c_uint32]
+ xcb.xcb_get_property_reply.restype = c_void_p
+ xcb.xcb_get_property_reply.argtypes = [c_void_p, c_uint, c_void_p]
+ xcb.xcb_get_property_value.restype = POINTER(c_uint32)
+ xcb.xcb_get_property_value.argtypes = [c_void_p]
+ xcb.xcb_get_property_value_length.restype = c_int
+ xcb.xcb_get_property_value_length.argtypes = [c_void_p]
+ xcb.xcb_generate_id.restype = c_uint32
+ xcb.xcb_generate_id.argtypes = [c_void_p]
+ xcb.xcb_create_pixmap.argtypes = [c_void_p, c_uint8, c_uint32, c_uint32, c_uint16, c_uint16]
+ xcb.xcb_create_gc.argtypes = [c_void_p, c_uint32, c_uint32, c_uint32, c_void_p]
+ xcb.xcb_put_image.argtypes = [c_void_p, c_uint8, c_uint32, c_uint32, c_uint16, c_uint16,
+ c_int16, c_int16, c_uint8, c_uint8, c_uint32, c_char_p]
+ xcb.xcb_free_gc.argtypes = [c_void_p, c_uint32]
+ xcb.xcb_change_property.argtypes = [c_void_p, c_uint8, c_uint32, c_uint32, c_uint32, c_uint8, c_uint32, c_void_p]
+ xcb.xcb_flush.argtypes = [c_void_p]
+
+ connection = QGuiApplication.instance().nativeInterface().connection()
+ root = xcb.xcb_setup_roots_iterator(xcb.xcb_get_setup(connection)).data[0]
+
+ def internAtom(name: bytes) -> int:
+ reply = xcb.xcb_intern_atom_reply(connection, xcb.xcb_intern_atom(connection, 0, len(name), name), None)
+ if not reply:
+ return 0
+ atom = reply.contents.atom
+ free(reply)
+ return atom
+
+ def supportedAtoms() -> set[int]:
+ netSupported = internAtom(b"_NET_SUPPORTED")
+ reply = xcb.xcb_get_property_reply(
+ connection, xcb.xcb_get_property(connection, 0, root, netSupported, XCB_ATOM_ATOM, 0, 4096), None)
+ if not reply:
+ return set()
+ values = xcb.xcb_get_property_value(reply)
+ atoms = {values[i] for i in range(xcb.xcb_get_property_value_length(reply) // 4)}
+ free(reply)
+ return atoms
+
+ atom = internAtom(b"_KDE_NET_WM_SHADOW")
+ if atom not in supportedAtoms():
+ return None
+
+ # pixmap 由所有窗口共用、存活到进程退出;KWin 读属性时才复制内容
+ built = buildTiles()
+ pixmaps = []
+ for tile in (built.top, built.topRight, built.right, built.bottomRight,
+ built.bottom, built.bottomLeft, built.left, built.topLeft):
+ pixmap, gc = xcb.xcb_generate_id(connection), xcb.xcb_generate_id(connection)
+ xcb.xcb_create_pixmap(connection, 32, pixmap, root, tile.width, tile.height)
+ xcb.xcb_create_gc(connection, gc, pixmap, 0, None)
+ xcb.xcb_put_image(connection, XCB_IMAGE_FORMAT_Z_PIXMAP, pixmap, gc, tile.width, tile.height,
+ 0, 0, 0, 32, len(tile.pixels), tile.pixels)
+ xcb.xcb_free_gc(connection, gc)
+ pixmaps.append(pixmap)
+ left, top, right, bottom = PADDING
+ data = (c_uint32 * 12)(*pixmaps, top, right, bottom, left)
+ xcb.xcb_flush(connection)
+ return X11Shadow(xcb, connection, atom, data)
diff --git a/docs/adr/0030-linux-window-shadow-via-kwin-protocol.md b/docs/adr/0030-linux-window-shadow-via-kwin-protocol.md
index b2d0359d..547651bb 100644
--- a/docs/adr/0030-linux-window-shadow-via-kwin-protocol.md
+++ b/docs/adr/0030-linux-window-shadow-via-kwin-protocol.md
@@ -1,14 +1,14 @@
-# Linux 窗口阴影只走 KWin 的 org_kde_kwin_shadow 协议
+# Linux 窗口阴影只走 KWin 的阴影协议
-Linux 上的窗口都是无边框窗口(qframelesswindow 设了 `FramelessWindowHint`),合成器不给它们画装饰,也就没有阴影。我们改用 KWin 专有的 `org_kde_kwin_shadow` 协议:把 8 块阴影贴图交给 KWin,由它画在 surface 外面。窗口几何不变,贴边、分屏、最大化都由合成器照常处理。这与 KDE 自己的 `KWindowShadow` 做法相同(Breeze 的菜单阴影就是这样实现的)。外观固定照搬 Breeze 默认的 Large 档,不读 `breezerc`。
+Linux 上的窗口都是无边框窗口(qframelesswindow 设了 `FramelessWindowHint`),合成器不给它们画装饰,也就没有阴影。我们改用 KWin 专有的阴影协议:Wayland 上是 `org_kde_kwin_shadow`,X11(含 XWayland)上是窗口属性 `_KDE_NET_WM_SHADOW`。两者都是把 8 块阴影贴图交给 KWin,由它画在窗口外面。窗口几何不变,贴边、分屏、最大化都由合成器照常处理。这与 KDE 自己的 `KWindowShadow` 在两个平台上的做法相同(Breeze 的菜单阴影就是这样实现的)。外观固定照搬 Breeze 默认的 Large 档,不读 `breezerc`。
-只有注册表里存在 `org_kde_kwin_shadow_manager` 时才生效。GNOME、wlroots 系和 X11 都维持没有阴影。目前只有 KWin 实现了这个协议;以后有别的合成器实现,会自动生效。
+Wayland 上要求注册表里存在 `org_kde_kwin_shadow_manager`,X11 上要求根窗口的 `_NET_SUPPORTED` 里有 `_KDE_NET_WM_SHADOW`,满足才生效。GNOME、wlroots 系和其他 X11 窗口管理器维持没有阴影。目前只有 KWin 支持这两种机制;以后有别的合成器支持,会自动生效。
-PySide6 不提供取窗口 `wl_surface` 的接口,系统里的 KF6 `KWindowShadow` 又链接系统 Qt,与 PySide6 自带的 Qt 冲突,所以只能用 ctypes 调 Qt 私有符号(与 ADR 0019 同理):先用 `QWindow::resolveInterface("QWaylandWindow", 1)` 取得私有接口,它是 `QtWaylandClient::QWaylandWindow` 的首个基类,指针可以直接作为 `this` 调用 `QWaylandWindow::wlSurface()`。缺少符号时只记一条警告,不画阴影。
+PySide6 不提供取窗口 `wl_surface` 的接口,系统里的 KF6 `KWindowShadow` 又链接系统 Qt,与 PySide6 自带的 Qt 冲突,所以只能用 ctypes 调 Qt 私有符号(与 ADR 0019 同理):先用 `QWindow::resolveInterface("QWaylandWindow", 1)` 取得私有接口,它是 `QtWaylandClient::QWaylandWindow` 的首个基类,指针可以直接作为 `this` 调用 `QWaylandWindow::wlSurface()`。缺少符号时只记一条警告,不画阴影。X11 不需要私有符号:xcb 连接取自公开的 `QX11Application`,窗口 id 就是 `winId()`。
## Considered Options
-- **自绘阴影**:开启透明背景,在窗口四周留出透明边距自己画阴影。Wayland 下必须调用 `xdg_surface.set_window_geometry` 告诉合成器窗口实际在哪,而 Qt 只在私有的 `setCustomMargins` 里调用它;不告诉的话,贴边和分屏会把透明边距算进窗口,留出一圈缝。GNOME 下也只能走这条路。
+- **自绘阴影**:开启透明背景,在窗口四周留出透明边距自己画阴影。X11 上这是 GTK 的做法,再用 `_GTK_FRAME_EXTENTS` 告诉窗口管理器边距多宽,跨窗口管理器通用,但没有合成器时会出问题。Wayland 下必须调用 `xdg_surface.set_window_geometry` 告诉合成器窗口实际在哪,而 Qt 只在私有的 `setCustomMargins` 里调用它;不告诉的话,贴边和分屏会把透明边距算进窗口,留出一圈缝。GNOME 下也只能走这条路。
- **改用系统标题栏**:KDE 会画原生边框和阴影,但 Fluent 标题栏和挂在上面的搜索框都要放弃,与 Windows、macOS 的观感也不再一致。
- **Plasma 6.8 的"Window manager draws titlebar, frame, and shadows"窗口规则**:在 Wayland 上不会自动生效,需要用户手动配置,也没有让客户端主动申请的协议。
- **同时自绘圆角**:Linux 上没有让合成器裁圆角的协议,只能自绘,与上面放弃自绘阴影的理由相同。窗口保持直角。
@@ -16,5 +16,5 @@ PySide6 不提供取窗口 `wl_surface` 的接口,系统里的 KF6 `KWindowSha
## Consequences
- 升级 PySide6 后,如果 Linux 上的阴影消失,先查日志里有没有符号加载失败的警告。
-- Qt 隐藏窗口时会回收 `wl_surface`,所以阴影在 `Hide` 时销毁,在下一次 `Expose` 时重建。
-- 验证只能在 KDE Wayland 上手动做:主窗口和对话框都有阴影;隐藏后再显示,阴影还在;最大化、贴边、分屏时窗口不留缝;在 GNOME 或 X11 下启动不报错。
+- Wayland 上 Qt 隐藏窗口时会回收 `wl_surface`,所以阴影在 `Hide` 时销毁,在下一次 `Expose` 时重建。X11 窗口 id 在隐藏和显示之间不变,属性设一次即可;如果以后要重建平台窗口(比如改窗口标志),需要重新设置。
+- 验证要在 KDE 上手动做,Wayland 和 X11 各跑一遍(X11 用 `QT_QPA_PLATFORM=xcb` 启动即可):主窗口和对话框都有阴影;隐藏后再显示,阴影还在;最大化、贴边、分屏时窗口不留缝;在 GNOME 下启动不报错。