aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/wayland/context.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/wayland/context.rs')
-rw-r--r--src/api/wayland/context.rs383
1 files changed, 183 insertions, 200 deletions
diff --git a/src/api/wayland/context.rs b/src/api/wayland/context.rs
index ad0977c..f303b54 100644
--- a/src/api/wayland/context.rs
+++ b/src/api/wayland/context.rs
@@ -1,228 +1,211 @@
-use super::wayland::core::{default_display, Display, Registry};
-use super::wayland::core::compositor::{Compositor, SurfaceId, WSurface};
-use super::wayland::core::output::Output;
-use super::wayland::core::seat::{ButtonState, Seat, Pointer, Keyboard, KeyState};
-use super::wayland::core::shell::Shell;
-use super::wayland_kbd::MappedKeyboard;
-use super::keyboard::keycode_to_vkey;
-
+use Event as GlutinEvent;
-use std::collections::{VecDeque, HashMap};
+use std::collections::{HashMap, VecDeque, HashSet};
use std::sync::{Arc, Mutex};
-use Event;
-use MouseButton;
-use ElementState;
+use libc::c_void;
+
+use wayland_client::{EventIterator, Proxy, ProxyId};
+use wayland_client::wayland::get_display;
+use wayland_client::wayland::compositor::{WlCompositor, WlSurface};
+use wayland_client::wayland::output::WlOutput;
+use wayland_client::wayland::seat::{WlSeat, WlPointer};
+use wayland_client::wayland::shell::{WlShell, WlShellSurface};
+use wayland_client::wayland::shm::WlShm;
+use wayland_client::wayland::subcompositor::WlSubcompositor;
-enum AnyKeyboard {
- RawKeyBoard(Keyboard),
- XKB(MappedKeyboard)
+use super::wayland_kbd::MappedKeyboard;
+use super::wayland_window::DecoratedSurface;
+
+lazy_static! {
+ pub static ref WAYLAND_CONTEXT: Option<WaylandContext> = {
+ WaylandContext::init()
+ };
+}
+
+wayland_env!(InnerEnv,
+ compositor: WlCompositor,
+ seat: WlSeat,
+ shell: WlShell,
+ shm: WlShm,
+ subcompositor: WlSubcompositor
+);
+
+pub struct WaylandFocuses {
+ pub pointer: Option<WlPointer>,
+ pub pointer_on: Option<ProxyId>,
+ pub pointer_at: Option<(f64, f64)>,
+ pub keyboard: Option<MappedKeyboard>,
+ pub keyboard_on: Option<ProxyId>
}
pub struct WaylandContext {
- pub display: Display,
- pub registry: Registry,
- pub compositor: Compositor,
- pub shell: Shell,
- pub seat: Seat,
- pointer: Option<Mutex<Pointer<WSurface>>>,
- keyboard: Option<AnyKeyboard>,
- windows_event_queues: Arc<Mutex<HashMap<SurfaceId, Arc<Mutex<VecDeque<Event>>>>>>,
- current_pointer_surface: Arc<Mutex<Option<SurfaceId>>>,
- current_keyboard_surface: Arc<Mutex<Option<SurfaceId>>>,
- pub outputs: Vec<Arc<Output>>
+ inner: InnerEnv,
+ iterator: Mutex<EventIterator>,
+ monitors: Vec<(WlOutput, u32, u32, String)>,
+ queues: Mutex<HashMap<ProxyId, Arc<Mutex<VecDeque<GlutinEvent>>>>>,
+ known_surfaces: Mutex<HashSet<ProxyId>>,
+ focuses: Mutex<WaylandFocuses>
}
impl WaylandContext {
- pub fn new() -> Option<WaylandContext> {
- let display = match default_display() {
- Some(d) => d,
- None => return None,
- };
- let registry = display.get_registry();
- // let the registry get its events
- display.sync_roundtrip();
- let compositor = match registry.get_compositor() {
- Some(c) => c,
- None => return None,
- };
- let shell = match registry.get_shell() {
- Some(s) => s,
- None => return None,
+ fn init() -> Option<WaylandContext> {
+ let display = match get_display() {
+ Some(display) => display,
+ None => return None
};
- let seat = match registry.get_seats().into_iter().next() {
- Some(s) => s,
- None => return None,
- };
- let outputs = registry.get_outputs().into_iter().map(Arc::new).collect::<Vec<_>>();
- // let the other globals get their events
- display.sync_roundtrip();
-
- let current_pointer_surface = Arc::new(Mutex::new(None));
-
- // rustc has trouble finding the correct type here, so we explicit it.
- let windows_event_queues = Arc::new(Mutex::new(
- HashMap::<SurfaceId, Arc<Mutex<VecDeque<Event>>>>::new()
- ));
-
- // handle pointer inputs
- let mut pointer = seat.get_pointer();
- if let Some(ref mut p) = pointer {
- // set the enter/leave callbacks
- let current_surface = current_pointer_surface.clone();
- p.set_enter_action(move |_, _, sid, x, y| {
- *current_surface.lock().unwrap() = Some(sid);
- });
- let current_surface = current_pointer_surface.clone();
- p.set_leave_action(move |_, _, sid| {
- *current_surface.lock().unwrap() = None;
- });
- // set the events callbacks
- let current_surface = current_pointer_surface.clone();
- let event_queues = windows_event_queues.clone();
- p.set_motion_action(move |_, _, x, y| {
- // dispatch to the appropriate queue
- let sid = *current_surface.lock().unwrap();
- if let Some(sid) = sid {
- let map = event_queues.lock().unwrap();
- if let Some(queue) = map.get(&sid) {
- queue.lock().unwrap().push_back(Event::MouseMoved((x as i32,y as i32)))
- }
- }
- });
- let current_surface = current_pointer_surface.clone();
- let event_queues = windows_event_queues.clone();
- p.set_button_action(move |_, _, sid, b, s| {
- let button = match b {
- 0x110 => MouseButton::Left,
- 0x111 => MouseButton::Right,
- 0x112 => MouseButton::Middle,
- _ => return
- };
- let state = match s {
- ButtonState::Released => ElementState::Released,
- ButtonState::Pressed => ElementState::Pressed
- };
- // dispatch to the appropriate queue
- let sid = *current_surface.lock().unwrap();
- if let Some(sid) = sid {
- let map = event_queues.lock().unwrap();
- if let Some(queue) = map.get(&sid) {
- queue.lock().unwrap().push_back(Event::MouseInput(state, button))
+
+ let (mut inner_env, iterator) = InnerEnv::init(display);
+
+ let mut outputs_events = EventIterator::new();
+
+ let mut monitors = inner_env.globals.iter()
+ .flat_map(|&(id, _, _)| inner_env.rebind_id::<WlOutput>(id))
+ .map(|(mut monitor, _)| {
+ monitor.set_evt_iterator(&outputs_events);
+ (monitor, 0, 0, String::new())
+ }).collect();
+
+ inner_env.display.sync_roundtrip().unwrap();
+
+ super::monitor::init_monitors(&mut monitors, outputs_events);
+
+ Some(WaylandContext {
+ inner: inner_env,
+ iterator: Mutex::new(iterator),
+ monitors: monitors,
+ queues: Mutex::new(HashMap::new()),
+ known_surfaces: Mutex::new(HashSet::new()),
+ focuses: Mutex::new(WaylandFocuses {
+ pointer: None,
+ pointer_on: None,
+ pointer_at: None,
+ keyboard: None,
+ keyboard_on: None
+ })
+ })
+ }
+
+ pub fn new_surface(&self) -> Option<(WlSurface, Arc<Mutex<VecDeque<GlutinEvent>>>)> {
+ self.inner.compositor.as_ref().map(|c| {
+ let s = c.0.create_surface();
+ let id = s.id();
+ let queue = {
+ let mut q = VecDeque::new();
+ q.push_back(GlutinEvent::Refresh);
+ Arc::new(Mutex::new(q))
+ };
+ self.queues.lock().unwrap().insert(id, queue.clone());
+ self.known_surfaces.lock().unwrap().insert(id);
+ (s, queue)
+ })
+ }
+
+ pub fn dropped_surface(&self, id: ProxyId) {
+ self.queues.lock().unwrap().remove(&id);
+ self.known_surfaces.lock().unwrap().remove(&id);
+ }
+
+ pub fn decorated_from(&self, surface: &WlSurface, width: i32, height: i32) -> Option<DecoratedSurface> {
+ let inner = &self.inner;
+ match (&inner.compositor, &inner.subcompositor, &inner.shm, &inner.shell) {
+ (&Some(ref compositor), &Some(ref subcompositor), &Some(ref shm), &Some(ref shell)) => {
+ DecoratedSurface::new(
+ surface, width, height,
+ &compositor.0, &subcompositor.0, &shm.0, &shell.0,
+ self.inner.rebind::<WlSeat>().map(|(seat, _)| seat)
+ ).ok()
+ }
+ _ => None
+ }
+ }
+
+ pub fn plain_from(&self, surface: &WlSurface, fullscreen: Option<ProxyId>) -> Option<WlShellSurface> {
+ use wayland_client::wayland::shell::WlShellSurfaceFullscreenMethod;
+
+ let inner = &self.inner;
+ if let Some((ref shell, _)) = inner.shell {
+ let shell_surface = shell.get_shell_surface(surface);
+ if let Some(monitor_id) = fullscreen {
+ for m in &self.monitors {
+ if m.0.id() == monitor_id {
+ shell_surface.set_fullscreen(
+ WlShellSurfaceFullscreenMethod::Default,
+ 0,
+ Some(&m.0)
+ );
+ return Some(shell_surface)
}
}
- });
+ }
+ shell_surface.set_toplevel();
+ Some(shell_surface)
+ } else {
+ None
}
+ }
- // handle keyboard inputs
- let mut keyboard = None;
- let current_keyboard_surface = Arc::new(Mutex::new(None));
- if let Some(mut wkbd) = seat.get_keyboard() {
- display.sync_roundtrip();
-
- let current_surface = current_keyboard_surface.clone();
- wkbd.set_enter_action(move |_, _, sid, _| {
- *current_surface.lock().unwrap() = Some(sid);
- });
- let current_surface = current_keyboard_surface.clone();
- wkbd.set_leave_action(move |_, _, sid| {
- *current_surface.lock().unwrap() = None;
- });
-
- let kbd = match MappedKeyboard::new(wkbd) {
- Ok(mkbd) => {
- // We managed to load a keymap
- let current_surface = current_keyboard_surface.clone();
- let event_queues = windows_event_queues.clone();
- mkbd.set_key_action(move |state, _, _, _, keycode, keystate| {
- let kstate = match keystate {
- KeyState::Released => ElementState::Released,
- KeyState::Pressed => ElementState::Pressed
- };
- let mut events = Vec::new();
- // key event
- events.push(Event::KeyboardInput(
- kstate,
- (keycode & 0xff) as u8,
- keycode_to_vkey(state, keycode)
- ));
- // utf8 events
- if kstate == ElementState::Pressed {
- if let Some(txt) = state.get_utf8(keycode) {
- events.extend(
- txt.chars().map(Event::ReceivedCharacter)
- );
- }
- }
- // dispatch to the appropriate queue
- let sid = *current_surface.lock().unwrap();
- if let Some(sid) = sid {
- let map = event_queues.lock().unwrap();
- if let Some(queue) = map.get(&sid) {
- queue.lock().unwrap().extend(events.into_iter());
- }
- }
- });
- AnyKeyboard::XKB(mkbd)
- },
- Err(mut rkbd) => {
- // fallback to raw inputs, no virtual keycodes
- let current_surface = current_keyboard_surface.clone();
- let event_queues = windows_event_queues.clone();
- rkbd.set_key_action(move |_, _, _, keycode, keystate| {
- let kstate = match keystate {
- KeyState::Released => ElementState::Released,
- KeyState::Pressed => ElementState::Pressed
- };
- let event = Event::KeyboardInput(kstate, (keycode & 0xff) as u8, None);
- // dispatch to the appropriate queue
- let sid = *current_surface.lock().unwrap();
- if let Some(sid) = sid {
- let map = event_queues.lock().unwrap();
- if let Some(queue) = map.get(&sid) {
- queue.lock().unwrap().push_back(event);
- }
- }
- });
- AnyKeyboard::RawKeyBoard(rkbd)
+ pub fn display_ptr(&self) -> *const c_void {
+ self.inner.display.ptr() as *const _
+ }
+
+ pub fn dispatch_events(&self) {
+ self.inner.display.dispatch_pending().unwrap();
+ let mut iterator = self.iterator.lock().unwrap();
+ let mut focuses = self.focuses.lock().unwrap();
+ let known_surfaces = self.known_surfaces.lock().unwrap();
+ let queues = self.queues.lock().unwrap();
+ // first, keyboard events
+ let kdb_evts = super::keyboard::translate_kbd_events(&mut *focuses, &known_surfaces);
+ for (evt, id) in kdb_evts {
+ if let Some(q) = queues.get(&id) {
+ q.lock().unwrap().push_back(evt);
+ }
+ }
+ // then, the rest
+ for evt in &mut *iterator {
+ if let Some((evt, id)) = super::events::translate_event(
+ evt, &mut *focuses, &known_surfaces,
+ self.inner.seat.as_ref().map(|s| &s.0))
+ {
+ if let Some(q) = queues.get(&id) {
+ q.lock().unwrap().push_back(evt);
}
- };
- keyboard = Some(kbd);
+ }
}
+ }
- Some(WaylandContext {
- display: display,
- registry: registry,
- compositor: compositor,
- shell: shell,
- seat: seat,
- pointer: pointer.map(|p| Mutex::new(p)),
- keyboard: keyboard,
- windows_event_queues: windows_event_queues,
- current_pointer_surface: current_pointer_surface,
- current_keyboard_surface: current_keyboard_surface,
- outputs: outputs
- })
+ pub fn flush_events(&self) -> ::std::io::Result<i32> {
+ self.inner.display.flush()
}
- pub fn register_surface(&self, sid: SurfaceId, queue: Arc<Mutex<VecDeque<Event>>>) {
- self.windows_event_queues.lock().unwrap().insert(sid, queue);
- if let Some(ref p) = self.pointer {
- p.lock().unwrap().add_handled_surface(sid);
- }
+ pub fn read_events(&self) -> ::std::io::Result<Option<i32>> {
+ let guard = match self.inner.display.prepare_read() {
+ Some(g) => g,
+ None => return Ok(None)
+ };
+ return guard.read_events().map(|i| Some(i));
+ }
+
+ pub fn monitor_ids(&self) -> Vec<ProxyId> {
+ self.monitors.iter().map(|o| o.0.id()).collect()
}
- pub fn deregister_surface(&self, sid: SurfaceId) {
- self.windows_event_queues.lock().unwrap().remove(&sid);
- if let Some(ref p) = self.pointer {
- p.lock().unwrap().remove_handled_surface(sid);
+ pub fn monitor_name(&self, pid: ProxyId) -> Option<String> {
+ for o in &self.monitors {
+ if o.0.id() == pid {
+ return Some(o.3.clone())
+ }
}
+ None
}
- pub fn push_event_for(&self, sid: SurfaceId, evt: Event) {
- let mut guard = self.windows_event_queues.lock().unwrap();
- if let Some(queue) = guard.get(&sid) {
- queue.lock().unwrap().push_back(evt);
+ pub fn monitor_dimensions(&self, pid: ProxyId) -> Option<(u32, u32)> {
+ for o in &self.monitors {
+ if o.0.id() == pid {
+ return Some((o.1, o.2))
+ }
}
+ None
}
}