diff --git a/crates/weft-compositor/src/protocols/mod.rs b/crates/weft-compositor/src/protocols/mod.rs index 18922b4..0993fad 100644 --- a/crates/weft-compositor/src/protocols/mod.rs +++ b/crates/weft-compositor/src/protocols/mod.rs @@ -33,6 +33,8 @@ pub struct WeftShellWindowData { pub role: String, pub surface: Option, pub closed: std::sync::atomic::AtomicBool, + /// The edge strip a panel reserved with set_exclusive_zone. + pub exclusive_zone: std::sync::Mutex>, } impl WeftShellState { @@ -41,7 +43,7 @@ impl WeftShellState { D: GlobalDispatch, D: 'static, { - let global = display.create_global::(2, ()); + let global = display.create_global::(3, ()); Self { _global: global, panels: Vec::new(), @@ -97,6 +99,7 @@ mod tests { role: "normal".into(), surface: None, closed: std::sync::atomic::AtomicBool::new(false), + exclusive_zone: std::sync::Mutex::new(None), }; assert_eq!(d.app_id, "com.example.test"); assert_eq!(d.title, "Test Window"); @@ -112,6 +115,7 @@ mod tests { role: String::new(), surface: None, closed: std::sync::atomic::AtomicBool::new(false), + exclusive_zone: std::sync::Mutex::new(None), }; assert!(!d.closed.load(Ordering::Relaxed)); d.closed.store(true, Ordering::Relaxed); @@ -122,14 +126,14 @@ mod tests { fn manager_interface_name_and_version() { let iface = ZweftShellManagerV1::interface(); assert_eq!(iface.name, "zweft_shell_manager_v1"); - assert_eq!(iface.version, 2); + assert_eq!(iface.version, 3); } #[test] fn window_interface_name_and_version() { let iface = ZweftShellWindowV1::interface(); assert_eq!(iface.name, "zweft_shell_window_v1"); - assert_eq!(iface.version, 2); + assert_eq!(iface.version, 3); } #[test] diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index f612358..363783f 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -542,6 +542,7 @@ impl Dispatch for WeftCompositorState { role, surface, closed: std::sync::atomic::AtomicBool::new(false), + exclusive_zone: std::sync::Mutex::new(None), }, ); if let Some(session) = session @@ -585,7 +586,7 @@ impl Dispatch for WeftCompositorState { impl Dispatch for WeftCompositorState { fn request( - _state: &mut Self, + state: &mut Self, _client: &Client, resource: &ZweftShellWindowV1, request: zweft_shell_window_v1::Request, @@ -613,6 +614,27 @@ impl Dispatch for WeftCompositorState { } => { resource.configure(x, y, width, height, 0); } + zweft_shell_window_v1::Request::SetExclusiveZone { edge, size } => { + let is_panel = state + .weft_shell_state + .panels() + .any(|panel| panel == resource); + let edge = edge.into_result().ok(); + match edge { + Some(edge) if is_panel && size >= 0 => { + let zone = (size > 0).then_some((edge, size)); + *data + .exclusive_zone + .lock() + .unwrap_or_else(|p| p.into_inner()) = zone; + state.layout_app_windows(); + } + _ => resource.post_error( + crate::protocols::server::zweft_shell_window_v1::Error::InvalidExclusiveZone, + "only a panel may reserve an edge, with a size of at least 0", + ), + } + } } } } diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs index df39eb4..c3a3add 100644 --- a/crates/weft-compositor/src/windows.rs +++ b/crates/weft-compositor/src/windows.rs @@ -10,7 +10,7 @@ use smithay::desktop::Window; use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; use smithay::reexports::wayland_server::Resource; use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; -use smithay::utils::SERIAL_COUNTER; +use smithay::utils::{Logical, Rectangle, SERIAL_COUNTER}; use smithay::wayland::seat::WaylandFocus; use crate::protocols::WeftShellWindowData; @@ -121,6 +121,7 @@ impl WeftCompositorState { for window in &others { self.space.raise_element(window, false); } + self.layout_app_windows(); } /// Maps a new toplevel. The first window a session maps in its lifetime @@ -145,8 +146,11 @@ impl WeftCompositorState { self.space.map_element(window.clone(), (0, 0), false); if self.is_panel(&window) { self.fit_panels(); - } else if first { - self.activate_window(&window); + } else { + self.layout_app_windows(); + if first { + self.activate_window(&window); + } } } @@ -187,6 +191,66 @@ impl WeftCompositorState { } } + /// The part of the output that application windows occupy: the output + /// less the strips the panels reserved. + pub fn work_area(&self) -> Option> { + use crate::protocols::server::zweft_shell_window_v1::Edge; + let output = self.space.outputs().next()?; + let mut area = self.space.output_geometry(output)?; + for panel in self.weft_shell_state.panels() { + let Some(data) = panel.data::() else { + continue; + }; + let zone = *data + .exclusive_zone + .lock() + .unwrap_or_else(|p| p.into_inner()); + let Some((edge, size)) = zone else { + continue; + }; + match edge { + Edge::Top => { + let size = size.min(area.size.h); + area.loc.y += size; + area.size.h -= size; + } + Edge::Bottom => area.size.h -= size.min(area.size.h), + Edge::Left => { + let size = size.min(area.size.w); + area.loc.x += size; + area.size.w -= size; + } + Edge::Right => area.size.w -= size.min(area.size.w), + } + } + Some(area) + } + + /// Places every application window to fill the work area, so none + /// covers the panel's reserved strips. + pub fn layout_app_windows(&mut self) { + let Some(area) = self.work_area() else { + return; + }; + let apps: Vec = self + .space + .elements() + .filter(|window| !self.is_panel(window)) + .cloned() + .collect(); + for window in apps { + if let Some(toplevel) = window.toplevel() { + toplevel.with_pending_state(|state| { + state.size = Some(area.size); + state.states.set(xdg_toplevel::State::Maximized); + }); + toplevel.send_pending_configure(); + } + // Re-mapped in stacking order, so the order is kept. + self.space.map_element(window.clone(), area.loc, false); + } + } + /// Sends the configure for every toplevel whose pending state changed, /// such as its activated state. fn send_pending_configures(&self) { diff --git a/crates/weft-servo-shell/src/shell_client.rs b/crates/weft-servo-shell/src/shell_client.rs index 7af2270..67b4f2b 100644 --- a/crates/weft-servo-shell/src/shell_client.rs +++ b/crates/weft-servo-shell/src/shell_client.rs @@ -9,6 +9,11 @@ use crate::protocols::{ client::{zweft_shell_manager_v1, zweft_shell_window_v1}, }; +/// Height in logical pixels of the taskbar strip the system UI draws along +/// the bottom of the output; it matches `--taskbar-height` in +/// `infra/shell/system-ui.html`. +pub const TASKBAR_HEIGHT: i32 = 48; + // ── Window state ────────────────────────────────────────────────────────────── pub struct ShellWindowState { @@ -72,7 +77,7 @@ impl Dispatch for AppData { } = event && interface == "zweft_shell_manager_v1" { - let mgr = registry.bind::(name, version.min(2), qh, ()); + let mgr = registry.bind::(name, version.min(3), qh, ()); state.manager = Some(mgr); } } @@ -213,6 +218,11 @@ impl ShellClient { &qh, (), ); + // The taskbar's strip at the bottom of the output is kept free of + // application windows. + if window.version() >= 3 { + window.set_exclusive_zone(zweft_shell_window_v1::Edge::Bottom, TASKBAR_HEIGHT); + } data.window = Some(window); event_queue diff --git a/docs/architecture.md b/docs/architecture.md index dd74a4d..f8abe0e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -8,7 +8,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web ### weft-compositor -Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and kept beneath application windows; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. A session's later windows are shown without taking focus. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. +Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and kept beneath application windows; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. A session's later windows are shown without taking focus. The panel reserves a strip along an output edge for the taskbar with `set_exclusive_zone` (weft-servo-shell reserves 48 logical pixels at the bottom), and the compositor lays every application window out to fill the remaining work area. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. ### weft-servo-shell diff --git a/docs/building.md b/docs/building.md index fc6fed2..d13bbd9 100644 --- a/docs/building.md +++ b/docs/building.md @@ -88,7 +88,7 @@ python3 tests/frame/check_counter.py # Counter round trip through the session python3 tests/frame/check_notes.py # Notes stores exactly what is typed, refuses stale saves python3 tests/frame/check_app_data.py # Notes data from the earlier layout is moved and kept python3 tests/frame/check_confinement.py # an app page reaches only its UI files and its session bridge -python3 tests/frame/check_activation.py # panel beneath two apps; launch and ACTIVATE_APP raise and focus +python3 tests/frame/check_activation.py # apps fill the work area above the taskbar; launch and ACTIVATE_APP raise and focus ``` ## Demo apps (wasm32-wasip2) diff --git a/docs/security.md b/docs/security.md index b18519a..d23d1cb 100644 --- a/docs/security.md +++ b/docs/security.md @@ -32,7 +32,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, rather than starting an app that could never show a window; if the compositor connection is lost before the session's connection is handed over, the session is stopped before the app shell starts. Frames for appd that its socket cannot take at once are queued and retried; a backlog beyond 4 MiB, from an appd that stopped reading, ends the connection and with it the sessions' clients. -Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement and close are not yet decided by the compositor per session (all toplevels start at the origin, and no work area is reserved for the taskbar, so an application window as large as the output hides it); only the first window a session maps in its lifetime takes keyboard focus when it maps, and a client connected through the display socket takes focus on map only while no other such window is mapped; and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. +Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; close is not yet decided by the compositor per session; application windows fill the work area the panel leaves, one in front at a time; only the first window a session maps in its lifetime takes keyboard focus when it maps, and a client connected through the display socket takes focus on map only while no other such window is mapped; and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. ## JavaScript Engine (SpiderMonkey) diff --git a/infra/shell/system-ui.html b/infra/shell/system-ui.html index 1b98e2f..dd019a6 100644 --- a/infra/shell/system-ui.html +++ b/infra/shell/system-ui.html @@ -9,6 +9,7 @@ color-scheme: dark; --surface-bg: rgba(255, 255, 255, 0.07); --surface-border: rgba(255, 255, 255, 0.12); + /* weft-servo-shell reserves this strip (TASKBAR_HEIGHT); keep them equal. */ --taskbar-height: 48px; --text-primary: rgba(255, 255, 255, 0.92); --text-secondary: rgba(255, 255, 255, 0.55); diff --git a/protocol/weft-shell-unstable-v1.xml b/protocol/weft-shell-unstable-v1.xml index f208b62..8ee68df 100644 --- a/protocol/weft-shell-unstable-v1.xml +++ b/protocol/weft-shell-unstable-v1.xml @@ -43,7 +43,7 @@ - + Bound once by weft-servo-shell. The manager owns the shell session. @@ -95,7 +95,7 @@ - + Represents one visual window. The compositor controls the effective @@ -121,6 +121,16 @@ summary="request sent after window_closed was received"/> + + + + + + + + + @@ -155,6 +165,19 @@ + + + Reserves a strip of the given size, in logical pixels, along an + edge of the output for shell chrome drawn by this panel, such as a + taskbar. The compositor places application windows only in the + work area that remains, so they never cover the strip. A size of 0 + releases the reservation; a later request replaces the earlier one. + Only a panel window may reserve a zone. + + + + + Sent by the compositor when the effective geometry or state diff --git a/tests/frame/check_activation.py b/tests/frame/check_activation.py index 3d46ee4..f5b3e1a 100755 --- a/tests/frame/check_activation.py +++ b/tests/frame/check_activation.py @@ -7,6 +7,8 @@ nested desktop (see session.py). Each application page switches between its colour and a lighter shade on every key press. In the presented pixels: - the panel fills the compositor output and stays beneath the applications; +- application windows fill the work area: the output less the strip the + shell reserves for its taskbar along the bottom, which stays visible; - each newly launched application is shown on top and receives keys; - ACTIVATE_APP, the request the taskbar sends, brings the other session's window back to the front with keyboard focus; @@ -35,6 +37,8 @@ APPS = { "org.weft.test.blue": ((0, 0, 200), (120, 120, 255)), } STARTUP_TIMEOUT = 120.0 +# The strip weft-servo-shell reserves at the bottom (TASKBAR_HEIGHT). +TASKBAR = 48 PANEL_PAGE = ('') @@ -97,10 +101,9 @@ def run(args, desktop, store, home): shot, panel = wait_for_color(desktop, GREEN, 60) if panel is None: raise AssertionError("the panel was not shown") - width, height, _ = parse_ppm(shot) - # The compositor sizes the panel to its whole output, which reaches the - # right and bottom edges of the nested desktop. - if panel[2] != width - 1 or panel[3] < height - 3: + # The compositor sizes the panel to its whole output, larger than a + # toplevel's default size. + if panel[2] - panel[0] + 1 < 1000 or panel[3] - panel[1] + 1 < 700: raise AssertionError(f"the panel does not fill the output: {panel}") t = Path(args.target) @@ -133,9 +136,13 @@ def run(args, desktop, store, home): box, _, _ = area(shot, color) hidden = all(area(shot, c)[0] is None for c in others) if box is not None and hidden: - # The panel stays visible beside the application window. - if area(shot, GREEN)[0] is None: - raise AssertionError("an application covered the panel") + # The application fills the work area: the panel's whole + # width, down to the reserved strip, which stays visible. + if box[0] != panel[0] or box[2] != panel[2] or box[1] != panel[1]: + raise AssertionError(f"{app_id} does not fill the work area: {box}") + if box[3] != panel[3] - TASKBAR: + raise AssertionError( + f"{app_id} does not end at the reserved strip: {box}, panel {panel}") return time.sleep(0.3) state = "pressed " if pressed else "" diff --git a/tests/frame/check_confinement.py b/tests/frame/check_confinement.py index c484905..d1ba9e7 100644 --- a/tests/frame/check_confinement.py +++ b/tests/frame/check_confinement.py @@ -52,8 +52,17 @@ APP_ID = "org.weft.test.confinement" PROBES = ["own-image", "bridge", "outside-image", "other-image", "linked-image", "http-image", "http-fetch", "websocket", "worker-fetch", "srcdoc-internals"] GREEN = (0, 192, 0) -# The app window's page area on the 1024x768 nested desktop. -PAGE_TOP, PAGE_HEIGHT, PAGE_X = 35, 600, 400 +# Where the probe bands are sampled across the app window's page. +PAGE_X = 400 +# The X root behind the nested compositor window. +ROOT_COLOUR = (0, 0, 0) + + +def page_rows(width, height, pixels): + """The first and last rows the app window's page covers at PAGE_X: the + compositor gives the application its whole work area.""" + rows = [y for y in range(height) if pixel(width, pixels, PAGE_X, y) != ROOT_COLOUR] + return (rows[0], rows[-1]) if rows else None def png(): @@ -169,10 +178,14 @@ def run(args, desktop, store, other_dir): deadline = time.monotonic() + 15 while True: - width, _, pixels = parse_ppm(desktop.capture()) + width, height, pixels = parse_ppm(desktop.capture()) + rows = page_rows(width, height, pixels) + if rows is None: + raise AssertionError("the test app's page is not on screen") + top, bottom = rows colours = {} for i, name in enumerate(PROBES): - y = PAGE_TOP + int((i + 0.5) * PAGE_HEIGHT / len(PROBES)) + y = top + int((i + 0.5) * (bottom - top + 1) / len(PROBES)) colours[name] = pixel(width, pixels, PAGE_X, y) if all(c == GREEN for c in colours.values()) or time.monotonic() > deadline: break diff --git a/tests/frame/check_notes.py b/tests/frame/check_notes.py index 2d562ee..a769e4a 100644 --- a/tests/frame/check_notes.py +++ b/tests/frame/check_notes.py @@ -35,14 +35,16 @@ from pathlib import Path sys.dont_write_bytecode = True from check_counter import AppdClient, free_port, read_endpoint # noqa: E402 -from session import ROOT, Desktop, SessionError, missing_tools # noqa: E402 +from session import ROOT, Desktop, SessionError, bounding_box, missing_tools, parse_ppm # noqa: E402 APP_ID = "org.weft.demo.notes" SEED = 'path C:\\new\\table "quoted"\tcaf\u00e9 \u2603' -# The Discard button in the 800x600 Notes window at the desktop's origin. -DISCARD = ("663", "68") +PAGE = (15, 15, 15) # The Notes page's background, #0f0f0f. +# The Discard button, right-aligned in the header: its offset from the +# window's right edge, and its height on the desktop. +DISCARD_FROM_RIGHT, DISCARD_Y = 137, 68 def runtime_pid(): @@ -87,6 +89,11 @@ def run(args, desktop, home, xdotool): if reply.get("type") != "APP_READY": raise AssertionError(f"Notes did not start: {reply}") time.sleep(2) + width, height, pixels = parse_ppm(desktop.capture()) + page = bounding_box(width, height, pixels, PAGE) + if page is None: + raise AssertionError("the Notes page is not on screen") + discard = (str(page[2] + 1 - DISCARD_FROM_RIGHT), str(DISCARD_Y)) def act(*arguments): subprocess.run([xdotool, *arguments], env={"DISPLAY": desktop.display}, @@ -125,7 +132,7 @@ def run(args, desktop, home, xdotool): os.kill(runtime, signal.SIGSTOP) try: - act("mousemove", "--sync", *DISCARD) + act("mousemove", "--sync", *discard) act("click", "1") act("mousemove", "--sync", "100", "300") act("click", "1") @@ -140,7 +147,7 @@ def run(args, desktop, home, xdotool): act("key", "ctrl+s") wait_for_file(notes, other + "!") notes.write_bytes(b"\xff not text") - act("mousemove", "--sync", *DISCARD) + act("mousemove", "--sync", *discard) act("click", "1") time.sleep(2) # The editor stays read-only and nothing can be saved over the file. @@ -151,7 +158,7 @@ def run(args, desktop, home, xdotool): time.sleep(2) if notes.read_bytes() != b"\xff not text": raise AssertionError("a failed load changed the stored notes") - act("mousemove", "--sync", *DISCARD) + act("mousemove", "--sync", *discard) notes.write_text("repaired", encoding="utf-8") act("click", "1") time.sleep(2) diff --git a/tests/frame/session.py b/tests/frame/session.py index 0b45837..c87ecf8 100644 --- a/tests/frame/session.py +++ b/tests/frame/session.py @@ -17,7 +17,8 @@ from pathlib import Path ROOT = Path(__file__).resolve().parents[2] -SCREEN = (1024, 768) +# Large enough to show the nested compositor's whole 1280x800 output. +SCREEN = (1400, 900) ANSI_ESCAPE = re.compile(r"\x1b\[[0-9;]*m") REQUIRED_TOOLS = ("Xvfb", "xwd", "convert") @@ -111,9 +112,11 @@ def _stop(process): class Desktop: """Xvfb plus weft-compositor; logs are written to `output/.log`.""" - def __init__(self, compositor, output, outputs=()): - """`outputs` names further files in `output` that this run will write.""" + def __init__(self, compositor, output, outputs=(), screen=SCREEN): + """`outputs` names further files in `output` that this run will write; + `screen` is the X screen size.""" self.compositor = Path(compositor) + self.screen = screen self.output = Path(output) self.own_outputs = ["xvfb.log", "compositor.log", "shell.log", *outputs] self.processes = [] @@ -180,7 +183,7 @@ class Desktop: try: xvfb = subprocess.Popen( ["Xvfb", "-displayfd", str(write_end), "-screen", "0", - f"{SCREEN[0]}x{SCREEN[1]}x24", "-nolisten", "tcp"], + f"{self.screen[0]}x{self.screen[1]}x24", "-nolisten", "tcp"], stdout=log, stderr=subprocess.STDOUT, env=self.env, start_new_session=True, pass_fds=(write_end,)) finally: