创建每个屏幕对应的信息条窗口
This commit is contained in:
142
src/xcb/window.c
142
src/xcb/window.c
@@ -1,10 +1,17 @@
|
||||
#include "window.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <xcb/xcb.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) {
|
||||
@@ -24,3 +31,138 @@ void set_window_class_instance(xcb_window_t window, const char *class,
|
||||
xcb_icccm_set_wm_class(conn, window, length, str);
|
||||
free(str);
|
||||
}
|
||||
|
||||
typedef struct visual_t {
|
||||
uint8_t depth;
|
||||
xcb_visualtype_t *visual;
|
||||
} visual_t;
|
||||
|
||||
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 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);
|
||||
}
|
||||
|
||||
void set_root_cursor(cursor_t cursor) {
|
||||
change_window_cursor(screen->root, cursor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user