wayland: gracefully handle missing libxkbcommon

This commit is contained in:
Victor Berger 2016-10-09 18:26:24 +02:00
parent c6f8ed2f81
commit 2d025a4a35

View file

@ -23,6 +23,11 @@ wayland_env!(InnerEnv,
subcompositor: wl_subcompositor::WlSubcompositor subcompositor: wl_subcompositor::WlSubcompositor
); );
enum KbdType {
Mapped(MappedKeyboard<KbdHandler>),
Plain(Option<Arc<Mutex<VecDeque<Event>>>>)
}
struct WaylandEnv { struct WaylandEnv {
registry: wl_registry::WlRegistry, registry: wl_registry::WlRegistry,
inner: EnvHandler<InnerEnv>, inner: EnvHandler<InnerEnv>,
@ -34,7 +39,7 @@ struct WaylandEnv {
mouse_focus: Option<Arc<Mutex<VecDeque<Event>>>>, mouse_focus: Option<Arc<Mutex<VecDeque<Event>>>>,
mouse_location: (i32, i32), mouse_location: (i32, i32),
kbd: Option<wl_keyboard::WlKeyboard>, kbd: Option<wl_keyboard::WlKeyboard>,
kbd_handler: MappedKeyboard<KbdHandler> kbd_handler: KbdType
} }
struct OutputInfo { struct OutputInfo {
@ -59,6 +64,10 @@ impl OutputInfo {
impl WaylandEnv { impl WaylandEnv {
fn new(registry: wl_registry::WlRegistry) -> WaylandEnv { fn new(registry: wl_registry::WlRegistry) -> WaylandEnv {
let kbd_handler = match MappedKeyboard::new(KbdHandler::new()) {
Ok(h) => KbdType::Mapped(h),
Err(_) => KbdType::Plain(None)
};
WaylandEnv { WaylandEnv {
registry: registry, registry: registry,
inner: EnvHandler::new(), inner: EnvHandler::new(),
@ -70,7 +79,7 @@ impl WaylandEnv {
mouse_focus: None, mouse_focus: None,
mouse_location: (0,0), mouse_location: (0,0),
kbd: None, kbd: None,
kbd_handler: MappedKeyboard::new(KbdHandler::new()).ok().expect("Missing libxkbcommon!") // TODO kbd_handler: kbd_handler
} }
} }
@ -497,7 +506,10 @@ impl wl_keyboard::Handler for WaylandEnv {
fd: ::std::os::unix::io::RawFd, fd: ::std::os::unix::io::RawFd,
size: u32) size: u32)
{ {
self.kbd_handler.keymap(evqh, proxy, format, fd, size) match self.kbd_handler {
KbdType::Mapped(ref mut h) => h.keymap(evqh, proxy, format, fd, size),
_ => ()
}
} }
fn enter(&mut self, fn enter(&mut self,
@ -507,15 +519,25 @@ impl wl_keyboard::Handler for WaylandEnv {
surface: &wl_surface::WlSurface, surface: &wl_surface::WlSurface,
keys: Vec<u8>) keys: Vec<u8>)
{ {
let mut opt_eviter = None;
for &(ref window, ref eviter) in &self.windows { for &(ref window, ref eviter) in &self.windows {
if window.equals(surface) { if window.equals(surface) {
self.kbd_handler.handler().target = Some(eviter.clone()); opt_eviter = Some(eviter.clone());
let mut guard = eviter.lock().unwrap(); break;
guard.push_back(Event::Focused(true));
break
} }
} }
self.kbd_handler.enter(evqh, proxy, serial, surface, keys) if let Some(ref eviter) = opt_eviter {
// send focused event
let mut guard = eviter.lock().unwrap();
guard.push_back(Event::Focused(true));
}
match self.kbd_handler {
KbdType::Mapped(ref mut h) => {
h.handler().target = opt_eviter;
h.enter(evqh, proxy, serial, surface, keys);
},
KbdType::Plain(ref mut opt) => { *opt = opt_eviter; }
}
} }
fn leave(&mut self, fn leave(&mut self,
@ -524,11 +546,18 @@ impl wl_keyboard::Handler for WaylandEnv {
serial: u32, serial: u32,
surface: &wl_surface::WlSurface) surface: &wl_surface::WlSurface)
{ {
if let Some(eviter) = self.kbd_handler.handler().target.take() { let opt_eviter = match self.kbd_handler {
KbdType::Mapped(ref mut h) => {
let eviter = h.handler().target.take();
h.leave(evqh, proxy, serial, surface);
eviter
},
KbdType::Plain(ref mut opt) => opt.take()
};
if let Some(eviter) = opt_eviter {
let mut guard = eviter.lock().unwrap(); let mut guard = eviter.lock().unwrap();
guard.push_back(Event::Focused(false)); guard.push_back(Event::Focused(false));
} }
self.kbd_handler.leave(evqh, proxy, serial, surface)
} }
fn key(&mut self, fn key(&mut self,
@ -539,7 +568,22 @@ impl wl_keyboard::Handler for WaylandEnv {
key: u32, key: u32,
state: wl_keyboard::KeyState) state: wl_keyboard::KeyState)
{ {
self.kbd_handler.key(evqh, proxy, serial, time, key, state) match self.kbd_handler {
KbdType::Mapped(ref mut h) => h.key(evqh, proxy, serial, time, key, state),
KbdType::Plain(Some(ref eviter)) => {
let state = match state {
wl_keyboard::KeyState::Pressed => ElementState::Pressed,
wl_keyboard::KeyState::Released => ElementState::Released,
};
let mut guard = eviter.lock().unwrap();
guard.push_back(Event::KeyboardInput(
state,
key as u8,
None
));
},
KbdType::Plain(None) => ()
}
} }
fn modifiers(&mut self, fn modifiers(&mut self,
@ -551,7 +595,11 @@ impl wl_keyboard::Handler for WaylandEnv {
mods_locked: u32, mods_locked: u32,
group: u32) group: u32)
{ {
self.kbd_handler.modifiers(evqh, proxy, serial, mods_depressed, mods_latched, mods_locked, group) match self.kbd_handler {
KbdType::Mapped(ref mut h) => h.modifiers(evqh, proxy, serial, mods_depressed,
mods_latched, mods_locked, group),
_ => ()
}
} }
fn repeat_info(&mut self, fn repeat_info(&mut self,
@ -560,7 +608,10 @@ impl wl_keyboard::Handler for WaylandEnv {
rate: i32, rate: i32,
delay: i32) delay: i32)
{ {
self.kbd_handler.repeat_info(evqh, proxy, rate, delay) match self.kbd_handler {
KbdType::Mapped(ref mut h) => h.repeat_info(evqh, proxy, rate, delay),
_ => ()
}
} }
} }