1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use std::cell::RefCell;
use std::rc::Rc;
use std::io::Error as IoError;
use std::os::raw::{c_int, c_void};
use std::os::unix::io::RawFd;
#[cfg(feature = "native_lib")]
use wayland_sys::server::*;
use wayland_commons::Implementation;
pub struct Source<E> {
_e: ::std::marker::PhantomData<*const E>,
#[cfg(feature = "native_lib")]
ptr: *mut wl_event_source,
#[cfg(feature = "native_lib")]
data: *mut Box<Implementation<(), E>>,
}
impl<E> Source<E> {
#[cfg(feature = "native_lib")]
pub(crate) fn make(ptr: *mut wl_event_source, data: Box<Box<Implementation<(), E>>>) -> Source<E> {
Source {
_e: ::std::marker::PhantomData,
ptr: ptr,
data: Box::into_raw(data),
}
}
pub fn remove(self) -> Box<Implementation<(), E>> {
#[cfg(not(feature = "native_lib"))]
{
unimplemented!()
}
#[cfg(feature = "native_lib")]
{
unsafe {
ffi_dispatch!(WAYLAND_SERVER_HANDLE, wl_event_source_remove, self.ptr);
let data = Box::from_raw(self.data);
*data
}
}
}
}
bitflags!{
pub struct FdInterest: u32 {
const READ = 0x01;
const WRITE = 0x02;
}
}
pub enum FdEvent {
Ready {
fd: RawFd,
mask: FdInterest,
},
Error {
fd: RawFd,
error: IoError,
},
}
impl Source<FdEvent> {
pub fn update_mask(&mut self, mask: FdInterest) {
#[cfg(not(feature = "native_lib"))]
{
unimplemented!()
}
#[cfg(feature = "native_lib")]
{
unsafe {
ffi_dispatch!(
WAYLAND_SERVER_HANDLE,
wl_event_source_fd_update,
self.ptr,
mask.bits()
);
}
}
}
}
pub(crate) unsafe extern "C" fn event_source_fd_dispatcher(fd: c_int, mask: u32, data: *mut c_void) -> c_int {
let ret = ::std::panic::catch_unwind(move || {
let implem = &mut *(data as *mut Box<Implementation<(), FdEvent>>);
if mask & 0x08 > 0 {
use nix::sys::socket;
let err = match socket::getsockopt(fd, socket::sockopt::SocketError) {
Ok(err) => err,
Err(_) => {
eprintln!(
"[wayland-server error] Error while retrieving error code on socket {}, aborting.",
fd
);
::libc::abort();
}
};
implem.receive(
FdEvent::Error {
fd: fd,
error: IoError::from_raw_os_error(err),
},
(),
);
} else if mask & 0x04 > 0 {
implem.receive(
FdEvent::Error {
fd: fd,
error: IoError::new(::std::io::ErrorKind::ConnectionAborted, ""),
},
(),
);
} else {
let mut bits = FdInterest::empty();
if mask & 0x02 > 0 {
bits = bits | FdInterest::WRITE;
}
if mask & 0x01 > 0 {
bits = bits | FdInterest::READ;
}
implem.receive(FdEvent::Ready { fd: fd, mask: bits }, ());
}
});
match ret {
Ok(()) => return 0,
Err(_) => {
eprintln!(
"[wayland-server error] A handler for fd {} event source panicked, aborting.",
fd
);
::libc::abort();
}
}
}
pub struct TimerEvent;
impl Source<TimerEvent> {
pub fn set_delay_ms(&mut self, delay: i32) {
unsafe {
ffi_dispatch!(
WAYLAND_SERVER_HANDLE,
wl_event_source_timer_update,
self.ptr,
delay
);
}
}
}
pub(crate) unsafe extern "C" fn event_source_timer_dispatcher(data: *mut c_void) -> c_int {
let ret = ::std::panic::catch_unwind(move || {
let implem = &mut *(data as *mut Box<Implementation<(), TimerEvent>>);
implem.receive(TimerEvent, ());
});
match ret {
Ok(()) => return 0,
Err(_) => {
eprintln!("[wayland-server error] A handler for a timer event source panicked, aborting.",);
::libc::abort();
}
}
}
pub struct SignalEvent(::nix::sys::signal::Signal);
pub(crate) unsafe extern "C" fn event_source_signal_dispatcher(signal: c_int, data: *mut c_void) -> c_int {
let ret = ::std::panic::catch_unwind(move || {
let implem = &mut *(data as *mut Box<Implementation<(), SignalEvent>>);
let sig = match ::nix::sys::signal::Signal::from_c_int(signal) {
Ok(sig) => sig,
Err(_) => {
eprintln!(
"[wayland-server error] Unknown signal in signal event source: {}, aborting.",
signal
);
::libc::abort();
}
};
implem.receive(SignalEvent(sig), ());
});
match ret {
Ok(()) => return 0,
Err(_) => {
eprintln!("[wayland-server error] A handler for a timer event source panicked, aborting.",);
::libc::abort();
}
}
}
pub struct IdleSource {
ptr: *mut wl_event_source,
data: Rc<RefCell<(Box<Implementation<(), ()>>, bool)>>,
}
impl IdleSource {
#[cfg(feature = "native_lib")]
pub(crate) fn make(
ptr: *mut wl_event_source,
data: Rc<RefCell<(Box<Implementation<(), ()>>, bool)>>,
) -> IdleSource {
IdleSource {
ptr: ptr,
data: data,
}
}
pub fn remove(self) -> Box<Implementation<(), ()>> {
let dispatched = self.data.borrow().1;
if !dispatched {
unsafe {
ffi_dispatch!(WAYLAND_SERVER_HANDLE, wl_event_source_remove, self.ptr);
let _ = Rc::from_raw(&*self.data);
}
}
let data = Rc::try_unwrap(self.data)
.unwrap_or_else(|_| panic!("Idle Rc was not singly owned."))
.into_inner();
data.0
}
}
pub(crate) unsafe extern "C" fn event_source_idle_dispatcher(data: *mut c_void) {
let ret = ::std::panic::catch_unwind(move || {
let data = &*(data as *mut RefCell<(Box<Implementation<(), ()>>, bool)>);
let mut data = data.borrow_mut();
data.0.receive((), ());
});
match ret {
Ok(()) => {
let data = Rc::from_raw(data as *mut RefCell<(Box<Implementation<(), ()>>, bool)>);
data.borrow_mut().1 = true;
}
Err(_) => {
eprintln!("[wayland-server error] A handler for a timer event source panicked, aborting.",);
::libc::abort();
}
}
}