diff --git a/compositor/host/auth.mbt b/compositor/host/auth.mbt index 770635e..9aab426 100644 --- a/compositor/host/auth.mbt +++ b/compositor/host/auth.mbt @@ -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 diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index c8afc9a..b37ab54 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -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(_) => () } } diff --git a/compositor/host/control.mbt b/compositor/host/control.mbt index c7849d9..23e5858 100644 --- a/compositor/host/control.mbt +++ b/compositor/host/control.mbt @@ -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) diff --git a/compositor/host/emoji_picker.mbt b/compositor/host/emoji_picker.mbt index 77e3af0..aa859b3 100644 --- a/compositor/host/emoji_picker.mbt +++ b/compositor/host/emoji_picker.mbt @@ -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) => { diff --git a/compositor/host/face_window.mbt b/compositor/host/face_window.mbt index d7b9408..c67d9bf 100644 --- a/compositor/host/face_window.mbt +++ b/compositor/host/face_window.mbt @@ -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 diff --git a/compositor/host/screenshot.mbt b/compositor/host/screenshot.mbt index 7482c1a..3f9f992 100644 --- a/compositor/host/screenshot.mbt +++ b/compositor/host/screenshot.mbt @@ -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) => diff --git a/compositor/host/touch.mbt b/compositor/host/touch.mbt new file mode 100644 index 0000000..7f99f24 --- /dev/null +++ b/compositor/host/touch.mbt @@ -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 +} diff --git a/compositor/host/touch_test.mbt b/compositor/host/touch_test.mbt new file mode 100644 index 0000000..92b682e --- /dev/null +++ b/compositor/host/touch_test.mbt @@ -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() +} diff --git a/compositor/kms/kms_native.mbt b/compositor/kms/kms_native.mbt index 362f0f8..fa81184 100644 --- a/compositor/kms/kms_native.mbt +++ b/compositor/kms/kms_native.mbt @@ -139,6 +139,17 @@ pub fn Kms::take_input(_self : Kms) -> Array[@nested.InputEvent] { c_arg(3), ), ) + 6 => { + let x = c_arg(3).to_double() / 256.0 + let y = c_arg(4).to_double() / 256.0 + match a { + 0 => out.push(Touch(Down(b, x, y))) + 1 => out.push(Touch(Motion(b, x, y))) + 2 => out.push(Touch(Up(b))) + 3 => out.push(Touch(Cancel)) + _ => out.push(Touch(Present)) + } + } _ => () } } diff --git a/compositor/kms/kms_stub.c b/compositor/kms/kms_stub.c index b828f94..774d89c 100644 --- a/compositor/kms/kms_stub.c +++ b/compositor/kms/kms_stub.c @@ -17,7 +17,6 @@ #include #include #include -#include #include #include @@ -32,10 +31,6 @@ #define KEY_F10_CODE 68 #define KEY_F11_CODE 87 #define KEY_F12_CODE 88 -#define BTN_LEFT_CODE 0x110 -#define BTN_RIGHT_CODE 0x111 -#define TOUCH_LONG_MS 500 -#define TOUCH_DRAG_PX 18 /* ---- damage ---- */ @@ -114,7 +109,8 @@ static const struct libinput_interface g_li_iface = { /* ---- input event queue --------------------------------------------------- */ -enum { EV_MOVE = 1, EV_BUTTON = 2, EV_KEY = 3, EV_MODS = 4, EV_AXIS = 5 }; +enum { EV_MOVE = 1, EV_BUTTON = 2, EV_KEY = 3, EV_MODS = 4, EV_AXIS = 5, EV_TOUCH = 6 }; +enum { TP_DOWN = 0, TP_MOTION = 1, TP_UP = 2, TP_CANCEL = 3, TP_PRESENT = 4 }; #define QCAP 512 static int32_t g_q[QCAP][5]; @@ -249,11 +245,7 @@ static int g_cur_ready; static int g_cur_shape = -1; static int g_px, g_py; /* pointer position (hotspot) */ static int g_hotx, g_hoty; -enum { TOUCH_IDLE = 0, TOUCH_PENDING = 1, TOUCH_DRAG = 2, TOUCH_LONG = 3 }; -static int g_touch_state; -static int32_t g_touch_slot; -static int g_touch_x0, g_touch_y0; -static int g_touch_timer = -1; +static int g_cur_hidden; /* fingers are in use: no cursor until a pointer moves */ static const char *ART_ARROW[] = { "X...........", "XX..........", "XoX.........", "XooX........", "XoooX.......", @@ -316,7 +308,7 @@ static void cursor_draw(int shape) { } static void cursor_apply(void) { - if (!g_cur_ready || !g_seat_enabled) return; + if (!g_cur_ready || !g_seat_enabled || g_cur_hidden) return; int ux = g_px, uy = g_py; if (g_rot != 0 && g_base_w > 0 && g_base_h > 0) { logical_to_unrot(g_px, g_py, &ux, &uy); @@ -325,28 +317,35 @@ static void cursor_apply(void) { drmModeMoveCursor(g_drm, g_crtc, ux * g_scale - g_hotx, uy * g_scale - g_hoty); } -/* ---- touch long-press timer ---------------------------------------------- */ - -static void touch_timer_arm(int ms) { - if (g_touch_timer < 0) return; - struct itimerspec its = {0}; - its.it_value.tv_sec = ms / 1000; - its.it_value.tv_nsec = (long)(ms % 1000) * 1000000L; - timerfd_settime(g_touch_timer, 0, &its, NULL); +static void cursor_hide(void) { + if (g_cur_hidden) return; + g_cur_hidden = 1; + if (g_cur_ready && g_seat_enabled) drmModeSetCursor(g_drm, g_crtc, 0, 0, 0); } -static void touch_timer_disarm(void) { - if (g_touch_timer < 0) return; - struct itimerspec its = {0}; - timerfd_settime(g_touch_timer, 0, &its, NULL); -} +/* ---- touch --------------------------------------------------------------- */ -static void touch_cancel(void) { - touch_timer_disarm(); - if (g_touch_state == TOUCH_DRAG) { - q_push(EV_BUTTON, BTN_LEFT_CODE, 0, 0, 0); +/* A finger's position in the rotated output, to 1/256 of a logical pixel. */ +static void touch_push(int phase, struct libinput_event_touch *t) { + double rx = libinput_event_touch_get_x_transformed(t, (uint32_t)g_base_w); + double ry = libinput_event_touch_get_y_transformed(t, (uint32_t)g_base_h); + double x = rx, y = ry; + switch (g_rot) { + case 1: x = ry; y = g_base_w - rx; break; + case 2: x = g_base_w - rx; y = g_base_h - ry; break; + case 3: x = g_base_h - ry; y = rx; break; + default: break; } - g_touch_state = TOUCH_IDLE; + if (x < 0) x = 0; + if (y < 0) y = 0; + if (x > g_width - 1) x = g_width - 1; + if (y > g_height - 1) y = g_height - 1; + q_push(EV_TOUCH, phase, libinput_event_touch_get_seat_slot(t), (int32_t)(x * 256.0), (int32_t)(y * 256.0)); +} + +/* Every finger's touch is over (the screen turned off or rotated, the VT switched away). */ +static void touch_cancel(void) { + q_push(EV_TOUCH, TP_CANCEL, 0, 0, 0); } /* ---- xkb + libinput ------------------------------------------------------ */ @@ -401,6 +400,7 @@ static void handle_li_event(struct libinput_event *ev) { } case LIBINPUT_EVENT_POINTER_MOTION: { struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev); + g_cur_hidden = 0; g_px += (int)libinput_event_pointer_get_dx(p); g_py += (int)libinput_event_pointer_get_dy(p); clamp_pointer(); @@ -410,6 +410,7 @@ static void handle_li_event(struct libinput_event *ev) { } case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE: { struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev); + g_cur_hidden = 0; int rx = (int)libinput_event_pointer_get_absolute_x_transformed(p, (uint32_t)g_base_w); int ry = (int)libinput_event_pointer_get_absolute_y_transformed(p, (uint32_t)g_base_h); unrot_to_logical(rx, ry, &g_px, &g_py); @@ -441,74 +442,25 @@ static void handle_li_event(struct libinput_event *ev) { } break; } - case LIBINPUT_EVENT_TOUCH_DOWN: { - struct libinput_event_touch *t = libinput_event_get_touch_event(ev); - if (g_touch_state == TOUCH_IDLE) { - g_touch_slot = libinput_event_touch_get_seat_slot(t); - int rx = (int)libinput_event_touch_get_x_transformed(t, (uint32_t)g_base_w); - int ry = (int)libinput_event_touch_get_y_transformed(t, (uint32_t)g_base_h); - unrot_to_logical(rx, ry, &g_px, &g_py); - clamp_pointer(); - g_touch_x0 = g_px; - g_touch_y0 = g_py; - g_touch_state = TOUCH_PENDING; - q_push(EV_MOVE, g_px, g_py, 0, 0); - cursor_apply(); - touch_timer_arm(TOUCH_LONG_MS); - } + case LIBINPUT_EVENT_DEVICE_ADDED: + if (libinput_device_has_capability(libinput_event_get_device(ev), LIBINPUT_DEVICE_CAP_TOUCH)) + q_push(EV_TOUCH, TP_PRESENT, 0, 0, 0); break; - } - case LIBINPUT_EVENT_TOUCH_MOTION: { - struct libinput_event_touch *t = libinput_event_get_touch_event(ev); - if (g_touch_state != TOUCH_IDLE && libinput_event_touch_get_seat_slot(t) == g_touch_slot) { - int rx = (int)libinput_event_touch_get_x_transformed(t, (uint32_t)g_base_w); - int ry = (int)libinput_event_touch_get_y_transformed(t, (uint32_t)g_base_h); - unrot_to_logical(rx, ry, &g_px, &g_py); - clamp_pointer(); - if (g_touch_state == TOUCH_PENDING) { - int dx = g_px - g_touch_x0; - int dy = g_py - g_touch_y0; - if (dx * dx + dy * dy > TOUCH_DRAG_PX * TOUCH_DRAG_PX) { - touch_timer_disarm(); - g_touch_state = TOUCH_DRAG; - q_push(EV_MOVE, g_touch_x0, g_touch_y0, 0, 0); - q_push(EV_BUTTON, BTN_LEFT_CODE, 1, 0, 0); - q_push(EV_MOVE, g_px, g_py, 0, 0); - cursor_apply(); - } - } else if (g_touch_state == TOUCH_DRAG) { - q_push(EV_MOVE, g_px, g_py, 0, 0); - cursor_apply(); - } - } + /* Fingers go to the compositor as they are: it decides which are an app's and which work the + * shell like a mouse (compositor/host/touch.mbt). */ + case LIBINPUT_EVENT_TOUCH_DOWN: + cursor_hide(); + touch_push(TP_DOWN, libinput_event_get_touch_event(ev)); break; - } - case LIBINPUT_EVENT_TOUCH_UP: { - struct libinput_event_touch *t = libinput_event_get_touch_event(ev); - if (g_touch_state != TOUCH_IDLE && libinput_event_touch_get_seat_slot(t) == g_touch_slot) { - int st = g_touch_state; - touch_timer_disarm(); - g_touch_state = TOUCH_IDLE; - if (st == TOUCH_PENDING) { - g_px = g_touch_x0; - g_py = g_touch_y0; - q_push(EV_MOVE, g_px, g_py, 0, 0); - q_push(EV_BUTTON, BTN_LEFT_CODE, 1, 0, 0); - q_push(EV_BUTTON, BTN_LEFT_CODE, 0, 0, 0); - cursor_apply(); - } else if (st == TOUCH_DRAG) { - q_push(EV_BUTTON, BTN_LEFT_CODE, 0, 0, 0); - } - } + case LIBINPUT_EVENT_TOUCH_MOTION: + touch_push(TP_MOTION, libinput_event_get_touch_event(ev)); break; - } - case LIBINPUT_EVENT_TOUCH_CANCEL: { - struct libinput_event_touch *t = libinput_event_get_touch_event(ev); - if (g_touch_state != TOUCH_IDLE && libinput_event_touch_get_seat_slot(t) == g_touch_slot) { - touch_cancel(); - } + case LIBINPUT_EVENT_TOUCH_UP: + q_push(EV_TOUCH, TP_UP, libinput_event_touch_get_seat_slot(libinput_event_get_touch_event(ev)), 0, 0); + break; + case LIBINPUT_EVENT_TOUCH_CANCEL: + touch_cancel(); break; - } default: break; } @@ -668,7 +620,6 @@ int32_t mp_kms_open(void) { fprintf(stderr, "kms: cannot start libinput on seat %s\n", g_seat ? libseat_seat_name(g_seat) : "?"); return -6; } - g_touch_timer = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); dispatch_input(); return 0; } @@ -697,13 +648,12 @@ int32_t mp_kms_set_rotation(int32_t rot) { MOONBIT_FFI_EXPORT int32_t mp_kms_poll(void) { if (!g_seat) return 0; - struct pollfd pfd[4]; + struct pollfd pfd[3]; int n = 0; pfd[n].fd = libseat_get_fd(g_seat); pfd[n].events = POLLIN; n++; pfd[n].fd = g_drm; pfd[n].events = POLLIN; n++; int lifd = g_li ? libinput_get_fd(g_li) : -1; pfd[n].fd = lifd; pfd[n].events = POLLIN; n++; - pfd[n].fd = g_touch_timer; pfd[n].events = POLLIN; n++; poll(pfd, (nfds_t)n, 0); libseat_dispatch(g_seat, 0); if (pfd[1].revents & POLLIN) { @@ -727,24 +677,10 @@ int32_t mp_kms_poll(void) { if (g_blank) set_dpms(0); } if (g_li) dispatch_input(); - if (g_touch_timer >= 0 && (pfd[3].revents & POLLIN)) { - uint64_t exp = 0; - while (read(g_touch_timer, &exp, sizeof exp) > 0) {} - if (g_touch_state == TOUCH_PENDING) { - g_touch_state = TOUCH_LONG; - g_px = g_touch_x0; - g_py = g_touch_y0; - q_push(EV_MOVE, g_px, g_py, 0, 0); - q_push(EV_BUTTON, BTN_RIGHT_CODE, 1, 0, 0); - q_push(EV_BUTTON, BTN_RIGHT_CODE, 0, 0, 0); - cursor_apply(); - } - } return g_seat_enabled; } -/* The fds mp_kms_poll services (0 seat, 1 DRM, 2 libinput, 3 touch timer), for an idle loop to - block on; -1 when not open. */ +/* The fds mp_kms_poll services (0 seat, 1 DRM, 2 libinput), for an idle loop to block on; -1 when not open. */ MOONBIT_FFI_EXPORT int32_t mp_kms_fd(int32_t i) { if (!g_seat) return -1; @@ -752,7 +688,6 @@ int32_t mp_kms_fd(int32_t i) { case 0: return libseat_get_fd(g_seat); case 1: return g_drm; case 2: return g_li ? libinput_get_fd(g_li) : -1; - case 3: return g_touch_timer; default: return -1; } } @@ -945,7 +880,6 @@ int32_t mp_kms_keymap_copy(moonbit_bytes_t out) { MOONBIT_FFI_EXPORT void mp_kms_close(void) { touch_cancel(); - if (g_touch_timer >= 0) { close(g_touch_timer); g_touch_timer = -1; } g_phys_w = 0; g_phys_h = 0; g_base_w = 0; g_base_h = 0; g_rot = 0; g_mm_w = 0; g_mm_h = 0; g_scale = 1; if (g_li) { libinput_unref(g_li); g_li = NULL; } if (g_udev) { udev_unref(g_udev); g_udev = NULL; } diff --git a/compositor/kms/kms_wasm.mbt b/compositor/kms/kms_wasm.mbt index 9e573db..880c2cb 100644 --- a/compositor/kms/kms_wasm.mbt +++ b/compositor/kms/kms_wasm.mbt @@ -74,6 +74,11 @@ pub fn Kms::take_input(_self : Kms) -> Array[@nested.InputEvent] { Key(k) => out.push(Key(k.code, k.pressed)) Modifiers(m) => out.push(Modifiers(m.depressed, m.latched, m.locked, m.group)) Scroll(d) => out.push(Scroll(d.axis, d.value, d.discrete)) + TouchDown(t) => out.push(Touch(Down(t.id, t.x, t.y))) + TouchMotion(t) => out.push(Touch(Motion(t.id, t.x, t.y))) + TouchUp(id) => out.push(Touch(Up(id))) + TouchCancel => out.push(Touch(Cancel)) + TouchPresent => out.push(Touch(Present)) } } out diff --git a/compositor/nested/nested.mbt b/compositor/nested/nested.mbt index 80c0344..c7108d2 100644 --- a/compositor/nested/nested.mbt +++ b/compositor/nested/nested.mbt @@ -12,6 +12,19 @@ pub(all) enum InputEvent { Scroll(UInt, Double, Int) Key(UInt, Bool) Modifiers(UInt, UInt, UInt, UInt) + /// The touchscreen (only from a login session's own screen). + Touch(TouchEvent) +} + +///| A finger (by id, reused once it is up) on the touchscreen, in output pixels. +pub(all) enum TouchEvent { + Down(Int, Double, Double) + Motion(Int, Double, Double) + Up(Int) + /// Every finger's touch is over, without a tap. + Cancel + /// The seat has a touchscreen. + Present } ///| diff --git a/compositor/wayland/server.mbt b/compositor/wayland/server.mbt index f44a298..4d4c0b6 100644 --- a/compositor/wayland/server.mbt +++ b/compositor/wayland/server.mbt @@ -226,6 +226,7 @@ struct ClientConn { toplevels : Map[UInt64, ToplevelState] positioners : Map[UInt, PosState] pointer_ids : Array[UInt] + touch_ids : Array[UInt] keyboard_ids : Array[UInt] mut seat_ver : UInt /// xdg_wm_base version the client bound (`wm_capabilities` arrived in v5). @@ -269,6 +270,7 @@ fn ClientConn::new(client_id : UInt, fd : Int) -> ClientConn { toplevels: {}, positioners: {}, pointer_ids: [], + touch_ids: [], keyboard_ids: [], seat_ver: 1U, xdg_ver: 1U, @@ -348,6 +350,8 @@ pub(all) struct WaylandServer { mut next_win_id : UInt64 mut next_serial : UInt mut keymap : Bytes + /// The seat has a touchscreen (wl_seat's capabilities). + mut touch : Bool /// Clipboard owner: (client id, wl_data_source id). mut selection : (UInt, UInt)? /// Primary selection owner (the last text selected in any app): (client id, source id). @@ -394,6 +398,7 @@ pub fn WaylandServer::listen( socket_path, listen_fd: fd, output_w, + touch: false, output_h, clients: [], win_to_client: {}, @@ -499,10 +504,15 @@ fn emit_shm_formats(c : ClientConn, shm_id : UInt) -> Unit { } ///| -fn emit_seat_info(c : ClientConn, seat_id : UInt, ver : UInt) -> Unit { - let w1 = WireWriter::new() - w1.u32(3U) // pointer (1) | keyboard (2) - c.send_msg(w1.finish_msg(seat_id, 0U)) +fn emit_seat_caps(c : ClientConn, seat_id : UInt, touch : Bool) -> Unit { + let w = WireWriter::new() + w.u32(if touch { 7U } else { 3U }) // pointer (1) | keyboard (2) | touch (4) + c.send_msg(w.finish_msg(seat_id, 0U)) +} + +///| +fn emit_seat_info(c : ClientConn, seat_id : UInt, ver : UInt, touch : Bool) -> Unit { + emit_seat_caps(c, seat_id, touch) // wl_seat.name is since version 2: a v1 client (vkcube) aborts on an event it has no handler for. if ver >= 2U { @@ -691,7 +701,7 @@ fn WaylandServer::dispatch_msg( } else if iface == "wl_seat" { c.objects.set(new_id, ObjSeat) c.seat_ver = ver - emit_seat_info(c, new_id, ver) + emit_seat_info(c, new_id, ver, self.touch) } else if iface == "wp_cursor_shape_manager_v1" { c.objects.set(new_id, ObjCursorShapeMgr) } else if iface == "wl_data_device_manager" { @@ -1598,6 +1608,13 @@ fn WaylandServer::dispatch_msg( } else if opcode == 2U { let tid = reader.u32() c.objects.set(tid, ObjTouch) + c.touch_ids.push(tid) + } + ObjTouch => + if opcode == 0U { // release + c.touch_ids.retain(id => id != obj_id) + c.objects.remove(obj_id) + send_delete_id(c, obj_id) } _ => () } @@ -2100,6 +2117,102 @@ fn WaylandServer::pointer_frame(_self : WaylandServer, c : ClientConn, pid : UIn } } +///| The seat has a touchscreen: clients are told, also those already connected. +pub fn WaylandServer::set_touch(self : WaylandServer) -> Unit { + if self.touch { + return + } + self.touch = true + for c in self.clients { + if c.alive { + for id, obj in c.objects { + if obj is ObjSeat { + emit_seat_caps(c, id, true) + } + } + } + } +} + +///| Whether the client of window or popup `win_id` listens to touches. +pub fn WaylandServer::takes_touch(self : WaylandServer, win_id : UInt64) -> Bool { + match self.win_client(win_id) { + Some((c, _)) => c.touch_ids.length() > 0 + None => false + } +} + +///| wl_touch.down of finger `id` at surface-local (x, y) of `sub`, the window's subsurface (its +/// wl_surface id), 0 for the window itself. +pub fn WaylandServer::touch_down( + self : WaylandServer, + win_id : UInt64, + time_ms : UInt, + id : Int, + x : Double, + y : Double, + sub? : UInt = 0U, +) -> Unit { + guard self.win_client(win_id) is Some((c, tl)) else { return } + let target = if sub != 0U && c.surfaces.contains(sub) { sub } else { tl.wl_surface_id } + if !c.surfaces.contains(target) { + return + } + for tid in c.touch_ids { + let w = WireWriter::new() + w.u32(self.next_serial()) + w.u32(time_ms) + w.u32(target) + w.i32(id) + w.i32(fixed(x)) + w.i32(fixed(y)) + c.send_msg(w.finish_msg(tid, 0U)) + c.send_msg(WireWriter::new().finish_msg(tid, 3U)) // frame + } +} + +///| +pub fn WaylandServer::touch_up(self : WaylandServer, win_id : UInt64, time_ms : UInt, id : Int) -> Unit { + guard self.win_client(win_id) is Some((c, _)) else { return } + for tid in c.touch_ids { + let w = WireWriter::new() + w.u32(self.next_serial()) + w.u32(time_ms) + w.i32(id) + c.send_msg(w.finish_msg(tid, 1U)) + c.send_msg(WireWriter::new().finish_msg(tid, 3U)) + } +} + +///| +pub fn WaylandServer::touch_motion( + self : WaylandServer, + win_id : UInt64, + time_ms : UInt, + id : Int, + x : Double, + y : Double, +) -> Unit { + guard self.win_client(win_id) is Some((c, _)) else { return } + for tid in c.touch_ids { + let w = WireWriter::new() + w.u32(time_ms) + w.i32(id) + w.i32(fixed(x)) + w.i32(fixed(y)) + c.send_msg(w.finish_msg(tid, 2U)) + c.send_msg(WireWriter::new().finish_msg(tid, 3U)) + } +} + +///| wl_touch.cancel: the compositor took the gesture. +pub fn WaylandServer::touch_cancel(self : WaylandServer, win_id : UInt64) -> Unit { + guard self.win_client(win_id) is Some((c, _)) else { return } + for tid in c.touch_ids { + c.send_msg(WireWriter::new().finish_msg(tid, 4U)) + } +} + ///| pub fn WaylandServer::keyboard_enter(self : WaylandServer, win_id : UInt64) -> Unit { match self.win_client(win_id) { diff --git a/docs/handset.md b/docs/handset.md index 6f93bf2..ecaac1d 100644 --- a/docs/handset.md +++ b/docs/handset.md @@ -14,7 +14,10 @@ Chandra includes KMS/libinput/compositor adaptations tailored for mainline Linux ## Touch Input & On-Screen Keyboard -- **Long-press right-click:** In [`compositor/kms/kms_stub.c`](../compositor/kms/kms_stub.c), a stationary touch held for `500 ms` converts to a synthetic right-click (`BTN_RIGHT`) via a `timerfd`, allowing context menus and secondary actions without a mouse. +- **Touch goes to apps as touch (`wl_touch`):** a finger on an app's own content is the app's: it scrolls with a thumb, flings, pinches and long-presses as that app does ([`compositor/host/touch.mbt`](../compositor/host/touch.mbt)). The seat only says it has a touchscreen when libinput finds one. If the app answers a touch with a window move, resize or drag and drop, its touch is cancelled and the finger drags from there. +- **Elsewhere a finger is a mouse:** on the panel, the on-screen keyboard, the desktop, title bars, dialogs, and apps that don't listen to touches, a tap clicks, a drag is a left-button drag, and a stationary touch held for `500 ms` is a right click. +- **Cursor:** hidden while fingers are in use; moving a mouse shows it again. +- **Testing:** `chandra-ctl input-touch down ID X Y`, `motion ID X Y`, `up ID`, `cancel` act like the touchscreen. - **Built-in OSK:** [`compositor/host/osk.mbt`](../compositor/host/osk.mbt) provides a 5-row virtual keyboard that opens automatically when Wayland text input (`zwp_text_input_v3`) gains focus and can also be toggled manually from the **`KB`** (`osk`) panel module. It sits directly above the panel, so the panel stays visible and usable while typing (a strict kiosk has no panel: there it reaches the bottom edge). ## Display Rotation, Flip, and Auto-Rotate diff --git a/interface/chandra/host/display/top.mbt b/interface/chandra/host/display/top.mbt index 9549255..05a3dec 100644 --- a/interface/chandra/host/display/top.mbt +++ b/interface/chandra/host/display/top.mbt @@ -23,12 +23,22 @@ pub(all) struct ScrollDelta { axis : UInt; value : Double; discrete : Int } ///| +/// A finger on the touchscreen, by id (reused once it is up), in output pixels. +pub(all) struct TouchPoint { + id : Int; x : Double; y : Double +} +///| pub(all) enum SeatEvent { PointerMove(PointerPos) PointerButton(KeyState) Key(KeyState) Modifiers(Modifiers) Scroll(ScrollDelta) + TouchDown(TouchPoint) + TouchMotion(TouchPoint) + TouchUp(Int) + TouchCancel + TouchPresent } ///| /// Take over the first connected output and the seat's input. None, with the reason on the @@ -109,7 +119,9 @@ pub fn set_power(on : Bool) -> Unit { /// Returns `(width << 16) | height` of the new logical output size. pub fn set_rotation(rot : UInt) -> UInt { - (wasmImportSetRotation((rot).reinterpret_as_int())).reinterpret_as_uint() + let result : Int = wasmImportSetRotation((rot).reinterpret_as_int()); + let ret = (result).reinterpret_as_uint() + return ret } ///| @@ -144,6 +156,26 @@ pub fn take_input() -> Array[SeatEvent] { SeatEvent::Scroll(ScrollDelta::{axis : (mbt_ffi_load32((iter_base) + 8)).reinterpret_as_uint(), value : mbt_ffi_loadf64((iter_base) + 16), discrete : mbt_ffi_load32((iter_base) + 24)}) } + 5 => { + + SeatEvent::TouchDown(TouchPoint::{id : mbt_ffi_load32((iter_base) + 8), x : mbt_ffi_loadf64((iter_base) + 16), y : mbt_ffi_loadf64((iter_base) + 24)}) + } + 6 => { + + SeatEvent::TouchMotion(TouchPoint::{id : mbt_ffi_load32((iter_base) + 8), x : mbt_ffi_loadf64((iter_base) + 16), y : mbt_ffi_loadf64((iter_base) + 24)}) + } + 7 => { + + SeatEvent::TouchUp(mbt_ffi_load32((iter_base) + 8)) + } + 8 => { + + SeatEvent::TouchCancel + } + 9 => { + + SeatEvent::TouchPresent + } _ => panic() } diff --git a/wasmhost/bin/display.mbt b/wasmhost/bin/display.mbt index 4942fea..0e851c8 100644 --- a/wasmhost/bin/display.mbt +++ b/wasmhost/bin/display.mbt @@ -57,7 +57,7 @@ extern "C" fn rs_present_kms() = "rs_present_kms" ///| The seat, DRM and input fds while the display is open (none otherwise), for an idle wait. fn display_wait_fds() -> Array[Int] { let out : Array[Int] = [] - for i in 0..<4 { + for i in 0..<3 { let fd = mp_kms_fd(i) if fd >= 0 { out.push(fd) @@ -116,6 +116,24 @@ fn take_input() -> Array[@wt.Val] { ), ), ) + 6 => { + let point = fn() -> @wt.Val? { + Some( + Record([ + ("id", S32(b)), + ("x", F64(mp_kms_arg(3).to_double() / 256.0)), + ("y", F64(mp_kms_arg(4).to_double() / 256.0)), + ]), + ) + } + match a { + 0 => out.push(Variant("touch-down", point())) + 1 => out.push(Variant("touch-motion", point())) + 2 => out.push(Variant("touch-up", Some(S32(b)))) + 3 => out.push(Variant("touch-cancel", None)) + _ => out.push(Variant("touch-present", None)) + } + } _ => () } } diff --git a/wit/host/display.wit b/wit/host/display.wit index 089b550..94a577f 100644 --- a/wit/host/display.wit +++ b/wit/host/display.wit @@ -39,12 +39,21 @@ interface display { /// Wheel/touchpad scroll: axis (0 vertical, 1 horizontal), distance in pixels (positive = /// down/right), wheel notches (0 when not from a wheel). record scroll-delta { axis: u32, value: f64, discrete: s32 } + /// A finger on the touchscreen, by id (reused once it is up), in output pixels. + record touch-point { id: s32, x: f64, y: f64 } variant seat-event { pointer-move(pointer-pos), pointer-button(key-state), key(key-state), modifiers(modifiers), scroll(scroll-delta), + touch-down(touch-point), + touch-motion(touch-point), + touch-up(s32), + /// Every finger's touch is over, without a tap. + touch-cancel, + /// The seat has a touchscreen. + touch-present, } /// Input queued since the last call. take-input: func() -> list;