use std::sync::{Arc, Mutex};
use protocol::wl_display;
use protocol::wl_registry;
use {Interface, NewProxy, Proxy};
struct Inner {
list: Vec<(u32, String, u32)>,
}
#[derive(Clone)]
pub struct GlobalManager {
inner: Arc<Mutex<Inner>>,
registry: wl_registry::WlRegistry,
}
#[derive(Debug, PartialEq)]
pub enum GlobalError {
Missing,
VersionTooLow(u32),
}
impl ::std::error::Error for GlobalError {
fn description(&self) -> &str {
match *self {
GlobalError::Missing => "The requested global was missing.",
GlobalError::VersionTooLow(_) => "The requested global's version is too low.",
}
}
}
impl ::std::fmt::Display for GlobalError {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
f.write_str(::std::error::Error::description(self))
}
}
pub enum GlobalEvent {
New {
id: u32,
interface: String,
version: u32,
},
Removed {
id: u32,
interface: String,
},
}
impl GlobalManager {
pub fn new(display: &wl_display::WlDisplay) -> GlobalManager {
let inner = Arc::new(Mutex::new(Inner { list: Vec::new() }));
let inner_clone = inner.clone();
let registry = display
.get_registry(|registry| {
registry.implement_closure(
move |msg, _proxy| {
let mut inner = inner.lock().unwrap();
match msg {
wl_registry::Event::Global {
name,
interface,
version,
} => {
inner.list.push((name, interface, version));
}
wl_registry::Event::GlobalRemove { name } => {
inner.list.retain(|&(n, _, _)| n != name);
}
_ => {}
}
},
(),
)
})
.expect("Attempted to create a GlobalManager from a dead display.");
GlobalManager {
inner: inner_clone,
registry,
}
}
pub fn new_with_cb<F>(display: &wl_display::WlDisplay, mut callback: F) -> GlobalManager
where
F: FnMut(GlobalEvent, wl_registry::WlRegistry) + 'static,
{
let inner = Arc::new(Mutex::new(Inner { list: Vec::new() }));
let inner_clone = inner.clone();
let registry = display
.get_registry(|registry| {
registry.implement_closure(
move |msg, proxy| {
let mut inner = inner.lock().unwrap();
let inner = &mut *inner;
match msg {
wl_registry::Event::Global {
name,
interface,
version,
} => {
inner.list.push((name, interface.clone(), version));
callback(
GlobalEvent::New {
id: name,
interface,
version,
},
proxy,
);
}
wl_registry::Event::GlobalRemove { name } => {
if let Some((i, _)) =
inner.list.iter().enumerate().find(|&(_, &(n, _, _))| n == name)
{
let (id, interface, _) = inner.list.swap_remove(i);
callback(
GlobalEvent::Removed {
id,
interface,
},
proxy,
);
} else {
panic!(
"Wayland protocol error: the server removed non-existing global \"{}\".",
name
);
}
},
_ => {}
}
},
(),
)
})
.expect("Attempted to create a GlobalManager from a dead display.");
GlobalManager {
inner: inner_clone,
registry,
}
}
pub fn instantiate_exact<I, F>(&self, version: u32, implementor: F) -> Result<I, GlobalError>
where
I: Interface + From<Proxy<I>>,
F: FnOnce(NewProxy<I>) -> I,
{
let inner = self.inner.lock().unwrap();
for &(id, ref interface, server_version) in &inner.list {
if interface == I::NAME {
if version > server_version {
return Err(GlobalError::VersionTooLow(server_version));
} else {
return Ok(self.registry.bind(version, id, implementor).unwrap());
}
}
}
Err(GlobalError::Missing)
}
pub fn instantiate_range<I, F>(
&self,
min_version: u32,
max_version: u32,
implementor: F,
) -> Result<I, GlobalError>
where
I: Interface + From<Proxy<I>>,
F: FnOnce(NewProxy<I>) -> I,
{
let inner = self.inner.lock().unwrap();
for &(id, ref interface, version) in &inner.list {
if interface == I::NAME {
if version >= min_version {
return Ok(self
.registry
.bind(::std::cmp::min(version, max_version), id, implementor)
.unwrap());
} else {
return Err(GlobalError::VersionTooLow(version));
}
}
}
Err(GlobalError::Missing)
}
pub fn list(&self) -> Vec<(u32, String, u32)> {
self.inner.lock().unwrap().list.clone()
}
}
pub trait GlobalImplementor<I: Interface> {
fn new_global(&mut self, global: NewProxy<I>) -> I;
fn error(&mut self, _version: u32) {}
}
impl<F, I: Interface> GlobalImplementor<I> for F
where
F: FnMut(NewProxy<I>) -> I,
{
fn new_global(&mut self, global: NewProxy<I>) -> I {
(*self)(global)
}
}
#[macro_export]
macro_rules! global_filter {
($([$interface:ty, $version:expr, $callback:expr]),*) => {
{
use $crate::protocol::wl_registry;
use $crate::{GlobalEvent, NewProxy, Interface, GlobalImplementor};
type Callback = Box<FnMut(u32, u32, wl_registry::WlRegistry)>;
let mut callbacks: Vec<(&'static str, Callback)> = Vec::new();
$({
let mut cb = { $callback };
callbacks.push((
<$interface as Interface>::NAME,
Box::new(move |id, version, registry: wl_registry::WlRegistry| {
if version < $version {
GlobalImplementor::<$interface>::error(&mut cb, version);
} else {
registry.bind::<$interface, _>(
version,
id,
|newp| GlobalImplementor::<$interface>::new_global(&mut cb, newp)
)
.expect("wl_registry died unexpectedly");
}
}) as Box<_>
));
})*
move |event: GlobalEvent, registry: wl_registry::WlRegistry| {
if let GlobalEvent::New { id, interface, version } = event {
for &mut (iface, ref mut cb) in &mut callbacks {
if iface == interface {
cb(id, version, registry);
break;
}
}
}
}
}
}
}