Compare commits

...

3 Commits

Author SHA1 Message Date
0c7fdf08df backend: X11 后端 window 信息查询接口使用原始封装代替语义化封装
- 拆分 window_get_skip_taskbar / window_get_urgency /
  window_get_states / window_get_geometry_mode 为更底层的
  window_get_atom_array / window_get_wm_hints / atom_to_window_state
- window_get_fixed_size 改为输出参数风格
- 扁平化 window_map_request_event_t,内联原 window_props_t 和
  window_geometry_t 的字段
- window_props_t → window_layer_props_t,仅保留 layer 分类所需的
  types/states
2026-04-09 05:08:39 +08:00
3884d3fc50 backend: X11 后端添加窗口几何信息查询并修正错误语义
- 添加 window_get_geometry/window_get_fixed_size/window_get_geometry_mode
- window_get_atom_array 改为 bool 返回,区分 XCB 调用错误和空数据
- window_get_types/window_get_states 属性为空时返回 true 而非 false
- handle_map_request 重排操作顺序:错误检查提前,失败路径无内存泄漏
- 补充 _NET_WM_STATE_MAXIMIZED_VERT/HORZ atom 注册
2026-04-09 02:23:06 +08:00
0edb6ad6aa backend: X11 后端添加窗口属性和状态查询接口 2026-04-08 19:32:53 +08:00
9 changed files with 356 additions and 26 deletions

View File

