fix(compositor): give a session focus on map only once in its lifetime

Unmapping a closed window made a session with no mapped window count
as new again, so destroying and recreating its only window took
keyboard focus from the application the user had activated. The
compositor now remembers which sessions already received focus on map
until the session ends.
This commit is contained in:
Marco Allegretti 2026-10-09 15:45:52 +00:00
parent 65c02231a0
commit 16af8d2a8c
No known key found for this signature in database
3 changed files with 31 additions and 10 deletions

View file

@ -46,6 +46,9 @@ pub struct WeftAppdIpc {
sessions: HashMap<u64, ClientId>,
/// Sessions whose first toplevel was reported to appd.
reported: HashSet<u64>,
/// Sessions that already received keyboard focus for a mapped window;
/// kept until the session ends, so recreating a window gains nothing.
focused_on_map: HashSet<u64>,
/// Where clients report their session's disconnection.
disconnected: Option<channel::Sender<(u64, ClientId)>>,
/// Bytes for appd that did not fit in the socket buffer, sent before
@ -67,6 +70,7 @@ impl WeftAppdIpc {
fds: VecDeque::new(),
sessions: HashMap::new(),
reported: HashSet::new(),
focused_on_map: HashSet::new(),
disconnected: None,
out_buf: Vec::new(),
flush_retry: None,
@ -150,6 +154,12 @@ impl WeftAppdIpc {
.ok();
}
/// Whether a window `session_id` maps now may take keyboard focus: only
/// the first one in the session's lifetime.
pub fn take_focus_on_map(&mut self, session_id: u64) -> bool {
self.sessions.contains_key(&session_id) && self.focused_on_map.insert(session_id)
}
/// Reports a session's first toplevel to appd.
pub fn surface_created(&mut self, session_id: u64) {
if self.sessions.contains_key(&session_id) && self.reported.insert(session_id) {
@ -250,6 +260,7 @@ impl WeftAppdIpc {
self.read_buf.clear();
self.fds.clear();
self.reported.clear();
self.focused_on_map.clear();
self.sessions.drain().map(|(_, client)| client).collect()
}
}
@ -265,6 +276,7 @@ fn handle_message(state: &mut WeftCompositorState, msg: AppdToCompositor) {
return;
};
ipc.reported.remove(&session_id);
ipc.focused_on_map.remove(&session_id);
if let Some(client) = ipc.sessions.remove(&session_id) {
tracing::info!(session_id, "closing the client of an ended session");
state
@ -389,6 +401,7 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> {
{
ipc.sessions.remove(&session_id);
ipc.reported.remove(&session_id);
ipc.focused_on_map.remove(&session_id);
tracing::info!(session_id, "session client disconnected");
ipc.send(&CompositorToAppd::ClientDisconnected { session_id });
}

View file

@ -123,17 +123,25 @@ 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.
/// Maps a new toplevel. The first window a session maps in its lifetime
/// comes to the front with keyboard focus; its later windows, including
/// recreated ones, are shown without taking focus, so a session cannot
/// take keystrokes meant for another application by creating windows.
/// A client outside any session (one connected through the display
/// socket) takes focus only while no other such window is mapped. A
/// panel is fitted and kept beneath.
pub fn map_new_window(&mut self, window: Window) {
let session = window_session(&window);
let first = !self
let first = match session {
Some(session_id) => self
.appd_ipc
.as_mut()
.is_some_and(|ipc| ipc.take_focus_on_map(session_id)),
None => !self
.space
.elements()
.any(|other| window_session(other) == session);
.any(|other| window_session(other).is_none()),
};
self.space.map_element(window.clone(), (0, 0), false);
if self.is_panel(&window) {
self.fit_panels();

View file

@ -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); 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.
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.
## JavaScript Engine (SpiderMonkey)