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
use std::cell::RefCell;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use downcast_rs::Downcast;
use wayland_commons::debug;
use wayland_commons::map::ObjectMap;
use wayland_commons::wire::Message;
use wayland_commons::{MessageGroup, ThreadGuard};
use crate::{DispatchData, Filter, Interface, Main, Resource};
mod clients;
mod display;
mod event_loop_glue;
mod globals;
mod resources;
pub(crate) use self::clients::ClientInner;
pub(crate) use self::display::DisplayInner;
pub(crate) use self::globals::GlobalInner;
pub(crate) use self::resources::ResourceInner;
use self::resources::ResourceDestructor;
static WAYLAND_DEBUG: AtomicBool = AtomicBool::new(false);
pub struct ResourceMap {
map: Arc<Mutex<ObjectMap<self::resources::ObjectMeta>>>,
client: ClientInner,
}
impl ResourceMap {
fn make(
map: Arc<Mutex<ObjectMap<self::resources::ObjectMeta>>>,
client: ClientInner,
) -> ResourceMap {
ResourceMap { map, client }
}
pub fn get<I: Interface + From<Resource<I>> + AsRef<Resource<I>>>(
&mut self,
id: u32,
) -> Option<Resource<I>> {
ResourceInner::from_id(id, self.map.clone(), self.client.clone()).map(|object| {
debug_assert!(I::NAME == "<anonymous>" || object.is_interface::<I>());
Resource::wrap(object)
})
}
pub fn get_new<I: Interface + AsRef<Resource<I>> + From<Resource<I>>>(
&mut self,
id: u32,
) -> Option<Main<I>> {
debug_assert!(self
.map
.lock()
.unwrap()
.find(id)
.map(|obj| obj.is_interface::<I>())
.unwrap_or(true));
ResourceInner::from_id(id, self.map.clone(), self.client.clone()).map(Main::wrap)
}
}
#[allow(clippy::large_enum_variant)]
pub(crate) enum Dispatched {
Yes,
NoDispatch(Message, ResourceInner),
BadMsg,
}
pub(crate) trait Dispatcher: Downcast {
fn dispatch(
&mut self,
msg: Message,
resource: ResourceInner,
map: &mut ResourceMap,
data: DispatchData,
) -> Dispatched;
}
mod dispatcher_impl {
#![allow(dead_code)]
use super::Dispatcher;
downcast_rs::impl_downcast!(Dispatcher);
}
pub(crate) struct ImplDispatcher<
I: Interface + From<Resource<I>> + AsRef<Resource<I>>,
F: FnMut(I::Request, Main<I>, DispatchData<'_>),
> {
_i: ::std::marker::PhantomData<&'static I>,
implementation: F,
}
impl<I, F> Dispatcher for ImplDispatcher<I, F>
where
I: Interface + From<Resource<I>> + AsRef<Resource<I>>,
F: FnMut(I::Request, Main<I>, DispatchData<'_>) + 'static,
I::Request: MessageGroup<Map = ResourceMap>,
{
fn dispatch(
&mut self,
msg: Message,
resource: ResourceInner,
map: &mut ResourceMap,
data: DispatchData,
) -> Dispatched {
let opcode = msg.opcode as usize;
if WAYLAND_DEBUG.load(Ordering::Relaxed) {
debug::print_dispatched_message(
resource.object.interface,
resource.id,
resource.object.requests[opcode].name,
&msg.args,
);
}
let message = match I::Request::from_raw(msg, map) {
Ok(msg) => msg,
Err(_) => return Dispatched::BadMsg,
};
if message.since() > resource.version() {
eprintln!(
"Received an request {} requiring version >= {} while resource {}@{} is version {}.",
resource.object.requests[opcode].name,
message.since(),
resource.object.interface,
resource.id,
resource.version()
);
return Dispatched::BadMsg;
}
if message.is_destructor() {
resource.object.meta.alive.store(false, Ordering::Release);
let mut kill = false;
if let Some(ref mut data) = *resource.client.data.lock().unwrap() {
data.schedule_destructor(resource.clone());
kill = data.delete_id(resource.id).is_err();
}
if kill {
resource.client.kill();
}
}
(self.implementation)(message, Main::<I>::wrap(resource), data);
Dispatched::Yes
}
}
pub(crate) fn make_dispatcher<I, E>(filter: Filter<E>) -> Arc<ThreadGuard<RefCell<dyn Dispatcher>>>
where
I: Interface + AsRef<Resource<I>> + From<Resource<I>>,
E: From<(Main<I>, I::Request)> + 'static,
I::Request: MessageGroup<Map = ResourceMap>,
{
Arc::new(ThreadGuard::new(RefCell::new(ImplDispatcher {
_i: ::std::marker::PhantomData,
implementation: move |evt, res, data| filter.send((res, evt).into(), data),
})))
}
pub(crate) fn default_dispatcher() -> Arc<ThreadGuard<RefCell<dyn Dispatcher>>> {
struct DefaultDisp;
impl Dispatcher for DefaultDisp {
fn dispatch(
&mut self,
msg: Message,
resource: ResourceInner,
_map: &mut ResourceMap,
_data: DispatchData,
) -> Dispatched {
Dispatched::NoDispatch(msg, resource)
}
}
Arc::new(ThreadGuard::new(RefCell::new(DefaultDisp)))
}
pub(crate) fn make_destructor<I, E>(filter: Filter<E>) -> Arc<ThreadGuard<ResourceDestructor>>
where
I: Interface + AsRef<Resource<I>> + From<Resource<I>>,
E: From<Resource<I>> + 'static,
{
Arc::new(ThreadGuard::new(RefCell::new(move |res, data: DispatchData<'_>| {
filter.send(Resource::<I>::wrap(res).into(), data)
})))
}