mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-23 22:01:31 +11:00
Merge pull request #674 from vberger/master
Wayland backend rewrite and activation.
This commit is contained in:
commit
f76aba3d37
36
Cargo.toml
36
Cargo.toml
|
@ -82,42 +82,42 @@ dwmapi-sys = "0.1"
|
|||
|
||||
[target.i686-unknown-linux-gnu.dependencies]
|
||||
osmesa-sys = "0.0.5"
|
||||
wayland-client = { version = "0.2.1", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.2.0"
|
||||
wayland-window = "0.1.0"
|
||||
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.3.3"
|
||||
wayland-window = "0.2.2"
|
||||
x11-dl = "~2.2"
|
||||
|
||||
[target.x86_64-unknown-linux-gnu.dependencies]
|
||||
osmesa-sys = "0.0.5"
|
||||
wayland-client = { version = "0.2.1", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.2.0"
|
||||
wayland-window = "0.1.0"
|
||||
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.3.3"
|
||||
wayland-window = "0.2.2"
|
||||
x11-dl = "~2.2"
|
||||
|
||||
[target.arm-unknown-linux-gnueabihf.dependencies]
|
||||
osmesa-sys = "0.0.5"
|
||||
wayland-client = { version = "0.2.1", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.2.0"
|
||||
wayland-window = "0.1.0"
|
||||
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.3.3"
|
||||
wayland-window = "0.2.2"
|
||||
x11-dl = "~2.2"
|
||||
|
||||
[target.aarch64-unknown-linux-gnu.dependencies]
|
||||
osmesa-sys = "0.0.5"
|
||||
wayland-client = { version = "0.2.1", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.2.0"
|
||||
wayland-window = "0.1.0"
|
||||
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.3.3"
|
||||
wayland-window = "0.2.2"
|
||||
x11-dl = "~2.2"
|
||||
|
||||
[target.x86_64-unknown-dragonfly.dependencies]
|
||||
osmesa-sys = "0.0.5"
|
||||
wayland-client = { version = "0.2.1", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.2.0"
|
||||
wayland-window = "0.1.0"
|
||||
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.3.3"
|
||||
wayland-window = "0.2.2"
|
||||
x11-dl = "~2.2"
|
||||
|
||||
[target.x86_64-unknown-freebsd.dependencies]
|
||||
osmesa-sys = "0.0.5"
|
||||
wayland-client = { version = "0.2.1", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.2.0"
|
||||
wayland-window = "0.1.0"
|
||||
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
|
||||
wayland-kbd = "0.3.3"
|
||||
wayland-window = "0.2.2"
|
||||
x11-dl = "~2.2"
|
||||
|
|
|
@ -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;
|
||||
|
||||
enum AnyKeyboard {
|
||||
RawKeyBoard(Keyboard),
|
||||
XKB(MappedKeyboard)
|
||||
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;
|
||||
|
||||
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,
|
||||
fn init() -> Option<WaylandContext> {
|
||||
let display = match get_display() {
|
||||
Some(display) => display,
|
||||
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,
|
||||
};
|
||||
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));
|
||||
let (mut inner_env, iterator) = InnerEnv::init(display);
|
||||
|
||||
// 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()
|
||||
));
|
||||
let mut outputs_events = EventIterator::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 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();
|
||||
|
||||
// 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();
|
||||
inner_env.display.sync_roundtrip().unwrap();
|
||||
|
||||
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)
|
||||
}
|
||||
};
|
||||
keyboard = Some(kbd);
|
||||
}
|
||||
super::monitor::init_monitors(&mut monitors, outputs_events);
|
||||
|
||||
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
|
||||
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 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 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 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 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
|
||||
}
|
||||
}
|
||||
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn flush_events(&self) -> ::std::io::Result<i32> {
|
||||
self.inner.display.flush()
|
||||
}
|
||||
|
||||
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 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 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
|
||||
}
|
||||
}
|
||||
|
|
110
src/api/wayland/events.rs
Normal file
110
src/api/wayland/events.rs
Normal file
|
@ -0,0 +1,110 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
use Event as GlutinEvent;
|
||||
use ElementState;
|
||||
use MouseButton;
|
||||
use MouseScrollDelta;
|
||||
|
||||
use wayland_client::Event as WaylandEvent;
|
||||
use wayland_client::ProxyId;
|
||||
use wayland_client::wayland::WaylandProtocolEvent as WPE;
|
||||
use wayland_client::wayland::seat::{WlSeat, WlSeatEvent, WlPointerEvent,
|
||||
WlPointerButtonState,
|
||||
WlPointerAxis, WlSeatCapability};
|
||||
|
||||
use super::wayland_kbd::MappedKeyboard;
|
||||
|
||||
use super::context::WaylandFocuses;
|
||||
|
||||
pub fn translate_event(
|
||||
evt: WaylandEvent,
|
||||
focuses: &mut WaylandFocuses,
|
||||
known_surfaces: &HashSet<ProxyId>,
|
||||
seat: Option<&WlSeat>,
|
||||
) -> Option<(GlutinEvent, ProxyId)>
|
||||
{
|
||||
let WaylandEvent::Wayland(wayland_evt) = evt;
|
||||
match wayland_evt {
|
||||
WPE::WlSeat(_, seat_evt) => match seat_evt {
|
||||
WlSeatEvent::Capabilities(cap) => {
|
||||
if cap.contains(WlSeatCapability::Pointer) && focuses.pointer.is_none() {
|
||||
if let Some(seat) = seat {
|
||||
focuses.pointer = Some(seat.get_pointer());
|
||||
}
|
||||
}
|
||||
if cap.contains(WlSeatCapability::Keyboard) && focuses.keyboard.is_none() {
|
||||
if let Some(seat) = seat {
|
||||
match MappedKeyboard::new(seat) {
|
||||
Ok(mk) => {
|
||||
focuses.keyboard = Some(mk)
|
||||
},
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
},
|
||||
_ => None
|
||||
},
|
||||
WPE::WlPointer(_, pointer_evt) => match pointer_evt {
|
||||
WlPointerEvent::Enter(_, surface, x, y) => {
|
||||
if known_surfaces.contains(&surface) {
|
||||
focuses.pointer_on = Some(surface);
|
||||
focuses.pointer_at = Some((x, y));
|
||||
Some((GlutinEvent::MouseMoved((x as i32, y as i32)), surface))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
WlPointerEvent::Leave(_, _) => {
|
||||
focuses.pointer_on = None;
|
||||
focuses.pointer_at = None;
|
||||
None
|
||||
}
|
||||
WlPointerEvent::Motion(_, x, y) => {
|
||||
if let Some(surface) = focuses.pointer_on {
|
||||
focuses.pointer_at = Some((x, y));
|
||||
Some((GlutinEvent::MouseMoved((x as i32, y as i32)), surface))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
WlPointerEvent::Button(_, _, button, state) => {
|
||||
if let Some(surface) = focuses.pointer_on {
|
||||
Some((GlutinEvent::MouseInput(
|
||||
match state {
|
||||
WlPointerButtonState::Pressed => ElementState::Pressed,
|
||||
WlPointerButtonState::Released => ElementState::Released
|
||||
},
|
||||
match button {
|
||||
0x110 => MouseButton::Left,
|
||||
0x111 => MouseButton::Right,
|
||||
0x112 => MouseButton::Middle,
|
||||
// TODO figure out the translation ?
|
||||
_ => return None
|
||||
}
|
||||
), surface))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
WlPointerEvent::Axis(_, axis, amplitude) => {
|
||||
if let Some(surface) = focuses.pointer_on {
|
||||
Some((GlutinEvent::MouseWheel(
|
||||
match axis {
|
||||
WlPointerAxis::VerticalScroll => {
|
||||
MouseScrollDelta::PixelDelta(amplitude as f32, 0.0)
|
||||
}
|
||||
WlPointerAxis::HorizontalScroll => {
|
||||
MouseScrollDelta::PixelDelta(0.0, amplitude as f32)
|
||||
}
|
||||
}
|
||||
), surface))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
}
|
|
@ -1,26 +1,85 @@
|
|||
use super::wayland_kbd::{KbState, keysyms};
|
||||
use std::collections::HashSet;
|
||||
|
||||
use Event as GlutinEvent;
|
||||
use ElementState;
|
||||
use VirtualKeyCode;
|
||||
|
||||
pub fn keycode_to_vkey(state: &KbState, keycode: u32) -> Option<VirtualKeyCode> {
|
||||
// first line is hard-coded because it must be case insensitive
|
||||
// and is a linux constant anyway
|
||||
match keycode {
|
||||
1 => return Some(VirtualKeyCode::Escape),
|
||||
2 => return Some(VirtualKeyCode::Key1),
|
||||
3 => return Some(VirtualKeyCode::Key2),
|
||||
4 => return Some(VirtualKeyCode::Key3),
|
||||
5 => return Some(VirtualKeyCode::Key4),
|
||||
6 => return Some(VirtualKeyCode::Key5),
|
||||
7 => return Some(VirtualKeyCode::Key6),
|
||||
8 => return Some(VirtualKeyCode::Key7),
|
||||
9 => return Some(VirtualKeyCode::Key8),
|
||||
10 => return Some(VirtualKeyCode::Key9),
|
||||
11 => return Some(VirtualKeyCode::Key0),
|
||||
_ => {}
|
||||
use wayland_client::ProxyId;
|
||||
use wayland_client::wayland::seat::{WlKeyboardEvent,WlKeyboardKeyState};
|
||||
|
||||
use super::wayland_kbd::MappedKeyboardEvent;
|
||||
|
||||
use super::context::WaylandFocuses;
|
||||
|
||||
pub fn translate_kbd_events(
|
||||
focuses: &mut WaylandFocuses,
|
||||
known_surfaces: &HashSet<ProxyId>,
|
||||
) -> Vec<(GlutinEvent, ProxyId)> {
|
||||
let mut out = Vec::new();
|
||||
if let Some(mkbd) = focuses.keyboard.as_mut() {
|
||||
for evt in mkbd {
|
||||
match evt {
|
||||
MappedKeyboardEvent::KeyEvent(kevt) => {
|
||||
if let Some(surface) = focuses.keyboard_on {
|
||||
let vkcode = match kevt.keycode {
|
||||
1 => Some(VirtualKeyCode::Escape),
|
||||
2 => Some(VirtualKeyCode::Key1),
|
||||
3 => Some(VirtualKeyCode::Key2),
|
||||
4 => Some(VirtualKeyCode::Key3),
|
||||
5 => Some(VirtualKeyCode::Key4),
|
||||
6 => Some(VirtualKeyCode::Key5),
|
||||
7 => Some(VirtualKeyCode::Key6),
|
||||
8 => Some(VirtualKeyCode::Key7),
|
||||
9 => Some(VirtualKeyCode::Key8),
|
||||
10 => Some(VirtualKeyCode::Key9),
|
||||
11 => Some(VirtualKeyCode::Key0),
|
||||
_ => kevt.as_symbol().and_then(keysym_to_vkey)
|
||||
};
|
||||
let text = kevt.as_utf8();
|
||||
out.push((
|
||||
GlutinEvent::KeyboardInput(
|
||||
match kevt.keystate {
|
||||
WlKeyboardKeyState::Pressed => ElementState::Pressed,
|
||||
WlKeyboardKeyState::Released =>ElementState::Released
|
||||
},
|
||||
(kevt.keycode & 0xff) as u8,
|
||||
vkcode
|
||||
),
|
||||
surface
|
||||
));
|
||||
if let Some(c) = text.and_then(|s| s.chars().next()) {
|
||||
out.push((
|
||||
GlutinEvent::ReceivedCharacter(c),
|
||||
surface
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
MappedKeyboardEvent::Other(oevt) => match oevt {
|
||||
WlKeyboardEvent::Enter(_, surface, _) => {
|
||||
if known_surfaces.contains(&surface) {
|
||||
focuses.keyboard_on = Some(surface);
|
||||
out.push((GlutinEvent::Focused(true), surface));
|
||||
}
|
||||
},
|
||||
WlKeyboardEvent::Leave(_, surface) => {
|
||||
if known_surfaces.contains(&surface) {
|
||||
focuses.keyboard_on = None;
|
||||
out.push((GlutinEvent::Focused(false), surface));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// for other keys, we use the keysym
|
||||
return match state.get_one_sym(keycode) {
|
||||
out
|
||||
}
|
||||
|
||||
pub fn keysym_to_vkey(keysym: u32) -> Option<VirtualKeyCode> {
|
||||
use super::wayland_kbd::keysyms;
|
||||
match keysym {
|
||||
// letters
|
||||
keysyms::XKB_KEY_A | keysyms::XKB_KEY_a => Some(VirtualKeyCode::A),
|
||||
keysyms::XKB_KEY_B | keysyms::XKB_KEY_b => Some(VirtualKeyCode::B),
|
||||
|
|
|
@ -1,495 +1,18 @@
|
|||
#![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"))]
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
use self::wayland::egl::{EGLSurface, is_egl_available};
|
||||
use self::wayland::core::Surface;
|
||||
use self::wayland::core::output::Output;
|
||||
use self::wayland::core::shell::{ShellSurface, ShellFullscreenMethod};
|
||||
pub use self::monitor::{MonitorId, get_available_monitors, get_primary_monitor};
|
||||
pub use self::window::{PollEventsIterator, WaitEventsIterator, Window, WindowProxy};
|
||||
|
||||
use self::wayland_window::{DecoratedSurface, SurfaceGuard, substract_borders};
|
||||
|
||||
use libc;
|
||||
use api::dlopen;
|
||||
use api::egl;
|
||||
use api::egl::Context as EglContext;
|
||||
|
||||
use ContextError;
|
||||
use CreationError;
|
||||
use Event;
|
||||
use PixelFormat;
|
||||
use CursorState;
|
||||
use MouseCursor;
|
||||
use GlAttributes;
|
||||
use GlContext;
|
||||
use PixelFormatRequirements;
|
||||
use WindowAttributes;
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::ffi::CString;
|
||||
|
||||
use platform::MonitorId as PlatformMonitorId;
|
||||
|
||||
use self::context::WaylandContext;
|
||||
|
||||
extern crate wayland_client as wayland;
|
||||
extern crate wayland_kbd;
|
||||
extern crate wayland_window;
|
||||
|
||||
mod context;
|
||||
mod events;
|
||||
mod keyboard;
|
||||
|
||||
lazy_static! {
|
||||
static ref WAYLAND_CONTEXT: Option<WaylandContext> = {
|
||||
WaylandContext::new()
|
||||
};
|
||||
}
|
||||
mod monitor;
|
||||
mod window;
|
||||
|
||||
#[inline]
|
||||
pub fn is_available() -> bool {
|
||||
WAYLAND_CONTEXT.is_some()
|
||||
}
|
||||
|
||||
enum ShellWindow {
|
||||
Plain(ShellSurface<EGLSurface>),
|
||||
Decorated(DecoratedSurface<EGLSurface>)
|
||||
}
|
||||
|
||||
impl ShellWindow {
|
||||
#[inline]
|
||||
fn get_shell(&mut self) -> ShellGuard {
|
||||
match self {
|
||||
&mut ShellWindow::Plain(ref mut s) => {
|
||||
ShellGuard::Plain(s)
|
||||
},
|
||||
&mut ShellWindow::Decorated(ref mut s) => {
|
||||
ShellGuard::Decorated(s.get_shell())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn resize(&mut self, w: i32, h: i32, x: i32, y: i32) {
|
||||
match self {
|
||||
&mut ShellWindow::Plain(ref s) => s.resize(w, h, x, y),
|
||||
&mut ShellWindow::Decorated(ref mut s) => {
|
||||
s.resize(w, h);
|
||||
s.get_shell().resize(w, h, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_cfg_callback(&mut self, arc: Arc<Mutex<(i32, i32, bool)>>) {
|
||||
match self {
|
||||
&mut ShellWindow::Decorated(ref mut s) => {
|
||||
s.get_shell().set_configure_callback(move |_, w, h| {
|
||||
let (w, h) = substract_borders(w, h);
|
||||
let mut guard = arc.lock().unwrap();
|
||||
*guard = (w, h, true);
|
||||
})
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum ShellGuard<'a> {
|
||||
Plain(&'a mut ShellSurface<EGLSurface>),
|
||||
Decorated(SurfaceGuard<'a, EGLSurface>)
|
||||
}
|
||||
|
||||
impl<'a> Deref for ShellGuard<'a> {
|
||||
type Target = ShellSurface<EGLSurface>;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &ShellSurface<EGLSurface> {
|
||||
match self {
|
||||
&ShellGuard::Plain(ref s) => s,
|
||||
&ShellGuard::Decorated(ref s) => s.deref()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DerefMut for ShellGuard<'a> {
|
||||
#[inline]
|
||||
fn deref_mut(&mut self) -> &mut ShellSurface<EGLSurface> {
|
||||
match self {
|
||||
&mut ShellGuard::Plain(ref mut s) => s,
|
||||
&mut ShellGuard::Decorated(ref mut s) => s.deref_mut()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Window {
|
||||
shell_window: Mutex<ShellWindow>,
|
||||
pending_events: Arc<Mutex<VecDeque<Event>>>,
|
||||
need_resize: Arc<Mutex<(i32, i32, bool)>>,
|
||||
resize_callback: Option<fn(u32, u32)>,
|
||||
pub context: EglContext,
|
||||
}
|
||||
|
||||
// private methods of wayalnd windows
|
||||
|
||||
impl Window {
|
||||
fn resize_if_needed(&self) -> bool {
|
||||
let mut guard = self.need_resize.lock().unwrap();
|
||||
let (w, h, b) = *guard;
|
||||
*guard = (0, 0, false);
|
||||
if b {
|
||||
let mut guard = self.shell_window.lock().unwrap();
|
||||
guard.resize(w, h, 0, 0);
|
||||
if let Some(f) = self.resize_callback {
|
||||
f(w as u32, h as u32);
|
||||
}
|
||||
if let Some(ref ctxt) = *WAYLAND_CONTEXT {
|
||||
let mut window_guard = self.shell_window.lock().unwrap();
|
||||
ctxt.push_event_for(
|
||||
window_guard.get_shell().get_wsurface().get_id(),
|
||||
Event::Resized(w as u32, h as u32)
|
||||
);
|
||||
}
|
||||
}
|
||||
b
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WindowProxy;
|
||||
|
||||
impl WindowProxy {
|
||||
#[inline]
|
||||
pub fn wakeup_event_loop(&self) {
|
||||
if let Some(ref ctxt) = *WAYLAND_CONTEXT {
|
||||
ctxt.display.sync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MonitorId {
|
||||
output: Arc<Output>
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_available_monitors() -> VecDeque<MonitorId> {
|
||||
WAYLAND_CONTEXT.as_ref().unwrap().outputs.iter().map(|o| MonitorId::new(o.clone())).collect()
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_primary_monitor() -> MonitorId {
|
||||
match WAYLAND_CONTEXT.as_ref().unwrap().outputs.iter().next() {
|
||||
Some(o) => MonitorId::new(o.clone()),
|
||||
None => panic!("No monitor is available.")
|
||||
}
|
||||
}
|
||||
|
||||
impl MonitorId {
|
||||
fn new(output: Arc<Output>) -> MonitorId {
|
||||
MonitorId {
|
||||
output: output
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_name(&self) -> Option<String> {
|
||||
Some(format!("{} - {}", self.output.manufacturer(), self.output.model()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_native_identifier(&self) -> ::native_monitor::NativeMonitorId {
|
||||
::native_monitor::NativeMonitorId::Unavailable
|
||||
}
|
||||
|
||||
pub fn get_dimensions(&self) -> (u32, u32) {
|
||||
let (w, h) = self.output.modes()
|
||||
.into_iter()
|
||||
.find(|m| m.is_current())
|
||||
.map(|m| (m.width, m.height))
|
||||
.unwrap();
|
||||
(w as u32, h as u32)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct PollEventsIterator<'a> {
|
||||
window: &'a Window,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for PollEventsIterator<'a> {
|
||||
type Item = Event;
|
||||
|
||||
fn next(&mut self) -> Option<Event> {
|
||||
if let Some(ref ctxt) = *WAYLAND_CONTEXT {
|
||||
ctxt.display.dispatch_pending();
|
||||
}
|
||||
if self.window.resize_if_needed() {
|
||||
Some(Event::Refresh)
|
||||
} else {
|
||||
self.window.pending_events.lock().unwrap().pop_front()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WaitEventsIterator<'a> {
|
||||
window: &'a Window,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for WaitEventsIterator<'a> {
|
||||
type Item = Event;
|
||||
|
||||
fn next(&mut self) -> Option<Event> {
|
||||
let mut evt = None;
|
||||
while evt.is_none() {
|
||||
if let Some(ref ctxt) = *WAYLAND_CONTEXT {
|
||||
ctxt.display.dispatch();
|
||||
}
|
||||
evt = if self.window.resize_if_needed() {
|
||||
Some(Event::Refresh)
|
||||
} else {
|
||||
self.window.pending_events.lock().unwrap().pop_front()
|
||||
};
|
||||
}
|
||||
evt
|
||||
}
|
||||
}
|
||||
|
||||
impl Window {
|
||||
pub fn new(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
|
||||
opengl: &GlAttributes<&Window>) -> Result<Window, CreationError>
|
||||
{
|
||||
use self::wayland::internals::FFI;
|
||||
|
||||
// not implemented
|
||||
assert!(window.min_dimensions.is_none());
|
||||
assert!(window.max_dimensions.is_none());
|
||||
|
||||
let wayland_context = match *WAYLAND_CONTEXT {
|
||||
Some(ref c) => c,
|
||||
None => return Err(CreationError::NotSupported),
|
||||
};
|
||||
|
||||
if !is_egl_available() { return Err(CreationError::NotSupported) }
|
||||
|
||||
let (w, h) = window.dimensions.unwrap_or((800, 600));
|
||||
|
||||
let surface = EGLSurface::new(
|
||||
wayland_context.compositor.create_surface(),
|
||||
w as i32,
|
||||
h as i32
|
||||
);
|
||||
|
||||
let mut shell_window = if let Some(PlatformMonitorId::Wayland(ref monitor)) = window.monitor {
|
||||
let shell_surface = wayland_context.shell.get_shell_surface(surface);
|
||||
shell_surface.set_fullscreen(ShellFullscreenMethod::Default, Some(&monitor.output));
|
||||
ShellWindow::Plain(shell_surface)
|
||||
} else {
|
||||
if window.decorations {
|
||||
ShellWindow::Decorated(match DecoratedSurface::new(
|
||||
surface,
|
||||
w as i32,
|
||||
h as i32,
|
||||
&wayland_context.registry,
|
||||
Some(&wayland_context.seat)
|
||||
) {
|
||||
Ok(s) => s,
|
||||
Err(_) => return Err(CreationError::NotSupported)
|
||||
})
|
||||
} else {
|
||||
ShellWindow::Plain(wayland_context.shell.get_shell_surface(surface))
|
||||
}
|
||||
};
|
||||
|
||||
let context = {
|
||||
let libegl = unsafe { dlopen::dlopen(b"libEGL.so\0".as_ptr() as *const _, dlopen::RTLD_NOW) };
|
||||
if libegl.is_null() {
|
||||
return Err(CreationError::NotSupported);
|
||||
}
|
||||
let egl = ::api::egl::ffi::egl::Egl::load_with(|sym| {
|
||||
let sym = CString::new(sym).unwrap();
|
||||
unsafe { dlopen::dlsym(libegl, sym.as_ptr()) }
|
||||
});
|
||||
try!(EglContext::new(
|
||||
egl,
|
||||
pf_reqs, &opengl.clone().map_sharing(|_| unimplemented!()), // TODO:
|
||||
egl::NativeDisplay::Wayland(Some(wayland_context.display.ptr() as *const _)))
|
||||
.and_then(|p| p.finish((**shell_window.get_shell()).ptr() as *const _))
|
||||
)
|
||||
};
|
||||
|
||||
// create a queue already containing a refresh event to trigger first draw
|
||||
// it's harmless and a removes the need to do a first swap_buffers() before
|
||||
// starting the event loop
|
||||
let events = Arc::new(Mutex::new({
|
||||
let mut v = VecDeque::new();
|
||||
v.push_back(Event::Refresh);
|
||||
v
|
||||
}));
|
||||
|
||||
wayland_context.register_surface(shell_window.get_shell().get_wsurface().get_id(),
|
||||
events.clone());
|
||||
|
||||
let need_resize = Arc::new(Mutex::new((0, 0, false)));
|
||||
|
||||
shell_window.set_cfg_callback(need_resize.clone());
|
||||
|
||||
wayland_context.display.flush().unwrap();
|
||||
|
||||
Ok(Window {
|
||||
shell_window: Mutex::new(shell_window),
|
||||
pending_events: events,
|
||||
need_resize: need_resize,
|
||||
resize_callback: None,
|
||||
context: context
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_title(&self, title: &str) {
|
||||
let ctitle = CString::new(title).unwrap();
|
||||
// intermediate variable is forced,
|
||||
// see https://github.com/rust-lang/rust/issues/22921
|
||||
let mut guard = self.shell_window.lock().unwrap();
|
||||
guard.get_shell().set_title(&ctitle);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn show(&self) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn hide(&self) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_position(&self) -> Option<(i32, i32)> {
|
||||
// not available with wayland
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_position(&self, _x: i32, _y: i32) {
|
||||
// not available with wayland
|
||||
}
|
||||
|
||||
pub fn get_inner_size(&self) -> Option<(u32, u32)> {
|
||||
// intermediate variables are forced,
|
||||
// see https://github.com/rust-lang/rust/issues/22921
|
||||
let mut guard = self.shell_window.lock().unwrap();
|
||||
let shell = guard.get_shell();
|
||||
let (w, h) = shell.get_attached_size();
|
||||
Some((w as u32, h as u32))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_outer_size(&self) -> Option<(u32, u32)> {
|
||||
// maybe available if we draw the border ourselves ?
|
||||
// but for now, no.
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_inner_size(&self, x: u32, y: u32) {
|
||||
self.shell_window.lock().unwrap().resize(x as i32, y as i32, 0, 0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn create_window_proxy(&self) -> WindowProxy {
|
||||
WindowProxy
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn poll_events(&self) -> PollEventsIterator {
|
||||
PollEventsIterator {
|
||||
window: self
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn wait_events(&self) -> WaitEventsIterator {
|
||||
WaitEventsIterator {
|
||||
window: self
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_window_resize_callback(&mut self, callback: Option<fn(u32, u32)>) {
|
||||
self.resize_callback = callback;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_cursor(&self, cursor: MouseCursor) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
|
||||
// TODO
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn hidpi_factor(&self) -> f32 {
|
||||
1.0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> {
|
||||
// TODO
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn platform_display(&self) -> *mut libc::c_void {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn platform_window(&self) -> *mut libc::c_void {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl GlContext for Window {
|
||||
#[inline]
|
||||
unsafe fn make_current(&self) -> Result<(), ContextError> {
|
||||
self.context.make_current()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_current(&self) -> bool {
|
||||
self.context.is_current()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_proc_address(&self, addr: &str) -> *const () {
|
||||
self.context.get_proc_address(addr)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn swap_buffers(&self) -> Result<(), ContextError> {
|
||||
self.context.swap_buffers()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_api(&self) -> ::Api {
|
||||
self.context.get_api()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_pixel_format(&self) -> PixelFormat {
|
||||
self.context.get_pixel_format().clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Window {
|
||||
fn drop(&mut self) {
|
||||
if let Some(ref ctxt) = *WAYLAND_CONTEXT {
|
||||
// intermediate variable is forced,
|
||||
// see https://github.com/rust-lang/rust/issues/22921
|
||||
let mut guard = self.shell_window.lock().unwrap();
|
||||
let shell = guard.get_shell();
|
||||
ctxt.deregister_surface(
|
||||
shell.get_wsurface().get_id()
|
||||
)
|
||||
}
|
||||
}
|
||||
context::WAYLAND_CONTEXT.is_some()
|
||||
}
|
||||
|
|
75
src/api/wayland/monitor.rs
Normal file
75
src/api/wayland/monitor.rs
Normal file
|
@ -0,0 +1,75 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use wayland_client::{ProxyId, EventIterator};
|
||||
use wayland_client::wayland::output::WlOutput;
|
||||
|
||||
use super::context::WAYLAND_CONTEXT;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MonitorId(ProxyId);
|
||||
|
||||
#[inline]
|
||||
pub fn get_available_monitors() -> VecDeque<MonitorId> {
|
||||
WAYLAND_CONTEXT.as_ref().map(|ctxt|
|
||||
ctxt.monitor_ids().into_iter().map(MonitorId).collect()
|
||||
).unwrap_or(VecDeque::new())
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_primary_monitor() -> MonitorId {
|
||||
WAYLAND_CONTEXT.as_ref().and_then(|ctxt|
|
||||
ctxt.monitor_ids().into_iter().next().map(MonitorId)
|
||||
).expect("wayland: No monitor available.")
|
||||
}
|
||||
|
||||
impl MonitorId {
|
||||
pub fn get_name(&self) -> Option<String> {
|
||||
WAYLAND_CONTEXT.as_ref().and_then(|ctxt| ctxt.monitor_name(self.0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_native_identifier(&self) -> ::native_monitor::NativeMonitorId {
|
||||
::native_monitor::NativeMonitorId::Unavailable
|
||||
}
|
||||
|
||||
pub fn get_dimensions(&self) -> (u32, u32) {
|
||||
WAYLAND_CONTEXT.as_ref().and_then(|ctxt| ctxt.monitor_dimensions(self.0)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn proxid_from_monitorid(x: &MonitorId) -> ProxyId {
|
||||
x.0
|
||||
}
|
||||
|
||||
pub fn init_monitors(outputs: &mut Vec<(WlOutput, u32, u32, String)>, evts: EventIterator) {
|
||||
use wayland_client::{Event, Proxy};
|
||||
use wayland_client::wayland::WaylandProtocolEvent;
|
||||
use wayland_client::wayland::output::{WlOutputEvent, WlOutputMode};
|
||||
|
||||
for evt in evts {
|
||||
match evt {
|
||||
Event::Wayland(WaylandProtocolEvent::WlOutput(pid, oevt)) => match oevt {
|
||||
WlOutputEvent::Geometry(_, _, _, _, _, maker, model, _) => {
|
||||
for o in outputs.iter_mut() {
|
||||
if o.0.id() == pid {
|
||||
o.3 = format!("{} - {}", maker, model);
|
||||
break
|
||||
}
|
||||
}
|
||||
},
|
||||
WlOutputEvent::Mode(flags, width, height, _) => {
|
||||
if flags.contains(WlOutputMode::Current) {
|
||||
for o in outputs.iter_mut() {
|
||||
if o.0.id() == pid {
|
||||
o.1 = width as u32;
|
||||
o.2 = height as u32;
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
370
src/api/wayland/window.rs
Normal file
370
src/api/wayland/window.rs
Normal file
|
@ -0,0 +1,370 @@
|
|||
use std::collections::VecDeque;
|
||||
use std::ffi::CString;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use libc;
|
||||
|
||||
use {ContextError, CreationError, CursorState, Event, GlAttributes, GlContext,
|
||||
MouseCursor, PixelFormat, PixelFormatRequirements, WindowAttributes};
|
||||
use api::dlopen;
|
||||
use api::egl;
|
||||
use api::egl::Context as EglContext;
|
||||
use platform::MonitorId as PlatformMonitorId;
|
||||
|
||||
use wayland_client::EventIterator;
|
||||
use wayland_client::egl as wegl;
|
||||
use wayland_client::wayland::shell::WlShellSurface;
|
||||
use super::wayland_window::{DecoratedSurface, add_borders, substract_borders};
|
||||
use super::context::{WaylandContext, WAYLAND_CONTEXT};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WindowProxy;
|
||||
|
||||
impl WindowProxy {
|
||||
#[inline]
|
||||
pub fn wakeup_event_loop(&self) {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Window {
|
||||
wayland_context: &'static WaylandContext,
|
||||
egl_surface: wegl::WlEglSurface,
|
||||
shell_window: Mutex<ShellWindow>,
|
||||
evt_queue: Arc<Mutex<VecDeque<Event>>>,
|
||||
inner_size: Mutex<(i32, i32)>,
|
||||
resize_callback: Option<fn(u32, u32)>,
|
||||
pub context: EglContext,
|
||||
}
|
||||
|
||||
impl Window {
|
||||
fn next_event(&self) -> Option<Event> {
|
||||
use wayland_client::Event as WEvent;
|
||||
use wayland_client::wayland::WaylandProtocolEvent;
|
||||
use wayland_client::wayland::shell::WlShellSurfaceEvent;
|
||||
|
||||
let mut newsize = None;
|
||||
let mut evt_queue_guard = self.evt_queue.lock().unwrap();
|
||||
|
||||
let mut shell_window_guard = self.shell_window.lock().unwrap();
|
||||
match *shell_window_guard {
|
||||
ShellWindow::Decorated(ref mut deco) => {
|
||||
for (_, w, h) in deco {
|
||||
newsize = Some((w, h));
|
||||
}
|
||||
},
|
||||
ShellWindow::Plain(ref plain, ref mut evtiter) => {
|
||||
for evt in evtiter {
|
||||
if let WEvent::Wayland(WaylandProtocolEvent::WlShellSurface(_, ssevt)) = evt {
|
||||
match ssevt {
|
||||
WlShellSurfaceEvent::Ping(u) => {
|
||||
plain.pong(u);
|
||||
},
|
||||
WlShellSurfaceEvent::Configure(_, w, h) => {
|
||||
newsize = Some((w, h));
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((w, h)) = newsize {
|
||||
let (w, h) = substract_borders(w, h);
|
||||
*self.inner_size.lock().unwrap() = (w, h);
|
||||
if let ShellWindow::Decorated(ref mut deco) = *shell_window_guard {
|
||||
deco.resize(w, h);
|
||||
}
|
||||
self.egl_surface.resize(w, h, 0, 0);
|
||||
if let Some(f) = self.resize_callback {
|
||||
f(w as u32, h as u32);
|
||||
}
|
||||
Some(Event::Resized(w as u32, h as u32))
|
||||
} else {
|
||||
evt_queue_guard.pop_front()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PollEventsIterator<'a> {
|
||||
window: &'a Window,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for PollEventsIterator<'a> {
|
||||
type Item = Event;
|
||||
|
||||
fn next(&mut self) -> Option<Event> {
|
||||
match self.window.next_event() {
|
||||
Some(evt) => return Some(evt),
|
||||
None => {}
|
||||
}
|
||||
// the queue was empty, try a dispatch and see the result
|
||||
self.window.wayland_context.dispatch_events();
|
||||
return self.window.next_event();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WaitEventsIterator<'a> {
|
||||
window: &'a Window,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for WaitEventsIterator<'a> {
|
||||
type Item = Event;
|
||||
|
||||
fn next(&mut self) -> Option<Event> {
|
||||
loop {
|
||||
match self.window.next_event() {
|
||||
Some(evt) => return Some(evt),
|
||||
None => {}
|
||||
}
|
||||
// the queue was empty, try a dispatch & read and see the result
|
||||
self.window.wayland_context.flush_events().expect("Connexion with the wayland compositor lost.");
|
||||
match self.window.wayland_context.read_events() {
|
||||
Ok(_) => {
|
||||
// events were read or dispatch is needed, in both cases, we dispatch
|
||||
self.window.wayland_context.dispatch_events()
|
||||
}
|
||||
Err(_) => panic!("Connexion with the wayland compositor lost.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum ShellWindow {
|
||||
Plain(WlShellSurface, EventIterator),
|
||||
Decorated(DecoratedSurface)
|
||||
}
|
||||
|
||||
impl Window {
|
||||
pub fn new(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
|
||||
opengl: &GlAttributes<&Window>) -> Result<Window, CreationError>
|
||||
{
|
||||
use wayland_client::Proxy;
|
||||
// not implemented
|
||||
assert!(window.min_dimensions.is_none());
|
||||
assert!(window.max_dimensions.is_none());
|
||||
|
||||
let wayland_context = match *WAYLAND_CONTEXT {
|
||||
Some(ref c) => c,
|
||||
None => return Err(CreationError::NotSupported),
|
||||
};
|
||||
|
||||
if !wegl::is_available() {
|
||||
return Err(CreationError::NotSupported)
|
||||
}
|
||||
|
||||
let (w, h) = window.dimensions.unwrap_or((800, 600));
|
||||
|
||||
let (surface, evt_queue) = match wayland_context.new_surface() {
|
||||
Some(t) => t,
|
||||
None => return Err(CreationError::NotSupported)
|
||||
};
|
||||
|
||||
let egl_surface = wegl::WlEglSurface::new(surface, w as i32, h as i32);
|
||||
|
||||
let context = {
|
||||
let libegl = unsafe { dlopen::dlopen(b"libEGL.so\0".as_ptr() as *const _, dlopen::RTLD_NOW) };
|
||||
if libegl.is_null() {
|
||||
return Err(CreationError::NotSupported);
|
||||
}
|
||||
let egl = ::api::egl::ffi::egl::Egl::load_with(|sym| {
|
||||
let sym = CString::new(sym).unwrap();
|
||||
unsafe { dlopen::dlsym(libegl, sym.as_ptr()) }
|
||||
});
|
||||
try!(EglContext::new(
|
||||
egl,
|
||||
pf_reqs, &opengl.clone().map_sharing(|_| unimplemented!()), // TODO:
|
||||
egl::NativeDisplay::Wayland(Some(wayland_context.display_ptr() as *const _)))
|
||||
.and_then(|p| p.finish(unsafe { egl_surface.egl_surfaceptr() } as *const _))
|
||||
)
|
||||
};
|
||||
|
||||
let shell_window = if let Some(PlatformMonitorId::Wayland(ref monitor_id)) = window.monitor {
|
||||
let pid = super::monitor::proxid_from_monitorid(monitor_id);
|
||||
match wayland_context.plain_from(&egl_surface, Some(pid)) {
|
||||
Some(mut s) => {
|
||||
let iter = EventIterator::new();
|
||||
s.set_evt_iterator(&iter);
|
||||
ShellWindow::Plain(s, iter)
|
||||
},
|
||||
None => return Err(CreationError::NotSupported)
|
||||
}
|
||||
} else if window.decorations {
|
||||
match wayland_context.decorated_from(&egl_surface, w as i32, h as i32) {
|
||||
Some(s) => ShellWindow::Decorated(s),
|
||||
None => return Err(CreationError::NotSupported)
|
||||
}
|
||||
} else {
|
||||
match wayland_context.plain_from(&egl_surface, None) {
|
||||
Some(mut s) => {
|
||||
let iter = EventIterator::new();
|
||||
s.set_evt_iterator(&iter);
|
||||
ShellWindow::Plain(s, iter)
|
||||
},
|
||||
None => return Err(CreationError::NotSupported)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Window {
|
||||
wayland_context: wayland_context,
|
||||
egl_surface: egl_surface,
|
||||
shell_window: Mutex::new(shell_window),
|
||||
evt_queue: evt_queue,
|
||||
inner_size: Mutex::new((w as i32, h as i32)),
|
||||
resize_callback: None,
|
||||
context: context
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_title(&self, title: &str) {
|
||||
let guard = self.shell_window.lock().unwrap();
|
||||
match *guard {
|
||||
ShellWindow::Plain(ref plain, _) => { plain.set_title(title.into()); },
|
||||
ShellWindow::Decorated(ref deco) => { deco.set_title(title.into()); }
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn show(&self) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn hide(&self) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_position(&self) -> Option<(i32, i32)> {
|
||||
// Not possible with wayland
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_position(&self, _x: i32, _y: i32) {
|
||||
// Not possible with wayland
|
||||
}
|
||||
|
||||
pub fn get_inner_size(&self) -> Option<(u32, u32)> {
|
||||
let (w, h) = *self.inner_size.lock().unwrap();
|
||||
Some((w as u32, h as u32))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_outer_size(&self) -> Option<(u32, u32)> {
|
||||
let (w, h) = *self.inner_size.lock().unwrap();
|
||||
let (w, h) = add_borders(w, h);
|
||||
Some((w as u32, h as u32))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_inner_size(&self, x: u32, y: u32) {
|
||||
let mut guard = self.shell_window.lock().unwrap();
|
||||
match *guard {
|
||||
ShellWindow::Decorated(ref mut deco) => { deco.resize(x as i32, y as i32); },
|
||||
_ => {}
|
||||
}
|
||||
self.egl_surface.resize(x as i32, y as i32, 0, 0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn create_window_proxy(&self) -> WindowProxy {
|
||||
WindowProxy
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn poll_events(&self) -> PollEventsIterator {
|
||||
PollEventsIterator {
|
||||
window: self
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn wait_events(&self) -> WaitEventsIterator {
|
||||
WaitEventsIterator {
|
||||
window: self
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_window_resize_callback(&mut self, callback: Option<fn(u32, u32)>) {
|
||||
self.resize_callback = callback;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_cursor(&self, _cursor: MouseCursor) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
|
||||
use CursorState::{Grab, Normal, Hide};
|
||||
// TODO : not yet possible on wayland to grab cursor
|
||||
match state {
|
||||
Grab => Err("Cursor cannot be grabbed on wayland yet.".to_string()),
|
||||
Hide => Err("Cursor cannot be hidden on wayland yet.".to_string()),
|
||||
Normal => Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn hidpi_factor(&self) -> f32 {
|
||||
1.0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_cursor_position(&self, _x: i32, _y: i32) -> Result<(), ()> {
|
||||
// TODO: not yet possible on wayland
|
||||
Err(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn platform_display(&self) -> *mut libc::c_void {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn platform_window(&self) -> *mut libc::c_void {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl GlContext for Window {
|
||||
#[inline]
|
||||
unsafe fn make_current(&self) -> Result<(), ContextError> {
|
||||
self.context.make_current()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_current(&self) -> bool {
|
||||
self.context.is_current()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_proc_address(&self, addr: &str) -> *const () {
|
||||
self.context.get_proc_address(addr)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn swap_buffers(&self) -> Result<(), ContextError> {
|
||||
self.context.swap_buffers()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_api(&self) -> ::Api {
|
||||
self.context.get_api()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_pixel_format(&self) -> PixelFormat {
|
||||
self.context.get_pixel_format().clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Window {
|
||||
fn drop(&mut self) {
|
||||
use wayland_client::Proxy;
|
||||
self.wayland_context.dropped_surface((*self.egl_surface).id())
|
||||
}
|
||||
}
|
|
@ -56,6 +56,9 @@ extern crate core_foundation;
|
|||
extern crate core_graphics;
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly"))]
|
||||
extern crate x11_dl;
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly"))]
|
||||
#[macro_use(wayland_env)]
|
||||
extern crate wayland_client;
|
||||
|
||||
pub use events::*;
|
||||
pub use headless::{HeadlessRendererBuilder, HeadlessContext};
|
||||
|
|
|
@ -30,7 +30,7 @@ enum Backend {
|
|||
lazy_static!(
|
||||
static ref BACKEND: Backend = {
|
||||
// Wayland backend is not production-ready yet so we disable it
|
||||
if false && wayland::is_available() {
|
||||
if wayland::is_available() {
|
||||
Backend::Wayland
|
||||
} else {
|
||||
match XConnection::new(Some(x_error_callback)) {
|
||||
|
|
Loading…
Reference in a new issue