Created
May 11, 2019 05:24
-
-
Save NBonaparte/ac5a000e05a29196df054ce99fe705c7 to your computer and use it in GitHub Desktop.
Minimal XCB + Cairo example in Zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const c = @cImport({ | |
@cInclude("xcb/xcb.h"); | |
@cInclude("cairo/cairo-xcb.h"); | |
@cInclude("cairo/cairo.h"); | |
}); | |
const std = @import("std"); | |
pub fn lookup_visual(s: *c.xcb_screen_t, visual: c.xcb_visualid_t) ?*c.xcb_visualtype_t { | |
var d = c.xcb_screen_allowed_depths_iterator(s); | |
while (d.rem != 0) { | |
var v = c.xcb_depth_visuals_iterator(d.data); | |
while (v.rem != 0) { | |
if (@ptrCast(*c.xcb_visualtype_t, v.data).visual_id == visual) { | |
return v.data; | |
} | |
c.xcb_visualtype_next(&v); | |
} | |
c.xcb_depth_next(&d); | |
} | |
return null; | |
} | |
pub fn main() void { | |
std.debug.warn("Starting...\n"); | |
var data: ?[*]const u8 = null; | |
var scr: ?[*]c_int = null; | |
const conn = c.xcb_connect(data, scr); | |
defer c.xcb_disconnect(conn); | |
// need to cast from unknown length pointer to single pointer | |
const s = @ptrCast(*c.xcb_screen_t, c.xcb_setup_roots_iterator(c.xcb_get_setup(conn)).data); | |
const window = c.xcb_generate_id(conn); | |
const win_class: u16 = @enumToInt(c.XCB_WINDOW_CLASS_INPUT_OUTPUT); | |
// TODO handle cookies | |
_ = c.xcb_create_window(conn, c.XCB_COPY_FROM_PARENT, window, s.root, 0, 0, 150, 150, 0, win_class, s.root_visual, 0, null); | |
_ = c.xcb_map_window(conn, window); | |
_ = c.xcb_flush(conn); | |
const vis = lookup_visual(s, s.root_visual); | |
var surf = c.cairo_xcb_surface_create(conn, window, vis, 150, 150); | |
if (surf == null) return; | |
var ctx = c.cairo_create(surf); | |
_ = c.xcb_flush(conn); | |
var toggle: bool = true; | |
while (true) { | |
if (toggle) { | |
c.cairo_set_source_rgb(ctx, 1, 1, 1); | |
} else { | |
c.cairo_set_source_rgb(ctx, 0, 0, 0); | |
} | |
toggle = !toggle; | |
c.cairo_paint(ctx); | |
c.cairo_surface_flush(surf); | |
_ = c.xcb_flush(conn); | |
std.os.time.sleep(1e9); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment