diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index dda0e4a..9ce550b 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -20,8 +20,15 @@ jobs: run: | curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash echo "$HOME/.moon/bin" >> "$GITHUB_PATH" + # Fetch the package registry index, or registry deps (jqlssg/moonedn) aren't found. + "$HOME/.moon/bin/moon" update - - name: Install wasm-tools, wit-bindgen, wasmtime + - name: Install native compositor build dependencies + run: | + apt-get update -q + apt-get install -y -q --no-install-recommends libseat-dev libdrm-dev libinput-dev libudev-dev libxkbcommon-dev + + - name: Install wasm-tools, wit-bindgen, wasmtime, wasmer C API run: scripts/setup-toolchain.sh - name: Install host dependencies (jco) diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt index 4d360bb..7bf4b22 100644 --- a/compositor/host/input_test.mbt +++ b/compositor/host/input_test.mbt @@ -191,7 +191,7 @@ test "dragging the divider between two tiled windows resizes them" { } ///| -test "the layout module opens a popup to pick Tiling, Monocle or Floating" { +test "the layout module toggles between tiling and floating" { let socket_path = "/tmp/wayland-moon-layout-" + @sys.getpid().to_string() + ".sock" @@ -200,33 +200,15 @@ test "the layout module opens a popup to pick Tiling, Monocle or Floating" { let _a = open_and_configure_client(comp, socket_path, "A", "a") let _b = open_and_configure_client(comp, socket_path, "B", "b") ignore(comp.compose_frame()) - let slot = comp.shell.slot_of("layout") let (cx, cy) = center_of(comp, "layout") comp.handle_input(@nested.InputEvent::PointerMove(cx, cy), 1U) assert_eq(comp.desired_cursor(), 4U) // it is a button: hand cursor - // Pick row `i` from the popup the button opens. - let pick = fn(i : Int, t : UInt) raise { - click_at(comp, cx.to_int(), cy.to_int(), t) // open - let pr = comp.shell.slot_popup_rect(slot) - assert_true(pr.w > 0) - click_at(comp, pr.x + pr.w / 2, pr.y + 4 + i * 28 + 14, t + 3U) - assert_eq(comp.shell.slot_popup_rect(slot).w, 0) - } assert_eq(comp.layout_mode, @wm.LayoutMode::Tiling) let tiled_w = comp.windows.get(1UL).unwrap().geometry.w - // Opening alone changes nothing. + // "Tile | Float": a click floats the windows, the next one tiles them again. click_at(comp, cx.to_int(), cy.to_int(), 2U) - assert_true(comp.shell.slot_popup_rect(slot).w > 0) - assert_eq(comp.layout_mode, @wm.LayoutMode::Tiling) - // Escape closes without picking. - comp.handle_input(@nested.InputEvent::Key(1U, true), 3U) - assert_eq(comp.shell.slot_popup_rect(slot).w, 0) - pick(1, 10U) - assert_eq(comp.layout_mode, @wm.LayoutMode::Monocle) - assert_true(comp.windows.get(1UL).unwrap().geometry.w > tiled_w + 100) - pick(2, 20U) assert_eq(comp.layout_mode, @wm.LayoutMode::Floating) - pick(0, 30U) + click_at(comp, cx.to_int(), cy.to_int(), 10U) assert_eq(comp.layout_mode, @wm.LayoutMode::Tiling) assert_eq(comp.windows.get(1UL).unwrap().geometry.w, tiled_w) } @@ -375,6 +357,7 @@ test "right-click an app in the dock for its window's menu: Minimize, Maximize, test "right-click a panel module to replace it in place, or remove it" { let comp = new_comp("swap") let before = comp.shell.slots() + let clock_at = before.search(comp.shell.slot_of("clock-digital")).unwrap() let clock = rect_of(comp, "clock-digital") assert_true(clock.w > 0) let find_row = fn(label : String) { @@ -404,18 +387,18 @@ test "right-click a panel module to replace it in place, or remove it" { assert_eq(comp.shell.slot_of("clock-digital"), "") let after = comp.shell.slots() assert_eq(after.length(), before.length()) - assert_eq(after[after.length() - 1], comp.shell.slot_of("clock-binary")) // same place in the panel + assert_eq(after[clock_at], comp.shell.slot_of("clock-binary")) // same place in the panel assert_true(rect_of(comp, "clock-binary").w > 0) // Escape dismisses the menu; "Remove widget" unloads the module. - let moon = rect_of(comp, "moon-disc") - click_at(comp, moon.x + moon.w / 2, moon.y + moon.h / 2, 20U, btn=273U) + let net = rect_of(comp, "network") + click_at(comp, net.x + net.w / 2, net.y + net.h / 2, 20U, btn=273U) assert_true(comp.shell.menu_open) comp.handle_input(@nested.InputEvent::Key(1U, true), 30U) assert_true(!comp.shell.menu_open) - click_at(comp, moon.x + moon.w / 2, moon.y + moon.h / 2, 40U, btn=273U) + click_at(comp, net.x + net.w / 2, net.y + net.h / 2, 40U, btn=273U) let m2 = comp.shell.menu_rect click_at(comp, m2.x + m2.w / 2, m2.y + 26 + 28 * find_row("Remove widget") + 14, 50U) - assert_eq(comp.shell.slot_of("moon-disc"), "") + assert_eq(comp.shell.slot_of("network"), "") assert_eq(comp.shell.slots().length(), before.length() - 1) } @@ -527,12 +510,12 @@ test "the panel is saved to panel.edn and restored" { let a = @shell.DesktopShell::new("x") a.persist = true a.set_output(theme, 1280, 720) - assert_true(a.unload_slot(a.slot_of("moon-disc"))) + assert_true(a.unload_slot(a.slot_of("network"))) assert_true(a.load_module("clock-binary")) let b = @shell.DesktopShell::new("x") b.persist = true b.set_output(theme, 1280, 720) - assert_eq(b.slot_of("moon-disc"), "") + assert_eq(b.slot_of("network"), "") assert_true(b.slot_of("clock-binary") != "") assert_eq(b.slots().length(), a.slots().length()) } @@ -666,23 +649,24 @@ test "the wasm picker opens the settings module's Panel tab, which loads and unl test "dragging a panel module reorders it" { let comp = new_comp("wdrag") let order0 = comp.shell.slots() - let moon = rect_of(comp, "moon-disc") + let net_at = order0.search(comp.shell.slot_of("network")).unwrap() + let net = rect_of(comp, "network") let clock = rect_of(comp, "clock-digital") - assert_true(moon.x < clock.x) + assert_true(net.x < clock.x) let cy = (clock.y + clock.h / 2).to_double() comp.handle_input(@nested.InputEvent::PointerMove((clock.x + clock.w / 2).to_double(), cy), 1U) assert_eq(comp.desired_cursor(), 4U) comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U) - // Drag the clock left past the moon. - comp.handle_input(@nested.InputEvent::PointerMove((moon.x + 2).to_double(), cy), 3U) + // Drag the clock left past the network module. + comp.handle_input(@nested.InputEvent::PointerMove((net.x + 2).to_double(), cy), 3U) assert_eq(comp.desired_cursor(), 17U) comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 4U) ignore(comp.compose_frame()) let order1 = comp.shell.slots() assert_eq(order1.length(), order0.length()) assert_true(order1 != order0) - assert_eq(order1[order1.length() - 2], comp.shell.slot_of("clock-digital")) - assert_true(rect_of(comp, "clock-digital").x < rect_of(comp, "moon-disc").x) + assert_eq(order1[net_at], comp.shell.slot_of("clock-digital")) + assert_true(rect_of(comp, "clock-digital").x < rect_of(comp, "network").x) assert_true(comp.desired_cursor() != 17U) } @@ -711,14 +695,14 @@ test "modules are added until the panel is full" { let n0 = comp.shell.slots().length() let mut added = 0 for _i = 0; _i < 40; _i = _i + 1 { - if comp.shell.load_module("moon-disc") { + if comp.shell.load_module("clock-binary") { // a clock can go in more than once added = added + 1 } } assert_true(added > 2 && added < 40) assert_eq(comp.shell.slots().length(), n0 + added) ignore(comp.compose_frame()) - assert_true(rect_of(comp, "moon-disc").w > 0) + assert_true(rect_of(comp, "clock-binary").w > 0) } ///| diff --git a/compositor/wasm/wasm_test.mbt b/compositor/wasm/wasm_test.mbt index 8ffd02f..09d581f 100644 --- a/compositor/wasm/wasm_test.mbt +++ b/compositor/wasm/wasm_test.mbt @@ -69,6 +69,22 @@ fn load_module(name : String) -> Plasmoid { } } +///| Like load_module, for a module that may not be built (e.g. moon-disc, which has no source +/// in the tree): None, with a skip notice, when it isn't in build/components. +fn load_optional_module(name : String) -> Plasmoid? { + match load("build/components/" + name + ".wasm", test_tokens()) { + Ok(p) => Some(p) + Err(_) => + match load("../../build/components/" + name + ".wasm", test_tokens()) { + Ok(p) => Some(p) + Err(_) => { + println("skip: \{name}.wasm is not in build/components (no source in the tree)") + None + } + } + } +} + ///| fn texts_of(ops : Array[@types.DrawOp]) -> Array[String] { let out : Array[String] = [] @@ -97,15 +113,15 @@ fn drain() -> Array[HostRequest] { test "a module's own settings (module-config) are listed and set through wasmer" { let p = load_module("clock-digital") let items = p.config_items() - // 12-hour by default: Time format, Show AM/PM, Show date. - assert_eq(items.length(), 3) + // 12-hour by default: Time format, Show AM/PM, Show date, then the calendar's two settings. + assert_eq(items.length(), 5) assert_eq(items[0], { key: "format", label: "Time format", choices: ["24-hour", "12-hour"], value: "12-hour" }) assert_eq(items[1].choices.length(), 0) // an on/off switch assert_true(p.set_config("format", "24-hour")) assert_false(p.set_config("format", "24-hour")) // unchanged assert_false(p.set_config("bogus", "x")) assert_eq(p.config_items()[0].value, "24-hour") - assert_eq(p.config_items().length(), 2) // no AM/PM switch in 24-hour mode + assert_eq(p.config_items().length(), 4) // no AM/PM switch in 24-hour mode let mut ampm = false for op in p.render(p.pref_w, p.pref_h, test_tokens()) { match op { @@ -218,12 +234,12 @@ test "the settings module's Panel tab lists host modules and asks to load, move, assert_false(ts.contains("x1")) } } - // Below the tab bar (40): heading (28), the two panel rows (30 each, buttons up at x 222-246, - // down 248-272, remove 284-308), heading (28), "Clocks" (22), two Add rows (42 each), Browse. + // Below the tab bar (40): heading (28), the two panel rows (30 each; in the 390-wide popup the + // buttons are up at x 292-316, down 318-342, remove 354-378), heading (28), "Clocks" (22), two Add rows (42 each), Browse. ignore(p.on_event(true, PointerDown(20, 82))) // w1's row itself: nothing - ignore(p.on_event(true, PointerDown(234, 82))) // w1 is first: up does nothing - ignore(p.on_event(true, PointerDown(260, 82))) // down - ignore(p.on_event(true, PointerDown(296, 112))) // w2's remove + ignore(p.on_event(true, PointerDown(304, 82))) // w1 is first: up does nothing + ignore(p.on_event(true, PointerDown(330, 82))) // down + ignore(p.on_event(true, PointerDown(366, 112))) // w2's remove ignore(p.on_event(true, PointerDown(20, 190))) // add the moon ignore(p.on_event(true, PointerDown(160, 285))) // browse assert_eq(drain(), [ @@ -252,9 +268,10 @@ test "interface explorer module toggles through the inspector capability" { ///| test "moon and digital clock modules render from the clock capability" { - let m = load_module("moon-disc") - assert_true(m.render(36, 36, test_tokens()).length() > 3) - m.unload() + if load_optional_module("moon-disc") is Some(m) { + assert_true(m.render(36, 36, test_tokens()).length() > 3) + m.unload() + } let c = load_module("clock-digital") let ts = texts_of(c.render(120, 36, test_tokens())) assert_true(ts[0].has_suffix("AM") || ts[0].has_suffix("PM")) // 12-hour by default diff --git a/config/desktop.binary-clock.json b/config/desktop.binary-clock.json index e505822..1d4e642 100644 --- a/config/desktop.binary-clock.json +++ b/config/desktop.binary-clock.json @@ -3,7 +3,7 @@ "theme": "moon-night", "shell": {"component": "shell", "grants": ["theme"]}, "panel": [ - {"id": "launcher", "component": "launcher", "align": "start", "grants": ["theme", "app-catalog", "app-launch"]}, + {"id": "launcher", "component": "launcher", "align": "start", "grants": ["theme", "app-catalog", "app-launch", "session", "power"]}, {"id": "clock", "component": "clock-binary", "align": "end", "grants": ["theme", "clock"]} ] } diff --git a/config/desktop.json b/config/desktop.json index 64b0a6d..ce2f546 100644 --- a/config/desktop.json +++ b/config/desktop.json @@ -3,7 +3,7 @@ "theme": "moon-night", "shell": {"component": "shell", "grants": ["theme"]}, "panel": [ - {"id": "launcher", "component": "launcher", "align": "start", "grants": ["theme", "app-catalog", "app-launch"]}, + {"id": "launcher", "component": "launcher", "align": "start", "grants": ["theme", "app-catalog", "app-launch", "session", "power"]}, {"id": "clock", "component": "clock-digital", "align": "end", "grants": ["theme", "clock"]} ] } diff --git a/host/src/platform.mjs b/host/src/platform.mjs index 1815b6a..7d81fa4 100644 --- a/host/src/platform.mjs +++ b/host/src/platform.mjs @@ -10,6 +10,9 @@ export function createPlatform({ now = () => new Date(), apps, tokens }) { launches: [], now, launchable: new Set(apps.map((a) => a.id)), + // Session and power requests are recorded, not acted on: this host is in-memory. + sessionRequests: [], + power: { screenOffMinutes: 10, suspendMinutes: 0, requests: [] }, }; /** Capability name -> WIT interface implementation (jco import shape). */ p.interfaces = { @@ -32,8 +35,21 @@ export function createPlatform({ now = () => new Date(), apps, tokens }) { return { tag: 'ok', val: undefined }; }, }), + session: () => ({ + logout() { p.sessionRequests.push('logout'); }, + reload() { p.sessionRequests.push('reload'); }, + }), + power: () => ({ + screenOffMinutes: () => p.power.screenOffMinutes, + setScreenOffMinutes(m) { p.power.screenOffMinutes = m; }, + suspendMinutes: () => p.power.suspendMinutes, + setSuspendMinutes(m) { p.power.suspendMinutes = m; }, + suspend() { p.power.requests.push('suspend'); }, + powerOff() { p.power.requests.push('power-off'); }, + reboot() { p.power.requests.push('reboot'); }, + }), }; return p; } -export const CAPABILITIES = ['theme', 'clock', 'app-catalog', 'app-launch']; +export const CAPABILITIES = ['theme', 'clock', 'app-catalog', 'app-launch', 'session', 'power']; diff --git a/host/test/desktop.test.mjs b/host/test/desktop.test.mjs index 4d5c48e..503ca0f 100644 --- a/host/test/desktop.test.mjs +++ b/host/test/desktop.test.mjs @@ -27,7 +27,7 @@ test('boots shell + widgets and composes a frame', async () => { assert.deepEqual(f.screen, { w: 1280, h: 720 }); assert.ok(f.ops.length > 50); const t = texts(f.ops); - assert.ok(t.includes('14:07'), `clock text missing: ${t}`); + assert.ok(t.includes('2:07 PM'), `clock text missing (Digital Clock starts in 12-hour mode): ${t}`); assert.ok(t.includes('Tue 24 Nov')); assert.ok(t.includes('chandra')); assert.ok(t.includes('Applications')); @@ -115,11 +115,11 @@ test('hot-swap: clock-digital -> clock-binary at runtime, other components untou const before = await Promise.all(files.map(async (f) => (await stat(f)).mtimeMs)); const { desktop } = await bootDesktop({ now: fixed }); const a = desktop.frame(); - assert.ok(texts(a.ops).includes('14:07')); + assert.ok(texts(a.ops).includes('2:07 PM')); const launcherInstance = desktop.widgets.find((w) => w.id === 'launcher').plasmoid; await desktop.swap('clock', 'clock-binary'); const b = desktop.frame(); - assert.ok(!texts(b.ops).includes('14:07')); + assert.ok(!texts(b.ops).includes('2:07 PM')); assert.ok(b.ops.filter((o) => o.tag === 'circle').length >= 20, 'binary clock dots drawn'); assert.equal(desktop.widgets.find((w) => w.id === 'launcher').plasmoid, launcherInstance, 'launcher instance reused'); const after = await Promise.all(files.map(async (f) => (await stat(f)).mtimeMs)); diff --git a/scripts/setup-toolchain.sh b/scripts/setup-toolchain.sh index 60cd27d..bb9108f 100755 --- a/scripts/setup-toolchain.sh +++ b/scripts/setup-toolchain.sh @@ -40,5 +40,16 @@ if [ "$(cat "$CAPI/VERSION" 2>/dev/null)" != "$WASMTIME_VERSION" ]; then cp -r "$tmp"/wasmtime-*-c-api/include "$tmp"/wasmtime-*-c-api/lib "$CAPI/" echo "$WASMTIME_VERSION" > "$CAPI/VERSION" fi +# The wasmer C API (libwasmer + wasm.h) for the native compositor (compositor/wasm, compositor/host, +# compositor/bin link -lwasmer): .tools/wasmer/{include,lib}. scripts/test.sh puts it on the C paths. +WASMER="$ROOT/.tools/wasmer" +if [ "$(cat "$WASMER/VERSION" 2>/dev/null)" != "$WASMER_VERSION" ]; then + echo ">> wasmer C API $WASMER_VERSION" + mkdir -p "$tmp/wasmer" + curl -fsSL "https://github.com/wasmerio/wasmer/releases/download/v$WASMER_VERSION/wasmer-linux-amd64.tar.gz" | tar xz -C "$tmp/wasmer" + rm -rf "$WASMER"; mkdir -p "$WASMER" + cp -r "$tmp"/wasmer/include "$tmp"/wasmer/lib "$WASMER/" + echo "$WASMER_VERSION" > "$WASMER/VERSION" +fi echo "installed into $BIN:" -"$BIN/wasm-tools" --version; "$BIN/wit-bindgen" --version; "$BIN/wasmtime" --version; echo "wasmtime C API $(cat "$CAPI/VERSION") in $CAPI" +"$BIN/wasm-tools" --version; "$BIN/wit-bindgen" --version; "$BIN/wasmtime" --version; echo "wasmtime C API $(cat "$CAPI/VERSION") in $CAPI"; echo "wasmer C API $(cat "$WASMER/VERSION") in $WASMER" diff --git a/scripts/test.sh b/scripts/test.sh index 40e4a94..3d4de9e 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -14,7 +14,11 @@ step "moon check" moon check step "moon test (pure logic, wasm target)" -moon test +# compositor/* is left out: on the wasm target those packages import chandra:host/* (the native +# host supplies them), so a plain wasm test runner can't instantiate them. They run natively below. +mapfile -t wasm_pkgs < <(find . -name moon.pkg -not -path './.mooncakes/*' -not -path './_build/*' \ + -not -path './.tools/*' -not -path './compositor/*' -not -path '*/node_modules/*' -printf '%h\n' | sed 's#^\./##' | sort) +moon test "${wasm_pkgs[@]}" step "build components" scripts/build.sh @@ -36,6 +40,14 @@ step "host integration tests (jco + Node)" (cd host && npm test) step "pure-MoonBit native Wayland compositor + wasi-gfx + e2e tests (compositor/)" +# The native compositor links the wasmer C API (scripts/setup-toolchain.sh puts it in .tools/wasmer). +WASMER="$ROOT/.tools/wasmer" +if [ -d "$WASMER/lib" ]; then + export C_INCLUDE_PATH="$WASMER/include${C_INCLUDE_PATH:+:$C_INCLUDE_PATH}" + export LIBRARY_PATH="$WASMER/lib${LIBRARY_PATH:+:$LIBRARY_PATH}" + export LD_LIBRARY_PATH="$WASMER/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" +fi XDG_CONFIG_HOME="$(mktemp -d)" CHANDRA_TEST_CONFIG=1 \ - moon test --target native --warn-list -a compositor/renderer compositor/gfx compositor/wasm compositor/host + moon test --target native --warn-list -a compositor/renderer compositor/gfx compositor/wasm \ + compositor/network compositor/portal compositor/host moon build --target native --warn-list -a compositor/bin diff --git a/scripts/versions.env b/scripts/versions.env index 5fcb6bc..e85bbcf 100644 --- a/scripts/versions.env +++ b/scripts/versions.env @@ -3,3 +3,4 @@ WASM_TOOLS_VERSION=1.263.0 WIT_BINDGEN_VERSION=0.63.0 WASMTIME_VERSION=51.0.0 JCO_VERSION=1.42.0 +WASMER_VERSION=6.1.0