Commit 24c309f7b709

Vincent Demeester <vincent@sbr.pm>
2026-10-15 13:58:14
feat: add Raffi display controls
Added Kyushu display controls so external monitors can be extended, mirrored, enabled, and disabled without Kanshi overriding the selected layout. Provisioned wl-mirror through Fedora and kept Kanshi active for Aomi. Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 6e3582b
Changed files (8)
docs/superpowers/plans/2026-10-07-kyushu-display-control.md
@@ -0,0 +1,86 @@
+# Kyushu Display Control Implementation Plan
+
+> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
+
+**Goal:** Add a Kyushu-only Raffi display menu that dynamically manages left/right extension, single-target laptop mirroring, and output power.
+
+**Architecture:** A shell helper performs validation, Niri IPC positioning, and managed `wl-mirror` lifecycle control. A Raffi script filter discovers connected external DRM outputs and emits actions for the helper. Kyushu installs `wl-mirror` and disables Kanshi; shared Niri configuration remains usable by Aomi.
+
+**Tech Stack:** Nix/Home Manager, Bash, `niri msg`, `wl-mirror`, systemd user units, Raffi, jq.
+
+## Global Constraints
+
+- Apply display-control behavior only to Kyushu.
+- Keep Aomi's shared Kanshi configuration working.
+- Use `eDP-*` as the mirror source and dynamically enumerate connected non-eDP DRM outputs.
+- Do not alter non-selected external outputs.
+- Use one named user mirror unit; never use broad `pkill` cleanup.
+- Do not commit unless explicitly requested.
+
+---
+
+## File structure
+
+- Modify `systems/kyushu/home.nix`: disable Kanshi.
+- Modify `imperative/kyushu/bootstrap.sh`: install Fedora-native `wl-mirror`.
+- Modify `home/common/desktop/sway/swayidle.nix`: make Kanshi wake restart safe when absent or disabled.
+- Create `dots/config/raffi/scripts/niri-display`: validate and perform named display actions.
+- Create `dots/config/raffi/scripts/niri-display-raffi`: enumerate external targets and emit Raffi JSON.
+- Modify `dots/config/raffi/raffi.yaml`: add the Kyushu Raffi filter entry.
+
+### Task 1: Host ownership and dependency configuration
+
+**Files:**
+- Modify: `systems/kyushu/home.nix`
+- Modify: `home/common/desktop/sway/swayidle.nix`
+
+**Produces:** Kyushu has a Fedora-native `wl-mirror` and no Kanshi service; other hosts retain their configured Kanshi behavior.
+
+- [ ] Add `services.kanshi.enable = false;` in Kyushu’s Home Manager module and add `wl-mirror` to the Kyushu bootstrap's native DNF package list.
+- [ ] Replace `systemctl --user restart kanshi` with `systemctl --user try-restart kanshi` in the common Niri power-on script.
+- [ ] Evaluate Kyushu’s Home Manager config and evaluate Aomi’s configuration to ensure both remain valid.
+
+### Task 2: Display action helper
+
+**Files:**
+- Create: `dots/config/raffi/scripts/niri-display`
+
+**Consumes:** `niri msg --json outputs`, `/sys/class/drm/*/status`, `systemctl --user`, `wl-mirror`.
+
+**Produces:** `niri-display {extend-left|extend-right|mirror|enable|disable} OUTPUT`.
+
+- [ ] Write the helper with strict Bash mode and functions to discover a connected eDP output, verify a selected external DRM connector, obtain each enabled output’s logical width, and stop the named mirror unit.
+- [ ] For `extend-right`, set eDP to `0,0` and target to eDP logical width, `0`.
+- [ ] For `extend-left`, set target to `0,0` and eDP to target logical width, `0`.
+- [ ] For `mirror`, apply the requested position, stop the existing named unit, then launch `wl-mirror` in a named systemd user scope targeting the selected output.
+- [ ] For `disable`, stop the mirror when relevant then power off only the selected output. For `enable`, power on only the selected output.
+- [ ] Run shellcheck and manually exercise helper argument validation and Niri JSON parsing without changing output state.
+
+### Task 3: Raffi action discovery
+
+**Files:**
+- Create: `dots/config/raffi/scripts/niri-display-raffi`
+- Modify: `dots/config/raffi/raffi.yaml`
+
+**Consumes:** Niri output JSON, connected DRM output names, and `niri-display` action interface.
+
+**Produces:** A `display` Raffi filter that emits left, right, mirror, enable, and disable entries per external connector.
+
+- [ ] Write the script filter using the existing Alfred-style JSON convention.
+- [ ] Join Niri’s enabled-output metadata with connected DRM connector names, exclude the eDP source, and display connector plus model/resolution when available.
+- [ ] Emit arguments in a safely parseable `ACTION:OUTPUT` format and filter titles/subtitles by Raffi’s query input.
+- [ ] Emit an explanatory zero-results item when no eDP source or external targets are present.
+- [ ] Add one Raffi script-filter entry whose action invokes `niri-display {value}`.
+- [ ] Run the script directly and validate its JSON with jq for the current HDMI target.
+
+### Task 4: End-to-end verification
+
+**Files:**
+- Verify: all files above
+
+- [ ] Run `shellcheck` on both new scripts.
+- [ ] Run the appropriate Kyushu Home Manager build/evaluation without switching configuration.
+- [ ] Test `extend-right` and `extend-left` with the connected HDMI display and inspect `niri msg --json outputs` positions.
+- [ ] Test `mirror`, verify the named user unit is running and `wl-mirror` is fullscreen on the chosen output, then test cleanup by switching to extend mode.
+- [ ] Test disable then enable for HDMI, confirming no mirror unit remains after disable.
+- [ ] Confirm no unrequested files changed with `git diff --check` and `git status --short`.
docs/superpowers/specs/2026-10-07-kyushu-display-control-design.md
@@ -0,0 +1,43 @@
+# Kyushu Display Control Design
+
+## Goal
+
+Provide a Kyushu-only Raffi menu to arrange the laptop panel and any currently connected external Niri outputs, and mirror the laptop panel to one chosen external output.
+
+## Decisions
+
+- Kyushu uses explicit manual display control; disable Kanshi on that host only.
+- Keep shared Kanshi configuration intact because Aomi uses its `aomi` profile.
+- The source for mirroring is the connected `eDP-*` laptop panel.
+- Discover target displays dynamically rather than hard-code `HDMI-A-1`.
+- Actions affect only the chosen external output and the laptop panel; all other external outputs remain unchanged.
+- Only one `wl-mirror` mirror session may run. Starting a new mirror stops the prior managed session.
+- Offer per-output extend-left, extend-right, mirror, enable, and disable actions.
+- Run the mirror in a named user systemd unit, so cleanup is exact rather than process-name based.
+
+## Architecture
+
+A Kyushu-specific `niri-display` helper invokes Niri IPC, discovers current outputs from Niri JSON and DRM connector state, and manages a `wl-mirror` user unit. `wl-mirror` is installed as a Fedora-native package by the Kyushu bootstrap so it uses the host Mesa/EGL driver stack. A Raffi script filter renders one action item per external display and dispatches the selected action to the helper.
+
+Enabled-output metadata comes from `niri msg --json outputs`. DRM `/sys/class/drm/card*-*/status` provides connector names for connected outputs which are currently disabled by Niri, allowing the menu to expose an enable action. The helper validates a connected eDP source and chosen connected external target before acting.
+
+## Layout semantics
+
+For an external target with logical width `target_width` and eDP source with logical width `edp_width`:
+
+- Extend right: eDP at `0,0`; target at `edp_width,0`.
+- Extend left: target at `0,0`; eDP at `target_width,0`.
+- Mirror uses the chosen placement then starts `wl-mirror eDP --fullscreen-output target --fullscreen`.
+
+The mirror is a fullscreen client, not compositor-level output cloning. Aspect-ratio differences are handled by `wl-mirror` fitting the source image.
+
+## Error handling
+
+- The Raffi list reports when no eDP source or no external connector is available.
+- The helper exits non-zero with an actionable error when Niri cannot be contacted, the source is absent, or a target is disconnected.
+- `disable` stops the managed mirror first when its target matches.
+- `enable` only enables the selected output; it preserves its layout and does not start mirroring.
+
+## Verification
+
+Build the Kyushu Home Manager configuration. With an HDMI display connected, test left/right extension, mirror start, switching mirror targets, disable, re-enable, and cable-disconnected errors. Confirm Kanshi remains enabled in Aomi's generated configuration and disabled in Kyushu's.
dots/config/raffi/scripts/niri-display
@@ -0,0 +1,127 @@
+#!/usr/bin/env bash
+# Manage the laptop panel and one selected external Niri output.
+# Usage: niri-display {extend-left|extend-right|mirror-left|mirror-right|enable|disable} OUTPUT
+set -euo pipefail
+
+readonly mirror_unit="niri-display-mirror.service"
+readonly mirror_target_file="${XDG_RUNTIME_DIR:?XDG_RUNTIME_DIR is required}/niri-display-mirror-target"
+
+action="${1:-}"
+target="${2:-}"
+
+usage() {
+    echo "usage: $(basename "$0") {extend-left|extend-right|mirror-left|mirror-right|enable|disable} OUTPUT" >&2
+    exit 2
+}
+
+[[ -n "$action" && -n "$target" && $# -eq 2 ]] || usage
+
+outputs_json() {
+    niri msg --json outputs
+}
+
+source_output() {
+    outputs_json | jq -er 'keys[] | select(test("^eDP-"))' | head -n1
+}
+
+logical_width() {
+    local output="$1"
+    outputs_json | jq -er --arg output "$output" '.[$output].logical.width'
+}
+
+is_connected() {
+    local output="$1"
+    local status
+    local connector
+
+    shopt -s nullglob
+    for status in /sys/class/drm/card*-*/status; do
+        connector=$(basename "$(dirname "$status")")
+        connector=$(sed -E 's/^card[0-9]+-//' <<< "$connector")
+        if [[ "$connector" == "$output" && $(<"$status") == "connected" ]]; then
+            return 0
+        fi
+    done
+    return 1
+}
+
+mirror_target() {
+    [[ -f "$mirror_target_file" ]] && cat "$mirror_target_file"
+}
+
+stop_mirror() {
+    systemctl --user stop "$mirror_unit" 2>/dev/null || true
+    rm -f "$mirror_target_file"
+}
+
+set_right() {
+    local source="$1"
+    local source_width
+    source_width=$(logical_width "$source")
+
+    niri msg output "$source" position set 0 0
+    niri msg output "$target" position set "$source_width" 0
+}
+
+set_left() {
+    local source="$1"
+    local target_width
+    target_width=$(logical_width "$target")
+
+    niri msg output "$target" position set 0 0
+    niri msg output "$source" position set "$target_width" 0
+}
+
+[[ "$target" != eDP-* ]] || {
+    echo "target must be an external output, got: $target" >&2
+    exit 2
+}
+
+is_connected "$target" || {
+    echo "output is not connected: $target" >&2
+    exit 1
+}
+
+case "$action" in
+    enable)
+        niri msg output "$target" on
+        ;;
+    disable)
+        current_mirror_target=$(mirror_target || true)
+        if [[ "$current_mirror_target" == "$target" ]]; then
+            stop_mirror
+        fi
+        niri msg output "$target" off
+        ;;
+    extend-left | extend-right | mirror-left | mirror-right)
+        source=$(source_output) || {
+            echo "no enabled eDP output is available as a mirror source" >&2
+            exit 1
+        }
+
+        niri msg output "$target" on
+        stop_mirror
+
+        case "$action" in
+            extend-right | mirror-right) set_right "$source" ;;
+            extend-left | mirror-left) set_left "$source" ;;
+        esac
+
+        if [[ "$action" == mirror-* ]]; then
+            wl_mirror=$(command -v wl-mirror) || {
+                echo "wl-mirror is not installed" >&2
+                exit 1
+            }
+            systemd-run --user --unit="${mirror_unit%.service}" --collect --service-type=exec \
+                "$wl_mirror" --fullscreen-output "$target" --fullscreen "$source"
+            sleep 0.1
+            if ! systemctl --user is-active --quiet "$mirror_unit"; then
+                rm -f "$mirror_target_file"
+                echo "wl-mirror failed to start; inspect: journalctl --user -u $mirror_unit" >&2
+                exit 1
+            fi
+            printf '%s\n' "$target" > "$mirror_target_file"
+        fi
+        ;;
+    *) usage ;;
+esac
dots/config/raffi/scripts/niri-display-raffi
@@ -0,0 +1,71 @@
+#!/usr/bin/env bash
+# List display actions for Raffi. Outputs Alfred Script Filter JSON.
+# Usage: niri-display-raffi [query]
+set -euo pipefail
+
+query="${1:-}"
+
+# This menu is enabled only on hosts provisioned with the native wl-mirror package.
+command -v wl-mirror >/dev/null || {
+    jq -n '{"items": []}'
+    exit 0
+}
+
+outputs_json=$(niri msg --json outputs 2>/dev/null) || {
+    jq -n '{"items": [{"title": "Niri is unavailable", "subtitle": "Start this from a Niri session", "arg": ""}]}'
+    exit 0
+}
+
+source=$(jq -r 'keys[] | select(test("^eDP-"))' <<< "$outputs_json" | head -n1)
+if [[ -z "$source" ]]; then
+    jq -n '{"items": [{"title": "No laptop display found", "subtitle": "An enabled eDP output is required as the mirror source", "arg": ""}]}'
+    exit 0
+fi
+
+add_item() {
+    local title="$1"
+    local subtitle="$2"
+    local arg="$3"
+
+    if [[ -n "$query" ]] && ! grep -Fqi -- "$query" <<< "$title $subtitle"; then
+        return
+    fi
+
+    items=$(jq --arg title "$title" --arg subtitle "$subtitle" --arg arg "$arg" \
+        '. + [{"title": $title, "subtitle": $subtitle, "arg": $arg}]' <<< "$items")
+}
+
+items="[]"
+targets=()
+shopt -s nullglob
+for status in /sys/class/drm/card*-*/status; do
+    [[ $(<"$status") == "connected" ]] || continue
+    target=$(basename "$(dirname "$status")")
+    target=$(sed -E 's/^card[0-9]+-//' <<< "$target")
+    [[ "$target" == eDP-* ]] && continue
+    targets+=("$target")
+done
+
+if [[ ${#targets[@]} -eq 0 ]]; then
+    jq -n '{"items": [{"title": "No external displays connected", "subtitle": "Connect an HDMI, DisplayPort, or dock display", "arg": ""}]}'
+    exit 0
+fi
+
+for target in "${targets[@]}"; do
+    details=$(jq -r --arg output "$target" '
+        .[$output] | select(.logical != null)
+        | "\(.make // "Unknown") \(.model // "display") — \(.logical.width)x\(.logical.height)"
+    ' <<< "$outputs_json")
+
+    if [[ -n "$details" ]]; then
+        add_item "Extend: $target on right" "$details" "extend-right $target"
+        add_item "Extend: $target on left" "$details" "extend-left $target"
+        add_item "Mirror laptop to $target on right" "$details" "mirror-right $target"
+        add_item "Mirror laptop to $target on left" "$details" "mirror-left $target"
+        add_item "Disable $target" "$details" "disable $target"
+    else
+        add_item "Enable $target" "Connected but disabled in Niri" "enable $target"
+    fi
+done
+
+jq -n --argjson items "$items" '{"items": $items}'
dots/config/raffi/raffi.yaml
@@ -81,6 +81,11 @@ addons:
     command: ~/.config/raffi/scripts/niri-windows-raffi
     icon: preferences-system-windows
     action: niri msg action focus-window --id {value}
+  - name: Displays
+    keyword: display
+    command: ~/.config/raffi/scripts/niri-display-raffi
+    icon: video-display
+    action: ~/.config/raffi/scripts/niri-display {value}
   - name: Kill Window
     keyword: kill
     command: ~/.config/raffi/scripts/niri-windows-raffi
home/common/desktop/sway/swayidle.nix
@@ -35,7 +35,7 @@ let
     for output in $(${niriCmd} msg outputs | ${pkgs.gnugrep}/bin/grep '^Output' | ${pkgs.gnugrep}/bin/grep -oP '\(\K[^)]+(?=\))'); do
       ${niriCmd} msg output "$output" on
     done
-    ${pkgs.systemd}/bin/systemctl --user restart kanshi
+    ${pkgs.systemd}/bin/systemctl --user try-restart kanshi
   '';
   powerOnCmd =
     if desktop == "niri" then
imperative/kyushu/bootstrap.sh
@@ -174,6 +174,7 @@ install_native_apps() {
 		powertop \
 		podman podman-docker \
 		niri \
+		wl-mirror \
 		kitty \
 		xwayland-satellite \
 		xdg-desktop-portal-gnome \
systems/kyushu/home.nix
@@ -1,9 +1,12 @@
-{ pkgs, ... }:
+{ lib, pkgs, ... }:
 {
   imports = [
     ../../home/common/profiles/work-fedora.nix
   ];
 
+  # Kyushu display layouts are selected explicitly through the Raffi display menu.
+  services.kanshi.enable = lib.mkForce false;
+
   home.file.".gmailctl/config.jsonnet".source = ./config.jsonnet;
   home.file.".gmailctl/gmailctl.libsonnet".source = ./gmailctl.libsonnet;