From ee3be25b9b16e7059e96ec4cd5882e73022b8c50 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Fri, 7 Jul 2023 18:42:30 -0400 Subject: [PATCH] refactor: read initial fd from env --- .../src/subscriptions/notifications.rs | 43 ++++--------------- 1 file changed, 9 insertions(+), 34 deletions(-) diff --git a/cosmic-applet-notifications/src/subscriptions/notifications.rs b/cosmic-applet-notifications/src/subscriptions/notifications.rs index dfb974bc..9e5ee67e 100644 --- a/cosmic-applet-notifications/src/subscriptions/notifications.rs +++ b/cosmic-applet-notifications/src/subscriptions/notifications.rs @@ -11,13 +11,12 @@ use std::os::unix::io::{FromRawFd, RawFd}; use tokio::{ io::{self, AsyncBufReadExt, BufReader}, net::UnixStream, - sync::oneshot, }; use tracing::{error, info, warn}; #[derive(Debug)] pub enum State { - WaitingForPanel, + Ready, WaitingForDaemon(UnixStream), WaitingForNotificationEvent(UnixStream), Finished, @@ -30,41 +29,16 @@ pub fn notifications() -> Subscription { std::any::TypeId::of::(), 50, |mut output| async move { - let mut state = State::WaitingForPanel; + let mut state = State::Ready; loop { match &mut state { - State::WaitingForPanel => { - info!("Waiting for panel to send us a stream"); - - let (tx, rx) = oneshot::channel(); - - std::thread::spawn(move || -> anyhow::Result<()> { - let mut msg = String::new(); - - if let Err(err) = std::io::stdin().read_line(&mut msg) { - error!("Failed to read line from panel: {}", err); - anyhow::bail!("Failed to read line from panel"); - } - - info!("Received fd from panel: {}", msg); - let Ok(raw_fd) = msg.trim().parse::() else { - error!("Failed to parse fd from panel"); - anyhow::bail!("Failed to parse fd from panel"); - }; - if raw_fd == 0 { - error!("Invalid fd received from panel"); - anyhow::bail!("Invalid fd received from panel"); - } - if let Err(err) = tx.send(raw_fd) { - error!("Failed to send fd to main thread: {}", err); - anyhow::bail!("Failed to send fd to main thread"); - } - Ok(()) - }); - - let Ok(raw_fd) = rx.await else { - error!("Failed to receive raw fd from panel"); + State::Ready => { + info!("Reading COSMIC_NOTIFICATIONS env var"); + let Ok(Some(raw_fd)) = std::env::var("COSMIC_NOTIFICATIONS") + .map(|fd| fd.parse::().ok()) else + { + error!("Failed to parse COSMIC_NOTIFICATIONS env var"); state = State::Finished; continue; }; @@ -76,6 +50,7 @@ pub fn notifications() -> Subscription { continue; }; state = State::WaitingForDaemon(stream); + } State::WaitingForDaemon(stream) => { info!("Waiting for panel to send us a stream");