refactor: read initial fd from env

This commit is contained in:
Ashley Wulber 2023-07-07 18:42:30 -04:00 committed by Ashley Wulber
parent 6bb6ee6a92
commit ee3be25b9b

View file

@ -11,13 +11,12 @@ use std::os::unix::io::{FromRawFd, RawFd};
use tokio::{ use tokio::{
io::{self, AsyncBufReadExt, BufReader}, io::{self, AsyncBufReadExt, BufReader},
net::UnixStream, net::UnixStream,
sync::oneshot,
}; };
use tracing::{error, info, warn}; use tracing::{error, info, warn};
#[derive(Debug)] #[derive(Debug)]
pub enum State { pub enum State {
WaitingForPanel, Ready,
WaitingForDaemon(UnixStream), WaitingForDaemon(UnixStream),
WaitingForNotificationEvent(UnixStream), WaitingForNotificationEvent(UnixStream),
Finished, Finished,
@ -30,41 +29,16 @@ pub fn notifications() -> Subscription<AppletEvent> {
std::any::TypeId::of::<SomeWorker>(), std::any::TypeId::of::<SomeWorker>(),
50, 50,
|mut output| async move { |mut output| async move {
let mut state = State::WaitingForPanel; let mut state = State::Ready;
loop { loop {
match &mut state { match &mut state {
State::WaitingForPanel => { State::Ready => {
info!("Waiting for panel to send us a stream"); info!("Reading COSMIC_NOTIFICATIONS env var");
let Ok(Some(raw_fd)) = std::env::var("COSMIC_NOTIFICATIONS")
let (tx, rx) = oneshot::channel(); .map(|fd| fd.parse::<RawFd>().ok()) else
{
std::thread::spawn(move || -> anyhow::Result<()> { error!("Failed to parse COSMIC_NOTIFICATIONS env var");
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::<RawFd>() 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 = State::Finished; state = State::Finished;
continue; continue;
}; };
@ -76,6 +50,7 @@ pub fn notifications() -> Subscription<AppletEvent> {
continue; continue;
}; };
state = State::WaitingForDaemon(stream); state = State::WaitingForDaemon(stream);
} }
State::WaitingForDaemon(stream) => { State::WaitingForDaemon(stream) => {
info!("Waiting for panel to send us a stream"); info!("Waiting for panel to send us a stream");