Shell: send niri commands over the IPC socket #38

Open
opened 2026-11-08 21:09:40 +01:00 by arne · 0 comments
Owner

Why

The shell already reads niri's event stream over the IPC socket (shell/Niri.qml, the Socket at the bottom). Commands still go the slow way: Niri.action() starts a niri msg action … process for every command.

That is fine for a click in the sidebar, but not for the spaces map (#36), where H/J/K/L moves are fired as fast as you press keys and some moves are sequences (focus a window, move it, give focus back):

  • Latency. Starting niri msg costs about 24 ms per command. Over a socket that is already open, a request takes about 0.4 ms (measured: three "Version" requests on one connection, 0.49 / 0.35 / 0.35 ms).
  • Order. Processes started together can reach niri out of order, so a focus → move → refocus sequence can land wrong. One connection answers requests one at a time, in the order sent.
  • Errors. execDetached throws away niri's reply, so a rejected command fails silently.

What niri accepts on the socket

Measured on niri 26.04:

  • One connection takes any number of requests: write one JSON request and a newline, read one reply line, repeat. The connection stays open.
  • Requests use the serde (PascalCase) form, not the CLI words. {"Action":{"focus-window":{"id":1}}} gives {"Err":"error parsing request"}.
  • A successful action replies {"Ok":"Handled"}.
  • An action on a window id that does not exist also replies {"Ok":"Handled"}. niri doesn't report it, so the shell can't rely on the reply to notice a stale id.
  • Workspaces can be addressed by id: {"Action":{"FocusWorkspace":{"reference":{"Id":1}}}} works. niri msg only takes a name or an index, which is why followTo() looks the workspace up by name or index today. The socket removes the need for that lookup.

Work

All in shell/Niri.qml:

  1. Add a second Socket for commands, next to the event stream socket. The event stream connection can't be reused, because once it has sent "EventStream" it only streams events.
  2. Have action() take a request object and write it as one JSON line, with a queue of commands still waiting for a reply.
  3. Read replies with a SplitParser, matching each reply to the oldest command waiting for one. Log anything that isn't {"Ok":…} with the request that caused it.
  4. Port the three commands that exist today:
    • focusWorkspace(ref): {"Action":{"FocusWorkspace":{"reference":{"Index":n}}}} for a number, {"Name":…} otherwise. Callers in Status.qml and Sidebar.qml stay the same.
    • Handing focus back from a picture-in-picture window: {"Action":{"FocusWindow":{"id":…}}}.
    • followTo(): {"Action":{"MoveWindowToWorkspace":{"window_id":…,"reference":{"Id":wsId},"focus":false}}}. The name/index lookup and the comment above it go away.
  5. Reconnect. If the socket drops (niri restarting, nested dev sessions), reconnect and resend the commands that never got a reply, or drop them and log it. Check whether the existing event stream socket reconnects on its own. If it doesn't, fix both the same way.

Power.qml runs niri msg action quit through execDetached. Leave it as it is: it runs once, and niri exits before any reply would arrive.

Out of scope

  • The spaces map itself (#36).
  • Any new commands the map needs (move column, consume/expel). They come with the map, built on this.

Done when

  • grep -n '"niri", "msg"' shell/ matches only Power.qml.
  • In the dev session: clicking a space in the sidebar and in the status list switches to it; a picture-in-picture window follows you to another space; a PiP opening doesn't steal focus.
  • Restarting niri in the dev session (or killing its socket) brings both sockets back without restarting the shell.
  • A deliberately bad request shows up in the shell log with niri's error.
  • nix fmt and nix flake check pass.

Notes

  • About 40–60 lines, small enough for one PR.
  • Wait for this before building the map (#36). The map's moves and its active-window marking (active_window_id, which Niri.qml already tracks for each workspace) both rely on it.
## Why The shell already reads niri's event stream over the IPC socket (`shell/Niri.qml`, the `Socket` at the bottom). Commands still go the slow way: `Niri.action()` starts a `niri msg action …` process for every command. That is fine for a click in the sidebar, but not for the spaces map (#36), where H/J/K/L moves are fired as fast as you press keys and some moves are sequences (focus a window, move it, give focus back): - **Latency.** Starting `niri msg` costs about 24 ms per command. Over a socket that is already open, a request takes about 0.4 ms (measured: three `"Version"` requests on one connection, 0.49 / 0.35 / 0.35 ms). - **Order.** Processes started together can reach niri out of order, so a focus → move → refocus sequence can land wrong. One connection answers requests one at a time, in the order sent. - **Errors.** `execDetached` throws away niri's reply, so a rejected command fails silently. ## What niri accepts on the socket Measured on niri 26.04: - One connection takes any number of requests: write one JSON request and a newline, read one reply line, repeat. The connection stays open. - Requests use the serde (PascalCase) form, not the CLI words. `{"Action":{"focus-window":{"id":1}}}` gives `{"Err":"error parsing request"}`. - A successful action replies `{"Ok":"Handled"}`. - **An action on a window id that does not exist also replies `{"Ok":"Handled"}`.** niri doesn't report it, so the shell can't rely on the reply to notice a stale id. - **Workspaces can be addressed by id**: `{"Action":{"FocusWorkspace":{"reference":{"Id":1}}}}` works. `niri msg` only takes a name or an index, which is why `followTo()` looks the workspace up by name or index today. The socket removes the need for that lookup. ## Work All in `shell/Niri.qml`: 1. Add a second `Socket` for commands, next to the event stream socket. The event stream connection can't be reused, because once it has sent `"EventStream"` it only streams events. 2. Have `action()` take a request object and write it as one JSON line, with a queue of commands still waiting for a reply. 3. Read replies with a `SplitParser`, matching each reply to the oldest command waiting for one. Log anything that isn't `{"Ok":…}` with the request that caused it. 4. Port the three commands that exist today: - `focusWorkspace(ref)`: `{"Action":{"FocusWorkspace":{"reference":{"Index":n}}}}` for a number, `{"Name":…}` otherwise. Callers in `Status.qml` and `Sidebar.qml` stay the same. - Handing focus back from a picture-in-picture window: `{"Action":{"FocusWindow":{"id":…}}}`. - `followTo()`: `{"Action":{"MoveWindowToWorkspace":{"window_id":…,"reference":{"Id":wsId},"focus":false}}}`. The name/index lookup and the comment above it go away. 5. Reconnect. If the socket drops (niri restarting, nested dev sessions), reconnect and resend the commands that never got a reply, or drop them and log it. Check whether the existing event stream socket reconnects on its own. If it doesn't, fix both the same way. `Power.qml` runs `niri msg action quit` through `execDetached`. Leave it as it is: it runs once, and niri exits before any reply would arrive. ## Out of scope - The spaces map itself (#36). - Any new commands the map needs (move column, consume/expel). They come with the map, built on this. ## Done when - `grep -n '"niri", "msg"' shell/` matches only `Power.qml`. - In the dev session: clicking a space in the sidebar and in the status list switches to it; a picture-in-picture window follows you to another space; a PiP opening doesn't steal focus. - Restarting niri in the dev session (or killing its socket) brings both sockets back without restarting the shell. - A deliberately bad request shows up in the shell log with niri's error. - `nix fmt` and `nix flake check` pass. ## Notes - About 40–60 lines, small enough for one PR. - Wait for this before building the map (#36). The map's moves and its active-window marking (`active_window_id`, which `Niri.qml` already tracks for each workspace) both rely on it.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bases/fjord#38
No description provided.