Skip to content

Commit

Permalink
Merge pull request #277 from LGFae/better-ipc-structs
Browse files Browse the repository at this point in the history
create ImageRequest and AnimationRequest structs
  • Loading branch information
LGFae committed Apr 13, 2024
2 parents aef2876 + 2d05079 commit 2ce35b8
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 32 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default-members = [".", "daemon"]

[package]
name = "swww"
version = "0.9.2"
version = "0.9.2-master"
authors = ["Leonardo Gibrowski Faé <leonardo.fae44@gmail.com>"]
edition = "2021"
rust-version = "1.74"
Expand Down Expand Up @@ -34,7 +34,7 @@ image = "0.25"
fast_image_resize = "3.0"
clap = { version = "4.5", features = ["derive", "wrap_help", "env"] }
rand = "0.8"
utils = { version = "0.9.2", path = "utils" }
utils = { version = "0.9.2-master", path = "utils" }

[dev-dependencies]
assert_cmd = "2.0"
Expand Down
4 changes: 2 additions & 2 deletions daemon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "swww-daemon"
version = "0.9.2"
version = "0.9.2-master"
authors = ["Leonardo Gibrowski Faé <leonardo.fae44@gmail.com>"]
edition = "2021"

Expand All @@ -26,6 +26,6 @@ spin_sleep = "1.2"

sd-notify = { version = "0.4.1" }