@@ -35,6 +35,56 @@ static void atoms_init(backend_t *backend) {
{"WM_WINDOW_ROLE", sizeof("WM_WINDOW_ROLE") - 1, &atoms->WM_WINDOW_ROLE},
{"WM_NAME", sizeof("WM_NAME") - 1, &atoms->WM_NAME},
{"_NET_WM_NAME", sizeof("_NET_WM_NAME") - 1, &atoms->_NET_WM_NAME},
{"_NET_WM_STATE", sizeof("_NET_WM_STATE") - 1, &atoms->_NET_WM_STATE},
{"_NET_WM_STATE_FULLSCREEN", sizeof("_NET_WM_STATE_FULLSCREEN") - 1,
&atoms->_NET_WM_STATE_FULLSCREEN},
{"_NET_WM_STATE_ABOVE", sizeof("_NET_WM_STATE_ABOVE") - 1,
&atoms->_NET_WM_STATE_ABOVE},
{"_NET_WM_STATE_STICKY", sizeof("_NET_WM_STATE_STICKY") - 1,
&atoms->_NET_WM_STATE_STICKY},
{"_NET_WM_STATE_MODAL", sizeof("_NET_WM_STATE_MODAL") - 1,
&atoms->_NET_WM_STATE_MODAL},
{"_NET_WM_STATE_SKIP_TASKBAR", sizeof("_NET_WM_STATE_SKIP_TASKBAR") - 1,
&atoms->_NET_WM_STATE_SKIP_TASKBAR},
{"_NET_WM_STATE_MAXIMIZED_VERT", sizeof("_NET_WM_STATE_MAXIMIZED_VERT") - 1,
&atoms->_NET_WM_STATE_MAXIMIZED_VERT},
{"_NET_WM_STATE_MAXIMIZED_HORZ", sizeof("_NET_WM_STATE_MAXIMIZED_HORZ") - 1,
&atoms->_NET_WM_STATE_MAXIMIZED_HORZ},
{"_NET_WM_WINDOW_TYPE", sizeof("_NET_WM_WINDOW_TYPE") - 1,
&atoms->_NET_WM_WINDOW_TYPE},
{"_NET_WM_WINDOW_TYPE_NORMAL", sizeof("_NET_WM_WINDOW_TYPE_NORMAL") - 1,
&atoms->_NET_WM_WINDOW_TYPE_NORMAL},
{"_NET_WM_WINDOW_TYPE_DESKTOP", sizeof("_NET_WM_WINDOW_TYPE_DESKTOP") - 1,
&atoms->_NET_WM_WINDOW_TYPE_DESKTOP},
{"_NET_WM_WINDOW_TYPE_DOCK", sizeof("_NET_WM_WINDOW_TYPE_DOCK") - 1,
&atoms->_NET_WM_WINDOW_TYPE_DOCK},
{"_NET_WM_WINDOW_TYPE_TOOLBAR", sizeof("_NET_WM_WINDOW_TYPE_TOOLBAR") - 1,
&atoms->_NET_WM_WINDOW_TYPE_TOOLBAR},
{"_NET_WM_WINDOW_TYPE_DIALOG", sizeof("_NET_WM_WINDOW_TYPE_DIALOG") - 1,
&atoms->_NET_WM_WINDOW_TYPE_DIALOG},
{"_NET_WM_WINDOW_TYPE_UTILITY", sizeof("_NET_WM_WINDOW_TYPE_UTILITY") - 1,
&atoms->_NET_WM_WINDOW_TYPE_UTILITY},
{"_NET_WM_WINDOW_TYPE_SPLASH", sizeof("_NET_WM_WINDOW_TYPE_SPLASH") - 1,
&atoms->_NET_WM_WINDOW_TYPE_SPLASH},
{"_NET_WM_WINDOW_TYPE_MENU", sizeof("_NET_WM_WINDOW_TYPE_MENU") - 1,
&atoms->_NET_WM_WINDOW_TYPE_MENU},
{"_NET_WM_WINDOW_TYPE_DROPDOWN_MENU",
sizeof("_NET_WM_WINDOW_TYPE_DROPDOWN_MENU") - 1,
&atoms->_NET_WM_WINDOW_TYPE_DROPDOWN_MENU},
{"_NET_WM_WINDOW_TYPE_POPUP_MENU",
sizeof("_NET_WM_WINDOW_TYPE_POPUP_MENU") - 1,
&atoms->_NET_WM_WINDOW_TYPE_POPUP_MENU},
{"_NET_WM_WINDOW_TYPE_TOOLTIP", sizeof("_NET_WM_WINDOW_TYPE_TOOLTIP") - 1,
&atoms->_NET_WM_WINDOW_TYPE_TOOLTIP},
{"_NET_WM_WINDOW_TYPE_COMBO", sizeof("_NET_WM_WINDOW_TYPE_COMBO") - 1,
&atoms->_NET_WM_WINDOW_TYPE_COMBO},
{"_NET_WM_WINDOW_TYPE_DND", sizeof("_NET_WM_WINDOW_TYPE_DND") - 1,
&atoms->_NET_WM_WINDOW_TYPE_DND},
{"_NET_WM_WINDOW_TYPE_NOTIFICATION",
sizeof("_NET_WM_WINDOW_TYPE_NOTIFICATION") - 1,
&atoms->_NET_WM_WINDOW_TYPE_NOTIFICATION},
};
xcb_intern_atom_cookie_t cookies[countof(atom_list)];

View File

@@ -3,26 +3,123 @@
#include <stdio.h>
#include <xcb/xcb.h>
#include <xcb/xcb_event.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xproto.h>
#include "backend/x11/window.h"
#include "base/memory.h"
#include "core/backend.h"
#include "core/types.h"
#include "core/window.h"
#include "internal.h" /* IWYU pragma: keep */
static window_state_t *derive_window_states(const atoms_t *atoms,
const xcb_atom_t *raw,
uint32_t count, size_t *out_count) {
if (!raw || count == 0) {
*out_count = 0;
return nullptr;
}
window_state_t *buf = p_new(window_state_t, count);
size_t valid = 0;
for (uint32_t i = 0; i < count; i++) {
window_state_t s = atom_to_window_state(atoms, raw[i]);
if (s != (window_state_t)-1) buf[valid++] = s;
}
if (valid == 0) {
p_delete(&buf);
*out_count = 0;
return nullptr;
}
*out_count = valid;
return buf;
}
static bool derive_skip_taskbar(const atoms_t *atoms, const xcb_atom_t *raw,
uint32_t count) {
for (uint32_t i = 0; i < count; i++)
if (raw[i] == atoms->_NET_WM_STATE_SKIP_TASKBAR) return true;
return false;
}
static window_geometry_mode_t geometry_mode_from_hints(
const xcb_icccm_wm_hints_t *hints) {
if (hints->initial_state == XCB_ICCCM_WM_STATE_ICONIC)
return ZDWM_GEOMETRY_MINIMIZED;
return ZDWM_GEOMETRY_NORMAL;
}
static window_geometry_mode_t geometry_mode_from_states(
const atoms_t *atoms, const xcb_atom_t *state_atoms, uint32_t state_count) {
for (uint32_t i = 0; i < state_count; i++) {
if (state_atoms[i] == atoms->_NET_WM_STATE_FULLSCREEN)
return ZDWM_GEOMETRY_FULLSCREEN;
if (state_atoms[i] == atoms->_NET_WM_STATE_MAXIMIZED_VERT ||
state_atoms[i] == atoms->_NET_WM_STATE_MAXIMIZED_HORZ)
return ZDWM_GEOMETRY_MAXIMIZED;
}
return ZDWM_GEOMETRY_NORMAL;
}
static bool handle_map_request(backend_t *backend, event_t *event,
const xcb_map_request_event_t *xcb_event) {
p_clear(event, 1);
xcb_window_t window = xcb_event->window;
p_clear(event, 1);
event->type = ZDWM_EVENT_WINDOW_MAP_REQUEST;
event->data.window_map_request.window = window;
window_metadata_t *metadata = &event->data.window_map_request.metadata;
metadata->role = window_get_role(backend, window);
metadata->title = window_get_title(backend, window);
window_get_class(backend, window, &metadata->class_name,
&metadata->instance_name);
/* TODO: */
window_map_request_event_t *ev = &event->data.window_map_request;
ev->window = (window_id_t)window;
ev->transient_for = window_get_transient_for(backend, window);
ev->geometry_mode = ZDWM_GEOMETRY_NORMAL;
xcb_get_window_attributes_reply_t *wa =
window_get_attributes(backend, window);
if (!wa) return false;
ev->override_redirect = (bool)wa->override_redirect;
p_delete(&wa);
xcb_icccm_wm_hints_t wm_hints = {0};
if (window_get_wm_hints(backend, window, &wm_hints)) {
ev->urgent = (bool)xcb_icccm_wm_hints_get_urgency(&wm_hints);
ev->geometry_mode = geometry_mode_from_hints(&wm_hints);
}
if (!window_get_fixed_size(backend, window, &ev->fixed_size)) return false;
if (!window_get_geometry(backend, window, &ev->rect)) return false;
const atoms_t *atoms = &backend->atoms;
xcb_atom_t *state_atoms = nullptr;
uint32_t state_count = 0;
if (!window_get_atom_array(backend, window, atoms->_NET_WM_STATE,
&state_atoms, &state_count)) {
return false;
}
if (ev->geometry_mode == ZDWM_GEOMETRY_NORMAL && state_atoms) {
ev->geometry_mode =
geometry_mode_from_states(atoms, state_atoms, state_count);
}
if (state_atoms) {
ev->skip_taskbar = derive_skip_taskbar(atoms, state_atoms, state_count);
ev->props.states = derive_window_states(atoms, state_atoms, state_count,
&ev->props.state_count);
}
p_delete(&state_atoms);
window_layer_props_t *props = &ev->props;
if (!window_get_types(backend, window, &props->types, &props->type_count)) {
p_delete(&props->states);
return false;
}
ev->metadata.role = window_get_role(backend, window);
ev->metadata.title = window_get_title(backend, window);
window_get_class(backend, window, &ev->metadata.class_name,
&ev->metadata.instance_name);
return true;
}

View File

