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

85 lines
2.5 KiB
Zig

const std = @import("std");
const xcb = @import("xcb");
pub fn main(init: std.process.Init) !void {
_ = init;
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_reply = connection.internAtomReply(wm_protocols_atom_cookie, null) orelse return error.XcbInternAtom;
defer std.c.free(wm_protocols_atom_reply);
const wm_protocols_atom = wm_protocols_atom_reply.atom;
const wm_delete_window_atom_reply = connection.internAtomReply(wm_delete_window_atom_cookie, null) orelse return error.XcbInternAtom;
defer std.c.free(wm_delete_window_atom_reply);
const wm_delete_window_atom = wm_delete_window_atom_reply.atom;
_ = connection.changeProperty(
.REPLACE,
window,
wm_protocols_atom,
.ATOM,
32,
1,
&wm_delete_window_atom,
);
_ = connection.mapWindow(window);
_ = connection.flush();
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_reply.atom)) {
running = false;
}
},
else => {},
}
} else {
std.debug.print("Error: xcb_wait_for_event returned null", .{});
return error.XcbWaitForEvent;
}
}
}