xcb: sample-driven ziggification
This commit is contained in:
@@ -1,83 +1,75 @@
|
||||
const std = @import("std");
|
||||
const xcb = @import("xcb");
|
||||
|
||||
const WM_PROTOCOLS = "WM_PROTOCOLS";
|
||||
const WM_DELETE_WINDOW = "WM_DELETE_WINDOW";
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
_ = init;
|
||||
|
||||
const connection = xcb.import.xcb_connect(null, null);
|
||||
defer xcb.import.xcb_disconnect(connection);
|
||||
const connection = xcb.Connection.connect(null, null);
|
||||
defer connection.disconnect();
|
||||
|
||||
{
|
||||
const res = xcb.import.xcb_connection_has_error(connection);
|
||||
if (res != 0) {
|
||||
std.debug.print("Error: xcb_connection_has_error returned {}", .{res});
|
||||
const error_code = connection.hasError();
|
||||
if (error_code != 0) {
|
||||
std.debug.print("Error: xcb_connection_has_error returned {}", .{error_code});
|
||||
return error.XcbConnection;
|
||||
}
|
||||
}
|
||||
|
||||
const screen = xcb.import.xcb_setup_roots_iterator(xcb.import.xcb_get_setup(connection)).data.?;
|
||||
const setup = connection.getSetup();
|
||||
const screen = setup.rootsIterator().data;
|
||||
|
||||
const mask: xcb.Cw = .{
|
||||
.BACK_PIXEL = true,
|
||||
.EVENT_MASK = true,
|
||||
};
|
||||
|
||||
const values = [_]u32{
|
||||
screen.white_pixel,
|
||||
@bitCast(xcb.EventMask{
|
||||
.KEY_PRESS = true,
|
||||
}),
|
||||
};
|
||||
|
||||
const window: xcb.Window = @enumFromInt(xcb.import.xcb_generate_id(connection));
|
||||
_ = xcb.import.xcb_create_window(
|
||||
connection,
|
||||
const window = @as(xcb.Window, @enumFromInt(connection.generateId()));
|
||||
_ = connection.createWindow(
|
||||
xcb.COPY_FROM_PARENT,
|
||||
window,
|
||||
screen.root,
|
||||
10,
|
||||
10,
|
||||
0,
|
||||
0,
|
||||
640,
|
||||
480,
|
||||
360,
|
||||
1,
|
||||
@intFromEnum(xcb.WindowClass.INPUT_OUTPUT),
|
||||
.INPUT_OUTPUT,
|
||||
screen.root_visual,
|
||||
@bitCast(mask),
|
||||
&values,
|
||||
.{
|
||||
.BACK_PIXEL = true,
|
||||
},
|
||||
&[_]u32{
|
||||
screen.black_pixel,
|
||||
},
|
||||
);
|
||||
|
||||
const wm_protocols_atom_cookie = xcb.import.xcb_intern_atom(connection, 1, WM_PROTOCOLS.len, WM_PROTOCOLS);
|
||||
const wm_delete_window_atom_cookie = xcb.import.xcb_intern_atom(connection, 0, WM_DELETE_WINDOW.len, WM_DELETE_WINDOW);
|
||||
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: *xcb.InternAtomReply = xcb.import.xcb_intern_atom_reply(connection, wm_protocols_atom_cookie, null) orelse return error.XcbInternAtom;
|
||||
const wm_delete_window_atom_reply: *xcb.InternAtomReply = xcb.import.xcb_intern_atom_reply(connection, wm_delete_window_atom_cookie, null) orelse return error.XcbInternAtom;
|
||||
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;
|
||||
|
||||
_ = xcb.import.xcb_change_property(
|
||||
connection,
|
||||
@intFromEnum(xcb.PropMode.REPLACE),
|
||||
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_reply.atom,
|
||||
wm_protocols_atom,
|
||||
.ATOM,
|
||||
32,
|
||||
1,
|
||||
&wm_delete_window_atom_reply.atom,
|
||||
&wm_delete_window_atom,
|
||||
);
|
||||
|
||||
_ = xcb.import.xcb_map_window(connection, window);
|
||||
_ = xcb.import.xcb_flush(connection);
|
||||
_ = connection.mapWindow(window);
|
||||
_ = connection.flush();
|
||||
|
||||
var running = true;
|
||||
while (running) {
|
||||
const maybe_event: ?*xcb.GenericEvent = xcb.import.xcb_wait_for_event(connection);
|
||||
if (maybe_event) |event| {
|
||||
if (connection.waitForEvent()) |event| {
|
||||
defer std.c.free(event);
|
||||
|
||||
switch (event.response_type & ~@as(u8, 0x80)) {
|
||||
xcb.CLIENT_MESSAGE => {
|
||||
const client_message_event: *xcb.ClientMessageEvent = @ptrCast(event);
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user