Skip to content

Commit ee3be25

Browse files
committed
refactor: read initial fd from env
1 parent 6bb6ee6 commit ee3be25

File tree

1 file changed

+9
-34
lines changed

1 file changed

+9
-34
lines changed

cosmic-applet-notifications/src/subscriptions/notifications.rs

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ use std::os::unix::io::{FromRawFd, RawFd};
1111
use tokio::{
1212
io::{self, AsyncBufReadExt, BufReader},
1313
net::UnixStream,
14-
sync::oneshot,
1514
};
1615
use tracing::{error, info, warn};
1716

1817
#[derive(Debug)]
1918
pub enum State {
20-
WaitingForPanel,
19+
Ready,
2120
WaitingForDaemon(UnixStream),
2221
WaitingForNotificationEvent(UnixStream),
2322
Finished,
@@ -30,41 +29,16 @@ pub fn notifications() -> Subscription<AppletEvent> {
3029
std::any::TypeId::of::<SomeWorker>(),
3130
50,
3231
|mut output| async move {
33-
let mut state = State::WaitingForPanel;
32+
let mut state = State::Ready;
3433

3534
loop {
3635
match &mut state {
37-
State::WaitingForPanel => {
38-
info!("Waiting for panel to send us a stream");
39-
40-
let (tx, rx) = oneshot::channel();
41-
42-
std::thread::spawn(move || -> anyhow::Result<()> {
43-
let mut msg = String::new();
44-
45-
if let Err(err) = std::io::stdin().read_line(&mut msg) {
46-
error!("Failed to read line from panel: {}", err);
47-
anyhow::bail!("Failed to read line from panel");
48-
}
49-
50-
info!("Received fd from panel: {}", msg);
51-
let Ok(raw_fd) = msg.trim().parse::<RawFd>() else {
52-
error!("Failed to parse fd from panel");
53-
anyhow::bail!("Failed to parse fd from panel");
54-
};
55-
if raw_fd == 0 {
56-
error!("Invalid fd received from panel");
57-
anyhow::bail!("Invalid fd received from panel");
58-
}
59-
if let Err(err) = tx.send(raw_fd) {
60-
error!("Failed to send fd to main thread: {}", err);
61-
anyhow::bail!("Failed to send fd to main thread");
62-
}
63-
Ok(())
64-
});
65-
66-
let Ok(raw_fd) = rx.await else {
67-
error!("Failed to receive raw fd from panel");
36+
State::Ready => {
37+
info!("Reading COSMIC_NOTIFICATIONS env var");
38+
let Ok(Some(raw_fd)) = std::env::var("COSMIC_NOTIFICATIONS")
39+
.map(|fd| fd.parse::<RawFd>().ok()) else
40+
{
41+
error!("Failed to parse COSMIC_NOTIFICATIONS env var");
6842
state = State::Finished;
6943
continue;
7044
};
@@ -76,6 +50,7 @@ pub fn notifications() -> Subscription<AppletEvent> {
7650
continue;
7751
};
7852
state = State::WaitingForDaemon(stream);
53+
7954
}
8055
State::WaitingForDaemon(stream) => {
8156
info!("Waiting for panel to send us a stream");

0 commit comments

Comments
 (0)