summaryrefslogtreecommitdiff
path: root/src/main.zig
blob: 2c30db550664f5eaed39e0dc26e21b0f5020a4bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const std = @import("std");
const c = @import("c.zig");

pub fn main() !void {
    var mask: u32 = 0;
    var values: [3]u32 = undefined;
    const string = "hello, XCB!";

    var rectangles = [_]c.xcb_rectangle_t{
        .{ .x = 40, .y = 40, .width = 20, .height = 20 },
    };

    const conn = c.xcb_connect(null, null) orelse return error.FailedToConnectToXServer;
    defer c.xcb_disconnect(conn);

    const screen: *c.xcb_screen_t = @ptrCast(c.xcb_setup_roots_iterator(c.xcb_get_setup(conn)).data);

    var foreground = c.xcb_generate_id(conn);
    mask = c.XCB_GC_FOREGROUND | c.XCB_GC_GRAPHICS_EXPOSURES;
    values[0] = screen.black_pixel;
    values[1] = 0;
    _ = c.xcb_create_gc(conn, foreground, screen.root, mask, @ptrCast(&values));

    var background = c.xcb_generate_id(conn);
    mask = c.XCB_GC_BACKGROUND | c.XCB_GC_GRAPHICS_EXPOSURES;
    values[0] = screen.white_pixel;
    values[1] = 0;
    _ = c.xcb_create_gc(conn, background, screen.root, mask, @ptrCast(&values));

    const win = c.xcb_generate_id(conn);
    mask = c.XCB_CW_BACK_PIXEL | c.XCB_CW_WIN_GRAVITY | c.XCB_CW_EVENT_MASK;
    values[0] = screen.white_pixel;
    values[1] = c.XCB_GRAVITY_NORTH_WEST;
    values[2] = c.XCB_EVENT_MASK_EXPOSURE | c.XCB_EVENT_MASK_KEY_PRESS;
    _ = c.xcb_create_window(conn, c.XCB_COPY_FROM_PARENT, win, screen.root, 0, 0, 150, 150, 0, c.XCB_WINDOW_CLASS_INPUT_OUTPUT, screen.root_visual, mask, @ptrCast(&values));

    _ = c.xcb_map_window(conn, win);
    _ = c.xcb_flush(conn);

    while (true) {
        var e = c.xcb_wait_for_event(conn) orelse break;
        defer std.c.free(e);
        switch (e.*.response_type & ~@as(u8, 0x80)) {
            c.XCB_EXPOSE => {
                _ = c.xcb_clear_area(conn, 0, win, 0, 0, 0, 0);
                _ = c.xcb_poly_rectangle(conn, win, foreground, 1, @ptrCast(&rectangles));
                _ = c.xcb_image_text_8(conn, string.len, win, background, 20, 20, string);
                _ = c.xcb_flush(conn);
            },
            c.XCB_KEY_PRESS => {
                //break;
            },
            else => {},
        }
    }
}