Files
castle/packages/xcb/sample/root.zig

96 lines
2.7 KiB
Zig

const std = @import("std");
const xcb = @import("xcb");
const Vulkan = @import("Vulkan.zig");
// Declarations picked up by "vulkan" module
pub const xcb_connection_t = xcb.Connection;
pub const xcb_visualid_t = xcb.Visualid;
pub const xcb_window_t = xcb.Window;
pub fn main(init: std.process.Init) !void {
const connection = xcb.Connection.connect(null, null);
defer connection.disconnect();
{
const error_code = connection.hasError();
if (error_code != 0) {
std.debug.print("Error: xcb_connection_has_error returned {}", .{error_code});
return error.XcbConnection;
}
}
const setup = connection.getSetup();
const screen = setup.rootsIterator().data;
const window = @as(xcb.Window, @enumFromInt(connection.generateId()));
_ = connection.createWindow(
xcb.COPY_FROM_PARENT,
window,
screen.root,
0,
0,
640,
360,
1,
.INPUT_OUTPUT,
screen.root_visual,
.{ .BACK_PIXEL = true },
&[_]u32{screen.black_pixel},
);
const wm_protocols_atom_cookie = connection.internAtom(true, "WM_PROTOCOLS");
const wm_delete_window_atom_cookie = connection.internAtom(false, "WM_DELETE_WINDOW");
const wm_protocols_atom = blk: {
const reply = connection.internAtomReply(wm_protocols_atom_cookie, null) orelse return error.XcbInternAtom;
const ret = reply.atom;
std.c.free(reply);
break :blk ret;
};
const wm_delete_window_atom = blk: {
const reply = connection.internAtomReply(wm_delete_window_atom_cookie, null) orelse return error.XcbInternAtom;
const ret = reply.atom;
std.c.free(reply);
break :blk ret;
};
_ = connection.changeProperty(
.REPLACE,
window,
wm_protocols_atom,
.ATOM,
32,
1,
&wm_delete_window_atom,
);
_ = connection.mapWindow(window);
_ = connection.flush();
var vulkan = try Vulkan.init(init.gpa, connection, window);
defer vulkan.deinit(init.gpa);
var running = true;
while (running) {
if (connection.waitForEvent()) |event| {
defer std.c.free(event);
switch (event.response_type & ~@as(u8, 0x80)) {
xcb.CLIENT_MESSAGE => {
const client_message_event = @as(*xcb.ClientMessageEvent, @ptrCast(event));
if (client_message_event.data.data32[0] == @intFromEnum(wm_delete_window_atom)) {
running = false;
}
},
else => {},
}
} else {
std.debug.print("Error: xcb_wait_for_event returned null", .{});
return error.XcbWaitForEvent;
}
}
}