dotfiles/scripts/randomwallpaper.zig

57 lines
1.6 KiB
Zig
Raw Normal View History

2021-12-30 20:33:06 +01:00
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(.{}){};
2021-12-30 20:45:04 +01:00
if (c.XOpenDisplay(null)) |display| {
2021-12-30 20:33:06 +01:00
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 {
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];
2021-12-30 20:45:04 +01:00
std.log.info("Setting wallpaper for screen {} with size {}x{}\n", .{ i, head.width, head.height });
2021-12-30 20:33:06 +01:00
var buf: [10]u8 = undefined;
2022-01-02 22:52:46 +01:00
const args = [_][]const u8{ "nitrogen", "--random", "--set-zoom-fill", try std.fmt.bufPrint(&buf, "--head={}", .{i}) };
2021-12-30 20:33:06 +01:00
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();
}
}