Files
zswm/src/xcb/window.c

303 lines
9.9 KiB
C

#include "window.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include <xcb/xcb_ewmh.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xproto.h>
#include "app.h"
#include "utils.h"
#include "xcb-private.h"
#include "xcb.h"
void set_window_class_instance(xcb_window_t window, const char *class,
const char *instance) {
size_t class_length = strlen(class) + 1;
size_t instance_length = strlen(instance) + 1;
size_t length = class_length + instance_length;
char *str = malloc(length * sizeof(char));
for (size_t i = 0; i < length; i++) {
if (i < class_length) {
str[i] = class[i];
} else {
str[i] = instance[i - class_length];
}
}
xcb_icccm_set_wm_class(conn, window, length, str);
free(str);
}
static char broken[] = "broken";
void get_window_class_instance(xcb_window_t window,
window_class_instance_t *class_instance) {
xcb_get_property_cookie_t cookie = xcb_icccm_get_wm_class(conn, window);
xcb_icccm_get_wm_class_reply_t prop;
const char *class = NULL;
const char *instance = NULL;
if (xcb_icccm_get_wm_class_reply(conn, cookie, &prop, NULL)) {
class = prop.class_name;
instance = prop.instance_name;
} else {
class = broken;
instance = broken;
}
strncpy(class_instance->class, class, LENGTH(class_instance->class));
strncpy(class_instance->instance, instance, LENGTH(class_instance->instance));
xcb_icccm_get_wm_class_reply_wipe(&prop);
}
static visual_t *find_alpha_visual() {
xcb_depth_iterator_t depth_iter;
depth_iter = xcb_screen_allowed_depths_iterator(screen);
for (; depth_iter.rem; xcb_depth_next(&depth_iter)) {
if (depth_iter.data->depth != 32) continue;
xcb_visualtype_iterator_t visual_iter;
visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
for (; visual_iter.rem; xcb_visualtype_next(&visual_iter)) {
if (visual_iter.data->_class == XCB_VISUAL_CLASS_TRUE_COLOR) {
visual_t *r = ecalloc(1, sizeof(visual_t));
r->visual = visual_iter.data;
r->depth = depth_iter.data->depth;
return r;
}
}
}
return NULL;
}
static visual_t *find_root_visual() {
xcb_depth_iterator_t depth_iter;
depth_iter = xcb_screen_allowed_depths_iterator(screen);
xcb_visualid_t vid = screen->root_visual;
for (; depth_iter.rem; xcb_depth_next(&depth_iter)) {
xcb_visualtype_iterator_t visual_iter;
visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
for (; visual_iter.rem; xcb_visualtype_next(&visual_iter)) {
if (visual_iter.data->visual_id == vid) {
visual_t *r = ecalloc(1, sizeof(visual_t));
r->visual = visual_iter.data;
r->depth = depth_iter.data->depth;
return r;
}
}
}
return NULL;
}
/**
* @brief 获取 xcb 的 visual 及其对应的 depth
* @param prefer_alpha 支持 alpha 的 visual 优先
* @returns 返回的信息使用后记得使用 free 释放
*/
visual_t *get_xcb_visual(bool prefer_alpha) {
if (!prefer_alpha) return find_root_visual();
visual_t *visual = find_alpha_visual();
if (visual == NULL) visual = find_root_visual();
return visual;
}
typedef struct bar_window_param_t {
uint8_t depth;
xcb_colormap_t colormap;
xcb_visualid_t visual_id;
xcb_visualtype_t *visual;
} bar_window_param_t;
static bar_window_param_t *prepare_bar_window_params(void) {
static xcb_colormap_t colormap = XCB_NONE;
visual_t *visual = get_xcb_visual(true);
xcb_visualid_t visual_id = visual->visual->visual_id;
if (colormap == XCB_NONE) {
colormap = xcb_generate_id(conn);
uint8_t alloc = XCB_COLORMAP_ALLOC_NONE;
xcb_create_colormap(conn, alloc, colormap, screen->root, visual_id);
}
bar_window_param_t *params = ecalloc(1, sizeof(bar_window_param_t));
params->depth = visual->depth;
params->visual = visual->visual;
params->visual_id = visual_id;
params->colormap = colormap;
free(visual);
return params;
}
bar_cairo_params_t *create_bar_window(int16_t x, int16_t y, uint16_t width,
uint16_t height, uint32_t background_argb,
cursor_t cursor) {
bar_window_param_t *params = prepare_bar_window_params();
xcb_visualtype_t *visual = params->visual;
xcb_window_t window = xcb_generate_id(conn);
uint32_t value_mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_BACK_PIXEL |
XCB_CW_BORDER_PIXEL | XCB_CW_EVENT_MASK |
XCB_CW_CURSOR | XCB_CW_COLORMAP;
const xcb_create_window_value_list_t value_list = {
.override_redirect = true,
.background_pixel = background_argb,
.border_pixel = 0,
.event_mask = XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_EXPOSURE,
.cursor = get_xcb_cursor(cursor),
.colormap = params->colormap,
};
xcb_void_cookie_t cookie;
cookie = xcb_create_window_aux_checked(
conn, params->depth, window, screen->root, x, y, width, height, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT, params->visual_id, value_mask, &value_list);
free(params);
if (xcb_request_check(conn, cookie)) die("create bar window:");
cookie = xcb_map_window(conn, window);
if (xcb_request_check(conn, cookie)) die("map bar window:");
set_window_class_instance(window, APP_NAME, APP_NAME);
xcb_flush(conn);
bar_cairo_params_t *p = ecalloc(1, sizeof(bar_cairo_params_t));
p->conn = conn;
p->visual = visual;
p->window = window;
return p;
}
void destroy_window(xcb_window_t window) { xcb_destroy_window(conn, window); }
void configure_window(xcb_window_t window, int32_t x, int32_t y, uint32_t width,
uint32_t height, uint32_t border_width) {
xcb_config_window_t config_mask =
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT | XCB_CONFIG_WINDOW_BORDER_WIDTH;
xcb_params_configure_window_t window_config = {
.x = x,
.y = y,
.width = width,
.height = height,
.border_width = border_width,
};
xcb_aux_configure_window(conn, window, config_mask, &window_config);
}
void change_window_cursor(xcb_window_t window, cursor_t cursor) {
xcb_change_window_attributes_value_list_t value_list = {
.cursor = get_xcb_cursor(cursor),
};
xcb_change_window_attributes_aux(conn, window, XCB_CW_CURSOR, &value_list);
xcb_flush(conn);
}
xcb_window_t get_root_window(void) { return screen->root; }
bool get_window_visible(xcb_window_t window) {
xcb_get_window_attributes_cookie_t cookie =
xcb_get_window_attributes(conn, window);
xcb_get_window_attributes_reply_t *reply =
xcb_get_window_attributes_reply(conn, cookie, NULL);
bool visible =
!reply->override_redirect && reply->map_state != XCB_MAP_STATE_UNVIEWABLE;
free(reply);
return visible;
}
xcb_window_t get_transient_window_for(xcb_window_t window) {
xcb_window_t transient_for = WINDOW_NONE;
xcb_get_property_cookie_t cookie =
xcb_icccm_get_wm_transient_for(conn, window);
if (xcb_icccm_get_wm_transient_for_reply(conn, cookie, &transient_for,
NULL)) {
return transient_for;
}
return WINDOW_NONE;
}
window_geometry_t *get_window_geometry(xcb_window_t window) {
xcb_get_geometry_cookie_t cookie = xcb_get_geometry(conn, window);
xcb_get_geometry_reply_t *reply = xcb_get_geometry_reply(conn, cookie, NULL);
window_geometry_t *geometry = ecalloc(1, sizeof(window_geometry_t));
geometry->x = reply->x;
geometry->y = reply->y;
geometry->width = reply->width;
geometry->height = reply->height;
geometry->border_width = reply->border_width;
free(reply);
return geometry;
}
void map_window(xcb_window_t window) {
xcb_map_window(conn, window);
xcb_map_subwindows(conn, window);
}
char *get_window_name(xcb_window_t window) {
xcb_get_property_cookie_t cookie = xcb_ewmh_get_wm_name(ewmh, window);
xcb_ewmh_get_utf8_strings_reply_t reply;
if (xcb_ewmh_get_wm_name_reply(ewmh, cookie, &reply, NULL)) {
char *name = ecalloc(reply.strings_len + 1, sizeof(char));
strncpy(name, reply.strings, reply.strings_len);
xcb_ewmh_get_utf8_strings_reply_wipe(&reply);
return name;
}
cookie = xcb_icccm_get_wm_name(conn, window);
xcb_icccm_get_text_property_reply_t prop;
if (xcb_icccm_get_wm_name_reply(conn, cookie, &prop, NULL)) {
char *name = ecalloc(prop.name_len + 1, sizeof(char));
strncpy(name, prop.name, prop.name_len);
xcb_icccm_get_text_property_reply_wipe(&prop);
return name;
}
{
char *name = ecalloc(LENGTH(broken), sizeof(char));
strncpy(name, broken, LENGTH(broken));
return name;
}
}
void set_window_event_mask(xcb_window_t window, bool clear) {
xcb_cw_t change_mask = XCB_CW_EVENT_MASK;
uint32_t event_mask = clear ? XCB_EVENT_MASK_NO_EVENT
: XCB_EVENT_MASK_ENTER_WINDOW |
XCB_EVENT_MASK_FOCUS_CHANGE |
XCB_EVENT_MASK_PROPERTY_CHANGE |
XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
xcb_params_cw_t params = {.event_mask = event_mask};
xcb_aux_change_window_attributes(conn, window, change_mask, &params);
}
void change_window_border_color(xcb_window_t window, uint32_t argb) {
xcb_params_cw_t params = {.border_pixel = argb};
xcb_aux_change_window_attributes(conn, window, XCB_CW_BORDER_PIXEL, &params);
}
void focus_window(xcb_window_t window) {
xcb_window_t root = screen->root;
if (window == WINDOW_NONE) {
xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root,
XCB_CURRENT_TIME);
xcb_delete_property(conn, root, ewmh->_NET_ACTIVE_WINDOW);
} else {
xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, window,
XCB_CURRENT_TIME);
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
ewmh->_NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 32, 1,
&window);
send_event(window, atom_wm_take_focus);
}
}