It's alive! XCB+Cairo drawing on a root window

This commit is contained in:
Casey 2024-04-09 16:47:34 +03:00
parent 679676e97f
commit 0043dc5ef5
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 94 additions and 8 deletions

View File

@ -17,7 +17,14 @@
void render(xcb_connection_t *connection, int wid, cairo_t *cr, int win_w, int win_h); void render(xcb_connection_t *connection, int wid, cairo_t *cr, int win_w, int win_h);
void set_window_title(xcb_connection_t *connection, xcb_window_t wid, const char *title); void set_window_title(xcb_connection_t *connection, xcb_window_t wid, const char *title);
bool get_window_geometry(xcb_connection_t *connection, xcb_window_t wid, xcb_rectangle_t *rect);
bool get_window_name(xcb_connection_t *connection, xcb_window_t wid, char *name);
bool get_window_attributes(xcb_connection_t *connection, xcb_window_t wid, xcb_get_window_attributes_reply_t *reply);
xcb_window_t find_subwindow(xcb_connection_t *connection, xcb_window_t win, int w, int h);
int main(void) { int main(void) {
char buffer[8192];
xcb_connection_t *connection = xcb_connect(getenv("DISPLAY"), NULL); xcb_connection_t *connection = xcb_connect(getenv("DISPLAY"), NULL);
if (!connection) { if (!connection) {
fprintf(stderr, "xcb_connect() failed\n"); fprintf(stderr, "xcb_connect() failed\n");
@ -25,7 +32,6 @@ int main(void) {
} }
xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data; xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
xcb_window_t wid = xcb_generate_id(connection);
xcb_visualtype_t *visual = NULL; xcb_visualtype_t *visual = NULL;
{ {
@ -44,19 +50,33 @@ int main(void) {
found_visual: {} found_visual: {}
} }
int win_w = 150, win_h = 150; xcb_window_t root = find_subwindow(connection, screen->root, -1, -1);
uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, xcb_window_t parent = find_subwindow(connection, root, screen->width_in_pixels, screen->height_in_pixels);
values[2] = {
printf("parent: 0x%08x\n", parent);
xcb_window_t wid = xcb_generate_id(connection);
int win_w = screen->width_in_pixels, win_h = screen->height_in_pixels;
uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_BACKING_STORE | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK,
values[] = {
screen->white_pixel, screen->white_pixel,
XCB_BACKING_STORE_ALWAYS,
1,
XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_FOCUS_CHANGE XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_FOCUS_CHANGE
}; };
xcb_create_window(connection, xcb_create_window(
XCB_COPY_FROM_PARENT, connection,
wid, screen->root, XCB_COPY_FROM_PARENT, /* depth */
wid, parent,
0, 0, win_w, win_h, 0, 0, 0, win_w, win_h, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_WINDOW_CLASS_INPUT_OUTPUT,
screen->root_visual, mask, values); screen->root_visual, mask, values);
{
const static uint32_t values[] = { XCB_STACK_MODE_BELOW };
xcb_configure_window(connection, wid, XCB_CONFIG_WINDOW_STACK_MODE, values);
}
xcb_map_window(connection, wid); xcb_map_window(connection, wid);
xcb_flush(connection); xcb_flush(connection);
@ -66,7 +86,6 @@ found_visual: {}
xcb_generic_event_t *event; xcb_generic_event_t *event;
bool running = true; bool running = true;
char buffer[8192];
while (running) { while (running) {
while (running && (event = xcb_poll_for_event(connection))) { while (running && (event = xcb_poll_for_event(connection))) {
switch (event->response_type & ~0x80) { switch (event->response_type & ~0x80) {
@ -121,6 +140,7 @@ found_visual: {}
xcb_destroy_window(connection, wid); xcb_destroy_window(connection, wid);
xcb_disconnect(connection); xcb_disconnect(connection);
return EXIT_SUCCESS;
} }
void render(xcb_connection_t *connection, int wid, cairo_t *cr, int win_w, int win_h) { void render(xcb_connection_t *connection, int wid, cairo_t *cr, int win_w, int win_h) {
@ -150,3 +170,69 @@ void set_window_title(xcb_connection_t *connection, xcb_window_t wid, const char
strlen(title), title); strlen(title), title);
xcb_flush(connection); xcb_flush(connection);
} }
bool get_window_geometry(xcb_connection_t *connection, xcb_window_t wid, xcb_rectangle_t *rect) {
xcb_get_geometry_cookie_t cookie = xcb_get_geometry(connection, wid);
xcb_get_geometry_reply_t *reply = xcb_get_geometry_reply(connection, cookie, 0);
if (!reply) return false;
rect->x = reply->x;
rect->y = reply->y;
rect->width = reply->width;
rect->height = reply->height;
free(reply);
return true;
}
bool get_window_name(xcb_connection_t *connection, xcb_window_t wid, char *name) {
xcb_get_property_cookie_t cookie = xcb_get_property(connection, 0, wid, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 0, 0);
xcb_get_property_reply_t *reply = xcb_get_property_reply(connection, cookie, 0);
if (!reply) {
name[0] = 1;
return false;
}
int len = xcb_get_property_value_length(reply);
if (len == 0) {
name[0] = 2;
free(reply);
return false;
}
strcpy(name, xcb_get_property_value(reply));
free(reply);
return true;
}
bool get_window_attributes(xcb_connection_t *connection, xcb_window_t wid, xcb_get_window_attributes_reply_t *out) {
xcb_get_window_attributes_cookie_t cookie = xcb_get_window_attributes(connection, wid);
xcb_get_window_attributes_reply_t *reply = xcb_get_window_attributes_reply(connection, cookie, 0);
if (!reply) return false;
memcpy(out, reply, sizeof(xcb_get_window_attributes_reply_t));
free(reply);
return true;
}
xcb_window_t find_subwindow(xcb_connection_t *connection, xcb_window_t win, int w, int h) {
xcb_query_tree_reply_t *qt_reply;
xcb_query_tree_cookie_t qt_cookie = xcb_query_tree(connection, win);
if ((qt_reply = xcb_query_tree_reply(connection, qt_cookie, 0))) {
xcb_window_t *children = xcb_query_tree_children(qt_reply);
xcb_rectangle_t rect;
xcb_get_window_attributes_reply_t attrs;
for (int i = 0; i < xcb_query_tree_children_length(qt_reply); i++) {
if (!get_window_geometry(connection, children[i], &rect)) continue;
if (!get_window_attributes(connection, children[i], &attrs)) continue;
if (attrs.map_state != 0 && rect.width == w && rect.height == h) {
free(qt_reply);
return children[i];
}
}
free(qt_reply);
}
return win;
}