Files
chandra/compositor/host/input_test.mbt
T
nandiandClaude Sonnet 5.5 59ea224d38
CI / test (push) Successful in 45s
12-hour clock and hover-dependent cursor
- timefmt.hhmm12 + render_digital(hour12=); compositor panel shows '7:51 PM'
  (wasm clock component keeps 24h by default)
- wp_cursor_shape_v1: server lets clients pick their cursor; nested window
  forwards the shape to the host. Launcher button/entries show a hand, the
  search field a text cursor, client windows what the client asked for.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 19:51:16 -08:00

116 lines
4.1 KiB
MoonBit

///| Input routing: host pointer/keyboard events reach the focused client window,
/// and the launcher button opens the shell's launcher popup.
///| Drain everything the compositor sent this client as (object, opcode) pairs.
fn drain_msgs(c : TestWaylandClient) -> Array[(UInt, UInt)] {
let scratch : FixedArray[Byte] = FixedArray::make(65536, b'\x00')
let raw : Array[Byte] = []
while @sys.poll_read(c.fd, 5) > 0 {
let (n, rfd) = @sys.recvmsg_fd(c.fd, scratch, 65536)
if rfd >= 0 {
@sys.close_fd(rfd)
}
if n <= 0 {
break
}
for i = 0; i < n; i = i + 1 {
raw.push(scratch[i])
}
}
let out : Array[(UInt, UInt)] = []
let mut off = 0
while off + 8 <= raw.length() {
let obj = @wayland.read_u32_le(raw, off)
let w2 = @wayland.read_u32_le(raw, off + 4)
let size = ((w2 >> 16) & 0xFFFFU).reinterpret_as_int()
if size < 8 {
break
}
out.push((obj, w2 & 0xFFFFU))
off = off + size
}
out
}
///|
fn has_msg(msgs : Array[(UInt, UInt)], obj : UInt, op : UInt) -> Bool {
msgs.iter().any(m => m.0 == obj && m.1 == op)
}
///|
test "host input reaches the focused client and the launcher popup opens" {
let socket_path = "/tmp/wayland-moon-input-" +
@sys.getpid().to_string() +
".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let (c, shm, surf, xdg, _tl, _deco) = open_and_configure_client(
comp, socket_path, "T", "t",
)
commit_shm_frame(comp, c, shm, surf, xdg, 1UL)
// Bind the seat and fetch its pointer + keyboard.
let reg_id = 2U
let seat_id = bind_global(c, reg_id, "wl_seat", 7U)
let ptr_id = c.alloc_id()
let w1 = @wayland.WireWriter::new()
w1.u32(ptr_id)
c.send(w1.finish_msg(seat_id, 0U))
let kbd_id = c.alloc_id()
let w2 = @wayland.WireWriter::new()
w2.u32(kbd_id)
c.send(w2.finish_msg(seat_id, 1U))
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
ignore(drain_msgs(c))
// Pointer over the window: enter + motion; press goes through as a button.
comp.handle_input(@nested.InputEvent::PointerMove(640.0, 300.0), 10U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 11U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 12U)
// Keyboard goes to the focused window.
comp.handle_input(@nested.InputEvent::Key(30U, true), 13U)
let msgs = drain_msgs(c)
assert_true(has_msg(msgs, ptr_id, 0U)) // wl_pointer.enter
assert_true(has_msg(msgs, ptr_id, 2U)) // motion
assert_true(has_msg(msgs, ptr_id, 3U)) // button
assert_true(has_msg(msgs, kbd_id, 3U)) // wl_keyboard.key
// Cursor: a client that asks for a text cursor gets it inside its window...
let shape_mgr = bind_global(c, reg_id, "wp_cursor_shape_manager_v1", 1U)
let shape_dev = c.alloc_id()
let w3 = @wayland.WireWriter::new()
w3.u32(shape_dev)
w3.u32(ptr_id)
c.send(w3.finish_msg(shape_mgr, 1U))
let w4 = @wayland.WireWriter::new()
w4.u32(1U) // serial
w4.u32(9U) // text
c.send(w4.finish_msg(shape_dev, 1U))
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
assert_eq(comp.desired_cursor(), 9U)
// Launcher: click the button -> popup opens and swallows keys.
ignore(comp.compose_frame()) // lays out the panel
comp.handle_input(@nested.InputEvent::PointerMove(60.0, 700.0), 20U)
assert_eq(comp.desired_cursor(), 4U) // ...but the launcher button always shows a hand
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 21U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 22U)
assert_true(comp.shell.popup_open())
comp.handle_input(@nested.InputEvent::Key(30U, true), 23U)
let after = drain_msgs(c)
assert_false(has_msg(after, kbd_id, 3U)) // swallowed by the launcher
// Off the shell and off any window: default arrow.
comp.handle_input(@nested.InputEvent::PointerMove(1270.0, 5.0), 25U)
assert_eq(comp.desired_cursor(), 1U)
// Escape closes it.
comp.handle_input(@nested.InputEvent::Key(1U, true), 24U)
assert_false(comp.shell.popup_open())
comp.close()
}