livewp/mess/cairo-xcb.c

239 lines
7.6 KiB
C

// x-run: ~/scripts/runc.sh % -lX11 -lcairo -lxcb -lm
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <cairo/cairo.h>
#include <cairo/cairo-xcb.h>
#include <unistd.h>
#include <math.h>
#define EVENT(T, A, B) T *A = (T *)B
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);
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) {
char buffer[8192];
xcb_connection_t *connection = xcb_connect(getenv("DISPLAY"), NULL);
if (!connection) {
fprintf(stderr, "xcb_connect() failed\n");
return EXIT_FAILURE;
}
xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
xcb_visualtype_t *visual = NULL;
{
for (
xcb_depth_iterator_t iter_depth = xcb_screen_allowed_depths_iterator(screen);
iter_depth.rem;
xcb_depth_next(&iter_depth)) {
int visual_length = xcb_depth_visuals_length(iter_depth.data);
visual = xcb_depth_visuals(iter_depth.data);
for (int i = 0; i < visual_length; i++) {
if (visual->visual_id == screen->root_visual) {
goto found_visual;
}
}
}
found_visual: {}
}
xcb_window_t root = find_subwindow(connection, screen->root, -1, -1);
xcb_window_t parent = find_subwindow(connection, root, screen->width_in_pixels, screen->height_in_pixels);
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,
XCB_BACKING_STORE_ALWAYS,
1,
XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_FOCUS_CHANGE
};
xcb_create_window(
connection,
XCB_COPY_FROM_PARENT, /* depth */
wid, parent,
0, 0, win_w, win_h, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
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_flush(connection);
cairo_surface_t *surface = cairo_xcb_surface_create(connection, wid, visual, win_w, win_h);
cairo_t *cr = cairo_create(surface);
xcb_generic_event_t *event;
bool running = true;
while (running) {
while (running && (event = xcb_poll_for_event(connection))) {
switch (event->response_type & ~0x80) {
case XCB_CONFIGURE_NOTIFY:
{
EVENT(xcb_configure_notify_event_t, evt, event);
if (win_w != evt->width || win_h != evt->height) {
printf("resize %dx%d -> %dx%d\n", win_w, win_h, evt->width, evt->height);
cairo_destroy(cr);
surface = cairo_xcb_surface_create(connection, wid, visual, evt->width, evt->height);
cr = cairo_create(surface);
win_w = evt->width;
win_h = evt->height;
}
}
break;
case XCB_REPARENT_NOTIFY:
printf("reparent\n");
break;
case XCB_MAP_NOTIFY:
printf("map\n");
break;
case XCB_FOCUS_IN:
break;
case XCB_FOCUS_OUT:
break;
case XCB_EXPOSE:
{
EVENT(xcb_expose_event_t, evt, event);
printf("expose %dx%d+%d+%d\n", evt->width, evt->height, evt->x, evt->y);
}
break;
case XCB_DESTROY_NOTIFY:
cairo_destroy(cr);
running = false;
break;
default:
printf("event: %d\n", event->response_type & ~0x80);
break;
}
free(event);
}
snprintf(buffer, 8191, "size: %dx%d", win_w, win_h);
set_window_title(connection, wid, buffer);
if (running) {
render(connection, wid, cr, win_w, win_h);
usleep(1000000 / 60);
}
}
xcb_destroy_window(connection, wid);
xcb_disconnect(connection);
return EXIT_SUCCESS;
}
void render(xcb_connection_t *connection, int wid, cairo_t *cr, int win_w, int win_h) {
static float t = 0.0;
xcb_clear_area(connection, 0, wid, 0, 0, 0, 0);
cairo_set_source_rgb(cr, 0.0, 0.5 - sin(t) * 0.5, 0.5 - cos(t) * 0.5);
cairo_paint(cr);
cairo_set_source_rgb(cr, sin(t) * 0.5 + 0.5, cos(t) * 0.5 + 0.5, 0);
cairo_set_line_width(cr, 5);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_BEVEL);
cairo_move_to(cr, win_w * 0.5, win_h * 0.5);
cairo_line_to(cr, (sin(t) * 0.5 + 0.5) * win_w, (cos(t) * 0.5 + 0.5) * win_h);
cairo_stroke(cr);
xcb_flush(connection);
t += 1.0 / 60.0;
}
void set_window_title(xcb_connection_t *connection, xcb_window_t wid, const char *title) {
xcb_change_property(connection,
XCB_PROP_MODE_REPLACE,
wid,
XCB_ATOM_WM_NAME,
XCB_ATOM_STRING,
8,
strlen(title), title);
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;
}