From a63f65806d72b9918d54a0e75e917c11770e7cba Mon Sep 17 00:00:00 2001 From: nandithebull Date: Tue, 24 Nov 2026 19:11:15 -0800 Subject: [PATCH] Make the desktop interactive: pointer/keyboard routing, launcher popup, app launching - 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 --- compositor/bin/main.mbt | 10 +- compositor/host/compositor.mbt | 183 +++++++++++++++++++++++++++++- compositor/host/input_test.mbt | 94 ++++++++++++++++ compositor/host/moon.pkg | 1 + compositor/nested/moon.pkg | 1 - compositor/nested/nested.mbt | 100 ++++++++++++----- compositor/shell/shell.mbt | 127 ++++++++++++++++++++- compositor/sys/sys.mbt | 16 +++ compositor/sys/sys_stub.c | 41 +++++++ compositor/wayland/server.mbt | 200 ++++++++++++++++++++++++++++++++- 10 files changed, 740 insertions(+), 33 deletions(-) create mode 100644 compositor/host/input_test.mbt diff --git a/compositor/bin/main.mbt b/compositor/bin/main.mbt index 6575d50..c17cb23 100644 --- a/compositor/bin/main.mbt +++ b/compositor/bin/main.mbt @@ -82,10 +82,16 @@ fn main { comp.step() match nested { Some(n) => { - if !n.poll(gfx_surface) { + if !n.poll() { break } - comp.handle_gfx_events(gfx_surface) + if n.keymap.length() > 0 { + comp.server.set_keymap(n.keymap) + } + let now_ms = @env.now().to_uint() + for ev in n.take_input() { + comp.handle_input(ev, now_ms) + } if n.frame_ready() { n.present(comp.present_to_gfx(gfx_ctx)) } diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index e9bc859..d10dc2f 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -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 => () + } + } + } +} diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt new file mode 100644 index 0000000..c34ce14 --- /dev/null +++ b/compositor/host/input_test.mbt @@ -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() +} diff --git a/compositor/host/moon.pkg b/compositor/host/moon.pkg index 4d79d69..bd9ccc6 100644 --- a/compositor/host/moon.pkg +++ b/compositor/host/moon.pkg @@ -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, diff --git a/compositor/nested/moon.pkg b/compositor/nested/moon.pkg index ad4c940..3507acb 100644 --- a/compositor/nested/moon.pkg +++ b/compositor/nested/moon.pkg @@ -3,7 +3,6 @@ import { "moonplasma/desktop/compositor/sys" @sys, "moonplasma/desktop/compositor/renderer" @renderer, "moonplasma/desktop/compositor/wayland" @wayland, - "moonplasma/desktop/compositor/gfx" @gfx, } supported_targets = "-all+native" diff --git a/compositor/nested/nested.mbt b/compositor/nested/nested.mbt index 248f531..f371320 100644 --- a/compositor/nested/nested.mbt +++ b/compositor/nested/nested.mbt @@ -1,10 +1,22 @@ ///| Nested backend: shows the compositor's frames in a window of the host /// Wayland session. The compositor is a plain Wayland *client* here (wl_shm /// buffers over SCM_RIGHTS + xdg_toplevel), reusing the same wire code and C -/// shim as the server side, and forwards host pointer input to the gfx surface. +/// shim as the server side, and queues host pointer/keyboard input for the compositor. + +///| Input forwarded from the host session (pointer in window pixels; key = evdev code). +pub(all) enum InputEvent { + PointerMove(Double, Double) + PointerButton(UInt, Bool) + Key(UInt, Bool) + Modifiers(UInt, UInt, UInt, UInt) +} ///| pub(all) struct NestedWindow { + input : Array[InputEvent] + fds : Array[Int] + mut keymap : Bytes + mut keyboard_id : UInt fd : Int rx : Array[Byte] globals : Map[String, (UInt, UInt)] @@ -60,12 +72,12 @@ fn NestedWindow::bind(self : NestedWindow, iface : String, ver : UInt) -> UInt { } ///| Read and dispatch everything pending on the host socket (non-blocking). -fn NestedWindow::pump_events(self : NestedWindow, surface : @gfx.Surface?) -> Unit { +fn NestedWindow::pump_events(self : NestedWindow) -> Unit { let scratch : FixedArray[Byte] = FixedArray::make(16384, b'\x00') while @sys.poll_read(self.fd, 0) > 0 { let (n, rfd) = @sys.recvmsg_fd(self.fd, scratch, 16384) if rfd >= 0 { - @sys.close_fd(rfd) + self.fds.push(rfd) } if n <= 0 { self.closed = true @@ -85,7 +97,7 @@ fn NestedWindow::pump_events(self : NestedWindow, surface : @gfx.Surface?) -> Un break } let r = @wayland.WireReader::from_array(self.rx, off + 8) - self.dispatch(obj, op, r, surface) + self.dispatch(obj, op, r) off = off + size } if off > 0 { @@ -106,7 +118,6 @@ fn NestedWindow::dispatch( obj : UInt, op : UInt, r : @wayland.WireReader, - surface : @gfx.Surface?, ) -> Unit { if obj == 1U { if op == 0U { @@ -149,15 +160,51 @@ fn NestedWindow::dispatch( self.closed = true } } else if obj == self.seat_id { - if op == 0U && self.pointer_id == 0U { + if op == 0U { let caps = r.u32() - if (caps & 1U) != 0U { + if (caps & 1U) != 0U && self.pointer_id == 0U { let id = self.alloc() let w = @wayland.WireWriter::new() w.u32(id) self.send(w.finish_msg(self.seat_id, 0U)) self.pointer_id = id } + if (caps & 2U) != 0U && self.keyboard_id == 0U { + let id = self.alloc() + let w = @wayland.WireWriter::new() + w.u32(id) + self.send(w.finish_msg(self.seat_id, 1U)) + self.keyboard_id = id + } + } + } else if self.keyboard_id != 0U && obj == self.keyboard_id { + match op { + 0U => { + // keymap(format, fd, size) + let _format = r.u32() + let size = r.u32().reinterpret_as_int() + if self.fds.length() > 0 { + let fd = self.fds.remove(0) + self.keymap = @sys.mmap_read(fd, size) + @sys.close_fd(fd) + } + } + 3U => { + let _serial = r.u32() + let _time = r.u32() + let key = r.u32() + let state = r.u32() + self.input.push(Key(key, state == 1U)) + } + 4U => { + let _serial = r.u32() + let dep = r.u32() + let lat = r.u32() + let lock = r.u32() + let group = r.u32() + self.input.push(Modifiers(dep, lat, lock, group)) + } + _ => () } } else if self.pointer_id != 0U && obj == self.pointer_id { match op { @@ -166,30 +213,20 @@ fn NestedWindow::dispatch( let _surf = r.u32() self.px = r.i32().to_double() / 256.0 self.py = r.i32().to_double() / 256.0 + self.input.push(PointerMove(self.px, self.py)) } 2U => { let _time = r.u32() self.px = r.i32().to_double() / 256.0 self.py = r.i32().to_double() / 256.0 - match surface { - Some(s) => s.push_pointer_move(self.px, self.py) - None => () - } + self.input.push(PointerMove(self.px, self.py)) } 3U => { let _serial = r.u32() let _time = r.u32() - let _button = r.u32() + let button = r.u32() let state = r.u32() - match surface { - Some(s) => - if state == 1U { - s.push_pointer_down(self.px, self.py) - } else { - s.push_pointer_up(self.px, self.py) - } - None => () - } + self.input.push(PointerButton(button, state == 1U)) } _ => () } @@ -214,7 +251,7 @@ fn NestedWindow::roundtrip(self : NestedWindow) -> Unit { let mut tries = 0 while !self.sync_done && !self.closed && tries < 500 { ignore(@sys.poll_read(self.fd, 10)) - self.pump_events(None) + self.pump_events() tries = tries + 1 } self.sync_cb = 0U @@ -241,6 +278,10 @@ pub fn NestedWindow::open( } let self : NestedWindow = { fd, + input: [], + fds: [], + keymap: Bytes::from_array([]), + keyboard_id: 0U, rx: [], globals: {}, live_buffers: {}, @@ -308,7 +349,7 @@ pub fn NestedWindow::open( let mut tries = 0 while !self.configured && !self.closed && tries < 500 { ignore(@sys.poll_read(self.fd, 10)) - self.pump_events(None) + self.pump_events() tries = tries + 1 } if !self.configured { @@ -318,13 +359,20 @@ pub fn NestedWindow::open( Some(self) } -///| Pump host events (forwarding pointer input to `surface`). False once the +///| Pump host events (input is queued; see take_input). False once the /// window was closed or the host went away. -pub fn NestedWindow::poll(self : NestedWindow, surface : @gfx.Surface) -> Bool { - self.pump_events(Some(surface)) +pub fn NestedWindow::poll(self : NestedWindow) -> Bool { + self.pump_events() !self.closed } +///| Take all input received since the last call. +pub fn NestedWindow::take_input(self : NestedWindow) -> Array[InputEvent] { + let out = self.input.copy() + self.input.clear() + out +} + ///| Show `pixmap` (RGBA8888) in the window. Skipped while the host has not yet /// consumed the previous frame, so a slow host never builds a backlog. pub fn NestedWindow::present(self : NestedWindow, pixmap : @renderer.Pixmap) -> Unit { diff --git a/compositor/shell/shell.mbt b/compositor/shell/shell.mbt index 6d3b669..b9b2c14 100644 --- a/compositor/shell/shell.mbt +++ b/compositor/shell/shell.mbt @@ -128,11 +128,120 @@ fn now() -> @types.Datetime { ///| The desktop shell: wallpaper + bottom panel with launcher, task list and clock. pub(all) struct DesktopShell { launcher_state : @launcher.State + apps : Array[@types.AppEntry] + exec : Map[String, String] + /// Wayland socket handed to launched apps as WAYLAND_DISPLAY. + display : String + mut screen_w : Int + mut launcher_rect : @scene.Rect + mut popup_rect : @scene.Rect +} + +///| Apps the launcher offers, kept only if their executable exists. +/// (id, name, comment, category, glyph, executable) +let candidates : Array[(String, String, String, String, String, String)] = [ + ("org.moonplasma.terminal", "Terminal", "Command-line shell", "System", ">_", "foot"), + ("org.moonplasma.konsole", "Konsole", "KDE terminal", "System", ">_", "konsole"), + ("org.moonplasma.files", "Files", "Browse folders and files", "Utilities", "F", "dolphin"), + ("org.moonplasma.browser", "Web Browser", "Browse the web", "Internet", "W", "firefox"), + ("org.moonplasma.editor", "Text Editor", "Edit plain text files", "Utilities", "T", "kate"), + ("org.moonplasma.settings", "System Settings", "Configure the desktop", "System", "S", "systemsettings"), +] + +///| +pub fn DesktopShell::new(display : String) -> DesktopShell { + let apps : Array[@types.AppEntry] = [] + let exec : Map[String, String] = {} + for c in candidates { + let (id, name, comment, category, glyph, bin) = c + if @sys.has_exe(bin) { + apps.push({ id, name, comment, category, glyph }) + exec.set(id, bin) + } + } + { + launcher_state: @launcher.State::new(), + apps, + exec, + display, + screen_w: 0, + launcher_rect: { x: 0, y: 0, w: 0, h: 0 }, + popup_rect: { x: 0, y: 0, w: 0, h: 0 }, + } } ///| -pub fn DesktopShell::new() -> DesktopShell { - { launcher_state: @launcher.State::new() } +fn inside(r : @scene.Rect, x : Int, y : Int) -> Bool { + r.w > 0 && x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h +} + +///| True while the launcher popup is open (it then owns keyboard input). +pub fn DesktopShell::popup_open(self : DesktopShell) -> Bool { + self.launcher_state.open +} + +///| True if (x, y) is over the launcher button or its open popup. +pub fn DesktopShell::hit(self : DesktopShell, x : Int, y : Int) -> Bool { + inside(self.launcher_rect, x, y) || + (self.launcher_state.open && inside(self.popup_rect, x, y)) +} + +///| +fn DesktopShell::launch(self : DesktopShell, id : String) -> Result[Unit, String] { + match self.exec.get(id) { + None => Err("no executable for " + id) + Some(bin) => + if @sys.spawn(bin, self.display) { + Ok(()) + } else { + Err("could not start " + bin) + } + } +} + +///| Pointer press at (x, y). Returns true when the shell consumed it. +pub fn DesktopShell::pointer_down(self : DesktopShell, x : Int, y : Int) -> Bool { + let launch = fn(id : String) { self.launch(id) } + if inside(self.launcher_rect, x, y) { + ignore( + @launcher.handle_event(self.apps, self.launcher_state, Widget, PointerDown({ x: x - self.launcher_rect.x, y: y - self.launcher_rect.y }), launch), + ) + true + } else if self.launcher_state.open { + if inside(self.popup_rect, x, y) { + ignore( + @launcher.handle_event(self.apps, self.launcher_state, Popup, PointerDown({ x: x - self.popup_rect.x, y: y - self.popup_rect.y }), launch), + ) + true + } else { + // Click outside: dismiss, and let the click through. + ignore(@launcher.handle_event(self.apps, self.launcher_state, Popup, FocusLost, launch)) + false + } + } else { + false + } +} + +///| Pointer motion; returns true when over the popup (so windows don't see it). +pub fn DesktopShell::pointer_move(self : DesktopShell, x : Int, y : Int) -> Bool { + if self.launcher_state.open { + let launch = fn(id : String) { self.launch(id) } + if inside(self.popup_rect, x, y) { + ignore( + @launcher.handle_event(self.apps, self.launcher_state, Popup, PointerMove({ x: x - self.popup_rect.x, y: y - self.popup_rect.y }), launch), + ) + return true + } + ignore(@launcher.handle_event(self.apps, self.launcher_state, Popup, PointerLeave, launch)) + } + false +} + +///| Key name as used by lib/launcher ("Escape", "Backspace", "Enter" or one character). +pub fn DesktopShell::key_down(self : DesktopShell, name : String) -> Unit { + let launch = fn(id : String) { self.launch(id) } + ignore(@launcher.handle_event(self.apps, self.launcher_state, Popup, KeyDown(name), launch)) } ///| Build the (background, panel) render-lists for the current frame. @@ -145,6 +254,7 @@ pub fn DesktopShell::render( window_titles : Array[String], ) -> (Array[@scene.DrawOp], Array[@scene.DrawOp]) { let tokens = conv_theme(theme) + self.screen_w = w let bg : Array[@scene.DrawOp] = [] translate(@libshell.render_background(@ui.size(w, h), tokens), 0, 0, bg) @@ -173,6 +283,9 @@ pub fn DesktopShell::render( _ => [] } translate(local, p.x, p.y, panel_ops) + if p.id == "launcher" { + self.launcher_rect = { x: p.x, y: p.y, w: p.w, h: p.h } + } if p.id == "tasks" { let mut tx = 0 for title in window_titles { @@ -194,5 +307,15 @@ pub fn DesktopShell::render( } } } + if self.launcher_state.open { + let size = @launcher.popup_size(self.apps, self.launcher_state) + let pw = size.w.reinterpret_as_int() + let ph = size.h.reinterpret_as_int() + let (px, py) = @panel.popup_position(w, h - theme.panel_height, self.launcher_rect.x, pw, ph, 6) + self.popup_rect = { x: px, y: py, w: pw, h: ph } + translate(@launcher.render_popup(self.apps, tokens, self.launcher_state), px, py, panel_ops) + } else { + self.popup_rect = { x: 0, y: 0, w: 0, h: 0 } + } (bg, panel_ops) } diff --git a/compositor/sys/sys.mbt b/compositor/sys/sys.mbt index 5285f68..541935f 100644 --- a/compositor/sys/sys.mbt +++ b/compositor/sys/sys.mbt @@ -178,3 +178,19 @@ extern "C" fn c_wall_time_field(which : Int) -> Int = "mp_wall_time_field" pub fn wall_time_field(which : Int) -> Int { c_wall_time_field(which) } + +///| +extern "C" fn c_has_exe(name : Bytes) -> Int = "mp_has_exe" + +///| +extern "C" fn c_spawn(cmd : Bytes, display : Bytes) -> Int = "mp_spawn" + +///| Whether `name` is an executable on PATH. +pub fn has_exe(name : String) -> Bool { + c_has_exe(str_to_utf8_bytes(name)) != 0 +} + +///| Launch `cmd` detached with WAYLAND_DISPLAY set to `display`. +pub fn spawn(cmd : String, display : String) -> Bool { + c_spawn(str_to_utf8_bytes(cmd), str_to_utf8_bytes(display)) == 0 +} diff --git a/compositor/sys/sys_stub.c b/compositor/sys/sys_stub.c index de36863..119ddc8 100644 --- a/compositor/sys/sys_stub.c +++ b/compositor/sys/sys_stub.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include "moonbit.h" @@ -310,3 +311,43 @@ int32_t mp_wall_time_field(int32_t which) { default: return 0; } } + +/* True when `name` resolves to an executable via PATH (or is an executable path). */ +MOONBIT_FFI_EXPORT +int32_t mp_has_exe(moonbit_bytes_t name_bytes) { + char name[256]; + bytes_to_cstr(name_bytes, name, sizeof(name)); + if (strchr(name, '/')) return access(name, X_OK) == 0; + const char *path = getenv("PATH"); + if (!path) return 0; + char *copy = strdup(path); + char *save = NULL; + int found = 0; + for (char *dir = strtok_r(copy, ":", &save); dir && !found; dir = strtok_r(NULL, ":", &save)) { + char full[1024]; + snprintf(full, sizeof(full), "%s/%s", dir, name); + if (access(full, X_OK) == 0) found = 1; + } + free(copy); + return found; +} + +/* Launch `cmd` detached, with WAYLAND_DISPLAY pointing at our socket. Returns 0 on fork success. */ +MOONBIT_FFI_EXPORT +int32_t mp_spawn(moonbit_bytes_t cmd_bytes, moonbit_bytes_t display_bytes) { + char cmd[256]; + char display[512]; + bytes_to_cstr(cmd_bytes, cmd, sizeof(cmd)); + bytes_to_cstr(display_bytes, display, sizeof(display)); + signal(SIGCHLD, SIG_IGN); /* no zombies */ + pid_t pid = fork(); + if (pid < 0) return -1; + if (pid == 0) { + setsid(); + setenv("WAYLAND_DISPLAY", display, 1); + unsetenv("DISPLAY"); + execlp(cmd, cmd, (char *)NULL); + _exit(127); + } + return 0; +} diff --git a/compositor/wayland/server.mbt b/compositor/wayland/server.mbt index 92c63e7..934223a 100644 --- a/compositor/wayland/server.mbt +++ b/compositor/wayland/server.mbt @@ -96,6 +96,9 @@ struct ClientConn { shm_buffers : Map[UInt, ShmBufferState] xdg_surfaces : Map[UInt, XdgSurfaceState] toplevels : Map[UInt64, ToplevelState] + pointer_ids : Array[UInt] + keyboard_ids : Array[UInt] + mut seat_ver : UInt mut alive : Bool } @@ -114,6 +117,9 @@ fn ClientConn::new(client_id : UInt, fd : Int) -> ClientConn { shm_buffers: {}, xdg_surfaces: {}, toplevels: {}, + pointer_ids: [], + keyboard_ids: [], + seat_ver: 1U, alive: true, } } @@ -158,6 +164,7 @@ pub(all) struct WaylandServer { mut next_client_id : UInt mut next_win_id : UInt64 mut next_serial : UInt + mut keymap : Bytes } ///| @@ -180,6 +187,7 @@ pub fn WaylandServer::listen( next_client_id: 1U, next_win_id: 1UL, next_serial: 100U, + keymap: Bytes::from_array([]), }) } @@ -362,7 +370,7 @@ fn WaylandServer::dispatch_msg( // bind(name: u32, interface: string, version: u32, id: new_id) let _name = reader.u32() let iface = reader.str() - let _ver = reader.u32() + let ver = reader.u32() let new_id = reader.u32() if iface == "wl_compositor" { c.objects.set(new_id, ObjCompositor) @@ -375,6 +383,7 @@ fn WaylandServer::dispatch_msg( c.objects.set(new_id, ObjDecoManager) } else if iface == "wl_seat" { c.objects.set(new_id, ObjSeat) + c.seat_ver = ver emit_seat_info(c, new_id) } else if iface == "wl_data_device_manager" { c.objects.set(new_id, ObjDataManager) @@ -679,9 +688,12 @@ fn WaylandServer::dispatch_msg( if opcode == 0U { let pid = reader.u32() c.objects.set(pid, ObjPointer) + c.pointer_ids.push(pid) } else if opcode == 1U { let kid = reader.u32() c.objects.set(kid, ObjKeyboard) + c.keyboard_ids.push(kid) + self.send_keymap(c, kid) } else if opcode == 2U { let tid = reader.u32() c.objects.set(tid, ObjTouch) @@ -861,3 +873,189 @@ pub fn WaylandServer::send_toplevel_close( None => () } } + +///| Keymap (xkb text, format xkb_v1) shared with every client keyboard; set by the backend. +pub fn WaylandServer::set_keymap(self : WaylandServer, keymap : Bytes) -> Unit { + self.keymap = keymap +} + +///| +fn WaylandServer::send_keymap(self : WaylandServer, c : ClientConn, kid : UInt) -> Unit { + if self.keymap.length() > 0 { + let fd = @sys.memfd_create("moon-plasma-keymap", self.keymap.length()) + if fd >= 0 { + ignore(@sys.pwrite_bytes(fd, self.keymap, 0)) + let w = WireWriter::new() + w.u32(1U) // xkb_v1 + w.u32(self.keymap.length().reinterpret_as_uint()) + if c.alive { + ignore(@sys.sendmsg_fd(c.fd, w.finish_msg(kid, 0U), fd)) + } + @sys.close_fd(fd) + } + } + if c.seat_ver >= 4U { + let r = WireWriter::new() + r.i32(25) // repeat rate (keys/s) + r.i32(400) // repeat delay (ms) + c.send_msg(r.finish_msg(kid, 5U)) + } +} + +///| +fn WaylandServer::win_client( + self : WaylandServer, + win_id : UInt64, +) -> (ClientConn, ToplevelState)? { + match self.win_to_client.get(win_id) { + None => None + Some(cid) => + match self.find_client(cid) { + None => None + Some(c) => + match c.toplevels.get(win_id) { + None => None + Some(tl) => Some((c, tl)) + } + } + } +} + +///| +fn fixed(v : Double) -> Int { + (v * 256.0).to_int() +} + +///| wl_pointer.enter at surface-local (x, y). +pub fn WaylandServer::pointer_enter(self : WaylandServer, win_id : UInt64, x : Double, y : Double) -> Unit { + match self.win_client(win_id) { + None => () + Some((c, tl)) => + for pid in c.pointer_ids { + let w = WireWriter::new() + w.u32(self.next_serial()) + w.u32(tl.wl_surface_id) + w.i32(fixed(x)) + w.i32(fixed(y)) + c.send_msg(w.finish_msg(pid, 0U)) + self.pointer_frame(c, pid) + } + } +} + +///| +pub fn WaylandServer::pointer_leave(self : WaylandServer, win_id : UInt64) -> Unit { + match self.win_client(win_id) { + None => () + Some((c, tl)) => + for pid in c.pointer_ids { + let w = WireWriter::new() + w.u32(self.next_serial()) + w.u32(tl.wl_surface_id) + c.send_msg(w.finish_msg(pid, 1U)) + self.pointer_frame(c, pid) + } + } +} + +///| +pub fn WaylandServer::pointer_motion(self : WaylandServer, win_id : UInt64, time_ms : UInt, x : Double, y : Double) -> Unit { + match self.win_client(win_id) { + None => () + Some((c, _)) => + for pid in c.pointer_ids { + let w = WireWriter::new() + w.u32(time_ms) + w.i32(fixed(x)) + w.i32(fixed(y)) + c.send_msg(w.finish_msg(pid, 2U)) + self.pointer_frame(c, pid) + } + } +} + +///| `button` is a Linux evdev code (BTN_LEFT = 0x110); `pressed` true on press. +pub fn WaylandServer::pointer_button(self : WaylandServer, win_id : UInt64, time_ms : UInt, button : UInt, pressed : Bool) -> Unit { + match self.win_client(win_id) { + None => () + Some((c, _)) => + for pid in c.pointer_ids { + let w = WireWriter::new() + w.u32(self.next_serial()) + w.u32(time_ms) + w.u32(button) + w.u32(if pressed { 1U } else { 0U }) + c.send_msg(w.finish_msg(pid, 3U)) + self.pointer_frame(c, pid) + } + } +} + +///| +fn WaylandServer::pointer_frame(_self : WaylandServer, c : ClientConn, pid : UInt) -> Unit { + if c.seat_ver >= 5U { + c.send_msg(WireWriter::new().finish_msg(pid, 5U)) + } +} + +///| +pub fn WaylandServer::keyboard_enter(self : WaylandServer, win_id : UInt64) -> Unit { + match self.win_client(win_id) { + None => () + Some((c, tl)) => + for kid in c.keyboard_ids { + let w = WireWriter::new() + w.u32(self.next_serial()) + w.u32(tl.wl_surface_id) + w.array([]) + c.send_msg(w.finish_msg(kid, 1U)) + } + } +} + +///| +pub fn WaylandServer::keyboard_leave(self : WaylandServer, win_id : UInt64) -> Unit { + match self.win_client(win_id) { + None => () + Some((c, tl)) => + for kid in c.keyboard_ids { + let w = WireWriter::new() + w.u32(self.next_serial()) + w.u32(tl.wl_surface_id) + c.send_msg(w.finish_msg(kid, 2U)) + } + } +} + +///| `key` is a Linux evdev keycode. +pub fn WaylandServer::keyboard_key(self : WaylandServer, win_id : UInt64, time_ms : UInt, key : UInt, pressed : Bool) -> Unit { + match self.win_client(win_id) { + None => () + Some((c, _)) => + for kid in c.keyboard_ids { + let w = WireWriter::new() + w.u32(self.next_serial()) + w.u32(time_ms) + w.u32(key) + w.u32(if pressed { 1U } else { 0U }) + c.send_msg(w.finish_msg(kid, 3U)) + } + } +} + +///| +pub fn WaylandServer::keyboard_modifiers(self : WaylandServer, win_id : UInt64, depressed : UInt, latched : UInt, locked : UInt, group : UInt) -> Unit { + match self.win_client(win_id) { + None => () + Some((c, _)) => + for kid in c.keyboard_ids { + let w = WireWriter::new() + w.u32(self.next_serial()) + w.u32(depressed) + w.u32(latched) + w.u32(locked) + w.u32(group) + c.send_msg(w.finish_msg(kid, 4U)) + } + } +}