Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: proprely use errors to correctly shutdown application & remove useless function in BannerRect #36

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions crates/backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::thread;

use anyhow::Context;
use tokio::sync::mpsc::unbounded_channel;

mod internal_messages;
mod render;
mod window;
mod window_manager;
mod internal_messages;

use dbus::actions::Action;
use dbus::server::Server;
Expand All @@ -18,16 +19,14 @@ pub async fn run() -> anyhow::Result<()> {

let (server_internal_channel, mut renderer) = Renderer::init()?;

thread::spawn(move || renderer.run());
let backend_thread = thread::spawn(move || renderer.run());

loop {
while let Ok(action) = receiver.try_recv() {
match action {
Action::Show(notification) => {
server_internal_channel.send_to_renderer(
internal_messages::ServerMessage::ShowNotification(
notification,
),
internal_messages::ServerMessage::ShowNotification(notification),
)?;
}
Action::Close(Some(id)) => {
Expand Down Expand Up @@ -55,24 +54,30 @@ pub async fn run() -> anyhow::Result<()> {
notification_id,
action_key,
} => todo!(),
internal_messages::RendererMessage::ClosedNotification {
id,
reason,
} => match reason {
//INFO: ignore the first one because it always emits in server.
dbus::actions::ClosingReason::CallCloseNotification => (),
other_reason => {
server
.emit_signal(dbus::actions::Signal::NotificationClosed {
notification_id: id,
reason: other_reason,
})
.await?;
internal_messages::RendererMessage::ClosedNotification { id, reason } => {
match reason {
//INFO: ignore the first one because it always emits in server.
dbus::actions::ClosingReason::CallCloseNotification => (),
other_reason => {
server
.emit_signal(dbus::actions::Signal::NotificationClosed {
notification_id: id,
reason: other_reason,
})
.await?;
}
}
},
}
}
}

if backend_thread.is_finished() {
return backend_thread
.join()
.expect("Join the backend thread to finish main thread")
.with_context(|| "The backend shutdowns due to error");
}

tokio::time::sleep(tokio::time::Duration::from_millis(20)).await;
std::hint::spin_loop();
}
Expand Down
16 changes: 8 additions & 8 deletions crates/backend/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Renderer {
))
}

pub(crate) fn run(&mut self) {
pub(crate) fn run(&mut self) -> anyhow::Result<()> {
let mut notifications_to_create = vec![];
let mut notifications_to_close = vec![];

Expand All @@ -59,7 +59,7 @@ impl Renderer {
.lock()
.expect("Acquire the Config struct before creating banners");
self.window_manager
.create_notifications(notifications_to_create, &config);
.create_notifications(notifications_to_create, &config)?;
notifications_to_create = vec![];
}

Expand All @@ -68,25 +68,25 @@ impl Renderer {
.lock()
.expect("Acquire the Config struct before closing banners");
self.window_manager
.close_notifications(&notifications_to_close, &config);
.close_notifications(&notifications_to_close, &config)?;
notifications_to_close.clear();
}
self.window_manager.remove_expired(
&CONFIG
.lock()
.expect("Acquire the Config struct before expiring banners"),
);
)?;

while let Some(message) = self.window_manager.pop_event() {
self.channel.send_to_server(message).unwrap();
self.channel.send_to_server(message)?;
}

self.window_manager.handle_actions(
&CONFIG
.lock()
.expect("Acquire the Config struct before handling actions"),
);
self.window_manager.dispatch();
)?;
self.window_manager.dispatch()?;

{
let mut config = CONFIG
Expand All @@ -95,7 +95,7 @@ impl Renderer {
match config.check_updates() {
ConfigState::NotFound | ConfigState::Updated => {
config.update();
self.window_manager.update_by_config(&config);
self.window_manager.update_by_config(&config)?;
}
ConfigState::NothingChanged => (),
};
Expand Down
5 changes: 0 additions & 5 deletions crates/backend/src/render/banner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ impl BannerRect {
self.created_at = time::Instant::now();
}

#[inline]
pub(crate) fn write_to_file(&self, file: &mut File) {
file.write_all(&self.framebuffer).unwrap();
}

#[inline]
pub(crate) fn framebuffer(&self) -> &[u8] {
&self.framebuffer
Expand Down
2 changes: 1 addition & 1 deletion crates/backend/src/render/border.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use derive_builder::Builder;

use super::{
widget::{Coverage, Draw, DrawColor},
color::Bgra,
types::Offset,
widget::{Coverage, Draw, DrawColor},
};

type Matrix<T> = Vec<Vec<T>>;
Expand Down
9 changes: 7 additions & 2 deletions crates/backend/src/render/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ impl TextRect {
while let Some(entity) = entities.front() {
if entity.offset == pos {
current_style += FontStyle::from(&entity.kind);
current_entities.push_back(entities.pop_front().unwrap());
// SAFETY: because of it acquires AFTER `while let Some(_)` it guarantee
// that acquired data always valid
current_entities
.push_back(unsafe { entities.pop_front().unwrap_unchecked() });
} else {
break;
}
Expand All @@ -93,7 +96,9 @@ impl TextRect {

while let Some(entity) = current_entities.front() {
if entity.offset + entity.length < pos {
let entity = current_entities.pop_front().unwrap();
// SAFETY: because of it acquires AFTER `while let Some(_)` it guarantee
// that acquired data always valid
let entity = unsafe { current_entities.pop_front().unwrap_unchecked() };
current_style -= FontStyle::from(&entity.kind);
} else {
break;
Expand Down
19 changes: 12 additions & 7 deletions crates/backend/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use wayland_protocols_wlr::layer_shell::v1::client::{
zwlr_layer_surface_v1::{self, Anchor},
};

use config::{self, Config};
use super::internal_messages::RendererMessage;
use config::{self, Config};
use dbus::notification::{self, Notification};

use super::render::{BannerRect, FontCollection, RectSize};
Expand Down Expand Up @@ -380,12 +380,17 @@ impl Window {
let buffer = Buffer::new();

if let None = self.shm_pool {
self.shm_pool = Some(self.shm.as_ref().unwrap().create_pool(
buffer.as_fd(),
self.rect_size.area() as i32 * 4,
qhandle,
(),
));
self.shm_pool = Some(
self.shm
.as_ref()
.expect("Must be wl_shm protocol to use create wl_shm_pool")
.create_pool(
buffer.as_fd(),
self.rect_size.area() as i32 * 4,
qhandle,
(),
),
);
}

self.buffer = Some(buffer);
Expand Down
Loading