dotfiles/scripts/randomwallpaper.zig

60 lines
1.7 KiB
Zig

// zig build-exe -lc -lX11 -lXinerama randomwallpaper.zig && mv randomwallpaper ~/.local/bin
const std = @import("std");
const mem = std.mem;
const c = @cImport({
@cInclude("X11/Xlib.h");
@cInclude("X11/extensions/Xinerama.h");
});
const Display = c.struct__XDisplay;
pub fn main() !void {
var alloc = std.heap.GeneralPurposeAllocator(.{}){};
const maybe_display = c.XOpenDisplay(null);
if (maybe_display) |display| {
defer _ = c.XCloseDisplay(display);
var dummy1: c_int = undefined;
var dummy2: c_int = undefined;
if (c.XineramaQueryExtension(display, &dummy1, &dummy2) != 0 and c.XineramaIsActive(display) != 0) {
try updateWallpapers(display, alloc.allocator());
} else {
return error.@"No Xinerama!";
}
}
}
fn updateWallpapers(display: *Display, alloc: mem.Allocator) !void {
const stdout = std.io.getStdOut().writer();
var heads: c_int = 0;
const info = c.XineramaQueryScreens(display, &heads);
defer _ = c.XFree(info);
var children = try alloc.alloc(*std.ChildProcess, @intCast(usize, heads));
defer alloc.free(children);
var i: usize = 0;
while (i < heads) {
const head = info[i];
try stdout.print("Setting wallpaper for screen {} with size {}x{}\n", .{ i, head.width, head.height });
var buf: [10]u8 = undefined;
const args = [_][]const u8{ "nitrogen", "--random", "--set-scaled", try std.fmt.bufPrint(&buf, "--head={}", .{i}) };
var child = try std.ChildProcess.init(&args, alloc);
try child.spawn();
children[i] = child;
i += 1;
}
for (children) |child| {
_ = try child.wait();
}
for (children) |child| {
child.deinit();
}
}