mirror of
https://mzte.de/git/LordMZTE/dotfiles.git
synced 2024-12-12 21:32:58 +01:00
rewrite randomwallpaper in rust
This commit is contained in:
parent
a5ac3020f9
commit
66d684db1c
6 changed files with 102 additions and 9 deletions
5
justfile
5
justfile
|
@ -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:
|
||||
|
|
|
@ -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
2
scripts/randomwallpaper/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
target/
|
||||
Cargo.lock
|
12
scripts/randomwallpaper/Cargo.toml
Normal file
12
scripts/randomwallpaper/Cargo.toml
Normal 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"] }
|
48
scripts/randomwallpaper/src/main.rs
Normal file
48
scripts/randomwallpaper/src/main.rs
Normal 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()
|
||||
}
|
36
scripts/randomwallpaper/src/xinerama.rs
Normal file
36
scripts/randomwallpaper/src/xinerama.rs
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue