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
use std::{io, os::unix::io::RawFd};
use calloop::{
generic::Generic, EventSource, InsertError, Interest, LoopHandle, Mode, PostAction,
RegistrationToken, TokenFactory,
};
use wayland_client::{EventQueue, ReadEventsGuard};
#[derive(Debug)]
pub struct WaylandSource {
queue: EventQueue,
fd: Generic<RawFd>,
read_guard: Option<ReadEventsGuard>,
}
impl WaylandSource {
pub fn new(queue: EventQueue) -> WaylandSource {
let fd = queue.display().get_connection_fd();
WaylandSource { queue, fd: Generic::new(fd, Interest::READ, Mode::Level), read_guard: None }
}
pub fn quick_insert<Data: 'static>(
self,
handle: LoopHandle<Data>,
) -> Result<RegistrationToken, InsertError<WaylandSource>> {
handle.insert_source(self, |(), queue, ddata| {
queue.dispatch_pending(ddata, |event, object, _| {
panic!(
"[calloop] Encountered an orphan event: {}@{} : {}",
event.interface,
object.as_ref().id(),
event.name
);
})
})
}
pub fn queue(&mut self) -> &mut EventQueue {
&mut self.queue
}
}
impl EventSource for WaylandSource {
type Event = ();
type Error = std::io::Error;
type Metadata = EventQueue;
type Ret = std::io::Result<u32>;
fn process_events<F>(
&mut self,
readiness: calloop::Readiness,
token: calloop::Token,
mut callback: F,
) -> std::io::Result<PostAction>
where
F: FnMut((), &mut EventQueue) -> std::io::Result<u32>,
{
let queue = &mut self.queue;
let read_guard = &mut self.read_guard;
self.fd.process_events(readiness, token, |_, _| {
if let Some(guard) = read_guard.take() {
if let Err(e) = guard.read_events() {
if e.kind() != io::ErrorKind::WouldBlock {
return Err(e);
}
}
}
loop {
match queue.prepare_read() {
Some(guard) => {
*read_guard = Some(guard);
break;
}
None => {
callback((), queue)?;
}
}
}
if let Err(e) = queue.display().flush() {
if e.kind() != io::ErrorKind::WouldBlock {
return Err(e);
}
}
Ok(PostAction::Continue)
})
}
fn register(
&mut self,
poll: &mut calloop::Poll,
token_factory: &mut TokenFactory,
) -> calloop::Result<()> {
self.fd.register(poll, token_factory)
}
fn reregister(
&mut self,
poll: &mut calloop::Poll,
token_factory: &mut TokenFactory,
) -> calloop::Result<()> {
self.fd.reregister(poll, token_factory)
}
fn unregister(&mut self, poll: &mut calloop::Poll) -> calloop::Result<()> {
self.fd.unregister(poll)
}
fn pre_run<F>(&mut self, mut callback: F) -> calloop::Result<()>
where
F: FnMut((), &mut EventQueue) -> std::io::Result<u32>,
{
debug_assert!(self.read_guard.is_none());
if let Err(e) = self.queue.display().flush() {
if e.kind() != io::ErrorKind::WouldBlock {
log::error!("Error trying to flush the wayland display: {}", e);
return Err(e.into());
}
}
loop {
match self.queue.prepare_read() {
Some(guard) => {
self.read_guard = Some(guard);
break;
}
None => {
callback((), &mut self.queue)?;
}
}
}
Ok(())
}
fn post_run<F>(&mut self, _: F) -> calloop::Result<()>
where
F: FnMut((), &mut EventQueue) -> std::io::Result<u32>,
{
self.read_guard = None;
Ok(())
}
}