wayland_bridge/
registry.rs1use crate::client::Client;
6use crate::object::MessageReceiver;
7use anyhow::Error;
8use fuchsia_wayland_core as wl;
9use std::mem;
10
11pub struct RegistryBuilder {
23 globals: Vec<Global>,
24}
25
26impl RegistryBuilder {
27 pub fn new() -> Self {
28 RegistryBuilder { globals: Vec::new() }
29 }
30
31 pub fn add_global<
39 I: wl::Interface + 'static,
40 F: FnMut(wl::ObjectId, u32, &mut Client) -> Result<Box<dyn MessageReceiver>, Error>
41 + Send
42 + 'static,
43 >(
44 &mut self,
45 _: I,
46 bind: F,
47 ) -> &mut Self {
48 self.globals.push(Global {
49 name: I::NAME,
50 version: I::VERSION,
51 requests: &I::REQUESTS,
52 bind_fn: Box::new(bind),
53 });
54 self
55 }
56
57 pub fn build(&mut self) -> Registry {
58 Registry { globals: mem::replace(&mut self.globals, vec![]) }
59 }
60}
61
62pub struct Registry {
64 globals: Vec<Global>,
65}
66
67impl Registry {
68 pub fn globals(&mut self) -> &mut [Global] {
69 self.globals.as_mut_slice()
70 }
71}
72
73pub struct Global {
74 name: &'static str,
75 version: u32,
76 requests: &'static wl::MessageGroupSpec,
77 bind_fn: Box<
78 dyn FnMut(wl::ObjectId, u32, &mut Client) -> Result<Box<dyn MessageReceiver>, Error> + Send,
79 >,
80}
81
82impl Global {
83 pub fn interface(&self) -> &str {
85 self.name
86 }
87
88 pub fn version(&self) -> u32 {
90 self.version
91 }
92
93 pub fn request_spec(&self) -> &'static wl::MessageGroupSpec {
95 self.requests
96 }
97
98 pub fn bind(
102 &mut self,
103 id: wl::ObjectId,
104 version: u32,
105 client: &mut Client,
106 ) -> Result<Box<dyn MessageReceiver>, Error> {
107 (*self.bind_fn)(id, version, client)
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 use std::sync::Arc;
116
117 use fuchsia_async as fasync;
118 use fuchsia_sync::Mutex;
119 use fuchsia_wayland_core::{Interface, IntoMessage};
120
121 use crate::display::Display;
122 use crate::object::RequestDispatcher;
123 use crate::test_protocol::*;
124
125 #[test]
126 fn registry_bind() -> Result<(), Error> {
127 #[derive(Default)]
130 struct Counts {
131 interface_1_bind_count: usize,
132 interface_2_bind_count: usize,
133 }
134 let counts: Arc<Mutex<Counts>> = Arc::new(Mutex::new(Default::default()));
135 let mut registry = RegistryBuilder::new();
136
137 {
138 let counts = counts.clone();
139 registry.add_global(TestInterface, move |_, _, _| {
140 counts.lock().interface_1_bind_count += 1;
141 Ok(Box::new(RequestDispatcher::new(TestReceiver::new())))
142 });
143 }
144 {
145 let counts = counts.clone();
146 registry.add_global(TestInterface2, move |_, _, _| {
147 counts.lock().interface_2_bind_count += 1;
148 Ok(Box::new(RequestDispatcher::new(TestReceiver::new())))
149 });
150 }
151
152 let registry = registry.build();
154 assert_eq!(0, counts.lock().interface_1_bind_count);
155 assert_eq!(0, counts.lock().interface_2_bind_count);
156
157 let (c1, _c2) = zx::Channel::create();
159 let _executor = fasync::LocalExecutor::new();
160 let display = Display::new_no_scenic(registry).expect("Failed to create display");
161 let mut client = Client::new(fasync::Channel::from_channel(c1), display.clone());
162
163 let receivers: Vec<Box<dyn MessageReceiver>> = display
164 .registry()
165 .lock()
166 .globals
167 .iter_mut()
168 .map(|g| g.bind(0, 0, &mut client).unwrap())
169 .collect();
170 for (id, r) in receivers.into_iter().enumerate() {
171 client.add_object_raw(id as u32, r, &TestInterface::REQUESTS)?;
172 }
173 assert_eq!(1, counts.lock().interface_1_bind_count);
174 assert_eq!(1, counts.lock().interface_2_bind_count);
175 assert_eq!(0, client.get_object::<TestReceiver>(0)?.count());
176 assert_eq!(0, client.get_object::<TestReceiver>(1)?.count());
177
178 client.handle_message(TestMessage::Message1.into_message(0)?)?;
180 client.handle_message(TestMessage::Message1.into_message(1)?)?;
181
182 assert_eq!(1, counts.lock().interface_1_bind_count);
183 assert_eq!(1, counts.lock().interface_2_bind_count);
184 assert_eq!(1, client.get_object::<TestReceiver>(0)?.count());
185 assert_eq!(1, client.get_object::<TestReceiver>(1)?.count());
186 Ok(())
187 }
188}