114 lines
4.7 KiB
Python
114 lines
4.7 KiB
Python
"""Мелкие операции с окнами X11 (в том числе под XWayland), которых нет в Qt.
|
|
|
|
Qt не умеет попросить оконный менеджер «не показывать окно в панели задач» и «не скрывать его при
|
|
показе рабочего стола» — флаг Qt.Tool в KDE Plasma этого не гарантирует. Поэтому работаем с
|
|
EWMH-свойствами напрямую. Нужна только libX11 (есть везде, где есть X11 / XWayland).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import ctypes
|
|
import ctypes.util
|
|
from typing import Optional
|
|
|
|
_CLIENT_MESSAGE = 33
|
|
_SUBSTRUCTURE_MASK = (1 << 19) | (1 << 20) # SubstructureNotify | SubstructureRedirect
|
|
_XA_ATOM = 4
|
|
_PROP_MODE_REPLACE = 0
|
|
|
|
WINDOW_TYPE_ATOMS = {
|
|
"normal": b"_NET_WM_WINDOW_TYPE_NORMAL",
|
|
"dock": b"_NET_WM_WINDOW_TYPE_DOCK",
|
|
"desktop": b"_NET_WM_WINDOW_TYPE_DESKTOP",
|
|
}
|
|
|
|
|
|
class _XClientMessageEvent(ctypes.Structure):
|
|
_fields_ = [
|
|
("type", ctypes.c_int), ("serial", ctypes.c_ulong), ("send_event", ctypes.c_int),
|
|
("display", ctypes.c_void_p), ("window", ctypes.c_ulong), ("message_type", ctypes.c_ulong),
|
|
("format", ctypes.c_int), ("data", ctypes.c_long * 5),
|
|
]
|
|
|
|
|
|
class _XEvent(ctypes.Union):
|
|
_fields_ = [("xclient", _XClientMessageEvent), ("pad", ctypes.c_long * 24)]
|
|
|
|
|
|
class _Conn:
|
|
"""Короткоживущее соединение с X-сервером."""
|
|
|
|
def __init__(self) -> None:
|
|
name = ctypes.util.find_library("X11")
|
|
if not name:
|
|
raise OSError("libX11 не найдена")
|
|
x = ctypes.CDLL(name)
|
|
x.XOpenDisplay.restype = ctypes.c_void_p
|
|
x.XOpenDisplay.argtypes = [ctypes.c_char_p]
|
|
x.XInternAtom.restype = ctypes.c_ulong
|
|
x.XInternAtom.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int]
|
|
x.XDefaultRootWindow.restype = ctypes.c_ulong
|
|
x.XDefaultRootWindow.argtypes = [ctypes.c_void_p]
|
|
x.XSendEvent.restype = ctypes.c_int
|
|
x.XSendEvent.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_long, ctypes.c_void_p]
|
|
x.XChangeProperty.restype = ctypes.c_int
|
|
x.XChangeProperty.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong,
|
|
ctypes.c_int, ctypes.c_int, ctypes.c_void_p, ctypes.c_int]
|
|
x.XFlush.argtypes = [ctypes.c_void_p]
|
|
x.XCloseDisplay.argtypes = [ctypes.c_void_p]
|
|
self.x = x
|
|
self.dpy = x.XOpenDisplay(None)
|
|
if not self.dpy:
|
|
raise OSError("не удалось подключиться к X-серверу")
|
|
|
|
def atom(self, name: bytes) -> int:
|
|
return self.x.XInternAtom(self.dpy, name, 0)
|
|
|
|
def close(self) -> None:
|
|
self.x.XFlush(self.dpy)
|
|
self.x.XCloseDisplay(self.dpy)
|
|
|
|
|
|
def set_skip_taskbar(window_id: int, skip: bool = True) -> bool:
|
|
"""Добавить (или убрать) для окна SKIP_TASKBAR и SKIP_PAGER (окно уже должно быть отображено)."""
|
|
try:
|
|
c = _Conn()
|
|
except OSError:
|
|
return False
|
|
try:
|
|
ev = _XEvent()
|
|
ev.xclient.type = _CLIENT_MESSAGE
|
|
ev.xclient.window = window_id
|
|
ev.xclient.message_type = c.atom(b"_NET_WM_STATE")
|
|
ev.xclient.format = 32
|
|
ev.xclient.data[0] = 1 if skip else 0 # _NET_WM_STATE_ADD / _REMOVE
|
|
ev.xclient.data[1] = c.atom(b"_NET_WM_STATE_SKIP_TASKBAR")
|
|
ev.xclient.data[2] = c.atom(b"_NET_WM_STATE_SKIP_PAGER")
|
|
ev.xclient.data[3] = 1 # источник: обычное приложение
|
|
c.x.XSendEvent(c.dpy, c.x.XDefaultRootWindow(c.dpy), 0, _SUBSTRUCTURE_MASK, ctypes.byref(ev))
|
|
return True
|
|
except Exception: # noqa: BLE001
|
|
return False
|
|
finally:
|
|
c.close()
|
|
|
|
|
|
def set_window_type(window_id: int, kind: str) -> bool:
|
|
"""Задать тип окна (_NET_WM_WINDOW_TYPE). Вызывать ДО показа окна: тогда оконный менеджер
|
|
прочитает его при отображении. Окна типа dock / desktop KDE не скрывает при «Показать рабочий стол»."""
|
|
atom_name: Optional[bytes] = WINDOW_TYPE_ATOMS.get(kind)
|
|
if atom_name is None:
|
|
return False
|
|
try:
|
|
c = _Conn()
|
|
except OSError:
|
|
return False
|
|
try:
|
|
data = (ctypes.c_long * 1)(c.atom(atom_name))
|
|
c.x.XChangeProperty(c.dpy, window_id, c.atom(b"_NET_WM_WINDOW_TYPE"), _XA_ATOM, 32,
|
|
_PROP_MODE_REPLACE, ctypes.byref(data), 1)
|
|
return True
|
|
except Exception: # noqa: BLE001
|
|
return False
|
|
finally:
|
|
c.close()
|