mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
fix(compositor): restore focus when a window closes and keep later windows from taking it
When the focused window closed, keys went to a dead surface until the next click; the topmost remaining application window, or the panel, now gets focus. Only a session's first window takes focus when it maps, so a client cannot take keystrokes meant for another application by creating windows. A panel is fitted and kept beneath however the shell orders its requests, and focusing the panel clears the applications' activated state. ACTIVATE_APP is refused while the compositor is not connected rather than queued. check_activation.py now also ends the focused application and checks that keys reach the remaining one.
This commit is contained in:
parent
aa67e7f190
commit
65c02231a0
6 changed files with 93 additions and 18 deletions
|
|
@ -556,8 +556,12 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response {
|
||||||
// The compositor owns stacking and focus; appd asks it to act
|
// The compositor owns stacking and focus; appd asks it to act
|
||||||
// for a session it supervises.
|
// for a session it supervises.
|
||||||
let sent = reg.compositor_tx.as_ref().is_some_and(|tx| {
|
let sent = reg.compositor_tx.as_ref().is_some_and(|tx| {
|
||||||
tx.try_send(weft_ipc_types::AppdToCompositor::AppFocusRequest { session_id }.into())
|
tx.is_connected()
|
||||||
.is_ok()
|
&& tx
|
||||||
|
.try_send(
|
||||||
|
weft_ipc_types::AppdToCompositor::AppFocusRequest { session_id }.into(),
|
||||||
|
)
|
||||||
|
.is_ok()
|
||||||
});
|
});
|
||||||
if sent {
|
if sent {
|
||||||
Response::AppState { session_id, state }
|
Response::AppState { session_id, state }
|
||||||
|
|
@ -1337,7 +1341,7 @@ mod tests {
|
||||||
));
|
));
|
||||||
|
|
||||||
// Without the compositor connection the request is refused.
|
// Without the compositor connection the request is refused.
|
||||||
drop(compositor);
|
connected.store(false, std::sync::atomic::Ordering::Release);
|
||||||
let unsent = dispatch(Request::ActivateApp { session_id }, ®istry).await;
|
let unsent = dispatch(Request::ActivateApp { session_id }, ®istry).await;
|
||||||
assert!(
|
assert!(
|
||||||
matches!(unsent, Response::Error { code: 503, .. }),
|
matches!(unsent, Response::Error { code: 503, .. }),
|
||||||
|
|
|
||||||
|
|
@ -266,6 +266,10 @@ impl XdgShellHandler for WeftCompositorState {
|
||||||
&mut self.xdg_shell_state
|
&mut self.xdg_shell_state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn toplevel_destroyed(&mut self, surface: ToplevelSurface) {
|
||||||
|
self.window_closed(surface.wl_surface());
|
||||||
|
}
|
||||||
|
|
||||||
fn new_toplevel(&mut self, surface: ToplevelSurface) {
|
fn new_toplevel(&mut self, surface: ToplevelSurface) {
|
||||||
surface.send_configure();
|
surface.send_configure();
|
||||||
let session = surface.wl_surface().client().and_then(|client| {
|
let session = surface.wl_surface().client().and_then(|client| {
|
||||||
|
|
@ -274,10 +278,7 @@ impl XdgShellHandler for WeftCompositorState {
|
||||||
.and_then(|data| data.session.as_ref().map(|s| s.session_id))
|
.and_then(|data| data.session.as_ref().map(|s| s.session_id))
|
||||||
});
|
});
|
||||||
let window = Window::new_wayland_window(surface);
|
let window = Window::new_wayland_window(surface);
|
||||||
self.space.map_element(window.clone(), (0, 0), false);
|
self.map_new_window(window);
|
||||||
// A new window comes to the front with focus; the shell's panel is
|
|
||||||
// recognised when it registers and is then kept beneath.
|
|
||||||
self.activate_window(&window);
|
|
||||||
if let (Some(session_id), Some(ipc)) = (session, self.appd_ipc.as_mut()) {
|
if let (Some(session_id), Some(ipc)) = (session, self.appd_ipc.as_mut()) {
|
||||||
ipc.surface_created(session_id);
|
ipc.surface_created(session_id);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
//! Stacking, focus and activation of toplevel windows.
|
//! Stacking, focus and activation of toplevel windows.
|
||||||
//!
|
//!
|
||||||
//! The compositor decides the order: the trusted shell's panel, which
|
//! The compositor decides the order: the trusted shell's panel, which
|
||||||
//! fills the output, stays beneath every application window; a newly mapped
|
//! fills the output, stays beneath every application window; a session's
|
||||||
//! application window, a clicked one and one appd asks to activate is raised
|
//! first window, a clicked window and one appd asks to activate is raised to
|
||||||
//! to the top, marked activated and given keyboard focus.
|
//! the top, marked activated and given keyboard focus. When the focused
|
||||||
|
//! window closes, the topmost remaining application window gets focus.
|
||||||
|
|
||||||
use smithay::desktop::Window;
|
use smithay::desktop::Window;
|
||||||
use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
|
use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
|
||||||
|
|
@ -37,8 +38,13 @@ impl WeftCompositorState {
|
||||||
/// activated and gives it keyboard focus. A panel is focused but stays
|
/// activated and gives it keyboard focus. A panel is focused but stays
|
||||||
/// beneath the applications.
|
/// beneath the applications.
|
||||||
pub fn activate_window(&mut self, window: &Window) {
|
pub fn activate_window(&mut self, window: &Window) {
|
||||||
let panel = self.is_panel(window);
|
if self.is_panel(window) {
|
||||||
if !panel {
|
// The panel takes focus without rising; no application window
|
||||||
|
// stays marked activated.
|
||||||
|
for other in self.space.elements() {
|
||||||
|
other.set_activated(false);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
self.space.raise_element(window, true);
|
self.space.raise_element(window, true);
|
||||||
}
|
}
|
||||||
self.send_pending_configures();
|
self.send_pending_configures();
|
||||||
|
|
@ -117,6 +123,62 @@ impl WeftCompositorState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Maps a new toplevel. A session's first window comes to the front with
|
||||||
|
/// keyboard focus; its later windows, and windows of clients outside
|
||||||
|
/// any session after the first, are shown without taking focus, so a
|
||||||
|
/// client cannot take keystrokes meant for another application by
|
||||||
|
/// creating windows. A panel is fitted and kept beneath.
|
||||||
|
pub fn map_new_window(&mut self, window: Window) {
|
||||||
|
let session = window_session(&window);
|
||||||
|
let first = !self
|
||||||
|
.space
|
||||||
|
.elements()
|
||||||
|
.any(|other| window_session(other) == session);
|
||||||
|
self.space.map_element(window.clone(), (0, 0), false);
|
||||||
|
if self.is_panel(&window) {
|
||||||
|
self.fit_panels();
|
||||||
|
} else if first {
|
||||||
|
self.activate_window(&window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unmaps a closed toplevel and, if it had keyboard focus, gives focus
|
||||||
|
/// to the topmost remaining application window, or the panel.
|
||||||
|
pub fn window_closed(&mut self, surface: &WlSurface) {
|
||||||
|
let Some(window) = self
|
||||||
|
.space
|
||||||
|
.elements()
|
||||||
|
.find(|window| window.wl_surface().is_some_and(|s| *s == *surface))
|
||||||
|
.cloned()
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
self.space.unmap_elem(&window);
|
||||||
|
let had_focus = self
|
||||||
|
.seat
|
||||||
|
.get_keyboard()
|
||||||
|
.and_then(|keyboard| keyboard.current_focus())
|
||||||
|
.is_none_or(|focus| focus == *surface);
|
||||||
|
if !had_focus {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let next = self
|
||||||
|
.space
|
||||||
|
.elements()
|
||||||
|
.rev()
|
||||||
|
.find(|window| !self.is_panel(window))
|
||||||
|
.or_else(|| self.space.elements().next())
|
||||||
|
.cloned();
|
||||||
|
match next {
|
||||||
|
Some(next) => self.activate_window(&next),
|
||||||
|
None => {
|
||||||
|
if let Some(keyboard) = self.seat.get_keyboard() {
|
||||||
|
keyboard.set_focus(self, None, SERIAL_COUNTER.next_serial());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Sends the configure for every toplevel whose pending state changed,
|
/// Sends the configure for every toplevel whose pending state changed,
|
||||||
/// such as its activated state.
|
/// such as its activated state.
|
||||||
fn send_pending_configures(&self) {
|
fn send_pending_configures(&self) {
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web
|
||||||
|
|
||||||
### weft-compositor
|
### 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 newly mapped, clicked or activated application window is raised, marked activated and given keyboard 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. 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
|
### weft-servo-shell
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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.
|
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); 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; 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); a session's first window takes keyboard focus when it maps, later ones do not; 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)
|
## JavaScript Engine (SpiderMonkey)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,14 @@
|
||||||
|
|
||||||
The harness runs weft-servo-shell with a solid green page as the system
|
The harness runs weft-servo-shell with a solid green page as the system
|
||||||
panel, and weft-appd with two test applications, one red and one blue, on a
|
panel, and weft-appd with two test applications, one red and one blue, on a
|
||||||
nested desktop (see session.py). Each application page turns a lighter shade
|
nested desktop (see session.py). Each application page switches between its
|
||||||
of its colour when it receives a key press. In the presented pixels:
|
colour and a lighter shade on every key press. In the presented pixels:
|
||||||
|
|
||||||
- the panel fills the compositor output and stays beneath the applications;
|
- the panel fills the compositor output and stays beneath the applications;
|
||||||
- each newly launched application is shown on top and receives keys;
|
- each newly launched application is shown on top and receives keys;
|
||||||
- ACTIVATE_APP, the request the taskbar sends, brings the other session's
|
- ACTIVATE_APP, the request the taskbar sends, brings the other session's
|
||||||
window back to the front with keyboard focus;
|
window back to the front with keyboard focus;
|
||||||
|
- when the focused application ends, the remaining one gets keyboard focus;
|
||||||
- ACTIVATE_APP for a session that is not running is refused.
|
- ACTIVATE_APP for a session that is not running is refused.
|
||||||
|
|
||||||
The applications are unsigned test packages, recorded as development content
|
The applications are unsigned test packages, recorded as development content
|
||||||
|
|
@ -42,8 +43,9 @@ PANEL_PAGE = ('<!DOCTYPE html><html><body style="margin:0;background:rgb(0,160,0
|
||||||
def app_page(color, pressed):
|
def app_page(color, pressed):
|
||||||
return ('<!DOCTYPE html><html><body style="margin:0;width:100vw;height:100vh;'
|
return ('<!DOCTYPE html><html><body style="margin:0;width:100vw;height:100vh;'
|
||||||
f'background:rgb{color}"><script>'
|
f'background:rgb{color}"><script>'
|
||||||
'addEventListener("keydown", function () {'
|
'var pressed = false;'
|
||||||
f'document.body.style.background = "rgb{pressed}"; }});'
|
'addEventListener("keydown", function () { pressed = !pressed;'
|
||||||
|
f'document.body.style.background = pressed ? "rgb{pressed}" : "rgb{color}"; }});'
|
||||||
'</script></body></html>')
|
'</script></body></html>')
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -173,6 +175,12 @@ def run(args, desktop, store, home):
|
||||||
activate(blue)
|
activate(blue)
|
||||||
shown(blue, pressed=True)
|
shown(blue, pressed=True)
|
||||||
|
|
||||||
|
# When the focused application ends, keyboard focus returns to the other.
|
||||||
|
appd.send({"type": "TERMINATE_APP", "session_id": sessions[blue]})
|
||||||
|
shown(red, pressed=True)
|
||||||
|
key("a")
|
||||||
|
shown(red)
|
||||||
|
|
||||||
appd.send({"type": "ACTIVATE_APP", "session_id": 999})
|
appd.send({"type": "ACTIVATE_APP", "session_id": 999})
|
||||||
reply = appd.wait_for(lambda m: m.get("type") == "ERROR", 10)
|
reply = appd.wait_for(lambda m: m.get("type") == "ERROR", 10)
|
||||||
if reply.get("code") != 404:
|
if reply.get("code") != 404:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue