Skip to content

Commit

Permalink
Wayland: Add relative pointer movement (#973)
Browse files Browse the repository at this point in the history
* Add relative pointer movement for Wayland

* Format changed code with rustfmt

* Wayland: merge window and device event queues into one

* Replace map_or_else call for simplification
  • Loading branch information
Cherser-s authored and goddessfreya committed Jun 25, 2019
1 parent dbe6a1b commit 3555de1
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 48 deletions.
101 changes: 70 additions & 31 deletions src/platform_impl/linux/wayland/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use std::{
time::Instant,
};

use smithay_client_toolkit::reexports::protocols::unstable::relative_pointer::v1::client::{
zwp_relative_pointer_manager_v1::ZwpRelativePointerManagerV1,
zwp_relative_pointer_v1::ZwpRelativePointerV1,
};

use crate::{
dpi::{PhysicalPosition, PhysicalSize},
event::ModifiersState,
Expand All @@ -15,7 +20,7 @@ use crate::{
platform_impl::platform::sticky_exit_callback,
};

use super::{window::WindowStore, WindowId};
use super::{window::WindowStore, DeviceId, WindowId};

use smithay_client_toolkit::{
output::OutputMgr,
Expand All @@ -26,33 +31,37 @@ use smithay_client_toolkit::{
Environment,
};

pub struct WindowEventsSink {
buffer: VecDeque<(crate::event::WindowEvent, crate::window::WindowId)>,
pub struct WindowEventsSink<T> {
buffer: VecDeque<crate::event::Event<T>>,
}

impl WindowEventsSink {
pub fn new() -> WindowEventsSink {
impl<T> WindowEventsSink<T> {
pub fn new() -> WindowEventsSink<T> {
WindowEventsSink {
buffer: VecDeque::new(),
}
}

pub fn send_event(&mut self, evt: crate::event::WindowEvent, wid: WindowId) {
self.buffer.push_back((
evt,
crate::window::WindowId(crate::platform_impl::WindowId::Wayland(wid)),
));
pub fn send_window_event(&mut self, evt: crate::event::WindowEvent, wid: WindowId) {
self.buffer.push_back(crate::event::Event::WindowEvent {
event: evt,
window_id: crate::window::WindowId(crate::platform_impl::WindowId::Wayland(wid)),
});
}

pub fn send_device_event(&mut self, evt: crate::event::DeviceEvent, dev_id: DeviceId) {
self.buffer.push_back(crate::event::Event::DeviceEvent {
event: evt,
device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(dev_id)),
});
}

fn empty_with<F, T>(&mut self, mut callback: F)
fn empty_with<F>(&mut self, mut callback: F)
where
F: FnMut(crate::event::Event<T>),
{
for (evt, wid) in self.buffer.drain(..) {
callback(crate::event::Event::WindowEvent {
event: evt,
window_id: wid,
})
for evt in self.buffer.drain(..) {
callback(evt)
}
}
}
Expand All @@ -65,7 +74,7 @@ pub struct EventLoop<T: 'static> {
// the output manager
pub outputs: OutputMgr,
// our sink, shared with some handlers, buffering the events
sink: Arc<Mutex<WindowEventsSink>>,
sink: Arc<Mutex<WindowEventsSink<T>>>,
pending_user_events: Rc<RefCell<VecDeque<T>>>,
_user_source: ::calloop::Source<::calloop::channel::Channel<T>>,
user_sender: ::calloop::channel::Sender<T>,
Expand Down Expand Up @@ -122,13 +131,14 @@ impl<T: 'static> EventLoop<T> {
.handle()
.insert_source(kbd_channel, move |evt, &mut ()| {
if let ::calloop::channel::Event::Msg((evt, wid)) = evt {
kbd_sink.lock().unwrap().send_event(evt, wid);
kbd_sink.lock().unwrap().send_window_event(evt, wid);
}
})
.unwrap();

let mut seat_manager = SeatManager {
sink: sink.clone(),
relative_pointer_manager_proxy: None,
store: store.clone(),
seats: seats.clone(),
kbd_sender,
Expand All @@ -143,6 +153,15 @@ impl<T: 'static> EventLoop<T> {
ref interface,
version,
} => {
if interface == "zwp_relative_pointer_manager_v1" {
seat_manager.relative_pointer_manager_proxy = Some(
registry
.bind(version, id, move |pointer_manager| {
pointer_manager.implement_closure(|_, _| (), ())
})
.unwrap(),
)
}
if interface == "wl_seat" {
seat_manager.add_seat(id, version, registry)
}
Expand Down Expand Up @@ -388,7 +407,7 @@ impl<T> EventLoop<T> {
let pruned = window_target.store.lock().unwrap().cleanup();
*cleanup_needed = false;
for wid in pruned {
sink.send_event(crate::event::WindowEvent::Destroyed, wid);
sink.send_window_event(crate::event::WindowEvent::Destroyed, wid);
}
}
}
Expand All @@ -400,7 +419,10 @@ impl<T> EventLoop<T> {
frame.resize(w, h);
frame.refresh();
let logical_size = crate::dpi::LogicalSize::new(w as f64, h as f64);
sink.send_event(crate::event::WindowEvent::Resized(logical_size), wid);
sink.send_window_event(
crate::event::WindowEvent::Resized(logical_size),
wid,
);
*size = (w, h);
} else if frame_refresh {
frame.refresh();
Expand All @@ -410,16 +432,16 @@ impl<T> EventLoop<T> {
}
}
if let Some(dpi) = new_dpi {
sink.send_event(
sink.send_window_event(
crate::event::WindowEvent::HiDpiFactorChanged(dpi as f64),
wid,
);
}
if refresh {
sink.send_event(crate::event::WindowEvent::RedrawRequested, wid);
sink.send_window_event(crate::event::WindowEvent::RedrawRequested, wid);
}
if closed {
sink.send_event(crate::event::WindowEvent::CloseRequested, wid);
sink.send_window_event(crate::event::WindowEvent::CloseRequested, wid);
}
},
)
Expand All @@ -430,21 +452,24 @@ impl<T> EventLoop<T> {
* Wayland protocol implementations
*/

struct SeatManager {
sink: Arc<Mutex<WindowEventsSink>>,
struct SeatManager<T: 'static> {
sink: Arc<Mutex<WindowEventsSink<T>>>,
store: Arc<Mutex<WindowStore>>,
seats: Arc<Mutex<Vec<(u32, wl_seat::WlSeat)>>>,
kbd_sender: ::calloop::channel::Sender<(crate::event::WindowEvent, super::WindowId)>,
relative_pointer_manager_proxy: Option<ZwpRelativePointerManagerV1>,
}

impl SeatManager {
impl<T: 'static> SeatManager<T> {
fn add_seat(&mut self, id: u32, version: u32, registry: wl_registry::WlRegistry) {
use std::cmp::min;

let mut seat_data = SeatData {
sink: self.sink.clone(),
store: self.store.clone(),
pointer: None,
relative_pointer: None,
relative_pointer_manager_proxy: self.relative_pointer_manager_proxy.as_ref().cloned(),
keyboard: None,
touch: None,
kbd_sender: self.kbd_sender.clone(),
Expand All @@ -470,17 +495,19 @@ impl SeatManager {
}
}

struct SeatData {
sink: Arc<Mutex<WindowEventsSink>>,
struct SeatData<T> {
sink: Arc<Mutex<WindowEventsSink<T>>>,
store: Arc<Mutex<WindowStore>>,
kbd_sender: ::calloop::channel::Sender<(crate::event::WindowEvent, super::WindowId)>,
pointer: Option<wl_pointer::WlPointer>,
relative_pointer: Option<ZwpRelativePointerV1>,
relative_pointer_manager_proxy: Option<ZwpRelativePointerManagerV1>,
keyboard: Option<wl_keyboard::WlKeyboard>,
touch: Option<wl_touch::WlTouch>,
modifiers_tracker: Arc<Mutex<ModifiersState>>,
}

impl SeatData {
impl<T: 'static> SeatData<T> {
fn receive(&mut self, evt: wl_seat::Event, seat: wl_seat::WlSeat) {
match evt {
wl_seat::Event::Name { .. } => (),
Expand All @@ -492,7 +519,19 @@ impl SeatData {
self.sink.clone(),
self.store.clone(),
self.modifiers_tracker.clone(),
))
));

self.relative_pointer =
self.relative_pointer_manager_proxy
.as_ref()
.and_then(|manager| {
super::pointer::implement_relative_pointer(
self.sink.clone(),
self.pointer.as_ref().unwrap(),
manager,
)
.ok()
})
}
// destroy pointer if applicable
if !capabilities.contains(wl_seat::Capability::Pointer) {
Expand Down Expand Up @@ -540,7 +579,7 @@ impl SeatData {
}
}

impl Drop for SeatData {
impl<T> Drop for SeatData<T> {
fn drop(&mut self) {
if let Some(pointer) = self.pointer.take() {
if pointer.as_ref().version() >= 3 {
Expand Down
48 changes: 37 additions & 11 deletions src/platform_impl/linux/wayland/pointer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::sync::{Arc, Mutex};

use crate::event::{
ElementState, ModifiersState, MouseButton, MouseScrollDelta, TouchPhase, WindowEvent,
DeviceEvent, ElementState, ModifiersState, MouseButton, MouseScrollDelta, TouchPhase,
WindowEvent,
};

use super::{event_loop::WindowEventsSink, window::WindowStore, DeviceId};
Expand All @@ -11,9 +12,14 @@ use smithay_client_toolkit::reexports::client::protocol::{
wl_seat,
};

pub fn implement_pointer(
use smithay_client_toolkit::reexports::protocols::unstable::relative_pointer::v1::client::{
zwp_relative_pointer_manager_v1::ZwpRelativePointerManagerV1, zwp_relative_pointer_v1::Event,
zwp_relative_pointer_v1::ZwpRelativePointerV1,
};

pub fn implement_pointer<T: 'static>(
seat: &wl_seat::WlSeat,
sink: Arc<Mutex<WindowEventsSink>>,
sink: Arc<Mutex<WindowEventsSink<T>>>,
store: Arc<Mutex<WindowStore>>,
modifiers_tracker: Arc<Mutex<ModifiersState>>,
) -> WlPointer {
Expand All @@ -37,15 +43,15 @@ pub fn implement_pointer(
let wid = store.find_wid(&surface);
if let Some(wid) = wid {
mouse_focus = Some(wid);
sink.send_event(
sink.send_window_event(
WindowEvent::CursorEntered {
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
),
},
wid,
);
sink.send_event(
sink.send_window_event(
WindowEvent::CursorMoved {
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
Expand All @@ -61,7 +67,7 @@ pub fn implement_pointer(
mouse_focus = None;
let wid = store.find_wid(&surface);
if let Some(wid) = wid {
sink.send_event(
sink.send_window_event(
WindowEvent::CursorLeft {
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
Expand All @@ -77,7 +83,7 @@ pub fn implement_pointer(
..
} => {
if let Some(wid) = mouse_focus {
sink.send_event(
sink.send_window_event(
WindowEvent::CursorMoved {
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
Expand All @@ -103,7 +109,7 @@ pub fn implement_pointer(
// TODO figure out the translation ?
_ => return,
};
sink.send_event(
sink.send_window_event(
WindowEvent::MouseInput {
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
Expand All @@ -127,7 +133,7 @@ pub fn implement_pointer(
wl_pointer::Axis::HorizontalScroll => x += value as f32,
_ => unreachable!(),
}
sink.send_event(
sink.send_window_event(
WindowEvent::MouseWheel {
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
Expand Down Expand Up @@ -161,7 +167,7 @@ pub fn implement_pointer(
let axis_discrete_buffer = axis_discrete_buffer.take();
if let Some(wid) = mouse_focus {
if let Some((x, y)) = axis_discrete_buffer {
sink.send_event(
sink.send_window_event(
WindowEvent::MouseWheel {
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
Expand All @@ -173,7 +179,7 @@ pub fn implement_pointer(
wid,
);
} else if let Some((x, y)) = axis_buffer {
sink.send_event(
sink.send_window_event(
WindowEvent::MouseWheel {
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
Expand Down Expand Up @@ -215,3 +221,23 @@ pub fn implement_pointer(
})
.unwrap()
}

pub fn implement_relative_pointer<T: 'static>(
sink: Arc<Mutex<WindowEventsSink<T>>>,
pointer: &WlPointer,
manager: &ZwpRelativePointerManagerV1,
) -> Result<ZwpRelativePointerV1, ()> {
manager.get_relative_pointer(pointer, |rel_pointer| {
rel_pointer.implement_closure(
move |evt, _rel_pointer| {
let mut sink = sink.lock().unwrap();
match evt {
Event::RelativeMotion { dx, dy, .. } => sink
.send_device_event(DeviceEvent::MouseMotion { delta: (dx, dy) }, DeviceId),
_ => unreachable!(),
}
},
(),
)
})
}
Loading

0 comments on commit 3555de1

Please sign in to comment.