RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Default XDG_DATA_DIRS to the XDG list and find the CUPS socket in /var/run
oxydizer6bec47fd2bdc
diff --git a/src/printing/client.zig b/src/printing/client.zigindex 7046ef0..d4d24c5 100644--- a/src/printing/client.zig+++ b/src/printing/client.zig@@ -235,7 +235,9 @@ pub const Client = struct {return state;};state.remote = (try endpoint.parseServer(socket)).unix == null;- state.admin_local = std.mem.eql(u8, socket, "/run/cups/cups.sock") or std.mem.eql(u8, socket, "/var/run/cups/cups.sock");+ state.admin_local = for (cups.local_sockets) |path| {+ if (std.mem.eql(u8, socket, path)) break true;+ } else false;if (self.watch) |*watch| if (watch.fd() < 0) {watch.deinit(a);self.watch = null;diff --git a/src/printing/cups.zig b/src/printing/cups.zigindex c2ca378..fe0674e 100644--- a/src/printing/cups.zig+++ b/src/printing/cups.zig@@ -62,13 +62,26 @@ pub const Snapshot = struct {}};+/// Where the local daemon listens: /run/cups on most Linux systems,+/// /var/run/cups elsewhere (FreeBSD).+pub const local_sockets = [_][]const u8{ "/run/cups/cups.sock", "/var/run/cups/cups.sock" };++/// The socket that exists, else the first (the daemon may not be running yet).+fn localSocket(io: std.Io) []const u8 {+ for (local_sockets) |path| {+ const stat = std.Io.Dir.cwd().statFile(io, path, .{}) catch continue;+ if (stat.kind == .unix_domain_socket) return path;+ }+ return local_sockets[0];+}+/// Reject unsupported remote/encrypted settings explicitly; never contact the/// local daemon when the user's CUPS configuration points somewhere else.pub fn server(a: A, io: std.Io, env: std.process.Environ) ![]const u8 {var arena = std.heap.ArenaAllocator.init(a);defer arena.deinit();const mem = arena.allocator();- var configured: []const u8 = "/run/cups/cups.sock";+ var configured: []const u8 = localSocket(io);var encryption: []const u8 = "IfRequested";const system = try readClientConfig(mem, io, "/etc/cups/client.conf");const system_config = try clientConfig(system);diff --git a/src/printing/direct.c b/src/printing/direct.cindex 71ddef8..6811eed 100644--- a/src/printing/direct.c+++ b/src/printing/direct.c@@ -319,6 +319,16 @@ static int execute(struct rediwm_cups *w) {}return send_request(w, r, NULL);}+/* The local daemon's socket: /run/cups on most Linux systems, /var/run/cups+ * elsewhere (FreeBSD). The first when neither exists yet; probed on every+ * connect, since the daemon may start after the worker. */+static const char *local_socket(void) {+ static const char *const paths[] = {"/run/cups/cups.sock", "/var/run/cups/cups.sock"};+ struct stat st;+ for (size_t i = 0; i < sizeof paths / sizeof paths[0]; i++)+ if (!stat(paths[i], &st) && S_ISSOCK(st.st_mode)) return paths[i];+ return paths[0];+}static void close_connection(struct rediwm_cups *w) {if (w->active_fd >= 0) close(w->active_fd);w->active_fd = -1;@@ -330,7 +340,7 @@ static void close_connection(struct rediwm_cups *w) {static void *run(void *data) {struct rediwm_cups *w = data;pthread_setname_np(pthread_self(), "rediwm-cups");- cupsSetServer("/run/cups/cups.sock");+ cupsSetServer(local_socket());cupsSetPasswordCB2(password_cb, w);pthread_mutex_lock(&w->mutex);while (!atomic_load(&w->stop)) {@@ -341,7 +351,11 @@ static void *run(void *data) {w->queued = 0;w->deadline = now() + 20;pthread_mutex_unlock(&w->mutex);- if (!w->http) w->http = httpConnect2("/run/cups/cups.sock", 631, NULL, AF_UNIX, HTTP_ENCRYPTION_NEVER, 1, 1000, NULL);+ if (!w->http) {+ const char *path = local_socket();+ cupsSetServer(path);+ w->http = httpConnect2(path, 631, NULL, AF_UNIX, HTTP_ENCRYPTION_NEVER, 1, 1000, NULL);+ }int result = 1;if (w->http) {pthread_mutex_lock(&w->mutex);diff --git a/src/session_main.zig b/src/session_main.zigindex 08af812..514a74d 100644--- a/src/session_main.zig+++ b/src/session_main.zig@@ -53,7 +53,7 @@ pub fn main(init: std.process.Init) !u8 {const compositor = try std.fs.path.joinZ(arena, &.{ exe_dir, "rediwm" });try env.put("PATH", try std.fmt.allocPrint(arena, "{s}:{s}", .{ exe_dir, env.get("PATH") orelse "/usr/bin:/bin" }));const prefix = std.fs.path.dirname(exe_dir) orelse "/";- try env.put("XDG_DATA_DIRS", try std.fmt.allocPrint(arena, "{s}/share:{s}", .{ prefix, env.get("XDG_DATA_DIRS") orelse "/usr/share" }));+ try env.put("XDG_DATA_DIRS", try dataDirs(arena, try std.fmt.allocPrint(arena, "{s}/share", .{prefix}), env.get("XDG_DATA_DIRS")));try env.put("XDG_CURRENT_DESKTOP", "rediwm");try env.put("XDG_SESSION_TYPE", "wayland");for ([_][]const u8{ "DISPLAY", "WAYLAND_DISPLAY", "WLR_BACKENDS", "WLR_WL_OUTPUTS", "WLR_HEADLESS_OUTPUTS", "REDIWM_NESTED", "REDIWM_SCALE" }) |key| _ = env.swapRemove(key);@@ -108,6 +108,33 @@ fn loginSession(arena: std.mem.Allocator, bus: *login_bus.Bus, runtime_env: ?[]ctry env.put("REDIWM_SESSION_PRIMARY", "1");}+/// XDG_DATA_DIRS with the install's own `share` first, once. The other+/// entries keep their order; unset or empty, the XDG default stands.+fn dataDirs(arena: std.mem.Allocator, own: []const u8, inherited: ?[]const u8) ![]const u8 {+ const base = if (inherited) |value| (if (value.len > 0) value else null) else null;+ var list: std.ArrayList(u8) = .empty;+ try list.appendSlice(arena, own);+ var it = std.mem.splitScalar(u8, base orelse "/usr/local/share:/usr/share", ':');+ while (it.next()) |dir| {+ if (dir.len == 0 or std.mem.eql(u8, dir, own)) continue;+ try list.append(arena, ':');+ try list.appendSlice(arena, dir);+ }+ return list.items;+}++test "data dirs list the install's share first and once" {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ // Unset or empty: the XDG default, so a /usr prefix keeps /usr/local/share.+ try std.testing.expectEqualStrings("/usr/share:/usr/local/share", try dataDirs(a, "/usr/share", null));+ try std.testing.expectEqualStrings("/usr/share:/usr/local/share", try dataDirs(a, "/usr/share", ""));+ try std.testing.expectEqualStrings("/usr/local/share:/usr/share", try dataDirs(a, "/usr/local/share", null));+ // Set: the install wins, the rest keep their order and lose duplicates.+ try std.testing.expectEqualStrings("/usr/local/share:/opt/a:/opt/b", try dataDirs(a, "/usr/local/share", "/opt/a:/usr/local/share::/opt/b"));+}+fn makePath(arena: std.mem.Allocator, path: []const u8) !void {var end: usize = 1;while (end <= path.len) : (end += 1) {