utils = { version = "0.9.2", path = "../utils" }
utils = { version = "0.9.2-master", path = "../utils" }
[dev-dependencies]
rand = "0.8"
8 changes: 4 additions & 4 deletions daemon/src/animations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ impl Animator {
pub(super) fn transition(
&mut self,
transition: ipc::Transition,
imgs: Box<[(Img, Box<[String]>)]>,
imgs: Box<[Img]>,
wallpapers: Vec<Vec<Arc<Wallpaper>>>,
) -> Answer {
match thread::Builder::new()
.stack_size(1 << 15)
.name("transition spawner".to_string())
.spawn(move || {
thread::scope(|s| {
for ((Img { img, path }, _), wallpapers) in imgs.iter().zip(wallpapers) {
for (Img { img, path }, wallpapers) in imgs.iter().zip(wallpapers) {
Self::spawn_transition_thread(s, &transition, img, path, wallpapers);
}
});
Expand Down Expand Up @@ -172,7 +172,7 @@ impl Animator {

pub(super) fn animate(
&mut self,
animations: Box<[(Animation, Box<[String]>)]>,
animations: Box<[Animation]>,
wallpapers: Vec<Vec<Arc<Wallpaper>>>,
) -> Answer {
let barrier = self.anim_barrier.clone();
Expand All @@ -181,7 +181,7 @@ impl Animator {
.name("animation spawner".to_string())
.spawn(move || {
thread::scope(|s| {
for ((animation, _), wallpapers) in animations.iter().zip(wallpapers) {
for (animation, wallpapers) in animations.iter().zip(wallpapers) {
let barrier = barrier.clone();
Self::spawn_animation_thread(s, animation, wallpapers, barrier);
}
Expand Down
22 changes: 15 additions & 7 deletions daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ use wayland_client::{
};

use utils::ipc::{
connect_to_socket, get_socket_path, read_socket, Answer, BgInfo, PixelFormat, Request,
connect_to_socket, get_socket_path, read_socket, AnimationRequest, Answer, BgInfo,
ImageRequest, PixelFormat, Request,
};

use animations::Animator;
Expand Down Expand Up @@ -362,9 +363,12 @@ impl Daemon {
};
let request = Request::receive(&bytes);
let answer = match request {
Request::Animation(animations) => {
Request::Animation(AnimationRequest {
animations,
outputs,
}) => {
let mut wallpapers = Vec::new();
for (_, names) in animations.iter() {
for names in outputs.iter() {
wallpapers.push(self.find_wallpapers_by_names(names));
}
self.animator.animate(animations, wallpapers)
Expand Down Expand Up @@ -400,16 +404,20 @@ impl Daemon {
Answer::Ok
}
Request::Query => Answer::Info(self.wallpapers_info()),
Request::Img((transitions, imgs)) => {
Request::Img(ImageRequest {
transition,
imgs,
outputs,
}) => {
let mut used_wallpapers = Vec::new();
for img in imgs.iter() {
let mut wallpapers = self.find_wallpapers_by_names(&img.1);
for names in outputs.iter() {
let mut wallpapers = self.find_wallpapers_by_names(names);
for wallpaper in wallpapers.iter_mut() {
wallpaper.stop_animations();
}
used_wallpapers.push(wallpapers);
}
self.animator.transition(transitions, imgs, used_wallpapers)
self.animator.transition(transition, imgs, used_wallpapers)
}
};
if let Err(e) = answer.send(&stream) {
Expand Down
16 changes: 10 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn make_img_request(
outputs: &[Vec<String>],
) -> Result<ipc::ImageRequest, String> {
let transition = make_transition(img);
let mut unique_requests = Vec::with_capacity(dims.len());
let mut img_req_builder = ipc::ImageRequestBuilder::new(transition);
for (dim, outputs) in dims.iter().zip(outputs) {
let path = match img.path.canonicalize() {
Ok(p) => p.to_string_lossy().to_string(),
Expand All @@ -204,13 +204,13 @@ fn make_img_request(
}
};

unique_requests.push((
img_req_builder.push(
ipc::Img { img, path },
outputs.to_owned().into_boxed_slice(),
));
);
}

Ok((transition, unique_requests.into_boxed_slice()))
Ok(img_req_builder.build())
}

#[allow(clippy::type_complexity)]
Expand Down Expand Up @@ -271,6 +271,8 @@ fn make_animation_request(
pixel_format: ipc::PixelFormat,
outputs: &[Vec<String>],
) -> Result<AnimationRequest, String> {
let mut anim_req_builder = ipc::AnimationRequestBuilder::new();

let filter = make_filter(&img.filter);
let mut animations = Vec::with_capacity(dims.len());
for (dim, outputs) in dims.iter().zip(outputs) {
Expand Down Expand Up @@ -303,9 +305,11 @@ fn make_animation_request(
.into_boxed_slice(),
pixel_format,
};
animations.push((animation, outputs.to_owned().into_boxed_slice()));

anim_req_builder.push(animation, outputs.to_owned().into_boxed_slice());
}
Ok(animations.into_boxed_slice())

Ok(anim_req_builder.build())
}

fn split_cmdline_outputs(outputs: &str) -> Box<[String]> {
Expand Down
2 changes: 1 addition & 1 deletion utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "utils"
version = "0.9.2"
version = "0.9.2-master"
authors = ["Leonardo Gibrowski Faé <leonardo.fae44@gmail.com>"]
edition = "2021"
license-file = "../LICENSE"
Expand Down
86 changes: 79 additions & 7 deletions utils/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,88 @@ pub struct Img {
pub img: Box<[u8]>,
}

#[derive(Encode, Decode)]
#[derive(Decode, Encode)]
pub struct Animation {
pub animation: Box<[(BitPack, Duration)]>,
pub path: String,
pub dimensions: (u32, u32),
pub pixel_format: PixelFormat,
}

pub type AnimationRequest = Box<[(Animation, Box<[String]>)]>;
pub type ImageRequest = (Transition, Box<[(Img, Box<[String]>)]>);
#[derive(Decode, Encode)]
pub struct AnimationRequest {
pub animations: Box<[Animation]>,
pub outputs: Box<[Box<[String]>]>,
}

pub struct AnimationRequestBuilder {
animations: Vec<Animation>,
outputs: Vec<Box<[String]>>,
}

impl AnimationRequestBuilder {
#[inline]
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
animations: Vec::new(),
outputs: Vec::new(),
}
}

#[inline]
pub fn push(&mut self, animation: Animation, outputs: Box<[String]>) {
self.animations.push(animation);
self.outputs.push(outputs);
}

#[inline]
pub fn build(self) -> AnimationRequest {
AnimationRequest {
animations: self.animations.into_boxed_slice(),
outputs: self.outputs.into_boxed_slice(),
}
}
}

#[derive(Decode, Encode)]
pub struct ImageRequest {
pub transition: Transition,
pub imgs: Box<[Img]>,
pub outputs: Box<[Box<[String]>]>,
}

pub struct ImageRequestBuilder {
pub transition: Transition,
pub imgs: Vec<Img>,
pub outputs: Vec<Box<[String]>>,
}

impl ImageRequestBuilder {
#[inline]
pub fn new(transition: Transition) -> Self {
Self {
transition,
imgs: Vec::new(),
outputs: Vec::new(),
}
}

#[inline]
pub fn push(&mut self, img: Img, outputs: Box<[String]>) {
self.imgs.push(img);
self.outputs.push(outputs);
}

#[inline]
pub fn build(self) -> ImageRequest {
ImageRequest {
transition: self.transition,
imgs: self.imgs.into_boxed_slice(),
outputs: self.outputs.into_boxed_slice(),
}
}
}

#[derive(Decode, Encode)]
pub enum Request {
Expand All @@ -224,9 +296,9 @@ impl Request {
pub fn send(&self, stream: &UnixStream) -> Result<(), String> {
let bytes = bitcode::encode(self);
std::thread::scope(|s| {
if let Self::Animation(animations) = self {
if let Self::Animation(AnimationRequest { animations, .. }) = self {
s.spawn(|| {
for (animation, _) in animations.iter() {
for animation in animations.iter() {
// only store the cache if we aren't reading from stdin
if animation.path != "-" {
if let Err(e) = cache::store_animation_frames(animation) {
Expand All @@ -243,8 +315,8 @@ impl Request {
if let Err(e) = writer.write_all(&bytes) {
Err(format!("failed to write serialized request: {e}"))
} else {
if let Self::Img((_, imgs)) = self {
for (Img { path, .. }, outputs) in imgs.iter() {
if let Self::Img(ImageRequest { imgs, outputs, .. }) = self {
for (Img { path, .. }, outputs) in imgs.iter().zip(outputs.iter()) {
for output in outputs.iter() {
if let Err(e) = super::cache::store(output, path) {
eprintln!("ERROR: failed to store cache: {e}");
Expand Down

0 comments on commit 2ce35b8

Please sign in to comment.