style: 更新 .clang-format 规则并重新格式化所有源文件

- 重构模块使用新的代码风格
- 老代码 (src 目录下的代码) 继续使用之前的代码风格

通过在需要不同代码风格的目录下放对应 .clang-format 配置文件实现
This commit is contained in:
2026-04-14 01:52:53 +08:00
parent 7b5404e8e0
commit 2fc4b33359
62 changed files with 1248 additions and 728 deletions

1
src/backend/.clang-format Symbolic link
View File

@@ -0,0 +1 @@
../../.clang-format

View File

@@ -14,8 +14,8 @@ static inline bool fully_contains(rect_t outer, rect_t inner) {
right(inner) <= right(outer) && bottom(inner) <= bottom(outer);
}
backend_detect_t *output_remove_duplication(const output_info_t *output,
const size_t count) {
backend_detect_t *
output_remove_duplication(const output_info_t *output, const size_t count) {
if (!output || count < 1) return nullptr;
bool *remove = p_new(bool, count);
@@ -42,14 +42,14 @@ backend_detect_t *output_remove_duplication(const output_info_t *output,
}
output_info_t *list = p_new(output_info_t, amount);
amount = 0;
amount = 0;
for (size_t i = 0; i < count; i++) {
if (remove[i]) continue;
list[amount++] = output[i];
}
p_delete(&remove);
backend_detect_t *detect = p_new(backend_detect_t, 1);
detect->outputs = list;
detect->output_count = amount;
detect->outputs = list;
detect->output_count = amount;
return detect;
}

View File

@@ -5,5 +5,5 @@
#include "core/backend.h"
#include "core/types.h"
backend_detect_t *output_remove_duplication(const output_info_t *output,
const size_t count);
backend_detect_t *
output_remove_duplication(const output_info_t *output, const size_t count);

View File

@@ -26,7 +26,7 @@ typedef struct atom_item_t {
static void atoms_init(backend_t *backend) {
xcb_connection_t *conn = backend->conn;
atoms_t *atoms = &backend->atoms;
atoms_t *atoms = &backend->atoms;
#define ATOM_ITEM(name) {#name, sizeof(#name) - 1, &atoms->name},
atom_item_t atom_list[] = {ATOM_LIST(ATOM_ITEM)};
@@ -44,15 +44,15 @@ static void atoms_init(backend_t *backend) {
if (!reply) continue;
atom_item_t *atom = &atom_list[i];
*atom->atom = reply->atom;
*atom->atom = reply->atom;
p_delete(&reply);
}
}
backend_t *backend_create(const char *display_name) {
int screen_num = 0;
int screen_num = 0;
xcb_connection_t *conn = xcb_connect(display_name, &screen_num);
int xcb_conn_error = xcb_connection_has_error(conn);
int xcb_conn_error = xcb_connection_has_error(conn);
if (xcb_conn_error) {
fatal("cannot open display %s, error %d", display_name, xcb_conn_error);
}
@@ -61,7 +61,7 @@ backend_t *backend_create(const char *display_name) {
if (!screen) fatal("cannot get screen info");
xcb_window_t root = screen->root;
uint32_t mask = XCB_CW_EVENT_MASK;
uint32_t mask = XCB_CW_EVENT_MASK;
const xcb_params_cw_t params = {
.event_mask = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
};
@@ -70,20 +70,24 @@ backend_t *backend_create(const char *display_name) {
if (xcb_request_check(conn, cookie)) {
fatal(
"another window manager is already running (cannot select "
"SubstructureRedirect)");
"SubstructureRedirect)"
);
}
backend_t *backend = p_new(backend_t, 1);
backend->conn = conn;
backend->screen = screen;
backend->screenp = screen_num;
backend->conn = conn;
backend->screen = screen;
backend->screenp = screen_num;
xcb_prefetch_extension_data(conn, &xcb_xfixes_id);
const xcb_query_extension_reply_t *query =
xcb_get_extension_data(conn, &xcb_xfixes_id);
if (query && query->present) {
xcb_xfixes_query_version_cookie_t cookie = xcb_xfixes_query_version(
conn, XCB_XFIXES_MAJOR_VERSION, XCB_XFIXES_MINOR_VERSION);
conn,
XCB_XFIXES_MAJOR_VERSION,
XCB_XFIXES_MINOR_VERSION
);
xcb_xfixes_query_version_reply_t *reply =
xcb_xfixes_query_version_reply(conn, cookie, nullptr);
@@ -108,15 +112,21 @@ void backend_destroy(backend_t *backend) {
free(backend);
}
static bool detect_monitor_by_randr(const backend_t *backend,
output_info_t **outputs, size_t *count) {
static bool detect_monitor_by_randr(
const backend_t *backend,
output_info_t **outputs,
size_t *count
) {
xcb_prefetch_extension_data(backend->conn, &xcb_randr_id);
const xcb_query_extension_reply_t *query =
xcb_get_extension_data(backend->conn, &xcb_randr_id);
if (!query || !query->present) return false;
xcb_randr_query_version_cookie_t cookie = xcb_randr_query_version(
backend->conn, XCB_RANDR_MAJOR_VERSION, XCB_RANDR_MINOR_VERSION);
backend->conn,
XCB_RANDR_MAJOR_VERSION,
XCB_RANDR_MINOR_VERSION
);
xcb_randr_query_version_reply_t *reply =
xcb_randr_query_version_reply(backend->conn, cookie, nullptr);
if (!reply) return false;
@@ -149,10 +159,10 @@ static bool detect_monitor_by_randr(const backend_t *backend,
xcb_randr_monitor_info_iterator_t iter =
xcb_randr_get_monitors_monitors_iterator(monitors_reply);
for (int i = 0; iter.rem; xcb_randr_monitor_info_next(&iter), i++) {
output_info_t *output = &output_list[i];
output->geometry.x = iter.data->x;
output->geometry.y = iter.data->y;
output->geometry.width = iter.data->width;
output_info_t *output = &output_list[i];
output->geometry.x = iter.data->x;
output->geometry.y = iter.data->y;
output->geometry.width = iter.data->width;
output->geometry.height = iter.data->height;
xcb_get_atom_name_cookie_t name_cookie =
@@ -161,8 +171,8 @@ static bool detect_monitor_by_randr(const backend_t *backend,
xcb_get_atom_name_reply(backend->conn, name_cookie, nullptr);
if (name_reply) {
char *name = xcb_get_atom_name_name(name_reply);
int length = xcb_get_atom_name_name_length(name_reply);
char *name = xcb_get_atom_name_name(name_reply);
int length = xcb_get_atom_name_name_length(name_reply);
output->name = p_strndup(name, length);
p_delete(&name_reply);
}
@@ -181,15 +191,21 @@ static bool detect_monitor_by_randr(const backend_t *backend,
return true;
}
static bool detect_monitor_by_xinerama(const backend_t *backend,
output_info_t **outputs, size_t *count) {
static bool detect_monitor_by_xinerama(
const backend_t *backend,
output_info_t **outputs,
size_t *count
) {
xcb_prefetch_extension_data(backend->conn, &xcb_xinerama_id);
const xcb_query_extension_reply_t *query =
xcb_get_extension_data(backend->conn, &xcb_xinerama_id);
if (!query || !query->present) return false;
xcb_xinerama_query_version_cookie_t cookie = xcb_xinerama_query_version(
backend->conn, XCB_XINERAMA_MAJOR_VERSION, XCB_XINERAMA_MINOR_VERSION);
backend->conn,
XCB_XINERAMA_MAJOR_VERSION,
XCB_XINERAMA_MINOR_VERSION
);
xcb_xinerama_query_version_reply_t *version_reply =
xcb_xinerama_query_version_reply(backend->conn, cookie, nullptr);
if (!version_reply) return false;
@@ -225,10 +241,10 @@ static bool detect_monitor_by_xinerama(const backend_t *backend,
output_info_t *output_list = p_new(output_info_t, len);
for (int i = 0; i < len; i++) {
output_info_t *output = &output_list[i];
output->geometry.x = screen_info[i].x_org;
output->geometry.y = screen_info[i].y_org;
output->geometry.width = screen_info[i].width;
output_info_t *output = &output_list[i];
output->geometry.x = screen_info[i].x_org;
output->geometry.y = screen_info[i].y_org;
output->geometry.width = screen_info[i].width;
output->geometry.height = screen_info[i].height;
}
p_delete(&screens_reply);
@@ -246,7 +262,7 @@ static bool detect_monitor_by_xinerama(const backend_t *backend,
backend_detect_t *backend_detect(backend_t *backend) {
output_info_t *outputs = nullptr;
size_t count = 0;
size_t count = 0;
if (detect_monitor_by_randr(backend, &outputs, &count)) {
backend_detect_t *detect = output_remove_duplication(outputs, count);
@@ -260,15 +276,15 @@ backend_detect_t *backend_detect(backend_t *backend) {
return detect;
}
output_info_t *output = p_new(output_info_t, 1);
output->geometry.x = 0;
output->geometry.y = 0;
output->geometry.width = backend->screen->width_in_pixels;
output_info_t *output = p_new(output_info_t, 1);
output->geometry.x = 0;
output->geometry.y = 0;
output->geometry.width = backend->screen->width_in_pixels;
output->geometry.height = backend->screen->height_in_pixels;
backend_detect_t *detect = p_new(backend_detect_t, 1);
detect->outputs = output;
detect->output_count = 1;
detect->outputs = output;
detect->output_count = 1;
return detect;
}
@@ -283,8 +299,11 @@ void backend_detect_destroy(backend_detect_t *detect) {
free(detect);
}
bool backend_apply_effect(backend_t *backend, const effect_t *effects,
size_t effect_count) {
bool backend_apply_effect(
backend_t *backend,
const effect_t *effects,
size_t effect_count
) {
/* TODO: */
return true;
}

View File

@@ -11,17 +11,20 @@
#include "core/backend.h"
#include "core/types.h"
#include "core/window.h"
#include "internal.h" /* IWYU pragma: keep */
#include "internal.h"
static window_state_t *derive_window_states(const atoms_t *atoms,
const xcb_atom_t *raw,
uint32_t count, size_t *out_count) {
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;
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;
@@ -35,22 +38,29 @@ static window_state_t *derive_window_states(const atoms_t *atoms,
return buf;
}
static bool derive_skip_taskbar(const atoms_t *atoms, const xcb_atom_t *raw,
uint32_t count) {
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) {
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) {
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;
@@ -61,17 +71,20 @@ static window_geometry_mode_t geometry_mode_from_states(
return ZDWM_GEOMETRY_NORMAL;
}
static bool handle_map_request(backend_t *backend, event_t *event,
const xcb_map_request_event_t *xcb_event) {
static bool handle_map_request(
backend_t *backend,
event_t *event,
const xcb_map_request_event_t *xcb_event
) {
xcb_window_t window = xcb_event->window;
event_reset(event);
event->type = ZDWM_EVENT_WINDOW_MAP_REQUEST;
window_map_request_event_t *ev = &event->as.window_map_request;
ev->window = (window_id_t)window;
ev->transient_for = window_get_transient_for(backend, window);
ev->geometry_mode = ZDWM_GEOMETRY_NORMAL;
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);
@@ -81,18 +94,23 @@ static bool handle_map_request(backend_t *backend, event_t *event,
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->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;
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)) {
uint32_t state_count = 0;
if (!window_get_atom_array(
backend,
window,
atoms->_NET_WM_STATE,
&state_atoms,
&state_count
)) {
return false;
}
@@ -103,8 +121,12 @@ static bool handle_map_request(backend_t *backend, event_t *event,
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);
ev->props.states = derive_window_states(
atoms,
state_atoms,
state_count,
&ev->props.state_count
);
}
p_delete(&state_atoms);
@@ -115,24 +137,34 @@ static bool handle_map_request(backend_t *backend, event_t *event,
return false;
}
ev->metadata.role = window_get_role(backend, window);
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);
window_get_class(
backend,
window,
&ev->metadata.class_name,
&ev->metadata.instance_name
);
return true;
}
static bool handle_unmap_notify(backend_t *backend, event_t *event,
const xcb_unmap_notify_event_t *xcb_event) {
static bool handle_unmap_notify(
backend_t *backend,
event_t *event,
const xcb_unmap_notify_event_t *xcb_event
) {
event_reset(event);
event->type = ZDWM_EVENT_WINDOW_REMOVE;
/* TODO: */
return true;
}
static bool handle_destroy_notify(backend_t *backend, event_t *event,
const xcb_destroy_notify_event_t *xcb_event) {
static bool handle_destroy_notify(
backend_t *backend,
event_t *event,
const xcb_destroy_notify_event_t *xcb_event
) {
event_reset(event);
event->type = ZDWM_EVENT_WINDOW_REMOVE;
/* TODO: */
@@ -140,8 +172,10 @@ static bool handle_destroy_notify(backend_t *backend, event_t *event,
}
static bool handle_configure_request(
backend_t *backend, event_t *event,
const xcb_configure_request_event_t *xcb_event) {
backend_t *backend,
event_t *event,
const xcb_configure_request_event_t *xcb_event
) {
event_reset(event);
event->type = ZDWM_EVENT_CONFIGURE_REQUEST;
/* TODO: */
@@ -156,7 +190,7 @@ bool backend_next_event(backend_t *backend, event_t *event) {
if (!raw_event) return false;
uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(raw_event);
bool handled = false;
bool handled = false;
printf("xcb event type: %u\n", response_type);
@@ -173,8 +207,8 @@ bool backend_next_event(backend_t *backend, event_t *event) {
#undef EVENT
default:
break;
default:
break;
}
p_delete(&raw_event);

View File

@@ -7,17 +7,28 @@
#include <xcb/xproto.h>
#include "base/memory.h"
#include "core/backend.h"
#include "internal.h"
static char *window_get_text_property(backend_t *backend, xcb_window_t window,
xcb_atom_t property) {
static char *window_get_text_property(
backend_t *backend,
xcb_window_t window,
xcb_atom_t property
) {
xcb_get_property_cookie_t cookie = xcb_get_property_unchecked(
backend->conn, false, window, property, XCB_ATOM_ANY, 0, UINT32_MAX);
backend->conn,
false,
window,
property,
XCB_ATOM_ANY,
0,
UINT32_MAX
);
xcb_get_property_reply_t *reply =
xcb_get_property_reply(backend->conn, cookie, nullptr);
if (!reply) return nullptr;
int length = xcb_get_property_value_length(reply);
int length = xcb_get_property_value_length(reply);
char *value = (char *)xcb_get_property_value(reply);
char *text = nullptr;
@@ -39,16 +50,20 @@ char *window_get_role(backend_t *backend, xcb_window_t window) {
char *window_get_title(backend_t *backend, xcb_window_t window) {
xcb_atom_t property = backend->atoms._NET_WM_NAME;
char *name = window_get_text_property(backend, window, property);
char *name = window_get_text_property(backend, window, property);
if (!name) {
property = backend->atoms.WM_NAME;
name = window_get_text_property(backend, window, property);
name = window_get_text_property(backend, window, property);
}
return name;
}
void window_get_class(backend_t *backend, xcb_window_t window, char **class_out,
char **instance_out) {
void window_get_class(
backend_t *backend,
xcb_window_t window,
char **class_out,
char **instance_out
) {
xcb_icccm_get_wm_class_reply_t prop;
xcb_get_property_cookie_t cookie =
xcb_icccm_get_wm_class_unchecked(backend->conn, window);
@@ -74,15 +89,15 @@ xcb_window_t window_get_transient_for(backend_t *backend, xcb_window_t window) {
return XCB_WINDOW_NONE;
}
xcb_get_window_attributes_reply_t *window_get_attributes(backend_t *backend,
xcb_window_t window) {
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) {
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;
@@ -106,13 +121,21 @@ static window_type_t atom_to_window_type(const atoms_t *atoms,
return ZDWM_WINDOW_TYPE_NORMAL;
}
bool window_get_types(backend_t *backend, xcb_window_t window,
window_type_t **types, size_t *count) {
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)) {
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) {
@@ -156,20 +179,31 @@ bool window_get_geometry(backend_t *backend, xcb_window_t window, rect_t *out) {
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->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) {
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);
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;
@@ -179,7 +213,7 @@ bool window_get_atom_array(backend_t *backend, xcb_window_t window,
*out_atoms =
p_copy((xcb_atom_t *)xcb_get_property_value(reply), reply->value_len);
} else {
*out_len = 0;
*out_len = 0;
*out_atoms = nullptr;
}
@@ -187,8 +221,11 @@ bool window_get_atom_array(backend_t *backend, xcb_window_t window,
return true;
}
bool window_get_wm_hints(backend_t *backend, xcb_window_t window,
xcb_icccm_wm_hints_t *out) {
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)) {

View File

@@ -35,18 +35,33 @@ char *window_get_title(backend_t *backend, xcb_window_t window);
* @param instance_out instance 信息返回指针,调用者持有其内存并负责调用 free
* 释放
*/
void window_get_class(backend_t *backend, xcb_window_t window, char **class_out,
char **instance_out);
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);
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);
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);