diff --git a/modules/hdmi-relink/src/main.zig b/modules/hdmi-relink/src/main.zig index 936a2d6..e802436 100644 --- a/modules/hdmi-relink/src/main.zig +++ b/modules/hdmi-relink/src/main.zig @@ -27,6 +27,12 @@ const c = @cImport({ @cInclude("xcb/screensaver.h"); }); +// libxcb 1.17 made xcb_extension_t opaque and Zig 0.16's translate-c refuses +// opaque-typed globals, so declare the extension key symbols by hand. +extern var xcb_randr_id: c.xcb_extension_t; +extern var xcb_dpms_id: c.xcb_extension_t; +extern var xcb_screensaver_id: c.xcb_extension_t; + const Config = struct { output: []const u8 = "HDMI-0", low_w: u16 = 1920, @@ -46,6 +52,18 @@ const Conn = struct { root: c.xcb_window_t, }; +/// std.Thread.sleep moved behind the Io interface in Zig 0.16; this daemon +/// only ever needs a plain blocking sleep. +fn sleepMs(ms: u64) void { + const ns = ms * std.time.ns_per_ms; + var req: std.os.linux.timespec = .{ + .sec = @intCast(ns / std.time.ns_per_s), + .nsec = @intCast(ns % std.time.ns_per_s), + }; + var rem: std.os.linux.timespec = undefined; + while (std.os.linux.errno(std.os.linux.nanosleep(&req, &rem)) == .INTR) req = rem; +} + fn logMsg(comptime fmt: []const u8, args: anytype) void { // stderr goes straight to the journal under systemd; no logger(1) exec std.debug.print("hdmi-relink: " ++ fmt ++ "\n", args); @@ -209,17 +227,26 @@ fn growScreen(conn: Conn, need_w: u32, need_h: u32) void { /// Returns the locked file on success, null if no lock could be established at /// all (in which case carry on unprotected), or error.Busy if another instance /// holds it. The lock is released when the file is closed, including on crash. -fn acquireLock() !?std.fs.File { - const dir = std.posix.getenv("XDG_RUNTIME_DIR") orelse "/tmp"; +fn acquireLock() !?std.posix.fd_t { + const dir: []const u8 = if (std.c.getenv("XDG_RUNTIME_DIR")) |v| std.mem.span(v) else "/tmp"; var buf: [256]u8 = undefined; const path = std.fmt.bufPrint(&buf, "{s}/hdmi-relink.lock", .{dir}) catch return null; - const f = std.fs.createFileAbsolute(path, .{ .truncate = false }) catch return null; - std.posix.flock(f.handle, std.posix.LOCK.EX | std.posix.LOCK.NB) catch |e| { - f.close(); - if (e == error.WouldBlock) return error.Busy; + // std.fs.File moved behind the Io interface in Zig 0.16; a bare fd is all + // this advisory lock needs. + const fd = std.posix.openat( + std.posix.AT.FDCWD, + path, + .{ .ACCMODE = .RDWR, .CREAT = true }, + 0o600, + ) catch return null; + const rc = std.os.linux.flock(fd, std.posix.LOCK.EX | std.posix.LOCK.NB); + const err = std.os.linux.errno(rc); + if (err != .SUCCESS) { + _ = std.os.linux.close(fd); + if (err == .AGAIN) return error.Busy; return null; - }; - return f; + } + return fd; } /// Re-initialise the named output's link. @@ -237,8 +264,9 @@ fn reinit(conn: Conn, cfg: Config) !void { logMsg("another instance is re-initialising {s}; skipping", .{cfg.output}); return; }; - defer if (lock) |f| f.close(); - + defer if (lock) |fd| { + _ = std.os.linux.close(fd); + }; const res = c.xcb_randr_get_screen_resources_current_reply( xc, c.xcb_randr_get_screen_resources_current(xc, conn.root), @@ -415,7 +443,7 @@ fn reinit(conn: Conn, cfg: Config) !void { // each SetCrtcConfig advances the config timestamp; a stale one is rejected cfg_ts = try configTimestamp(xc, conn.root); try setCrtc(xc, crtc, cfg_ts, x, y, mode, rotation, outs); - std.Thread.sleep(cfg.hold_ms * std.time.ns_per_ms); + sleepMs(cfg.hold_ms); } _ = c.xcb_flush(xc); @@ -447,7 +475,7 @@ fn watch(conn: Conn, cfg: Config) !void { var slept = prev_level != on; while (true) { - std.Thread.sleep(cfg.interval_ms * std.time.ns_per_ms); + sleepMs(cfg.interval_ms); if (c.xcb_connection_has_error(conn.xc) != 0) { logMsg("X connection lost; exiting for restart", .{}); @@ -484,7 +512,7 @@ fn watch(conn: Conn, cfg: Config) !void { }); slept = false; dpmsForceOn(conn.xc); - std.Thread.sleep(cfg.settle_ms * std.time.ns_per_ms); + sleepMs(cfg.settle_ms); reinit(conn, cfg) catch |e| logMsg("re-init failed: {s}", .{@errorName(e)}); } } @@ -525,24 +553,26 @@ fn parseArgs(cfg: *Config, args: []const [:0]const u8) !void { } } -pub fn main() !void { - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; +pub fn main(init: std.process.Init.Minimal) !void { + var gpa = std.heap.DebugAllocator(.{}){}; defer _ = gpa.deinit(); const alloc = gpa.allocator(); - const args = try std.process.argsAlloc(alloc); - defer std.process.argsFree(alloc, args); + var args = std.ArrayList([:0]const u8).empty; + defer args.deinit(alloc); + var it = std.process.Args.Iterator.init(init.args); + while (it.next()) |a| try args.append(alloc, a); var cfg = Config{}; - try parseArgs(&cfg, args); + try parseArgs(&cfg, args.items); + const conn = try connect(); defer c.xcb_disconnect(conn.xc); - try requireExt(conn.xc, &c.xcb_randr_id, "RANDR"); - try requireExt(conn.xc, &c.xcb_dpms_id, "DPMS"); - try requireExt(conn.xc, &c.xcb_screensaver_id, "MIT-SCREEN-SAVER"); - negotiate(conn.xc); + try requireExt(conn.xc, &xcb_randr_id, "RANDR"); + try requireExt(conn.xc, &xcb_dpms_id, "DPMS"); + try requireExt(conn.xc, &xcb_screensaver_id, "MIT-SCREEN-SAVER"); if (cfg.watch) try watch(conn, cfg) else try reinit(conn, cfg); }