commit e54909a53f7a6ef17135e60b9809ceb373596d33
parent 355f19114ce3c7f696618cf6142560571884305c
Author: Matsuda Kenji <info@mtkn.jp>
Date: Fri, 19 Dec 2025 13:07:03 +0900
fill background
Diffstat:
| M | xcb.c | | | 39 | ++++++++++++++++++++++++++++++++------- |
1 file changed, 32 insertions(+), 7 deletions(-)
diff --git a/xcb.c b/xcb.c
@@ -1,11 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h> // for pause(void)
+#include <stdarg.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <xcb/render.h>
void fatal(char *msg);
+void fatalf(char *fmt, ...);
int findPictFormat(xcb_connection_t *conn, int dept, xcb_render_pictformat_t *return_pictformat);
int main(void) {
@@ -15,17 +16,16 @@ int main(void) {
}
xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
xcb_window_t window = xcb_generate_id(conn);
- xcb_create_window_value_list_t attr = {
- .event_mask = XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_EXPOSURE,
- };
+ uint32_t attr = XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_EXPOSURE;
+ int width = 800, height = 600;
xcb_create_window(conn, XCB_COPY_FROM_PARENT, window, screen->root,
- 0, 0, 800, 600, 0,
+ 0, 0, width, height, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
screen->root_visual,
XCB_CW_EVENT_MASK, &attr);
xcb_map_window(conn, window);
- xcb_render_pictformat_t pictformat, pictformat24, pictformat32;
+ xcb_render_pictformat_t pictformat = 0, pictformat24, pictformat32;
if (findPictFormat(conn, 32, &pictformat32) < 0) {
fatal("32bit pictformat not found");
}
@@ -40,9 +40,25 @@ int main(void) {
fatal("unsupported depth");
}
+ xcb_render_picture_t picture = xcb_generate_id(conn);
+ xcb_render_create_picture(conn, picture, window, pictformat, 0, NULL);
+ xcb_render_color_t color = { .red = 0xffff, .green = 0xffff, .blue = 0xeaea, .alpha = 0xffff };
+ xcb_rectangle_t rectangle = { .x = 0, .y = 0, .width = width, .height = height };
+ xcb_render_fill_rectangles(conn, XCB_RENDER_PICT_OP_SRC, picture, color, 1, &rectangle);
+
xcb_flush(conn);
- pause();
+ int quit = 0;
+ xcb_generic_event_t *event;
+ while (!quit) {
+ xcb_render_fill_rectangles(conn, XCB_RENDER_PICT_OP_SRC, picture, color, 1, &rectangle);
+ xcb_flush(conn);
+
+ event = xcb_wait_for_event(conn);
+ if ((event->response_type & ~0x80) == XCB_KEY_PRESS && ((xcb_key_press_event_t *)event)->detail == 49) {
+ quit = 1;
+ }
+ }
xcb_disconnect(conn);
return 0;
@@ -53,6 +69,15 @@ void fatal(char *msg) {
exit(1);
}
+void fatalf(char *fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fprintf(stderr, "\n");
+ exit(1);
+}
+
int findPictFormat(xcb_connection_t *conn, int depth, xcb_render_pictformat_t *return_pictformat) {
xcb_generic_error_t *error;
xcb_render_query_pict_formats_cookie_t cookie = xcb_render_query_pict_formats(conn);