diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | build.zig | 34 | ||||
m--------- | depends/xcb-zig | 0 | ||||
-rw-r--r-- | src/c.zig | 7 | ||||
-rw-r--r-- | src/main.zig | 56 |
6 files changed, 102 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee7098f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +zig-out/ +zig-cache/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e24cac9 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "depends/xcb-zig"] + path = depends/xcb-zig + url = https://repo.dec05eba.com/xcb-zig diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..63a7964 --- /dev/null +++ b/build.zig @@ -0,0 +1,34 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "xcb-zig-example", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + const xcb_dep = b.anonymousDependency("depends/xcb-zig", @import("depends/xcb-zig/build.zig"), .{ + .target = target, + .optimize = optimize, + }); + + exe.linkLibrary(xcb_dep.artifact("xcb-zig")); + exe.strip = optimize != std.builtin.OptimizeMode.Debug; + exe.want_lto = optimize != std.builtin.OptimizeMode.Debug; + exe.single_threaded = true; + + b.installArtifact(exe); + + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/depends/xcb-zig b/depends/xcb-zig new file mode 160000 +Subproject ae002920f8eb038ff3c4c5179967da524d2b36f diff --git a/src/c.zig b/src/c.zig new file mode 100644 index 0000000..395245b --- /dev/null +++ b/src/c.zig @@ -0,0 +1,7 @@ +pub usingnamespace @cImport({ + @cInclude("xcb/xcb.h"); + + // Add more define here if you want to include more keys. See depends/libxcb/depends/xorgproto/include/X11/keysymdef.h + @cDefine("XK_TECHNICAL", ""); + @cInclude("X11/keysymdef.h"); +});
\ No newline at end of file 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 |