Make the desktop interactive: pointer/keyboard routing, launcher popup, app launching
CI / test (push) Successful in 25s
CI / test (push) Successful in 25s
- server: wl_pointer/wl_keyboard events (enter/leave/motion/button/key/modifiers), host keymap shared with client keyboards - nested: receives host pointer + keyboard + keymap, queues InputEvents - host: routes input to the launcher popup or the window under the pointer; click-to-focus moves keyboard focus - shell: launcher button opens lib/launcher popup; entries start real apps (foot, konsole, dolphin, firefox, kate, systemsettings if installed) with WAYLAND_DISPLAY pointed at the compositor - sys: has_exe + spawn - e2e test for input delivery and the launcher popup Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a1b85474f8
commit
a63f65806d
10 files changed
+740
-33
No files matched your search
@@ -23,6 +23,11 @@ pub(all) struct MoonPlasmaCompositor {
|
||||
windows : Map[UInt64, HostWindow]
|
||||
damage : @scene.DamageTracker
|
||||
shell : @shell.DesktopShell
|
||||
mut ptr_x : Double
|
||||
mut ptr_y : Double
|
||||
mut ptr_win : UInt64?
|
||||
mut kbd_win : UInt64?
|
||||
mut mods : UInt
|
||||
mut layout_mode : @wm.LayoutMode
|
||||
}
|
||||
|
||||
@@ -54,7 +59,12 @@ pub fn MoonPlasmaCompositor::new(
|
||||
work_area,
|
||||
windows: {},
|
||||
damage: @scene.DamageTracker::new(),
|
||||
shell: @shell.DesktopShell::new(),
|
||||
shell: @shell.DesktopShell::new(socket_path),
|
||||
ptr_x: 0.0,
|
||||
ptr_y: 0.0,
|
||||
ptr_win: None,
|
||||
kbd_win: None,
|
||||
mods: 0U,
|
||||
layout_mode: @wm.LayoutMode::Tiling,
|
||||
})
|
||||
}
|
||||
@@ -91,6 +101,7 @@ fn MoonPlasmaCompositor::apply_actions(
|
||||
for id, w in self.windows {
|
||||
w.focused = id == wid
|
||||
}
|
||||
self.set_kbd_focus(Some(wid))
|
||||
self.damage.mark_full()
|
||||
}
|
||||
@wm.WmAction::Raise(_) => self.damage.mark_full()
|
||||
@@ -177,6 +188,12 @@ pub fn MoonPlasmaCompositor::step(self : MoonPlasmaCompositor) -> Unit {
|
||||
}
|
||||
@wayland.CompositorEvent::ToplevelClosed(wid) => {
|
||||
ignore(self.windows.remove(wid))
|
||||
if self.ptr_win == Some(wid) {
|
||||
self.ptr_win = None
|
||||
}
|
||||
if self.kbd_win == Some(wid) {
|
||||
self.kbd_win = None
|
||||
}
|
||||
let actions = self.wm.close_window(wid, self.work_area)
|
||||
self.apply_actions(actions)
|
||||
}
|
||||
@@ -321,3 +338,167 @@ pub fn MoonPlasmaCompositor::handle_gfx_events(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///| Move keyboard focus (wl_keyboard leave/enter + current modifiers).
|
||||
fn MoonPlasmaCompositor::set_kbd_focus(self : MoonPlasmaCompositor, wid : UInt64?) -> Unit {
|
||||
if self.kbd_win == wid {
|
||||
return
|
||||
}
|
||||
match self.kbd_win {
|
||||
Some(old) => self.server.keyboard_leave(old)
|
||||
None => ()
|
||||
}
|
||||
self.kbd_win = wid
|
||||
match wid {
|
||||
Some(w) => {
|
||||
self.server.keyboard_enter(w)
|
||||
self.server.keyboard_modifiers(w, self.mods, 0U, 0U, 0U)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
///| Top-most window under (x, y): (id, in client area). The titlebar counts as the window but not the client.
|
||||
fn MoonPlasmaCompositor::window_at(self : MoonPlasmaCompositor, x : Int, y : Int) -> (UInt64, Bool)? {
|
||||
let order = self.wm.stacking_order()
|
||||
let mut i = order.length() - 1
|
||||
while i >= 0 {
|
||||
match self.windows.get(order[i]) {
|
||||
Some(win) => {
|
||||
let g = win.geometry
|
||||
if x >= g.x && x < g.x + g.w && y >= g.y && y < g.y + g.h {
|
||||
return Some((win.id, true))
|
||||
}
|
||||
if x >= g.x - 2 && x < g.x + g.w + 2 && y >= g.y - 32 && y < g.y {
|
||||
return Some((win.id, false))
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
i = i - 1
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
///|
|
||||
fn MoonPlasmaCompositor::leave_pointer(self : MoonPlasmaCompositor) -> Unit {
|
||||
match self.ptr_win {
|
||||
Some(w) => {
|
||||
self.server.pointer_leave(w)
|
||||
self.ptr_win = None
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
///| evdev keycode -> the key name lib/launcher understands (US layout).
|
||||
fn key_name(code : UInt, shift : Bool) -> String {
|
||||
let rows = [
|
||||
(16U, "qwertyuiop"),
|
||||
(30U, "asdfghjkl"),
|
||||
(44U, "zxcvbnm"),
|
||||
(2U, "1234567890"),
|
||||
]
|
||||
for r in rows {
|
||||
let (first, chars) = r
|
||||
if code >= first && code < first + chars.length().reinterpret_as_uint() {
|
||||
let c = chars[(code - first).reinterpret_as_int()]
|
||||
return if shift && c >= 'a' && c <= 'z' {
|
||||
Int::unsafe_to_char(c.to_int() - 32).to_string()
|
||||
} else {
|
||||
c.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
match code {
|
||||
1U => "Escape"
|
||||
14U => "Backspace"
|
||||
28U => "Enter"
|
||||
57U => " "
|
||||
12U => "-"
|
||||
52U => "."
|
||||
53U => "/"
|
||||
_ => ""
|
||||
}
|
||||
}
|
||||
|
||||
///| Route host input: shell (launcher) first, then the window under the pointer
|
||||
/// / the keyboard-focused window.
|
||||
pub fn MoonPlasmaCompositor::handle_input(
|
||||
self : MoonPlasmaCompositor,
|
||||
ev : @nested.InputEvent,
|
||||
time_ms : UInt,
|
||||
) -> Unit {
|
||||
match ev {
|
||||
PointerMove(x, y) => {
|
||||
self.ptr_x = x
|
||||
self.ptr_y = y
|
||||
let xi = x.to_int()
|
||||
let yi = y.to_int()
|
||||
if self.shell.pointer_move(xi, yi) || self.shell.hit(xi, yi) {
|
||||
self.leave_pointer()
|
||||
return
|
||||
}
|
||||
match self.window_at(xi, yi) {
|
||||
Some((wid, true)) =>
|
||||
match self.windows.get(wid) {
|
||||
Some(win) => {
|
||||
let lx = x - win.geometry.x.to_double()
|
||||
let ly = y - win.geometry.y.to_double()
|
||||
if self.ptr_win != Some(wid) {
|
||||
self.leave_pointer()
|
||||
self.ptr_win = Some(wid)
|
||||
self.server.pointer_enter(wid, lx, ly)
|
||||
}
|
||||
self.server.pointer_motion(wid, time_ms, lx, ly)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
_ => self.leave_pointer()
|
||||
}
|
||||
}
|
||||
PointerButton(button, pressed) => {
|
||||
let xi = self.ptr_x.to_int()
|
||||
let yi = self.ptr_y.to_int()
|
||||
if pressed {
|
||||
if self.shell.pointer_down(xi, yi) {
|
||||
return
|
||||
}
|
||||
match self.window_at(xi, yi) {
|
||||
Some((wid, _)) => {
|
||||
self.apply_actions([
|
||||
@wm.WmAction::Focus(wid),
|
||||
@wm.WmAction::Raise(wid),
|
||||
])
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
match self.ptr_win {
|
||||
Some(w) => self.server.pointer_button(w, time_ms, button, pressed)
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
Key(code, pressed) =>
|
||||
if self.shell.popup_open() {
|
||||
if pressed {
|
||||
let name = key_name(code, (self.mods & 1U) != 0U)
|
||||
if name.length() > 0 {
|
||||
self.shell.key_down(name)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match self.kbd_win {
|
||||
Some(w) => self.server.keyboard_key(w, time_ms, code, pressed)
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
Modifiers(dep, lat, lock, group) => {
|
||||
self.mods = dep
|
||||
match self.kbd_win {
|
||||
Some(w) => self.server.keyboard_modifiers(w, dep, lat, lock, group)
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
///| 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
|
||||
|
||||
// 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)
|
||||
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
|
||||
// Escape closes it.
|
||||
comp.handle_input(@nested.InputEvent::Key(1U, true), 24U)
|
||||
assert_false(comp.shell.popup_open())
|
||||
comp.close()
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
"moonplasma/desktop/compositor/sys" @sys,
|
||||
"moonplasma/desktop/compositor/scene" @scene,
|
||||
"moonplasma/desktop/compositor/shell" @shell,
|
||||
"moonplasma/desktop/compositor/nested" @nested,
|
||||
"moonplasma/desktop/compositor/renderer" @renderer,
|
||||
"moonplasma/desktop/compositor/wayland" @wayland,
|
||||
"moonplasma/desktop/compositor/gfx" @gfx,
|
||||
|
||||
Reference in new issue
Block a user