Skip to content

Commit

Permalink
update c subdirectory to zig version 0.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed Sep 10, 2023
1 parent 4470a63 commit 80e84e4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
34 changes: 22 additions & 12 deletions c/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,37 @@ const std = @import("std");

pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const optimize = b.standardOptimizeOption(.{});

const x11_lib = b.addStaticLibrary("x11", "x11.zig");
x11_lib.setTarget(target);
x11_lib.setBuildMode(mode);
x11_lib.addIncludePath(b.pathJoin(&.{ b.build_root, "include" }));
x11_lib.addPackagePath("x", b.pathJoin(&.{ b.build_root, "../x.zig" }));
const x_mod = b.createModule(.{
.source_file = .{ .path = b.pathJoin(&.{ b.build_root.path.?, "../x.zig" }) },
});

const x11_lib = b.addStaticLibrary(.{
.name = "x11",
.root_source_file = .{ .path = "x11.zig" },
.target = target,
.optimize = optimize,
});
x11_lib.addIncludePath(.{ .path = b.pathJoin(&.{ b.build_root.path.?, "include" }) });
x11_lib.addModule("x", x_mod);
// I *think* we'll want to link libc here because it's probably guaranteed that
// the application will be linking libc and not linking libc means we could have
// discrepancies, for example, zig's start code that initializes the environment
// variables wouldn't have run
x11_lib.linkLibC();
x11_lib.install();
b.installArtifact(x11_lib);

{
const exe = b.addExecutable("hellox11", "example/hellox11.c");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.addIncludePath(b.pathJoin(&.{ b.build_root, "include" }));
const exe = b.addExecutable(.{
.name = "hellox11",
.root_source_file = .{ .path = "example/hellox11.c" },
.target = target,
.optimize = optimize,
});
exe.addIncludePath(.{ .path = b.pathJoin(&.{ b.build_root.path.?, "include" })});
exe.linkLibC();
exe.linkLibrary(x11_lib);
exe.install();
b.installArtifact(exe);
}
}
3 changes: 2 additions & 1 deletion c/example/hellox11.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>

#include <X11/Xlib.h>
Expand Down Expand Up @@ -86,7 +87,7 @@ int main(int argc, char *argv[])
logf("TODO: handle key press!");
break;
default:
error("unknown even type %d", event.type);
errorf("unknown even type %d", event.type);
exit(1);
}
}
Expand Down
26 changes: 13 additions & 13 deletions c/x11.zig
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,20 @@ fn openDisplay(display_spec_opt: ?[*:0]const u8) error{Reported, OutOfMemory}!*c

const setup_screens_ptr = connect_setup.getScreensPtr(format_list_limit);
const screens = try c_allocator.alloc(c.Screen, fixed.root_screen_count);
std.log.debug("screens allocated to 0x{x}", .{@ptrToInt(screens.ptr)});
std.log.debug("screens allocated to 0x{x}", .{@intFromPtr(screens.ptr)});
errdefer c_allocator.free(screens);
for (screens) |*screen_dst, screen_index| {
for (screens, 0..) |*screen_dst, screen_index| {
const screen_src = &setup_screens_ptr[screen_index];
inline for (@typeInfo(@TypeOf(screen_src.*)).Struct.fields) |field| {
std.log.debug("SCREEN {}| {s}: {any}", .{screen_index, field.name, @field(screen_src, field.name)});
}
std.log.debug("screen_ptr is 0x{x}", .{@ptrToInt(screen_dst)});
std.log.debug("screen_ptr is 0x{x}", .{@intFromPtr(screen_dst)});
screen_dst.* = .{
.display = &display.public,
.root = screen_src.root,
.root_visual_num = screen_src.root_visual,
.white_pixel = @intCast(c_ulong, screen_src.white_pixel),
.black_pixel = @intCast(c_ulong, screen_src.black_pixel),
.white_pixel = @intCast(screen_src.white_pixel),
.black_pixel = @intCast(screen_src.black_pixel),
};
}

Expand All @@ -142,7 +142,7 @@ fn openDisplay(display_spec_opt: ?[*:0]const u8) error{Reported, OutOfMemory}!*c
.proto_major_version = connect_setup_header.proto_major_ver,
.proto_minor_version = connect_setup_header.proto_minor_ver,
.default_screen = 0,
.nscreens = @intCast(c_uint, fixed.root_screen_count),
.nscreens = @intCast(fixed.root_screen_count),
.screens = screens.ptr,
},
.resource_id_base = fixed.resource_id_base,
Expand All @@ -165,7 +165,7 @@ export fn XCloseDisplay(display_opt: ?*c.Display) c_int {
}

c_allocator.free(display_full.read_buf);
c_allocator.free(display.screens[0 .. @intCast(usize, display.nscreens)]);
c_allocator.free(display.screens[0 .. @intCast(display.nscreens)]);
//c_allocator.free(@ptrCast([*]u8, display.connect_setup)[0 .. display.connect_setup_len]);
c_allocator.destroy(display);
return 0;
Expand Down Expand Up @@ -196,9 +196,9 @@ export fn XCreateSimpleWindow(
.parent_window_id = root_window,
// TODO: set this correctly
.depth = 0,
.x = @intCast(u16, x_pos), .y = @intCast(u16, y),
.width = @intCast(u16, width), .height = @intCast(u16, height),
.border_width = @intCast(u16, border_width),
.x = @intCast(x_pos), .y = @intCast(y),
.width = @intCast(width), .height = @intCast(height),
.border_width = @intCast(border_width),
// TODO
.class = .copy_from_parent,
// .class = .input_output,
Expand Down Expand Up @@ -273,7 +273,7 @@ export fn XCreateGC(
generateErrorEvent(display_full);
};

return @ptrCast(c.GC, gc);
return @ptrCast(gc);
}

export fn XMapRaised(display: *c.Display, window: c.Window) c_int {
Expand Down Expand Up @@ -332,7 +332,7 @@ export fn XNextEvent(display: *c.Display, event: *c.XEvent) c_int {
.xexpose = .{
.@"type" = c.Expose,
.serial = e.sequence,
.send_event = @boolToInt((display_full.read_buf[0] & 0x80) != 0),
.send_event = @intFromBool((display_full.read_buf[0] & 0x80) != 0),
.display = display,
.window = e.window,
.x = e.x, .y = e.y,
Expand All @@ -353,7 +353,7 @@ export fn XSelectInput(display: *c.Display, window: c.Window, event_mask: c_ulon

var msg_buf: [x.change_window_attributes.max_len]u8 = undefined;
const len = x.change_window_attributes.serialize(&msg_buf, window, .{
.event_mask = @intCast(u32, event_mask),
.event_mask = @intCast(event_mask),
});
sendAll(display.fd, msg_buf[0 .. len]) catch |err| {
reportErrorRaw("failed to send ChangeWindowAttributes message with {s}", .{@errorName(err)});
Expand Down

0 comments on commit 80e84e4

Please sign in to comment.