Skip to content

Commit

Permalink
zig update 0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed Apr 20, 2024
1 parent 2106354 commit 854457e
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 181 deletions.
53 changes: 31 additions & 22 deletions DoubleBuffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const DoubleBuffer = @This();

const builtin = @import("builtin");
const std = @import("std");
const os = std.os;
const posix = std.posix;
const ContiguousReadBuffer = @import("ContiguousReadBuffer.zig");

const impl: enum { memfd, shm, windows } = switch (builtin.os.tag) {
Expand All @@ -22,8 +22,8 @@ const impl: enum { memfd, shm, windows } = switch (builtin.os.tag) {
ptr: [*]align(std.mem.page_size) u8,
half_len: usize,
data: switch (impl) {
.memfd => os.fd_t,
.shm => os.fd_t,
.memfd => posix.fd_t,
.shm => posix.fd_t,
.windows => win32.HANDLE,
},

Expand All @@ -35,8 +35,8 @@ pub const InitOptions = struct {
pub fn init(half_len: usize, opt: InitOptions) !DoubleBuffer {
switch (impl) {
.memfd => {
const fd = try os.memfd_createZ(opt.memfd_name, 0);
errdefer os.close(fd);
const fd = try posix.memfd_createZ(opt.memfd_name, 0);
errdefer posix.close(fd);
const ptr = try mapFdDouble(fd, half_len);
return .{
.ptr = ptr,
Expand All @@ -57,7 +57,7 @@ pub fn init(half_len: usize, opt: InitOptions) !DoubleBuffer {
var unique_name_buf: [rand_hex_len + 1]u8 = undefined;
const unique_name = blk: {
var rand_bytes: [rand_byte_len]u8 = undefined;
try os.getrandom(&rand_bytes);
try posix.getrandom(&rand_bytes);
break :blk std.fmt.bufPrintZ(
&unique_name_buf,
"{}",
Expand All @@ -68,15 +68,15 @@ pub fn init(half_len: usize, opt: InitOptions) !DoubleBuffer {

const fd = std.c.shm_open(
unique_name,
std.os.O.RDWR | std.os.O.CREAT | std.os.O.EXCL,
std.os.S.IRUSR | std.os.S.IWUSR,
std.posix.O.RDWR | std.posix.O.CREAT | std.posix.O.EXCL,
std.posix.S.IRUSR | std.posix.S.IWUSR,
);
if (fd == -1) switch (@as(std.os.E, @enumFromInt(std.c._errno().*))) {
if (fd == -1) switch (@as(std.posix.E, @enumFromInt(std.c._errno().*))) {
.EXIST => return error.PathAlreadyExists,
.NAMETOOLONG => return error.NameTooLong,
else => |err| return std.os.unexpectedErrno(err),
else => |err| return std.posix.unexpectedErrno(err),
};
errdefer os.close(fd);
errdefer posix.close(fd);
const ptr = try mapFdDouble(fd, half_len);
return .{
.ptr = ptr,
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn init(half_len: usize, opt: InitOptions) !DoubleBuffer {
) orelse switch (win32.GetLastError()) {
else => |err| return std.os.windows.unexpectedError(err),
};
errdefer os.close(map);
errdefer posix.close(map);

const ptr_again = win32.MapViewOfFile3FromApp(
map,
Expand Down Expand Up @@ -168,13 +168,13 @@ pub fn init(half_len: usize, opt: InitOptions) !DoubleBuffer {
pub fn deinit(self: DoubleBuffer) void {
switch (impl) {
.memfd, .shm => {
os.munmap(self.ptr[0 .. self.half_len * 2]);
os.close(self.data);
posix.munmap(self.ptr[0 .. self.half_len * 2]);
posix.close(self.data);
},
.windows => {
std.debug.assert(0 != win32.UnmapViewOfFile(self.ptr + self.half_len));
std.debug.assert(0 != win32.UnmapViewOfFile(self.ptr));
errdefer os.close(self.data);
errdefer posix.close(self.data);
},
}
}
Expand All @@ -187,14 +187,23 @@ pub fn contiguousReadBuffer(self: DoubleBuffer) ContiguousReadBuffer {
}


pub fn mapFdDouble(fd: os.fd_t, half_size: usize) ![*]align(std.mem.page_size) u8 {
pub fn mapFdDouble(fd: posix.fd_t, half_size: usize) ![*]align(std.mem.page_size) u8 {
std.debug.assert((half_size % std.mem.page_size) == 0);
try os.ftruncate(fd, half_size);
const ptr = (try os.mmap(null, 2 * half_size, os.PROT.NONE, os.MAP.PRIVATE | os.MAP.ANONYMOUS, -1, 0)).ptr;
_ = try os.mmap(ptr,
half_size, os.PROT.READ | os.PROT.WRITE, os.MAP.SHARED | os.MAP.FIXED, fd, 0);
_ = try os.mmap(@alignCast(ptr + half_size),
half_size, os.PROT.READ | os.PROT.WRITE, os.MAP.SHARED | os.MAP.FIXED, fd, 0);
try posix.ftruncate(fd, half_size);
const ptr = (try posix.mmap(
null,
2 * half_size,
posix.PROT.NONE,
.{ .TYPE = .PRIVATE, .ANONYMOUS = true },
-1,
0,
)).ptr;
_ = try posix.mmap(
ptr, half_size, posix.PROT.READ | posix.PROT.WRITE, .{ .TYPE = .SHARED, .FIXED = true }, fd, 0
);
_ = try posix.mmap(
@alignCast(ptr + half_size), half_size, posix.PROT.READ | posix.PROT.WRITE, .{ .TYPE = .SHARED, .FIXED = true }, fd, 0
);
return ptr;
}

Expand Down
18 changes: 9 additions & 9 deletions MappedFile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,36 @@ pub fn init(filename: []const u8, opt: Options) !MappedFile {
else => |err| std.os.windows.unexpectedError(err),
};
errdefer std.debug.assert(0 != win32.UnmapViewOfFile(ptr));

return .{
.mem = @as([*]align(std.mem.page_size)u8, @alignCast(@ptrCast(ptr)))[0 .. file_size],
.mapping = mapping,
};
};
}
return .{
.mem = try std.os.mmap(
.mem = try std.posix.mmap(
null,
file_size,
switch (opt.mode) {
.read_only => std.os.PROT.READ,
.read_write => std.os.PROT.READ | std.os.PROT.WRITE,
.read_only => std.posix.PROT.READ,
.read_write => std.posix.PROT.READ | std.posix.PROT.WRITE,
},
std.os.MAP.PRIVATE,
.{ .TYPE = .PRIVATE },
file.handle,
0,
),
.mapping = {},
};
};
}

pub fn unmap(self: MappedFile) void {
if (builtin.os.tag == .windows) {
if (self.mem.len != 0) {
std.debug.assert(0 != win32.UnmapViewOfFile(self.mem.ptr));
std.os.windows.CloseHandle(self.mapping);
}
} else {
std.os.munmap(self.mem);
std.posix.munmap(self.mem);
}
}

Expand Down
14 changes: 3 additions & 11 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
const builtin = @import("builtin");
const std = @import("std");

const is_zig_0_11 = std.mem.eql(u8, builtin.zig_version_string, "0.11.0");

pub fn build(b: *std.Build) void {
if (is_zig_0_11) {
_ = b.addModule("zigx", .{
.source_file = .{ .path = "x.zig" },
});
} else {
_ = b.addModule("zigx", .{
.root_source_file = .{ .path = "x.zig" },
});
}
_ = b.addModule("zigx", .{
.root_source_file = .{ .path = "x.zig" },
});
}
20 changes: 10 additions & 10 deletions common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const std = @import("std");
const x = @import("x.zig");
const common = @This();

pub const SocketReader = std.io.Reader(std.os.socket_t, std.os.RecvFromError, readSocket);
pub const SocketReader = std.io.Reader(std.posix.socket_t, std.posix.RecvFromError, readSocket);

pub fn send(sock: std.os.socket_t, data: []const u8) !void {
pub fn send(sock: std.posix.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});
Expand All @@ -13,7 +13,7 @@ pub fn send(sock: std.os.socket_t, data: []const u8) !void {
}

pub const ConnectResult = struct {
sock: std.os.socket_t,
sock: std.posix.socket_t,
setup: x.ConnectSetup,
pub fn reader(self: ConnectResult) SocketReader {
return .{ .context = self.sock };
Expand All @@ -24,7 +24,7 @@ pub const ConnectResult = struct {
};

pub fn connectSetupMaxAuth(
sock: std.os.socket_t,
sock: std.posix.socket_t,
comptime max_auth_len: usize,
auth_name: x.Slice(u16, [*]const u8),
auth_data: x.Slice(u16, [*]const u8),
Expand All @@ -37,7 +37,7 @@ pub fn connectSetupMaxAuth(
}

pub fn connectSetup(
sock: std.os.socket_t,
sock: std.posix.socket_t,
msg: []u8,
auth_name: x.Slice(u16, [*]const u8),
auth_data: x.Slice(u16, [*]const u8),
Expand Down Expand Up @@ -76,7 +76,7 @@ pub fn connectSetup(

fn connectSetupAuth(
display_num: ?u32,
sock: std.os.socket_t,
sock: std.posix.socket_t,
auth_filename: []const u8,
) !?u16 {
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Expand Down Expand Up @@ -132,12 +132,12 @@ 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);
std.process.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);
std.process.exit(0xff);
};
errdefer x.disconnect(sock);

Expand All @@ -161,7 +161,7 @@ pub fn connect(allocator: std.mem.Allocator) !ConnectResult {
}

std.log.err("the X server rejected our connect setup message", .{});
std.os.exit(0xff);
std.process.exit(0xff);
};

const connect_setup = x.ConnectSetup {
Expand All @@ -183,6 +183,6 @@ pub fn asReply(comptime T: type, msg_bytes: []align(4) u8) !*T {
return @alignCast(@ptrCast(generic_msg));
}

fn readSocket(sock: std.os.socket_t, buffer: []u8) !usize {
fn readSocket(sock: std.posix.socket_t, buffer: []u8) !usize {
return x.readSock(sock, buffer, 0);
}
4 changes: 2 additions & 2 deletions example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const window_height = 400;
pub fn main() !u8 {
try x.wsaStartup();
const conn = try common.connect(allocator);
defer std.os.shutdown(conn.sock, .both) catch {};
defer std.posix.shutdown(conn.sock, .both) catch {};

const screen = blk: {
const fixed = conn.setup.fixed();
Expand Down Expand Up @@ -234,7 +234,7 @@ const FontDims = struct {
font_ascent: i16, // pixels up from the text basepoint to the top of the text
};

fn render(sock: std.os.socket_t, drawable_id: u32, bg_gc_id: u32, fg_gc_id: u32, font_dims: FontDims) !void {
fn render(sock: std.posix.socket_t, drawable_id: u32, bg_gc_id: u32, fg_gc_id: u32, font_dims: FontDims) !void {
{
var msg: [x.poly_fill_rectangle.getLen(1)]u8 = undefined;
x.poly_fill_rectangle.serialize(&msg, .{
Expand Down
24 changes: 12 additions & 12 deletions fontviewer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Ids = struct {
pub fn main() !u8 {
try x.wsaStartup();
const conn = try common.connect(allocator);
defer std.os.shutdown(conn.sock, .both) catch {};
defer std.posix.shutdown(conn.sock, .both) catch {};

const Key = enum {
left, right,
Expand Down Expand Up @@ -242,7 +242,7 @@ const State = struct {
pub fn onExpose(
self: *State,
msg: *x.Event.Expose,
sock: std.os.socket_t,
sock: std.posix.socket_t,
ids: Ids,
fonts: []x.Slice(u8, [*]const u8),
) !void {
Expand All @@ -258,7 +258,7 @@ const State = struct {

fn getDesiredFont(
self: *State,
sock: std.os.socket_t,
sock: std.posix.socket_t,
ids: Ids,
fonts: []x.Slice(u8, [*]const u8),
) !void {
Expand Down Expand Up @@ -295,7 +295,7 @@ const State = struct {

pub fn onQueryFontError(self: *State,
msg: *x.ServerMsg.Error.Font,
sock: std.os.socket_t,
sock: std.posix.socket_t,
ids: Ids,
fonts: []x.Slice(u8, [*]const u8),
) !void {
Expand All @@ -315,7 +315,7 @@ const State = struct {
pub fn onReply(
self: *State,
reply_msg: *align(4) x.ServerMsg.Reply,
sock: std.os.socket_t,
sock: std.posix.socket_t,
ids: Ids,
fonts: []x.Slice(u8, [*]const u8),
) !void {
Expand All @@ -338,7 +338,7 @@ const State = struct {
fn atIdleCheckDesiredFont(
self: *State,
idle: Idle,
sock: std.os.socket_t,
sock: std.posix.socket_t,
ids: Ids,
fonts: []x.Slice(u8, [*]const u8),
) !void {
Expand All @@ -349,7 +349,7 @@ const State = struct {

pub fn updateDesiredFont(
self: *State,
sock: std.os.socket_t,
sock: std.posix.socket_t,
ids: Ids,
fonts: []x.Slice(u8, [*]const u8),
new_desired_font_index: usize,
Expand All @@ -365,7 +365,7 @@ const State = struct {
}
};

fn render(sock: std.os.socket_t, ids: Ids, fonts: []x.Slice(u8, [*]const u8), font_index: usize, font_info: *const x.ServerMsg.QueryFont) !void {
fn render(sock: std.posix.socket_t, ids: Ids, fonts: []x.Slice(u8, [*]const u8), font_index: usize, font_info: *const x.ServerMsg.QueryFont) !void {

const font_name = fonts[font_index];
//std.log.info("rendering font '{s}'", .{font_name});
Expand Down Expand Up @@ -400,7 +400,7 @@ fn render(sock: std.os.socket_t, ids: Ids, fonts: []x.Slice(u8, [*]const u8), fo
try renderText(sock, ids.window(), ids.gcText(), 10, 10 + (font_height * 6), "abcdefghijklmnopqrstuvwxyz", .{});
}

fn renderNoFontInfo(sock: std.os.socket_t, ids: Ids, fonts: []x.Slice(u8, [*]const u8), font_index: usize, still_open: bool) !void {
fn renderNoFontInfo(sock: std.posix.socket_t, ids: Ids, fonts: []x.Slice(u8, [*]const u8), font_index: usize, still_open: bool) !void {
_ = still_open;
const font_name = fonts[font_index];
_ = font_name;
Expand Down Expand Up @@ -429,11 +429,11 @@ fn renderNoFontInfo(sock: std.os.socket_t, ids: Ids, fonts: []x.Slice(u8, [*]con
//try renderText(sock, ids.window(), ids.gcText(), 10, 90, "Failed to query font info", .{});
}

fn renderText(sock: std.os.socket_t, drawable_id: u32, gc_id: u32, x_coord: i16, y: i16, comptime fmt: []const u8, args: anytype) !void {
fn renderText(sock: std.posix.socket_t, drawable_id: u32, gc_id: u32, x_coord: i16, y: i16, comptime fmt: []const u8, args: anytype) !void {
const str_len_u64 = std.fmt.count(fmt, args);
const str_len = std.math.cast(u8, str_len_u64) orelse
std.debug.panic("render large string {} not implemented", .{str_len_u64});

const msg_len = x.image_text8.getLen(str_len);
const msg = try allocator.alloc(u8, msg_len);
defer allocator.free(msg);
Expand All @@ -447,7 +447,7 @@ fn renderText(sock: std.os.socket_t, drawable_id: u32, gc_id: u32, x_coord: i16,
try common.send(sock, msg);
}

fn openAndQueryFont(sock: std.os.socket_t, font_id: u32, font_name: x.Slice(u8, [*]const u8)) !void {
fn openAndQueryFont(sock: std.posix.socket_t, font_id: u32, font_name: x.Slice(u8, [*]const u8)) !void {
// TODO: combine these into 1 send
std.log.info("open and query '{s}'", .{font_name});
{
Expand Down
2 changes: 1 addition & 1 deletion getserverfontnames.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const allocator = arena.allocator();
pub fn main() !void {
try x.wsaStartup();
const conn = try common.connect(allocator);
defer std.os.shutdown(conn.sock, .both) catch {};
defer std.posix.shutdown(conn.sock, .both) catch {};

{
const pattern_string = "*";
Expand Down
Loading

0 comments on commit 854457e

Please sign in to comment.