livewp/mess/cairo-xcb.c

122 lines
3.5 KiB
C

// x-run: ~/scripts/runc.sh % -lX11 -lcairo -lxcb -lm
#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);
int main(void) {
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_window_t wid = xcb_generate_id(connection);
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: {}
}
int win_w = 150, win_h = 150;
uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
values[2] = {
screen->white_pixel,
XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_FOCUS_CHANGE
};
xcb_create_window(connection,
XCB_COPY_FROM_PARENT,
wid, screen->root,
0, 0, win_w, win_h, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
screen->root_visual, mask, 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 ((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, win_w, win_h);
cr = cairo_create(surface);
win_w = evt->width;
win_h = evt->height;
}
}
break;
case XCB_FOCUS_IN:
printf("focus in\n");
break;
case XCB_FOCUS_OUT:
printf("focus out\n");
break;
case XCB_EXPOSE:
printf("expose\n");
break;
case XCB_DESTROY_NOTIFY:
cairo_destroy(cr);
running = false;
break;
default:
printf("event: %d\n", event->response_type);
break;
}
free(event);
}
if (running) {
render(connection, wid, cr, win_w, win_h);
usleep(1000000 / 60);
}
}
xcb_destroy_window(connection, wid);
xcb_disconnect(connection);
}
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, sin(t) * 0.5 + 0.5, cos(t) * 0.5 + 0.5, 0);
cairo_rectangle(cr, win_w * 0.5, win_h * 0.5, sin(t) * 0.5 * win_w, cos(t) * 0.5 * win_h);
cairo_fill(cr);
xcb_flush(connection);
t += 1.0 / 60.0;
}