rewrite randomwallpaper in zig

This commit is contained in:
LordMZTE 2021-12-30 20:33:06 +01:00
parent 575252dc67
commit e28746aebe
3 changed files with 61 additions and 52 deletions

1
scripts/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
zig-cache/

View file

@ -1,52 +0,0 @@
// clang -lX11 -lXinerama randomwallpaper.c -o ~/.local/bin/randomwallpaper
// this script updates the desktop wallpapers using the nitrogen tool to random
// wallpapers on all displays.
#include <X11/Xlib.h>
#include <X11/extensions/Xinerama.h>
#include <stdio.h>
#include <stdlib.h>
void update_wallpapers(Display *d);
int main(int argc, char *argv[]) {
Display *d = XOpenDisplay(NULL);
if (d) {
int dummy1, dummy2;
// check if xinerama is supported and enabled
if (XineramaQueryExtension(d, &dummy1, &dummy2) && XineramaIsActive(d)) {
update_wallpapers(d);
} else {
puts("No Xinerama!\n");
XCloseDisplay(d);
return 1;
}
XCloseDisplay(d);
return 0;
} else {
puts("Couldn't open display!\n");
// I return with 0 here, as I don't really wanna see it as an error if this
// script is run without an X server running.
return 0;
}
}
void update_wallpapers(Display *d) {
int heads = 0;
XineramaScreenInfo *info = XineramaQueryScreens(d, &heads);
if (heads > 0) {
for (int i = 0; i < heads; i++) {
// works as long as the user doesn't have over 999 monitors :P
char command[42];
sprintf(command, "nitrogen --random --set-scaled --head=%d", i);
printf("Setting wallpaper for screen %d with size %dx%d\n", i,
info[i].width, info[i].height);
system(command);
}
}
XFree(info);
}

View file

@ -0,0 +1,60 @@
// 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();
}
}