Files
chandra/compositor/host/touch.mbt
T
nandiandClaude Opus 5.5 e30d9cc71f
Deploy to the handset / build-and-deploy (push) Successful in 2m20s
CI / test (push) Failing after 2m56s
Touch: fingers reach apps as wl_touch, so a thumb scrolls
Every touch was turned into a mouse drag in the seat code, which selects text
instead of scrolling. The seat now reports raw touches and the compositor
routes them: to the app as wl_touch on its own content, and to the pointer
(tap, drag, hold for right click, as before) everywhere else and for apps that
don't listen to touches. The cursor is hidden while fingers are in use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 22:22:14 -08:00

356 lines
9.9 KiB
MoonBit

///| Touch. Fingers on an app's own content go to the app (wl_touch), which scrolls, pinches and
/// long-presses as it knows how. Everywhere else (the panel, the on-screen keyboard, the desktop,
/// title bars, dialogs, apps that don't listen to touches) one finger works like a mouse: a tap
/// clicks, a drag is a left-button drag, and holding still is a right click.
///| Holding still this long is a right click (apps time their own).
let touch_long_ms : UInt64 = 500UL
///| A finger that moved less than this is still tapping or holding.
let touch_drag_px : Double = 18.0
///|
enum TouchMode {
/// Nothing, or fingers left over from a gesture that is over.
NoTouch
/// Finger `id` works the pointer.
AsPointer(TouchPointer)
/// Fingers on an app's surface.
InClient(TouchClient)
}
///|
struct TouchPointer {
id : Int
x0 : Double
y0 : Double
since : UInt64
mut phase : TouchPhase
}
///|
enum TouchPhase {
Pending
Dragging
/// The right click was made: the rest of the touch does nothing.
Held
} derive(Eq)
///|
struct TouchClient {
/// The window or popup, and its subsurface (0: itself).
win : UInt64
sub : UInt
/// The fingers down, and where each is and was put down.
fingers : Map[Int, (Double, Double)]
x0 : Double
y0 : Double
first : Int
/// Still a tap: one finger that hasn't moved.
mut tap : Bool
}
///|
struct TouchState {
mut mode : TouchMode
}
///|
fn TouchState::new() -> TouchState {
{ mode: NoTouch }
}
///|
fn ChandraCompositor::touch_input(
self : ChandraCompositor,
ev : @nested.TouchEvent,
time_ms : UInt,
) -> Unit {
match ev {
Present => self.server.set_touch()
Down(id, x, y) => {
self.server.set_touch()
self.touch_down(id, x, y, time_ms)
}
Motion(id, x, y) => self.touch_motion(id, x, y, time_ms)
Up(id) => self.touch_up(id, time_ms)
Cancel => self.touch_cancel(time_ms)
}
}
///|
fn ChandraCompositor::touch_down(
self : ChandraCompositor,
id : Int,
x : Double,
y : Double,
time_ms : UInt,
) -> Unit {
match self.touch.mode {
AsPointer(_) => () // one finger works the pointer; others do nothing
InClient(t) => {
t.tap = false
t.fingers.set(id, (x, y))
self.note_activity(@sys.monotonic_ms())
let (lx, ly) = self.touch_local(t, x, y)
self.server.touch_down(t.win, time_ms, id, lx, ly, sub=t.sub)
}
NoTouch =>
match self.touch_target(x, y) {
Some((win, sub, lx, ly)) => {
self.note_activity(@sys.monotonic_ms())
self.input_time = time_ms
self.super_solo = false
self.leave_pointer()
self.press_fwd = false
self.shell.desktop_leave()
self.ptr_x = x
self.ptr_y = y
if self.windows.contains(win) {
self.activate_window(win)
}
self.touch.mode = InClient({
win,
sub,
fingers: Map::from_array([(id, (x, y))]),
x0: x,
y0: y,
first: id,
tap: true,
})
self.server.touch_down(win, time_ms, id, lx, ly, sub~)
}
None => {
self.touch.mode = AsPointer({ id, x0: x, y0: y, since: @sys.monotonic_ms(), phase: Pending })
self.handle_input_inner(PointerMove(x, y), time_ms)
}
}
}
}
///|
fn ChandraCompositor::touch_motion(
self : ChandraCompositor,
id : Int,
x : Double,
y : Double,
time_ms : UInt,
) -> Unit {
match self.touch.mode {
AsPointer(t) =>
if t.id == id {
if t.phase == Pending && moved(x - t.x0, y - t.y0) {
t.phase = Dragging
self.handle_input_inner(PointerMove(t.x0, t.y0), time_ms)
self.handle_input_inner(PointerButton(272U, true), time_ms)
}
if t.phase == Dragging {
self.handle_input_inner(PointerMove(x, y), time_ms)
}
}
InClient(t) =>
if t.fingers.contains(id) {
t.fingers.set(id, (x, y))
if moved(x - t.x0, y - t.y0) {
t.tap = false
}
if id == t.first {
self.ptr_x = x
self.ptr_y = y
}
self.note_activity(@sys.monotonic_ms())
let (lx, ly) = self.touch_local(t, x, y)
self.server.touch_motion(t.win, time_ms, id, lx, ly)
}
NoTouch => ()
}
}
///|
fn ChandraCompositor::touch_up(self : ChandraCompositor, id : Int, time_ms : UInt) -> Unit {
match self.touch.mode {
AsPointer(t) =>
if t.id == id {
self.touch.mode = NoTouch
match t.phase {
Pending => {
self.handle_input_inner(PointerMove(t.x0, t.y0), time_ms)
self.handle_input_inner(PointerButton(272U, true), time_ms)
self.handle_input_inner(PointerButton(272U, false), time_ms)
}
Dragging => self.handle_input_inner(PointerButton(272U, false), time_ms)
Held => ()
}
}
InClient(t) =>
if t.fingers.contains(id) {
t.fingers.remove(id)
self.server.touch_up(t.win, time_ms, id)
if t.fingers.is_empty() {
self.touch.mode = NoTouch
// A tap in a text field brings a hidden on-screen keyboard back; scrolling doesn't.
if t.tap {
self.osk_on_window_click(t.win)
}
}
}
NoTouch => ()
}
}
///|
fn ChandraCompositor::touch_cancel(self : ChandraCompositor, time_ms : UInt) -> Unit {
let mode = self.touch.mode
self.touch.mode = NoTouch
match mode {
AsPointer(t) =>
if t.phase == Dragging {
self.handle_input_inner(PointerButton(272U, false), time_ms)
}
InClient(t) => self.server.touch_cancel(t.win)
NoTouch => ()
}
}
///| Long presses that are due.
fn ChandraCompositor::touch_tick(self : ChandraCompositor) -> Unit {
guard self.touch.mode is AsPointer(t) && t.phase == Pending else { return }
let now = @sys.monotonic_ms()
if now - t.since < touch_long_ms {
return
}
t.phase = Held
let time_ms = now.to_uint()
self.handle_input_inner(PointerMove(t.x0, t.y0), time_ms)
self.handle_input_inner(PointerButton(273U, true), time_ms)
self.handle_input_inner(PointerButton(273U, false), time_ms)
self.osk_sync()
}
///| Milliseconds until `touch_tick` has something to do (-1: nothing).
fn ChandraCompositor::touch_wake_ms(self : ChandraCompositor, now : UInt64) -> Int {
guard self.touch.mode is AsPointer(t) && t.phase == Pending else { return -1 }
let due = t.since + touch_long_ms
if due > now {
(due - now).to_int()
} else {
0
}
}
///| The client's surface a first finger at (x, y) belongs to: (window or popup, subsurface,
/// surface-local position). None: it works the pointer.
fn ChandraCompositor::touch_target(
self : ChandraCompositor,
x : Double,
y : Double,
) -> (UInt64, UInt, Double, Double)? {
let xi = x.to_int()
let yi = y.to_int()
// Something other than the apps has the input, or the press.
if self.screen_off ||
self.manual_screen_off ||
self.locked ||
self.auth is Some(_) ||
self.auth_notice is Some(_) ||
self.shell.face.window is Some(_) ||
self.osk_hit_any(xi, yi) ||
self.dnd ||
self.emoji_ui is Some(_) ||
self.shot is Some(_) ||
self.shot_edit is Some(_) ||
(self.rec is Some(_) && self.rec_pill().contains(xi, yi)) ||
self.ptr_grab ||
self.overview is Some(_) ||
self.shell.inspecting() ||
self.shell.popup_open() ||
self.wm.drag is Some(_) ||
self.split_drag is Some(_) {
return None
}
match self.popup_at(xi, yi) {
Some(p) => {
let r = self.popup_rect(p)
return if self.server.takes_touch(p.id) {
Some((p.id, 0U, x - r.x.to_double(), y - r.y.to_double()))
} else {
None
}
}
None => ()
}
// A tap beside an open menu closes it.
if self.popups.length() > 0 || self.shell.hit(xi, yi) || self.handle_at(xi, yi) is Some(_) {
return None
}
guard self.window_at(xi, yi) is Some((wid, true)) else { return None }
// A floating window's edges resize it.
if self.resize_at(xi, yi) is Some(_) || !self.server.takes_touch(wid) {
return None
}
guard self.windows.get(wid) is Some(win) else { return None }
let (sub, lx, ly) = win.surface_at(x, y)
Some((wid, sub, lx, ly))
}
///| Screen point (x, y) in the coordinates of the surface the touch began on.
fn ChandraCompositor::touch_local(
self : ChandraCompositor,
t : TouchClient,
x : Double,
y : Double,
) -> (Double, Double) {
match self.windows.get(t.win) {
Some(win) => win.surface_local(t.sub, x, y)
None => {
for p in self.popups {
if p.id == t.win {
let r = self.popup_rect(p)
return (x - r.x.to_double(), y - r.y.to_double())
}
}
(x, y)
}
}
}
///| A client answered a press with a drag, a move or a resize (`act` starts it, when a button is
/// held on window `win`). If the press is a finger's, the gesture becomes a pointer drag: the
/// client's touch is cancelled and the finger drags from here on.
fn ChandraCompositor::touch_handover(self : ChandraCompositor, win : UInt64, act : () -> Unit) -> Unit {
match self.touch.mode {
InClient(t) if t.fingers.length() == 1 &&
(win == t.win || win == 0UL || self.server.same_client(win, t.win)) => {
self.server.touch_cancel(t.win)
let (lx, ly) = self.touch_local(t, self.ptr_x, self.ptr_y)
self.ptr_win = Some(t.win)
self.ptr_sub = t.sub
self.server.pointer_enter(t.win, lx, ly, sub=t.sub)
self.press_fwd = true
act()
if self.press_fwd {
// It wasn't started (a tiled window can't be resized): the touch is just over.
self.press_fwd = false
self.leave_pointer()
self.touch.mode = NoTouch
} else {
for id, _ in t.fingers {
self.touch.mode = AsPointer({
id,
x0: t.x0,
y0: t.y0,
since: @sys.monotonic_ms(),
phase: Dragging,
})
}
}
}
_ => act()
}
}
///|
fn moved(dx : Double, dy : Double) -> Bool {
dx * dx + dy * dy > touch_drag_px * touch_drag_px
}