Skip to content

Commit

Permalink
flush the wayland_fd correctly from animations
Browse files Browse the repository at this point in the history
event_fd solved a problem where we wouldn't flush the wayland file
descriptor when drawing, and so buffer would never reach the server.

Now, we've allowed for other threads to flush the wayland fd by calling
into a static global. This sidesteps the previous issue without being
necessary to recreate the event_fd.
  • Loading branch information
LGFae committed Apr 27, 2024
1 parent 8fddd47 commit 699d16d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
6 changes: 5 additions & 1 deletion daemon/src/animations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,20 @@ impl Animator {
continue;
}

wallpapers[i].draw();
i += 1;
}

if wallpapers.is_empty() {
return;
}

for wallpaper in &wallpapers {
wallpaper.draw();
}
let timeout = duration.saturating_sub(now.elapsed());
spin_sleep::sleep(timeout);
crate::flush_wayland();

now = std::time::Instant::now();
}
})
Expand Down
16 changes: 7 additions & 9 deletions daemon/src/animations/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ impl Transition {
}

fn updt_wallpapers(&mut self, now: &mut Instant) {
let fps = self.fps;
let mut i = 0;
while i < self.wallpapers.len() {
let token = &self.animation_tokens[i];
Expand All @@ -93,8 +92,12 @@ impl Transition {
}
i += 1;
}
let timeout = fps.saturating_sub(now.elapsed());

self.wallpapers.iter().for_each(|w| w.draw());

let timeout = self.fps.saturating_sub(now.elapsed());
spin_sleep::sleep(timeout);
crate::flush_wayland();
*now = Instant::now();
}

Expand All @@ -113,6 +116,8 @@ impl Transition {
for wallpaper in self.wallpapers.iter() {
wallpaper.draw();
}

crate::flush_wayland();
}

fn simple(&mut self, new_img: &[u8]) {
Expand All @@ -136,11 +141,6 @@ impl Transition {
}
});
}

for wallpaper in self.wallpapers.iter() {
wallpaper.draw();
}

self.updt_wallpapers(&mut now);
}
}
Expand Down Expand Up @@ -383,8 +383,6 @@ impl Transition {
.for_each(|(i, (old, new))| f(i, old, new));
});
});

self.wallpapers.iter().for_each(|w| w.draw());
}
}

Expand Down
13 changes: 11 additions & 2 deletions daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use std::{
};

use wayland_client::{
backend::WeakBackend,
globals::{registry_queue_init, GlobalList, GlobalListContents},
protocol::{
wl_callback::WlCallback,
Expand Down Expand Up @@ -72,6 +73,7 @@ fn should_daemon_exit() -> bool {
}

static PIXEL_FORMAT: OnceLock<PixelFormat> = OnceLock::new();
static BACKEND: OnceLock<WeakBackend> = OnceLock::new();

#[inline]
pub fn wl_shm_format() -> wl_shm::Format {
Expand All @@ -83,6 +85,12 @@ pub fn wl_shm_format() -> wl_shm::Format {
}
}

#[inline]
pub fn flush_wayland() {
debug_assert!(BACKEND.get().is_some());
BACKEND.get().unwrap().upgrade().unwrap().flush().unwrap();
}

#[inline]
pub fn pixel_format() -> PixelFormat {
debug_assert!(PIXEL_FORMAT.get().is_some());
Expand Down Expand Up @@ -112,6 +120,7 @@ fn main() -> Result<(), String> {
setup_signals();

let conn = Connection::connect_to_env().expect("failed to connect to the wayland server");
BACKEND.set(conn.backend().downgrade()).unwrap();
// Enumerate the list of globals to get the protocols the server implements.
let (globals, mut event_queue) =
registry_queue_init(&conn).expect("failed to initialize the event queue");
Expand Down Expand Up @@ -152,7 +161,7 @@ fn main() -> Result<(), String> {
let events = {
let connection_fd = read_guard.connection_fd();
let mut fds = [
PollFd::new(&connection_fd, PollFlags::IN | PollFlags::OUT),
PollFd::new(&connection_fd, PollFlags::IN),
PollFd::new(&listener.0, PollFlags::IN),
];

Expand All @@ -167,7 +176,7 @@ fn main() -> Result<(), String> {
[fds[0].revents(), fds[1].revents()]
};

if events[0].contains(PollFlags::IN) {
if !events[0].is_empty() {
match read_guard.read() {
Ok(_) => {
event_queue
Expand Down

0 comments on commit 699d16d

Please sign in to comment.