From 8dabc0ffb9af093c3c9a586a860a8a3e2ab87ec6 Mon Sep 17 00:00:00 2001 From: Peter Li Date: Sun, 4 Oct 2026 15:41:02 -0700 Subject: [PATCH] updating uhdk to 0.17.0 --- build.zig | 8 +-- build.zig-E | 138 +++++++++++++++++++++++++++++++++++++++++ src/Profile.zig | 2 +- src/api/tray.zig | 6 +- src/callbacks.zig | 2 +- src/core_tests.zig | 2 +- src/linux/Keyboard.zig | 2 +- src/linux/build.zig | 41 ++++++++++-- src/root.zig | 6 +- 9 files changed, 188 insertions(+), 19 deletions(-) create mode 100644 build.zig-E diff --git a/build.zig b/build.zig index d643169..475756e 100644 --- a/build.zig +++ b/build.zig @@ -56,11 +56,11 @@ pub fn build(b: *std.Build) void { // Create a library artifact without installing it. Instrumented static builds // require the consumer to link a matching Tracy client. -pub fn createLibrary(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, use_llvm: ?bool, shared: bool, instrumented: bool) *std.Build.Step.Compile { +pub fn createLibrary(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, use_llvm: ?bool, shared: bool, instrumented: bool) *std.Build.Step.Compile { return createLibraryNamed(b, target, optimize, use_llvm, shared, instrumented, if (instrumented) "uhdk-instrumented" else "uhdk"); } -fn createLibraryNamed(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, use_llvm: ?bool, shared: bool, instrumented: bool, artifact_name: []const u8) *std.Build.Step.Compile { +fn createLibraryNamed(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, use_llvm: ?bool, shared: bool, instrumented: bool, artifact_name: []const u8) *std.Build.Step.Compile { std.debug.assert(!shared or !instrumented); const module = b.createModule(.{ @@ -68,7 +68,7 @@ fn createLibraryNamed(b: *std.Build, target: std.Build.ResolvedTarget, optimize: .target = target, .optimize = optimize, .link_libc = true, - .unwind_tables = if (optimize == .ReleaseSmall) .none else null, + .unwind_tables = if (optimize == .small) .none else null, }); const native = b.addTranslateC(.{ @@ -124,7 +124,7 @@ fn createLibraryNamed(b: *std.Build, target: std.Build.ResolvedTarget, optimize: return lib; } -pub fn createProfileModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, enabled: bool) *std.Build.Module { +pub fn createProfileModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, enabled: bool) *std.Build.Module { const options = b.addOptions(); options.addOption(bool, "enabled", enabled); diff --git a/build.zig-E b/build.zig-E new file mode 100644 index 0000000..d643169 --- /dev/null +++ b/build.zig-E @@ -0,0 +1,138 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + + const optimize = b.standardOptimizeOption(.{}); + const use_llvm = b.option(bool, "use_llvm", "Use LLVM for Zig compilation") orelse null; + const shared = b.option(bool, "shared", "Build libuhdk as a shared library instead of static") orelse false; + const tracy = b.option(bool, "tracy", "Emit Tracy 0.14.1 on-demand hooks; consumer must link the client") orelse false; + + if (tracy and shared) @panic("Tracy instrumentation requires static linking"); + + const lib = createLibraryNamed(b, target, optimize, use_llvm, shared, tracy, "uhdk"); + const module = lib.root_module; + const install_lib = b.addInstallArtifact(lib, .{}); + const install_headers = b.addInstallDirectory(.{ + .source_dir = b.path("include"), + .install_dir = .header, + .install_subdir = "", + }); + + const lib_step = b.step("lib", "Build and install the Uhdk library and headers"); + lib_step.dependOn(&install_lib.step); + lib_step.dependOn(&install_headers.step); + b.getInstallStep().dependOn(&install_lib.step); + b.getInstallStep().dependOn(&install_headers.step); + + const unit = b.addTest(.{ .root_module = module }); + + const check_step = b.step("check", "Compile library and core tests without running them"); + check_step.dependOn(&lib.step); + check_step.dependOn(&unit.step); + + const unit_run = b.addRunArtifact(unit); + b.step("test", "Run core library tests").dependOn(&unit_run.step); + + const public_c = b.addTranslateC(.{ + .root_source_file = b.path("include/uhdk.h"), + .target = target, + .optimize = optimize, + }); + public_c.defineCMacro("UHDK_ENABLE_OPENGL", "1"); + public_c.defineCMacro("UHDK_ENABLE_NATIVE", "1"); + public_c.addIncludePath(b.path("include")); + + if (shared) + public_c.defineCMacro("UHDK_SHARED", "1"); + + const public_module = b.addModule("uhdk", .{ + .root_source_file = public_c.getOutput(), + .target = target, + .optimize = optimize, + }); + public_module.linkLibrary(lib); +} + +// Create a library artifact without installing it. Instrumented static builds +// require the consumer to link a matching Tracy client. +pub fn createLibrary(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, use_llvm: ?bool, shared: bool, instrumented: bool) *std.Build.Step.Compile { + return createLibraryNamed(b, target, optimize, use_llvm, shared, instrumented, if (instrumented) "uhdk-instrumented" else "uhdk"); +} + +fn createLibraryNamed(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, use_llvm: ?bool, shared: bool, instrumented: bool, artifact_name: []const u8) *std.Build.Step.Compile { + std.debug.assert(!shared or !instrumented); + + const module = b.createModule(.{ + .root_source_file = b.path("src/root.zig"), + .target = target, + .optimize = optimize, + .link_libc = true, + .unwind_tables = if (optimize == .ReleaseSmall) .none else null, + }); + + const native = b.addTranslateC(.{ + .root_source_file = b.path("src/native.h"), + .target = target, + .optimize = optimize, + }); + + native.defineCMacro("UHDK_ENABLE_OPENGL", "1"); + native.addIncludePath(b.path("include")); + module.addImport("native", native.createModule()); + module.addImport("profile", createProfileModule(b, target, optimize, instrumented)); + module.addIncludePath(b.path("include")); + module.addIncludePath(b.path("src")); + + if (instrumented) + module.addCMacro("UHDK_INTERNAL_TRACY", "1"); + + if (target.result.os.tag == .macos) { + module.addCSourceFile(.{ + .file = b.path("src/macos/native.m"), + .flags = &.{ + "-fobjc-arc", "-fblocks", "-Wno-deprecated-declarations", + "-Wall", "-Wextra", "-Werror", + }, + }); + module.linkFramework("Cocoa", .{}); + module.linkFramework("OpenGL", .{}); + module.linkFramework("CoreServices", .{}); + module.linkFramework("CoreVideo", .{}); + } else if (target.result.os.tag == .linux) { + @import("src/linux/build.zig").configure(b, module, target, optimize); + } else { + module.addCSourceFile(.{ .file = b.path("src/windows/native.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror", "-DUNICODE", "-D_UNICODE" } }); + for ([_][]const u8{ "user32", "gdi32", "opengl32", "shell32", "ole32", "uuid", "imm32", "comctl32", "shlwapi", "advapi32", "api-ms-win-core-synch-l1-2-0" }) |name| module.linkSystemLibrary(name, .{}); + } + + module.addCSourceFile(.{ .file = b.path("src/gl_loader.c"), .flags = &.{ "-std=c99", "-Wall", "-Wextra", "-Werror", "-fvisibility=hidden" } }); + module.addCSourceFile(.{ .file = b.path("src/platform_io.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } }); + + const lib = b.addLibrary(.{ + .name = artifact_name, + .linkage = if (shared) .dynamic else .static, + .use_llvm = use_llvm, + .root_module = module, + }); + if (shared and target.result.os.tag == .linux) + lib.version_script = b.path("src/linux/exports.map"); + + // temp + // lib.root_module.strip = false; + + return lib; +} + +pub fn createProfileModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, enabled: bool) *std.Build.Module { + const options = b.addOptions(); + options.addOption(bool, "enabled", enabled); + + const profile = b.createModule(.{ + .root_source_file = b.path("src/Profile.zig"), + .target = target, + .optimize = optimize, + }); + profile.addOptions("profile_options", options); + return profile; +} diff --git a/src/Profile.zig b/src/Profile.zig index 9238705..3fbdfc2 100644 --- a/src/Profile.zig +++ b/src/Profile.zig @@ -2,7 +2,7 @@ const std = @import("std"); const builtin = @import("builtin"); pub const enabled = @import("profile_options").enabled; -const verify_stack_order = builtin.mode == .Debug or builtin.mode == .ReleaseSafe; +const verify_stack_order = builtin.mode == .debug or builtin.mode == .safe; // Tracy 0.14.1 C ABI with TRACY_ON_DEMAND; the consumer supplies the client. // No C header translation or references to Tracy symbols in disabled builds. diff --git a/src/api/tray.zig b/src/api/tray.zig index b1dcdd2..eca6df2 100644 --- a/src/api/tray.zig +++ b/src/api/tray.zig @@ -56,9 +56,9 @@ fn snapshot(object: *Resource, desc: [*c]const c.uhdk_tray_desc) !*Resource.Tray s.* = .{ .arena = .init(object.gpa), .desc = d }; errdefer s.arena.deinit(); const a = s.arena.allocator(); - s.desc.id = abi.textSpan(try a.dupeZ(u8, abi.textBytes(d.id))); - s.desc.title = abi.textSpan(try a.dupeZ(u8, abi.textBytes(d.title))); - s.desc.tooltip = abi.textSpan(try a.dupeZ(u8, abi.textBytes(d.tooltip))); + s.desc.id = abi.textSpan(try a.dupeSentinel(u8, abi.textBytes(d.id), 0)); + s.desc.title = abi.textSpan(try a.dupeSentinel(u8, abi.textBytes(d.title), 0)); + s.desc.tooltip = abi.textSpan(try a.dupeSentinel(u8, abi.textBytes(d.tooltip), 0)); s.desc.icon.pixels = try abi.copyBytes(a, icon.pixels); if (d.menu) |menu| runtime.retain(runtime.fromObjectHandle(menu)); return s; diff --git a/src/callbacks.zig b/src/callbacks.zig index 3a83541..076fbc9 100644 --- a/src/callbacks.zig +++ b/src/callbacks.zig @@ -110,7 +110,7 @@ export fn z_file_init( @memcpy(std.mem.sliceAsBytes(wide), abi.bytes(path)); object.payload.file.path = std.unicode.wtf16LeToWtf8AllocZ(arena, wide) catch return abi.OOM; } else { - object.payload.file.path = arena.dupeZ(u8, abi.bytes(path)) catch return abi.OOM; + object.payload.file.path = arena.dupeSentinel(u8, abi.bytes(path), 0) catch return abi.OOM; } object.payload.file.uri = arena.dupe(u8, abi.textBytes(uri)) catch return abi.OOM; diff --git a/src/core_tests.zig b/src/core_tests.zig index ac020e2..d425c08 100644 --- a/src/core_tests.zig +++ b/src/core_tests.zig @@ -183,7 +183,7 @@ test "poll allocation failure keeps queued terminal completion retryable" { application.main_thread = std.Thread.getCurrentId(); const handle = runtime.toAppHandle(application); - var bytes = [_]u8{42} ** 20000; + var bytes: [20000]u8 = @splat(42); var event = std.mem.zeroes(c.z_event); event.type = c.UHDK_EVENT_REQUEST_DONE; event.data.request = .{ .tag = 123, .kind = c.UHDK_REQUEST_FILE_READ, .status = abi.OK, .diagnostic = .{ .ptr = null, .len = 0 }, .value = .{ .bytes = .{ .ptr = &bytes, .len = bytes.len } } }; diff --git a/src/linux/Keyboard.zig b/src/linux/Keyboard.zig index 0e6ecf2..945ecbd 100644 --- a/src/linux/Keyboard.zig +++ b/src/linux/Keyboard.zig @@ -6,7 +6,7 @@ const n = @import("native"); /// physical positions, independently of the compositor-supplied XKB layout. pub fn physical(code: u32) u32 { const map = comptime blk: { - var t = [_]u16{0} ** 256; + var t: [256]u16 = @splat(0); const pairs = [_][2]u16{ .{ 1, 41 }, .{ 12, 45 }, .{ 13, 46 }, .{ 14, 42 }, .{ 15, 43 }, .{ 26, 47 }, .{ 27, 48 }, .{ 28, 40 }, .{ 29, 224 }, .{ 39, 51 }, .{ 40, 52 }, .{ 41, 53 }, .{ 42, 225 }, .{ 43, 49 }, .{ 51, 54 }, .{ 52, 55 }, .{ 53, 56 }, .{ 54, 229 }, diff --git a/src/linux/build.zig b/src/linux/build.zig index d88a8b0..7e79b78 100644 --- a/src/linux/build.zig +++ b/src/linux/build.zig @@ -1,22 +1,53 @@ const std = @import("std"); -pub fn configure(b: *std.Build, module: *std.Build.Module, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) void { +pub fn configure(b: *std.Build, module: *std.Build.Module, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize) void { const bindings = b.addTranslateC(.{ .root_source_file = b.path("src/linux/bindings.h"), .target = target, .optimize = optimize, }); - for ([_][]const u8{ "src/linux", "src/linux/vendor/wayland", "src/linux/vendor", "src/linux/vendor/dbus", "src/linux/vendor/khronos", "src/linux/protocols/generated" }) |path| { + + for ([_][]const u8{ + "src/linux", + "src/linux/vendor/wayland", + "src/linux/vendor", + "src/linux/vendor/dbus", + "src/linux/vendor/khronos", + "src/linux/protocols/generated", + }) |path| { bindings.addIncludePath(b.path(path)); module.addIncludePath(b.path(path)); } module.addImport("linux_c", bindings.createModule()); - module.addCSourceFile(.{ .file = b.path("src/linux/libraries.c"), .flags = &.{ "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-fvisibility=hidden" } }); - for ([_][]const u8{ "wayland", "xdg-shell", "xdg-decoration-unstable-v1", "viewporter", "fractional-scale-v1", "cursor-shape-v1", "pointer-constraints-unstable-v1", "tablet-v2", "text-input-unstable-v3", "xdg-activation-v1", "xdg-foreign-unstable-v2", "xdg-toplevel-icon-v1", "appmenu" }) |name| { + module.addCSourceFile(.{ + .file = b.path("src/linux/libraries.c"), + .flags = &.{ "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-fvisibility=hidden" }, + }); + + for ([_][]const u8{ + "wayland", + "xdg-shell", + "xdg-decoration-unstable-v1", + "viewporter", + "fractional-scale-v1", + "cursor-shape-v1", + "pointer-constraints-unstable-v1", + "tablet-v2", + "text-input-unstable-v3", + "xdg-activation-v1", + "xdg-foreign-unstable-v2", + "xdg-toplevel-icon-v1", + "appmenu", + }) |name| { module.addCSourceFile(.{ .file = b.path(b.fmt("src/linux/protocols/generated/{s}.c", .{name})), - .flags = &.{ "-std=c99", "-fdata-sections", "-include", b.path("src/linux/protocols/generated/private-symbols.h").getPath(b) }, + .flags = &.{ + "-std=c99", + "-fdata-sections", + "-include", + "private-symbols.h", + }, }); } diff --git a/src/root.zig b/src/root.zig index 2226941..c50cbe3 100644 --- a/src/root.zig +++ b/src/root.zig @@ -33,9 +33,9 @@ test { } fn checkExports(comptime module: type) void { - for (@typeInfo(module).@"struct".decls) |declaration| { - if (!std.mem.startsWith(u8, declaration.name, "uhdk_")) continue; - if (@TypeOf(@field(module, declaration.name)) != @TypeOf(@field(c, declaration.name))) { + for (@typeInfo(module).@"struct".decl_names) |declaration| { + if (!std.mem.startsWith(u8, declaration, "uhdk_")) continue; + if (@TypeOf(@field(module, declaration)) != @TypeOf(@field(c, declaration))) { @compileError("C signature mismatch: " ++ declaration.name); } }