rewrite randomwallpaper in rust

This commit is contained in:
LordMZTE 2022-02-16 00:29:50 +01:00
parent a5ac3020f9
commit 66d684db1c
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
6 changed files with 102 additions and 9 deletions

View file

@ -8,13 +8,16 @@ yaml-language-server
"
install-scripts target=(`echo $HOME` + "/.local/bin"):
cargo build --release --manifest-path scripts/randomwallpaper/Cargo.toml
cp scripts/randomwallpaper/target/release/randomwallpaper {{target}}/randomwallpaper
opam install --yes clap
cd scripts/playtwitch && dune build
chmod -R +w scripts/playtwitch
cp scripts/playtwitch/_build/default/playtwitch.exe {{target}}/playtwitch
ln -sf \
`pwd`/scripts/{start-joshuto,withjava,randomwallpaper} \
`pwd`/scripts/{start-joshuto,withjava} \
{{target}}
install-lsps-paru:

View file

@ -1,8 +0,0 @@
#!/bin/sh
mons=$(xrandr --listactivemonitors | wc -l)
mons=$((mons-1))
feh --bg-fill $(
fd -L '\.(png|jpg)' /usr/share/backgrounds/ ~/.local/share/backgrounds/ \
| shuf | head -n 2
)

2
scripts/randomwallpaper/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target/
Cargo.lock

View file

@ -0,0 +1,12 @@
[package]
name = "randomwallpaper"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.53"
rand = "0.8.5"
walkdir = "2.3.2"
x11 = { version = "2.19.1", features = ["xinerama", "xlib"] }

View file

@ -0,0 +1,48 @@
use anyhow::Context;
use rand::prelude::{IteratorRandom, SliceRandom};
use std::{
path::{Path, PathBuf},
process::Command,
};
use walkdir::{DirEntry, WalkDir};
use xinerama::head_count;
mod xinerama;
fn main() -> anyhow::Result<()> {
let paths = [
PathBuf::from("/usr/share/backgrounds"),
Path::new(&std::env::var("HOME").context("couldn't get home directory")?)
.join(".local/share/backgrounds"),
];
let wallpapers = paths
.into_iter()
.flat_map(dir_iter)
.flatten()
.filter(|d| {
d.path()
.extension()
.map(|e| ["png", "jpg"].contains(&&*e.to_string_lossy()))
.unwrap_or(false)
})
.map(DirEntry::into_path);
let mut wallpapers = wallpapers.choose_multiple(
&mut rand::thread_rng(),
head_count().context("Failed to get head count")? as usize,
);
wallpapers.shuffle(&mut rand::thread_rng());
Command::new("feh")
.arg("--bg-fill")
.args(&wallpapers)
.status()?;
Ok(())
}
fn dir_iter(p: impl AsRef<Path>) -> impl Iterator<Item = walkdir::Result<DirEntry>> {
WalkDir::new(p).follow_links(true).into_iter()
}

View file

@ -0,0 +1,36 @@
use std::mem::MaybeUninit;
use anyhow::bail;
use x11::{
xinerama::{XineramaIsActive, XineramaQueryScreens},
xlib::{XCloseDisplay, XFree, XOpenDisplay},
};
pub fn head_count() -> anyhow::Result<i32> {
unsafe {
let display = XOpenDisplay(b":0\0".as_ptr() as _);
if display.is_null() {
bail!("Couldn't open display");
}
if XineramaIsActive(display) != 1 {
XCloseDisplay(display);
bail!("Xinerama is inactive");
}
let mut screens = MaybeUninit::uninit();
let info = XineramaQueryScreens(display, screens.as_mut_ptr());
if info.is_null() {
XCloseDisplay(display);
bail!("Failed to query screens");
}
let count = screens.assume_init();
XFree(info as _);
XCloseDisplay(display);
Ok(count)
}
}