diff options
Diffstat (limited to 'src/main.zig')
-rw-r--r-- | src/main.zig | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..2c30db5 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,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 => {}, + } + } +}
\ No newline at end of file |