]> git.lizzy.rs Git - bspwm.git/blob - tests/test_window.c
bspwm: port rounded corners patch to latest version
[bspwm.git] / tests / test_window.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <string.h>
5 #include <xcb/xcb_event.h>
6 #include <xcb/xcb_icccm.h>
7
8 #define TEST_WINDOW_IC  "test\0Test"
9
10 bool get_atom(xcb_connection_t *dpy, char *name, xcb_atom_t *atom)
11 {
12         bool ret = true;
13         xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen(name), name), NULL);
14         if (reply != NULL) {
15                 *atom = reply->atom;
16         } else {
17                 ret = false;
18         }
19         free(reply);
20         return ret;
21 }
22
23 void check_request(xcb_connection_t *dpy, xcb_void_cookie_t cookie, char *msg)
24 {
25         xcb_generic_error_t *err = xcb_request_check(dpy, cookie);
26         if (err != NULL) {
27                 fprintf(stderr, "%s: error code: %u.\n", msg, err->error_code);
28                 xcb_disconnect(dpy);
29                 exit(-1);
30         }
31 }
32
33 xcb_gc_t get_font_gc(xcb_connection_t *dpy, xcb_window_t win, const char *font_name)
34 {
35         xcb_void_cookie_t ck;
36         xcb_font_t font = xcb_generate_id(dpy);
37         ck = xcb_open_font_checked(dpy, font, strlen(font_name), font_name);
38         check_request(dpy, ck, "Can't open font");
39         xcb_gcontext_t gc = xcb_generate_id(dpy);
40         uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
41         uint32_t values[] = {0xffcccccc, 0xff111111, font};
42         xcb_create_gc(dpy, gc, win, mask, values);
43         xcb_close_font(dpy, font);
44         return gc;
45 }
46
47 void render_text(xcb_connection_t *dpy, xcb_window_t win, int16_t x, int16_t y)
48 {
49         char id[11];
50         xcb_void_cookie_t ck;
51         snprintf(id, sizeof(id), "0x%08X", win);
52         xcb_gcontext_t gc = get_font_gc(dpy, win, "-*-fixed-medium-*-*-*-18-*-*-*-*-*-*-*");
53         /* Doesn't work without _checked */
54         ck = xcb_image_text_8_checked(dpy, strlen(id), win, gc, x, y, id);
55         check_request(dpy, ck, "Can't draw text");
56         xcb_free_gc(dpy, gc);
57 }
58
59
60 int main(void)
61 {
62         xcb_connection_t *dpy = xcb_connect(NULL, NULL);
63         if (dpy == NULL) {
64                 fprintf(stderr, "Can't connect to X.\n");
65                 return EXIT_FAILURE;
66         }
67         xcb_atom_t WM_PROTOCOLS, WM_DELETE_WINDOW;
68         if (!get_atom(dpy, "WM_PROTOCOLS", &WM_PROTOCOLS) ||
69             !get_atom(dpy, "WM_DELETE_WINDOW", &WM_DELETE_WINDOW)) {
70                 fprintf(stderr, "Can't get required atoms.\n");
71                 xcb_disconnect(dpy);
72                 return EXIT_FAILURE;
73         }
74         xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
75         if (screen == NULL) {
76                 fprintf(stderr, "Can't get current screen.\n");
77                 xcb_disconnect(dpy);
78                 return EXIT_FAILURE;
79         }
80         xcb_window_t win = xcb_generate_id(dpy);
81         uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
82         uint32_t values[] = {0xff111111, XCB_EVENT_MASK_EXPOSURE};
83         xcb_create_window(dpy, XCB_COPY_FROM_PARENT, win, screen->root, 0, 0, 320, 240, 2,
84                           XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT, mask, values);
85         xcb_icccm_set_wm_class(dpy, win, sizeof(TEST_WINDOW_IC), TEST_WINDOW_IC);
86         xcb_map_window(dpy, win);
87         xcb_flush(dpy);
88         xcb_generic_event_t *evt;
89         bool running = true;
90         while (running && (evt = xcb_wait_for_event(dpy)) != NULL) {
91                 uint8_t rt = XCB_EVENT_RESPONSE_TYPE(evt);
92                 if (rt == XCB_CLIENT_MESSAGE)  {
93                         xcb_client_message_event_t *cme = (xcb_client_message_event_t *) evt;
94                         if (cme->type == WM_PROTOCOLS && cme->data.data32[0] == WM_DELETE_WINDOW) {
95                                 running = false;
96                         }
97                 } else if (rt == XCB_EXPOSE) {
98                         render_text(dpy, win, 12, 24);
99                 }
100                 free(evt);
101         }
102         xcb_destroy_window(dpy, win);
103         xcb_disconnect(dpy);
104         return EXIT_SUCCESS;
105 }