Skip to content

Commit

Permalink
wayland: implement wl_touch
Browse files Browse the repository at this point in the history
  • Loading branch information
Sporif committed Apr 21, 2024
1 parent 1d0a4c0 commit d393e11
Show file tree
Hide file tree
Showing 11 changed files with 515 additions and 29 deletions.
23 changes: 23 additions & 0 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ pub enum KeyState {
Pressed,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct TouchPosition {
pub x: Fixed,
pub y: Fixed,
pub x_transformed: Fixed,
pub y_transformed: Fixed,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum TouchEvent {
Down { pos: TouchPosition },
Up,
Motion { pos: TouchPosition },
Cancel,
Frame,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ScrollAxis {
Horizontal = HORIZONTAL_SCROLL as _,
Expand Down Expand Up @@ -244,6 +261,12 @@ pub enum InputEvent {
state: KeyState,
},

Touch {
seat_slot: i32,
time_usec: u64,
event: TouchEvent,
},

AxisPx {
dist: Fixed,
axis: ScrollAxis,
Expand Down
64 changes: 63 additions & 1 deletion src/backends/metal/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
backend::{AxisSource, InputEvent, KeyState, ScrollAxis},
backend::{AxisSource, InputEvent, KeyState, ScrollAxis, TouchEvent, TouchPosition},
backends::metal::MetalBackend,
fixed::Fixed,
libinput::{
Expand Down Expand Up @@ -91,6 +91,11 @@ impl MetalBackend {
c::LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS => {
self.handle_pointer_axis(event, AxisSource::Continuous)
}
c::LIBINPUT_EVENT_TOUCH_DOWN => self.handle_touch_down(event),
c::LIBINPUT_EVENT_TOUCH_UP => self.handle_touch_up(event),
c::LIBINPUT_EVENT_TOUCH_MOTION => self.handle_touch_motion(event),
c::LIBINPUT_EVENT_TOUCH_CANCEL => self.handle_touch_cancel(event),
c::LIBINPUT_EVENT_TOUCH_FRAME => self.handle_touch_frame(event),
_ => {}
}
}
Expand Down Expand Up @@ -217,4 +222,61 @@ impl MetalBackend {
dy_unaccelerated: Fixed::from_f64(dy_unaccelerated),
});
}

fn handle_touch_down(self: &Rc<Self>, event: LibInputEvent) {
let (event, dev) = unpack!(self, event, touch_event);
let pos = TouchPosition {
x: Fixed::from_f64(event.x()),
y: Fixed::from_f64(event.y()),
x_transformed: Fixed::from_f64(event.x_transformed(1)),
y_transformed: Fixed::from_f64(event.y_transformed(1)),
};
dev.event(InputEvent::Touch {
seat_slot: event.seat_slot(),
time_usec: event.time_usec(),
event: TouchEvent::Down { pos },
})
}

fn handle_touch_up(self: &Rc<Self>, event: LibInputEvent) {
let (event, dev) = unpack!(self, event, touch_event);
dev.event(InputEvent::Touch {
seat_slot: event.seat_slot(),
time_usec: event.time_usec(),
event: TouchEvent::Up,
})
}

fn handle_touch_motion(self: &Rc<Self>, event: LibInputEvent) {
let (event, dev) = unpack!(self, event, touch_event);
let pos = TouchPosition {
x: Fixed::from_f64(event.x()),
y: Fixed::from_f64(event.y()),
x_transformed: Fixed::from_f64(event.x_transformed(1)),
y_transformed: Fixed::from_f64(event.y_transformed(1)),
};
dev.event(InputEvent::Touch {
seat_slot: event.seat_slot(),
time_usec: event.time_usec(),
event: TouchEvent::Motion { pos },
})
}

fn handle_touch_cancel(self: &Rc<Self>, event: LibInputEvent) {
let (event, dev) = unpack!(self, event, touch_event);
dev.event(InputEvent::Touch {
seat_slot: event.seat_slot(),
time_usec: event.time_usec(),
event: TouchEvent::Cancel,
})
}

fn handle_touch_frame(self: &Rc<Self>, event: LibInputEvent) {
let (event, dev) = unpack!(self, event, touch_event);
dev.event(InputEvent::Touch {
seat_slot: 0,
time_usec: event.time_usec(),
event: TouchEvent::Frame,
})
}
}
18 changes: 15 additions & 3 deletions src/ifs/wl_seat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod event_handling;
mod kb_owner;
mod pointer_owner;
mod touch_owner;

pub mod text_input;
pub mod wl_keyboard;
pub mod wl_pointer;
Expand Down Expand Up @@ -41,6 +43,7 @@ use {
zwp_input_method_keyboard_grab_v2::ZwpInputMethodKeyboardGrabV2,
zwp_input_method_v2::ZwpInputMethodV2, zwp_text_input_v3::ZwpTextInputV3,
},
touch_owner::TouchOwnerHolder,
wl_keyboard::{WlKeyboard, WlKeyboardError, REPEAT_INFO_SINCE},
wl_pointer::WlPointer,
wl_touch::WlTouch,
Expand All @@ -66,7 +69,7 @@ use {
},
wire::{
wl_seat::*, ExtIdleNotificationV1Id, WlDataDeviceId, WlKeyboardId, WlPointerId,
WlSeatId, ZwlrDataControlDeviceV1Id, ZwpPrimarySelectionDeviceV1Id,
WlSeatId, WlTouchId, ZwlrDataControlDeviceV1Id, ZwpPrimarySelectionDeviceV1Id,
ZwpRelativePointerV1Id, ZwpTextInputV3Id,
},
xkbcommon::{DynKeyboardState, KeyboardState, KeymapId, XkbKeymap, XkbState},
Expand All @@ -90,7 +93,6 @@ pub use {

pub const POINTER: u32 = 1;
const KEYBOARD: u32 = 2;
#[allow(dead_code)]
const TOUCH: u32 = 4;

#[allow(dead_code)]
Expand Down Expand Up @@ -138,6 +140,7 @@ pub struct WlSeatGlobal {
pointer_stack_modified: Cell<bool>,
found_tree: RefCell<Vec<FoundNode>>,
keyboard_node: CloneCell<Rc<dyn Node>>,
touch_node: CloneCell<Rc<dyn Node>>,
bindings: RefCell<AHashMap<ClientId, AHashMap<WlSeatId, Rc<WlSeat>>>>,
x_data_devices: SmallMap<XIpcDeviceId, Rc<XIpcDevice>, 1>,
data_devices: RefCell<AHashMap<ClientId, AHashMap<WlDataDeviceId, Rc<WlDataDevice>>>>,
Expand All @@ -162,6 +165,7 @@ pub struct WlSeatGlobal {
primary_selection_serial: Cell<u32>,
pointer_owner: PointerOwnerHolder,
kb_owner: KbOwnerHolder,
touch_owner: TouchOwnerHolder,
dropped_dnd: RefCell<Option<DroppedDnd>>,
shortcuts: RefCell<AHashMap<u32, SmallMap<u32, u32, 2>>>,
queue_link: Cell<Option<LinkedNode<Rc<Self>>>>,
Expand Down Expand Up @@ -213,6 +217,7 @@ impl WlSeatGlobal {
pointer_stack_modified: Cell::new(false),
found_tree: RefCell::new(vec![]),
keyboard_node: CloneCell::new(state.root.clone()),
touch_node: CloneCell::new(state.root.clone()),
bindings: Default::default(),
x_data_devices: Default::default(),
data_devices: RefCell::new(Default::default()),
Expand All @@ -230,6 +235,7 @@ impl WlSeatGlobal {
primary_selection_serial: Cell::new(0),
pointer_owner: Default::default(),
kb_owner: Default::default(),
touch_owner: Default::default(),
dropped_dnd: RefCell::new(None),
shortcuts: Default::default(),
queue_link: Cell::new(None),
Expand Down Expand Up @@ -1053,6 +1059,7 @@ impl WlSeatGlobal {
mem::take(self.pointer_stack.borrow_mut().deref_mut());
mem::take(self.found_tree.borrow_mut().deref_mut());
self.keyboard_node.set(self.state.root.clone());
self.touch_node.set(self.state.root.clone());
self.state
.root
.clone()
Expand All @@ -1068,6 +1075,7 @@ impl WlSeatGlobal {
self.primary_selection.set(None);
self.pointer_owner.clear();
self.kb_owner.clear();
self.touch_owner.clear();
*self.dropped_dnd.borrow_mut() = None;
self.queue_link.set(None);
self.tree_changed_handler.set(None);
Expand Down Expand Up @@ -1100,6 +1108,7 @@ impl WlSeatGlobal {
pointers: Default::default(),
relative_pointers: Default::default(),
keyboards: Default::default(),
touches: Default::default(),
version,
tracker: Default::default(),
});
Expand Down Expand Up @@ -1192,6 +1201,7 @@ pub struct WlSeat {
pointers: CopyHashMap<WlPointerId, Rc<WlPointer>>,
relative_pointers: CopyHashMap<ZwpRelativePointerV1Id, Rc<ZwpRelativePointerV1>>,
keyboards: CopyHashMap<WlKeyboardId, Rc<WlKeyboard>>,
touches: CopyHashMap<WlTouchId, Rc<WlTouch>>,
version: Version,
tracker: Tracker<Self>,
}
Expand All @@ -1202,7 +1212,7 @@ impl WlSeat {
fn send_capabilities(self: &Rc<Self>) {
self.client.event(Capabilities {
self_id: self.id,
capabilities: POINTER | KEYBOARD,
capabilities: POINTER | KEYBOARD | TOUCH,
})
}

Expand Down Expand Up @@ -1261,6 +1271,7 @@ impl WlSeatRequestHandler for WlSeat {
let p = Rc::new(WlTouch::new(req.id, slf));
track!(self.client, p);
self.client.add_client_obj(&p)?;
self.touches.set(req.id, p);
Ok(())
}

Expand Down Expand Up @@ -1298,6 +1309,7 @@ impl Object for WlSeat {
self.pointers.clear();
self.relative_pointers.clear();
self.keyboards.clear();
self.touches.clear();
}
}

Expand Down
Loading

0 comments on commit d393e11

Please sign in to comment.