@@ -11,6 +11,30 @@ typedef struct atoms_t {
xcb_atom_t WM_WINDOW_ROLE;
xcb_atom_t WM_NAME;
xcb_atom_t _NET_WM_NAME;
xcb_atom_t _NET_WM_STATE;
xcb_atom_t _NET_WM_STATE_FULLSCREEN;
xcb_atom_t _NET_WM_STATE_ABOVE;
xcb_atom_t _NET_WM_STATE_STICKY;
xcb_atom_t _NET_WM_STATE_MODAL;
xcb_atom_t _NET_WM_STATE_SKIP_TASKBAR;
xcb_atom_t _NET_WM_STATE_MAXIMIZED_VERT;
xcb_atom_t _NET_WM_STATE_MAXIMIZED_HORZ;
xcb_atom_t _NET_WM_WINDOW_TYPE;
xcb_atom_t _NET_WM_WINDOW_TYPE_NORMAL;
xcb_atom_t _NET_WM_WINDOW_TYPE_DESKTOP;
xcb_atom_t _NET_WM_WINDOW_TYPE_DOCK;
xcb_atom_t _NET_WM_WINDOW_TYPE_TOOLBAR;
xcb_atom_t _NET_WM_WINDOW_TYPE_DIALOG;
xcb_atom_t _NET_WM_WINDOW_TYPE_UTILITY;
xcb_atom_t _NET_WM_WINDOW_TYPE_SPLASH;
xcb_atom_t _NET_WM_WINDOW_TYPE_MENU;
xcb_atom_t _NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
xcb_atom_t _NET_WM_WINDOW_TYPE_POPUP_MENU;
xcb_atom_t _NET_WM_WINDOW_TYPE_TOOLTIP;
xcb_atom_t _NET_WM_WINDOW_TYPE_COMBO;
xcb_atom_t _NET_WM_WINDOW_TYPE_DND;
xcb_atom_t _NET_WM_WINDOW_TYPE_NOTIFICATION;
} atoms_t;
struct backend_t {

View File

@@ -61,3 +61,148 @@ void window_get_class(backend_t *backend, xcb_window_t window, char **class_out,
xcb_icccm_get_wm_class_reply_wipe(&prop);
}
xcb_window_t window_get_transient_for(backend_t *backend, xcb_window_t window) {
xcb_connection_t *conn = backend->conn;
xcb_get_property_cookie_t cookie =
xcb_icccm_get_wm_transient_for_unchecked(conn, window);
xcb_window_t trans_for = XCB_WINDOW_NONE;
if (xcb_icccm_get_wm_transient_for_reply(conn, cookie, &trans_for, nullptr)) {
return trans_for;
}
return XCB_WINDOW_NONE;
}
xcb_get_window_attributes_reply_t *window_get_attributes(backend_t *backend,
xcb_window_t window) {
xcb_get_window_attributes_cookie_t cookie =
xcb_get_window_attributes(backend->conn, window);
return xcb_get_window_attributes_reply(backend->conn, cookie, nullptr);
}
static window_type_t atom_to_window_type(const atoms_t *atoms,
xcb_atom_t atom) {
if (atom == atoms->_NET_WM_WINDOW_TYPE_DESKTOP)
return ZDWM_WINDOW_TYPE_DESKTOP;
if (atom == atoms->_NET_WM_WINDOW_TYPE_DOCK) return ZDWM_WINDOW_TYPE_DOCK;
if (atom == atoms->_NET_WM_WINDOW_TYPE_TOOLBAR)
return ZDWM_WINDOW_TYPE_TOOLBAR;
if (atom == atoms->_NET_WM_WINDOW_TYPE_DIALOG) return ZDWM_WINDOW_TYPE_DIALOG;
if (atom == atoms->_NET_WM_WINDOW_TYPE_UTILITY)
return ZDWM_WINDOW_TYPE_UTILITY;
if (atom == atoms->_NET_WM_WINDOW_TYPE_SPLASH) return ZDWM_WINDOW_TYPE_SPLASH;
if (atom == atoms->_NET_WM_WINDOW_TYPE_MENU) return ZDWM_WINDOW_TYPE_MENU;
if (atom == atoms->_NET_WM_WINDOW_TYPE_DROPDOWN_MENU)
return ZDWM_WINDOW_TYPE_DROPDOWN_MENU;
if (atom == atoms->_NET_WM_WINDOW_TYPE_POPUP_MENU)
return ZDWM_WINDOW_TYPE_POPUP_MENU;
if (atom == atoms->_NET_WM_WINDOW_TYPE_TOOLTIP)
return ZDWM_WINDOW_TYPE_TOOLTIP;
if (atom == atoms->_NET_WM_WINDOW_TYPE_COMBO) return ZDWM_WINDOW_TYPE_COMBO;
if (atom == atoms->_NET_WM_WINDOW_TYPE_DND) return ZDWM_WINDOW_TYPE_DND;
if (atom == atoms->_NET_WM_WINDOW_TYPE_NOTIFICATION)
return ZDWM_WINDOW_TYPE_NOTIFICATION;
return ZDWM_WINDOW_TYPE_NORMAL;
}
bool window_get_types(backend_t *backend, xcb_window_t window,
window_type_t **types, size_t *count) {
uint32_t atom_count = 0;
xcb_atom_t *atoms = nullptr;
if (!window_get_atom_array(backend, window,
backend->atoms._NET_WM_WINDOW_TYPE, &atoms,
&atom_count)) {
return false;
}
if (!atoms) {
*types = nullptr;
*count = 0;
return true;
}
*count = atom_count;
*types = p_new(window_type_t, atom_count);
for (uint32_t i = 0; i < atom_count; i++) {
(*types)[i] = atom_to_window_type(&backend->atoms, atoms[i]);
}
p_delete(&atoms);
return true;
}
bool window_get_fixed_size(backend_t *backend, xcb_window_t window, bool *out) {
xcb_size_hints_t hints = {0};
xcb_connection_t *conn = backend->conn;
xcb_get_property_cookie_t cookie =
xcb_icccm_get_wm_normal_hints_unchecked(conn, window);
if (!xcb_icccm_get_wm_normal_hints_reply(conn, cookie, &hints, nullptr)) {
return false;
}
if ((hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) &&
(hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)) {
*out = hints.min_width == hints.max_width &&
hints.min_height == hints.max_height;
} else {
*out = false;
}
return true;
}
bool window_get_geometry(backend_t *backend, xcb_window_t window, rect_t *out) {
xcb_get_geometry_cookie_t cookie = xcb_get_geometry(backend->conn, window);
xcb_get_geometry_reply_t *reply =
xcb_get_geometry_reply(backend->conn, cookie, nullptr);
if (!reply) return false;
out->x = reply->x;
out->y = reply->y;
out->width = reply->width;
out->height = reply->height;
p_delete(&reply);
return true;
}
bool window_get_atom_array(backend_t *backend, xcb_window_t window,
xcb_atom_t property, xcb_atom_t **out_atoms,
uint32_t *out_len) {
xcb_get_property_cookie_t cookie = xcb_get_property_unchecked(
backend->conn, false, window, property, XCB_ATOM_ATOM, 0, UINT32_MAX);
xcb_get_property_reply_t *reply =
xcb_get_property_reply(backend->conn, cookie, nullptr);
if (!reply) return false;
if (reply->type == XCB_ATOM_ATOM && reply->format == 32 && reply->value_len) {
*out_len = reply->value_len;
*out_atoms =
p_copy((xcb_atom_t *)xcb_get_property_value(reply), reply->value_len);
} else {
*out_len = 0;
*out_atoms = nullptr;
}
p_delete(&reply);
return true;
}
bool window_get_wm_hints(backend_t *backend, xcb_window_t window,
xcb_icccm_wm_hints_t *out) {
xcb_get_property_cookie_t cookie =
xcb_icccm_get_wm_hints_unchecked(backend->conn, window);
if (!xcb_icccm_get_wm_hints_reply(backend->conn, cookie, out, nullptr)) {
p_clear(out, 1);
return false;
}
return true;
}
window_state_t atom_to_window_state(const atoms_t *atoms, xcb_atom_t atom) {
if (atom == atoms->_NET_WM_STATE_FULLSCREEN)
return ZDWM_WINDOW_STATE_FULLSCREEN;
if (atom == atoms->_NET_WM_STATE_ABOVE) return ZDWM_WINDOW_STATE_ABOVE;
if (atom == atoms->_NET_WM_STATE_STICKY) return ZDWM_WINDOW_STATE_STICKY;
if (atom == atoms->_NET_WM_STATE_MODAL) return ZDWM_WINDOW_STATE_MODAL;
return (window_state_t)-1;
}

View File

@@ -1,8 +1,14 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xproto.h>
#include "core/backend.h"
#include "core/window.h"
typedef struct atoms_t atoms_t;
/**
* @brief 获取窗口的 role 属性
@@ -31,3 +37,16 @@ char *window_get_title(backend_t *backend, xcb_window_t window);
*/
void window_get_class(backend_t *backend, xcb_window_t window, char **class_out,
char **instance_out);
xcb_window_t window_get_transient_for(backend_t *backend, xcb_window_t window);
xcb_get_window_attributes_reply_t *window_get_attributes(backend_t *backend,
xcb_window_t window);
bool window_get_types(backend_t *backend, xcb_window_t window,
window_type_t **types, size_t *count);
bool window_get_fixed_size(backend_t *backend, xcb_window_t window, bool *out);
bool window_get_geometry(backend_t *backend, xcb_window_t window, rect_t *out);
bool window_get_atom_array(backend_t *backend, xcb_window_t window,
xcb_atom_t property, xcb_atom_t **out_atoms,
uint32_t *out_len);
bool window_get_wm_hints(backend_t *backend, xcb_window_t window,
xcb_icccm_wm_hints_t *out);
window_state_t atom_to_window_state(const atoms_t *atoms, xcb_atom_t atom);

View File

@@ -64,9 +64,16 @@ typedef struct pointer_enter_event_t {
typedef struct window_map_request_event_t {
window_id_t window;
window_props_t props;
window_id_t transient_for;
bool override_redirect;
bool skip_taskbar;
bool urgent;
bool fixed_size;
window_geometry_mode_t geometry_mode;
rect_t rect;
window_layer_props_t props;
window_metadata_t metadata;
window_geometry_mode_t geometry;
} window_map_request_event_t;
typedef enum window_remove_reason_t {

View File

@@ -88,7 +88,7 @@ bool layer_stack_lower(layer_stack_t *layer, window_id_t window) {
return true;
}
layer_type_t layer_classify(const window_props_t *props) {
layer_type_t layer_classify(const window_layer_props_t *props) {
for (size_t i = 0; i < props->type_count; ++i) {
window_type_t type = props->types[i];
if (type == ZDWM_WINDOW_TYPE_NOTIFICATION) return ZDWM_WINDOW_LAYER_OVERLAY;

View File

@@ -57,4 +57,4 @@ bool layer_stack_raise(layer_stack_t *layer, window_id_t window);
*/
bool layer_stack_lower(layer_stack_t *layer, window_id_t window);
layer_type_t layer_classify(const window_props_t *props);
layer_type_t layer_classify(const window_layer_props_t *props);

View File

@@ -30,19 +30,13 @@ typedef enum window_state_t {
ZDWM_WINDOW_STATE_STICKY,
} window_state_t;
typedef struct window_props_t {
window_id_t transient_for;
bool override_redirect;
bool skip_taskbar;
bool urgent;
typedef struct window_layer_props_t {
window_type_t *types;
size_t type_count;
window_state_t *states;
size_t state_count;
} window_props_t;
} window_layer_props_t;
typedef struct window_metadata_t {
char *title;
@@ -51,9 +45,3 @@ typedef struct window_metadata_t {
char *class_name;
char *instance_name;
} window_metadata_t;
typedef struct window_geometry_t {
window_geometry_mode_t mode;
rect_t rect;
bool fixed_size;
} window_geometry_t;