Touch: fingers reach apps as wl_touch, so a thumb scrolls
Deploy to the handset / build-and-deploy (push) Successful in 2m20s
CI / test (push) Failing after 2m56s

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>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-29 22:22:14 -08:00
1 parent 98a99aaa36
commit e30d9cc71f
17 files changed
+786 -126

No files matched your search

+1
View File
@@ -440,6 +440,7 @@ fn ChandraCompositor::auth_notice_input(self : ChandraCompositor, ev : @nested.I
///| Input while the dialog is up: all of it is the dialog's.
fn ChandraCompositor::auth_input(self : ChandraCompositor, ui : AuthUi, ev : @nested.InputEvent) -> Bool {
match ev {
Touch(_) => ()
Key(code, pressed) => {
if code == key_lsuper || code == key_rsuper {
self.super_solo = false
+15 -4
View File
@@ -345,6 +345,8 @@ pub(all) struct ChandraCompositor {
mut vol_osd : VolOsd?
mut vol_pct : Int
mut vol_muted : Bool
/// Fingers on the screen (touch.mbt).
touch : TouchState
mut vol_repeat_key : UInt
mut vol_repeat_action : @shell.ShortcutAction?
mut vol_repeat_next_ms : UInt64
@@ -513,6 +515,7 @@ pub fn ChandraCompositor::new(
vol_osd: None,
vol_pct: 0,
vol_muted: false,
touch: TouchState::new(),
vol_repeat_key: 0U,
vol_repeat_action: None,
vol_repeat_next_ms: 0UL,
@@ -786,6 +789,7 @@ pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
self.face_window_tick()
self.control_tick()
self.volume_tick()
self.touch_tick()
self.shell.launch_tick()
let events = self.server.poll_events()
for ev in events {
@@ -930,7 +934,7 @@ pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
}
None => release_snapshot(snap)
}
@wayland.CompositorEvent::DragStarted(win) => self.dnd_start(win, self.input_time)
@wayland.CompositorEvent::DragStarted(win) => self.touch_handover(win, () => self.dnd_start(win, self.input_time))
@wayland.CompositorEvent::DragIcon(snap, dx, dy) =>
if self.dnd {
release_snapshot(self.dnd_icon)
@@ -965,7 +969,7 @@ pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
}
None => ()
}
@wayland.CompositorEvent::ToplevelResizeRequest(wid, edges) => self.client_resize(wid, edges)
@wayland.CompositorEvent::ToplevelResizeRequest(wid, edges) => self.touch_handover(wid, () => self.client_resize(wid, edges))
@wayland.CompositorEvent::PopupOpened(wid, parent, rx, ry, w, h) => {
// Keyboard focus stays on the parent until the popup grabs and maps: moving it away
// early makes clients (Chrome, Qt) take the keyboard leave as "dismiss the menu".
@@ -1066,7 +1070,7 @@ pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMoveRequest(wid) => self.client_move(wid)
@wayland.CompositorEvent::ToplevelMoveRequest(wid) => self.touch_handover(wid, () => self.client_move(wid))
}
}
self.session_tick()
@@ -1102,6 +1106,8 @@ pub fn ChandraCompositor::wake_timeout(self : ChandraCompositor, idle_ms : Int)
let d = if g >= 0 && (d < 0 || g < d) { g } else { d }
let l = self.shell.launch_wake_ms(now)
let d = if l >= 0 && (d < 0 || l < d) { l } else { d }
let t = self.touch_wake_ms(now)
let d = if t >= 0 && (d < 0 || t < d) { t } else { d }
let v = self.volume_wake_ms(now)
let d = if v >= 0 && (d < 0 || v < d) { v } else { d }
if d >= 0 && d < idle_ms {
@@ -2126,7 +2132,10 @@ pub fn ChandraCompositor::handle_input(
ev : @nested.InputEvent,
time_ms : UInt,
) -> Unit {
self.handle_input_inner(ev, time_ms)
match ev {
Touch(t) => self.touch_input(t, time_ms)
_ => self.handle_input_inner(ev, time_ms)
}
self.osk_sync()
}
@@ -2801,6 +2810,8 @@ fn ChandraCompositor::dispatch_input_inner(
None => ()
}
}
// touch_input (touch.mbt) took these.
Touch(_) => ()
}
}
+14
View File
@@ -769,6 +769,20 @@ pub fn ChandraCompositor::control_command(
_ => (["error invalid button args"], false)
}
}
"input-touch" => {
// A finger, as the touchscreen would report it (docs/handset.md).
let usage = (["error usage: input-touch down|motion ID X Y | up ID | cancel"], false)
let args = words[1:].to_array().map(parse_int_opt)
let ev : @nested.TouchEvent = match (words.get(1), args) {
(Some("down"), [_, Some(id), Some(x), Some(y)]) => Down(id, x.to_double(), y.to_double())
(Some("motion"), [_, Some(id), Some(x), Some(y)]) => Motion(id, x.to_double(), y.to_double())
(Some("up"), [_, Some(id)]) => Up(id)
(Some("cancel"), [_]) => Cancel
_ => return usage
}
self.handle_input(Touch(ev), @sys.monotonic_ms().to_uint())
(["ok"], true)
}
"input-scroll" => {
if words.length() != 4 {
return (["error usage: input-scroll AXIS VAL DISCRETE"], false)
+1
View File
@@ -295,6 +295,7 @@ fn ChandraCompositor::emoji_input(self : ChandraCompositor, ev : @nested.InputEv
None => return false
}
match ev {
Touch(_) => false
// Other releases (the '.' of Super+., a key held since before) take the usual path.
Key(_, false) => false
Key(code, true) => {
+1
View File
@@ -96,6 +96,7 @@ fn ChandraCompositor::face_window_input(self : ChandraCompositor, ev : @nested.I
_ => return false
}
match ev {
Touch(_) => false
Key(code, pressed) => {
if code == key_lsuper || code == key_rsuper {
self.super_solo = false
+1
View File
@@ -613,6 +613,7 @@ fn offset_ops(ops : Array[@scene.DrawOp], dx : Int, dy : Int) -> Array[@scene.Dr
/// True when the event was used (it doesn't go on to the shell or clients).
fn ChandraCompositor::screenshot_input(self : ChandraCompositor, ev : @nested.InputEvent, time_ms : UInt) -> Bool {
match ev {
Touch(_) => false
Key(code, pressed) => self.screenshot_key(code, pressed)
Modifiers(_, _, _, _) => false
PointerMove(x, y) =>
+355
View File
@@ -0,0 +1,355 @@
///| 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
}
+138
View File
@@ -0,0 +1,138 @@
///| Touch (touch.mbt): wl_touch for fingers on an app's content, the pointer everywhere else.
///|
/// A 540x1170 handset with one window, whose client has the pointer and (with `touch`) wl_touch.
fn touch_setup(tag : String, touch : Bool) -> (@host.ChandraCompositor, TestWaylandClient, UInt, UInt) raise {
let path = "/tmp/wayland-moon-touch-" + tag + "-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(path, @scene.ThemeTokens::moon_night(), 540, 1170).unwrap()
comp.handle_input(Touch(Present), 1U)
let (c, shm, surf, xdg, _, _) = open_and_configure_client(comp, path, "T", "t")
commit_shm_frame(comp, c, shm, surf, xdg, 1UL)
let seat = bind_global(c, 2U, "wl_seat", 7U)
let get = fn(op : UInt) {
let id = c.alloc_id()
let w = @wayland.WireWriter::new()
w.u32(id)
c.send(w.finish_msg(seat, op))
id
}
let ptr = get(0U)
let t = if touch { get(2U) } else { 0U }
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
ignore(drain_msgs(c))
(comp, c, ptr, t)
}
///|
fn window_middle(comp : @host.ChandraCompositor) -> (Double, Double) {
let g = comp.windows.get(1UL).unwrap().geometry
((g.x + g.w / 2).to_double(), (g.y + g.h / 2).to_double())
}
///|
test "a finger on an app that listens goes to it as a touch, and no pointer is faked" {
let (comp, c, ptr, t) = touch_setup("client", true)
let (x, y) = window_middle(comp)
comp.handle_input(Touch(Down(0, x, y)), 10U)
comp.handle_input(Touch(Motion(0, x, y - 60.0)), 20U)
comp.handle_input(Touch(Down(1, x + 40.0, y)), 25U) // a second finger
comp.handle_input(Touch(Up(1)), 28U)
comp.handle_input(Touch(Up(0)), 30U)
let msgs = drain_msgs(c)
assert_eq(
msgs.iter().filter(m => m.0 == t).map(m => m.1).to_array(),
[0U, 3U, 2U, 3U, 0U, 3U, 1U, 3U, 1U, 3U], // down motion down up up, each with its frame
)
assert_false(msgs.iter().any(m => m.0 == ptr && m.1 != 1U))
// Holding still is the app's to time: no right click.
comp.handle_input(Touch(Down(0, x, y)), 40U)
@sys.sleep_ms(520)
comp.step()
let msgs = drain_msgs(c)
assert_false(msgs.iter().any(m => m.0 == ptr))
// The screen turning off (or rotating) cancels.
comp.handle_input(Touch(Cancel), 50U)
comp.handle_input(Touch(Up(0)), 51U)
let msgs = drain_msgs(c)
assert_true(has_msg(msgs, t, 4U))
assert_false(has_msg(msgs, t, 1U))
comp.close()
}
///|
test "an app that doesn't listen to touches gets the pointer: tap, drag and hold" {
let (comp, c, ptr, _) = touch_setup("pointer", false)
let (x, y) = window_middle(comp)
let buttons = fn() { drain_msgs(c).iter().filter(m => m.0 == ptr && m.1 == 3U).count() }
comp.handle_input(Touch(Down(0, x, y)), 10U)
assert_eq(buttons(), 0)
comp.handle_input(Touch(Up(0)), 20U)
assert_eq(buttons(), 2) // pressed and released
comp.handle_input(Touch(Down(0, x, y)), 30U)
comp.handle_input(Touch(Motion(0, x + 5.0, y)), 31U)
assert_eq(buttons(), 0)
comp.handle_input(Touch(Motion(0, x + 30.0, y)), 32U)
assert_eq(buttons(), 1)
comp.handle_input(Touch(Up(0)), 33U)
assert_eq(buttons(), 1)
comp.handle_input(Touch(Down(0, x, y)), 40U)
assert_true(comp.wake_timeout(5000) <= 500)
@sys.sleep_ms(520)
comp.step()
assert_eq(buttons(), 2) // the right button
comp.handle_input(Touch(Up(0)), 41U)
assert_eq(buttons(), 0)
comp.close()
}
///|
test "the panel takes a finger as before, and the seat says it has a touchscreen" {
let (comp, c, _, t) = touch_setup("panel", true)
comp.handle_input(Touch(Down(0, 20.0, 1160.0)), 10U)
comp.handle_input(Touch(Up(0)), 20U)
assert_false(drain_msgs(c).iter().any(m => m.0 == t))
let (out, ok) = comp.control_command("input-touch down 0 10")
assert_false(ok)
assert_true(out[0].has_prefix("error usage"))
comp.close()
}
///|
test "a window moved by its own title bar follows the finger that asked for it" {
let (comp, c, shm, surf, xdg, tl) = csd_setup("touchmove")
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let seat = bind_global(c, 2U, "wl_seat", 7U)
let t = c.alloc_id()
let w = @wayland.WireWriter::new()
w.u32(t)
c.send(w.finish_msg(seat, 2U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
let before = comp.windows.get(1UL).unwrap().geometry
let r = comp.windows.get(1UL).unwrap().client_rect()
let (px, py) = ((r.x + r.w / 2).to_double(), (r.y + 15).to_double())
comp.handle_input(Touch(Down(0, px, py)), 10U)
ignore(drain_msgs(c))
let mv = @wayland.WireWriter::new()
mv.u32(0U)
mv.u32(1U)
c.send(mv.finish_msg(tl, 5U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
assert_true(comp.wm.drag is Some(_))
assert_true(has_msg(drain_msgs(c), t, 4U)) // its touch is cancelled
comp.handle_input(Touch(Motion(0, px + 60.0, py + 40.0)), 12U)
comp.handle_input(Touch(Up(0)), 13U)
let after = comp.windows.get(1UL).unwrap().geometry
assert_eq((after.x - before.x, after.y - before.y), (60, 40))
assert_true(comp.wm.drag is None)
comp.close()
}