1
- use tokio:: {
2
- io:: { AsyncBufReadExt , BufReader } ,
3
- net:: UnixStream ,
4
- sync:: oneshot,
5
- } ;
6
- use tracing:: { error, info} ;
7
1
use cosmic:: {
8
2
iced:: {
9
3
futures:: { self , SinkExt } ,
@@ -12,8 +6,14 @@ use cosmic::{
12
6
iced_futures:: Subscription ,
13
7
} ;
14
8
use cosmic_notifications_util:: AppletEvent ;
15
- use std:: os:: unix:: io:: { FromRawFd , RawFd } ;
16
9
use sendfd:: RecvWithFd ;
10
+ use std:: os:: unix:: io:: { FromRawFd , RawFd } ;
11
+ use tokio:: {
12
+ io:: { self , AsyncBufReadExt , BufReader } ,
13
+ net:: UnixStream ,
14
+ sync:: oneshot,
15
+ } ;
16
+ use tracing:: { error, info, warn} ;
17
17
18
18
#[ derive( Debug ) ]
19
19
pub enum State {
@@ -42,14 +42,14 @@ pub fn notifications() -> Subscription<AppletEvent> {
42
42
std:: thread:: spawn ( move || -> anyhow:: Result < ( ) > {
43
43
let mut msg = String :: new ( ) ;
44
44
std:: io:: stdin ( ) . read_line ( & mut msg) ?;
45
- let raw_fd = msg. trim ( ) . parse :: < RawFd > ( ) ?;
45
+ let raw_fd = msg. trim ( ) . parse :: < RawFd > ( ) ?;
46
46
if raw_fd == 0 {
47
47
anyhow:: bail!( "Invalid fd received from panel" ) ;
48
48
}
49
49
_ = tx. send ( raw_fd) ;
50
50
Ok ( ( ) )
51
51
} ) ;
52
-
52
+
53
53
let Ok ( raw_fd) = rx. await else {
54
54
error ! ( "Failed to receive raw fd from panel" ) ;
55
55
state = State :: Finished ;
@@ -63,10 +63,9 @@ pub fn notifications() -> Subscription<AppletEvent> {
63
63
continue ;
64
64
} ;
65
65
state = State :: WaitingForDaemon ( stream) ;
66
-
67
66
}
68
67
State :: WaitingForDaemon ( stream) => {
69
- info ! ( "Waiting for daemon to send us a stream" ) ;
68
+ info ! ( "Waiting for panel to send us a stream" ) ;
70
69
if let Err ( err) = stream. readable ( ) . await {
71
70
error ! ( "Failed to wait for stream to be readable {}" , err) ;
72
71
state = State :: Finished ;
@@ -79,21 +78,35 @@ pub fn notifications() -> Subscription<AppletEvent> {
79
78
80
79
match stream. recv_with_fd ( & mut buf, & mut fd_buf) {
81
80
Ok ( ( data_cnt, fd_cnt) ) => {
81
+ if data_cnt == 0 && fd_cnt == 0 {
82
+ warn ! ( "Received EOF from panel" ) ;
83
+ state = State :: Finished ;
84
+
85
+ continue ;
86
+ }
82
87
if data_cnt != 4 || fd_cnt != 1 {
83
- error ! ( "Invalid data received from panel" ) ;
88
+ error ! (
89
+ "Invalid data received from panel {} {}" ,
90
+ data_cnt, fd_cnt
91
+ ) ;
84
92
state = State :: Finished ;
85
93
86
94
continue ;
87
95
}
88
- let notif_stream = unsafe { std:: os:: unix:: net:: UnixStream :: from_raw_fd ( fd_buf[ 0 ] ) } ;
96
+ let notif_stream = unsafe {
97
+ std:: os:: unix:: net:: UnixStream :: from_raw_fd ( fd_buf[ 0 ] )
98
+ } ;
89
99
let Ok ( notif_stream) = UnixStream :: from_std ( notif_stream) else {
90
100
error ! ( "Failed to convert raw fd to unix stream" ) ;
91
101
state = State :: Finished ;
92
102
continue ;
93
103
} ;
94
-
104
+
95
105
state = State :: WaitingForNotificationEvent ( notif_stream) ;
96
106
}
107
+ Err ( ref e) if e. kind ( ) == io:: ErrorKind :: WouldBlock => {
108
+ continue ;
109
+ }
97
110
Err ( err) => {
98
111
error ! ( "Failed to receive fd from panel: {}" , err) ;
99
112
state = State :: Finished ;
@@ -103,6 +116,7 @@ pub fn notifications() -> Subscription<AppletEvent> {
103
116
}
104
117
}
105
118
State :: WaitingForNotificationEvent ( stream) => {
119
+ info ! ( "Waiting for notification event" ) ;
106
120
let mut reader = BufReader :: new ( stream) ;
107
121
// todo read messages
108
122
let mut request_buf = String :: with_capacity ( 1024 ) ;
@@ -124,6 +138,6 @@ pub fn notifications() -> Subscription<AppletEvent> {
124
138
}
125
139
}
126
140
}
127
- }
141
+ } ,
128
142
)
129
- }
143
+ }
0 commit comments