-
Notifications
You must be signed in to change notification settings - Fork 0
/
x11common.zig
92 lines (80 loc) · 3.24 KB
/
x11common.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const std = @import("std");
const x = @import("x");
const common = @This();
pub const SocketReader = std.io.Reader(std.os.socket_t, std.os.RecvFromError, readSocket);
pub fn send(sock: std.os.socket_t, data: []const u8) !void {
const sent = try x.writeSock(sock, data, 0);
if (sent != data.len) {
std.log.err("send {} only sent {}\n", .{data.len, sent});
return error.DidNotSendAllData;
}
}
pub const ConnectResult = struct {
sock: std.os.socket_t,
setup: x.ConnectSetup,
pub fn reader(self: ConnectResult) SocketReader {
return .{ .context = self.sock };
}
pub fn send(self: ConnectResult, data: []const u8) !void {
try common.send(self.sock, data);
}
};
pub fn connect(allocator: std.mem.Allocator) !ConnectResult {
const display = x.getDisplay();
const parsed_display = x.parseDisplay(display) catch |err| {
std.log.err("invalid display '{s}': {s}", .{display, @errorName(err)});
std.os.exit(0xff);
};
const sock = x.connect(display, parsed_display) catch |err| {
std.log.err("failed to connect to display '{s}': {s}", .{display, @errorName(err)});
std.os.exit(0xff);
};
errdefer x.disconnect(sock);
{
const len = comptime x.connect_setup.getLen(0, 0);
var msg: [len]u8 = undefined;
x.connect_setup.serialize(&msg, 11, 0, .{ .ptr = undefined, .len = 0 }, .{ .ptr = undefined, .len = 0 });
try send(sock, &msg);
}
const reader = SocketReader { .context = sock };
const connect_setup_header = try x.readConnectSetupHeader(reader, .{});
switch (connect_setup_header.status) {
.failed => {
std.log.err("connect setup failed, version={}.{}, reason='{s}'", .{
connect_setup_header.proto_major_ver,
connect_setup_header.proto_minor_ver,
connect_setup_header.readFailReason(reader),
});
return error.ConnectSetupFailed;
},
.authenticate => {
std.log.err("AUTHENTICATE! not implemented", .{});
return error.NotImplemetned;
},
.success => {
// TODO: check version?
std.log.debug("SUCCESS! version {}.{}", .{connect_setup_header.proto_major_ver, connect_setup_header.proto_minor_ver});
},
else => |status| {
std.log.err("Error: expected 0, 1 or 2 as first byte of connect setup reply, but got {}", .{status});
return error.MalformedXReply;
}
}
const connect_setup = x.ConnectSetup {
.buf = try allocator.allocWithOptions(u8, connect_setup_header.getReplyLen(), 4, null),
};
std.log.debug("connect setup reply is {} bytes", .{connect_setup.buf.len});
try x.readFull(reader, connect_setup.buf);
return ConnectResult{ .sock = sock, .setup = connect_setup };
}
pub fn asReply(comptime T: type, msg_bytes: []align(4) u8) !*T {
const generic_msg: *x.ServerMsg.Generic = @ptrCast(msg_bytes.ptr);
if (generic_msg.kind != .reply) {
std.log.err("expected reply but got {}", .{generic_msg});
return error.UnexpectedReply;
}
return @ptrCast(generic_msg);
}
fn readSocket(sock: std.os.socket_t, buffer: []u8) !usize {
return x.readSock(sock, buffer, 0);
}