Repository navigation
Add display layout preview with timed rollback #14533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| #!/bin/bash | ||
|
|
||
| # omarchy:summary=Preview, keep, or revert display layouts | ||
| # omarchy:args=state | preview JSON | keep TOKEN | revert TOKEN | ||
|
|
||
| set -euo pipefail | ||
| umask 077 | ||
|
|
||
| STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/omarchy/monitor-layout" | ||
| CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/hypr/monitors.lua" | ||
| mkdir -p "$STATE_DIR" | ||
| exec 9>"$STATE_DIR/lock" | ||
| flock 9 | ||
| exec 8>"${STATE_DIR%/*}/monitor-config.lock" | ||
| flock 8 | ||
|
|
||
| fail() { echo "$*" >&2; exit 1; } | ||
|
|
||
| publish_config() { | ||
| local replacement="$1" expected="$2" candidate displaced | ||
| [[ -f $CONFIG && ! -L $CONFIG ]] || return 1 | ||
| sha256sum --check --status "$expected" || return 1 | ||
| candidate=$(sha256sum "$replacement" | cut -d ' ' -f 1) | ||
| # Supported writers share the lock. External editors cannot be locked out: | ||
| # exchange retains the exact file displaced at publication, then checks it. | ||
| # Never discard either version when an uncooperative writer races the swap. | ||
| mv --exchange --no-copy -T "$replacement" "$CONFIG" || return 1 | ||
| displaced=$(sha256sum "$replacement" | cut -d ' ' -f 1) | ||
| if [[ $displaced == "$(cut -d ' ' -f 1 "$expected")" && ! -L $replacement ]]; then | ||
| return 0 | ||
| else | ||
| if [[ ! -L $CONFIG && $(sha256sum "$CONFIG" | cut -d ' ' -f 1) == "$candidate" ]]; then | ||
| mv --exchange --no-copy -T "$replacement" "$CONFIG" || return 1 | ||
| fi | ||
| echo "Concurrent monitors.lua edit; retained displaced version at $replacement" >&2 | ||
| return 1 | ||
| fi | ||
| } | ||
|
|
||
| evaluate() { | ||
| local result | ||
| result=$(hyprctl eval "(function() $1 end)()") | ||
| if [[ $result != "ok" ]]; then | ||
| echo "$result" >&2 | ||
| return 1 | ||
| fi | ||
| } | ||
|
|
||
| monitor_code() { | ||
| jq -r '.displays[] | "hl.monitor({ output = \(.name | tojson), mode = \(.mode | tojson), position = \(( (.x|tostring) + "x" + (.y|tostring)) | tojson), scale = \(.scale), transform = \(.transform), disabled = false })"' "$1" | ||
| } | ||
|
|
||
| layout_matches() { | ||
| jq -e --argjson monitors "$(hyprctl -j monitors all)" ' | ||
| all(.displays[]; . as $d | | ||
| (.mode | capture("^(?<width>[0-9]+)x(?<height>[0-9]+)@(?<rate>[0-9.]+)$")) as $mode | | ||
| any($monitors[]; .name == $d.name and .disabled != true and | ||
| .width == ($mode.width|tonumber) and .height == ($mode.height|tonumber) and | ||
| ((.refreshRate - ($mode.rate|tonumber)) | fabs) < 0.15 and | ||
| .x == $d.x and .y == $d.y and .transform == $d.transform and | ||
| ((.scale - $d.scale) | fabs) < 0.01)) | ||
| ' "$STATE_DIR/after.json" >/dev/null | ||
| } | ||
|
|
||
| restore() { | ||
| # Reload restores the saved monitor rules, then restore the runtime | ||
| # positions captured before the preview. | ||
| # If Keep was interrupted after publishing the new file, roll it back only | ||
| # while it still matches our candidate. Never overwrite a subsequent edit. | ||
| if [[ -f $STATE_DIR/config.changed ]]; then | ||
| if sha256sum --check --status "$STATE_DIR/candidate.sha256"; then | ||
| local replacement | ||
| replacement=$(mktemp "${CONFIG%/*}/.monitors.XXXXXX") | ||
| cp -p "$STATE_DIR/monitors.lua.backup" "$replacement" | ||
| if publish_config "$replacement" "$STATE_DIR/candidate.sha256"; then | ||
| rm -f "$replacement" | ||
| else | ||
| echo "Recovery preserved the changed monitors.lua and $replacement" >&2 | ||
| fi | ||
| fi | ||
| rm -f "$STATE_DIR/config.changed" | ||
| fi | ||
| hyprctl reload >/dev/null | ||
| local monitors | ||
| monitors=$(hyprctl -j monitors all) | ||
| jq --argjson monitors "$monitors" ' | ||
| .displays |= map(select(.name as $name | any($monitors[]; .name == $name))) | ||
| ' "$STATE_DIR/before.json" >"$STATE_DIR/restore.json" | ||
| evaluate "$(monitor_code "$STATE_DIR/restore.json")" | ||
| rm -f "$STATE_DIR/pending.json" | ||
| } | ||
|
|
||
| case "${1:-}" in | ||
| state) | ||
| monitors=$(hyprctl -j monitors all) | ||
| pending=null | ||
| [[ ! -f $STATE_DIR/pending.json ]] || pending=$(cat "$STATE_DIR/pending.json") | ||
| preview=null | ||
| [[ $pending == "null" ]] || preview=$(cat "$STATE_DIR/after.json") | ||
| jq -n --argjson monitors "$monitors" --argjson pending "$pending" --argjson preview "$preview" \ | ||
| '{monitors: $monitors, pending: $pending, preview: $preview}' | ||
| ;; | ||
| preview) | ||
| [[ ! -f $STATE_DIR/pending.json ]] || fail "Keep or revert the current preview first" | ||
| monitors=$(hyprctl -j monitors all) | ||
| request=${2:-} | ||
| # JSON is data only. Connector names, modes and coordinates are validated | ||
| # before generating Lua. | ||
| jq -e --argjson monitors "$monitors" ' | ||
| type == "object" and keys == ["displays"] and (.displays | type == "array" and length > 0) and | ||
| ([.displays[].name] | length == (unique | length)) and | ||
| (.displays as $displays | all($displays[]; | ||
| . as $d | [.name, .mode] | all(.[]; type == "string") ) ) and | ||
| all(.displays[]; . as $d | | ||
| (.name | test("^[A-Za-z0-9._-]+$")) and | ||
| (.mode | test("^[0-9]+x[0-9]+@[0-9]+([.][0-9]+)?$")) and | ||
| ([.x, .y, .transform] | all(.[]; type == "number" and . == floor)) and | ||
| (.x >= -32768 and .x <= 32768 and .y >= -32768 and .y <= 32768) and | ||
| (.transform >= 0 and .transform <= 3) and | ||
| (.scale | type == "number" and . >= 1 and . <= 4) and | ||
| any($monitors[]; .name == $d.name and .disabled != true and | ||
| (.mirrorOf == "none" or .mirrorOf == null) and | ||
| any(.availableModes[]; sub("Hz$"; "") == $d.mode))) and | ||
| ([.displays[].name] | sort) == ([$monitors[] | select(.disabled != true) | .name] | sort) | ||
| ' <<<"$request" >/dev/null || fail "Invalid layout; use active displays and their supported modes" | ||
| jq -n --argjson monitors "$monitors" '{ | ||
| displays: [$monitors[] | select(.disabled != true) | { | ||
| name, mode: ((.width|tostring) + "x" + (.height|tostring) + "@" + (.refreshRate|tostring)), | ||
| x, y, scale, transform | ||
| }] | ||
| }' >"$STATE_DIR/before.json" | ||
| printf '%s\n' "$request" >"$STATE_DIR/after.json" | ||
| [[ -f $CONFIG && ! -L $CONFIG ]] || fail "monitors.lua must be a regular file" | ||
| sha256sum "$CONFIG" >"$STATE_DIR/config.sha256" | ||
| token=$(cat /proc/sys/kernel/random/uuid) | ||
| expires=$(( $(date +%s) + 20 )) | ||
| jq -n --arg token "$token" --argjson expires "$expires" '{token: $token, expires: $expires}' >"$STATE_DIR/pending.json" | ||
| set -E | ||
| trap 'trap - ERR; restore' ERR | ||
| # User service survives launcher exit, shell/plugin reload and loss of the | ||
| # popup. Pass the compositor instance and XDG roots explicitly. | ||
| systemd-run --user --quiet --collect --property=Type=exec \ | ||
| --setenv="HYPRLAND_INSTANCE_SIGNATURE=${HYPRLAND_INSTANCE_SIGNATURE:-}" \ | ||
| --setenv="XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}" \ | ||
| --setenv="XDG_STATE_HOME=${XDG_STATE_HOME:-$HOME/.local/state}" \ | ||
| /bin/bash -c 'sleep 20; exec "$1" revert "$2"' bash "$(readlink -f "${BASH_SOURCE[0]}")" "$token" | ||
| evaluate "$(monitor_code "$STATE_DIR/after.json")" | ||
| layout_matches || { restore; fail "The compositor could not apply the requested layout"; } | ||
| cat "$STATE_DIR/pending.json" | ||
| ;; | ||
| keep | revert) | ||
| [[ -f $STATE_DIR/pending.json ]] || fail "No pending preview" | ||
| [[ ${2:-} == "$(jq -r .token "$STATE_DIR/pending.json")" ]] || fail "Stale preview token" | ||
| if [[ $1 == "revert" ]]; then | ||
| restore | ||
| else | ||
| (( $(date +%s) < $(jq -r .expires "$STATE_DIR/pending.json") )) || { restore; fail "Preview expired"; } | ||
| sha256sum --check --status "$STATE_DIR/config.sha256" || { restore; fail "monitors.lua changed during preview"; } | ||
| monitors=$(hyprctl -j monitors all) | ||
| jq -e --argjson monitors "$monitors" ' | ||
| ([.displays[].name] | sort) == ([$monitors[] | select(.disabled != true) | .name] | sort) | ||
| ' "$STATE_DIR/after.json" >/dev/null || { restore; fail "Connected displays changed during preview"; } | ||
| layout_matches || { restore; fail "Display settings changed during preview"; } | ||
| begin_count=$(awk '/^-- BEGIN OMARCHY DISPLAY LAYOUT$/{n++} END{print n+0}' "$CONFIG") | ||
| end_count=$(awk '/^-- END OMARCHY DISPLAY LAYOUT$/{n++} END{print n+0}' "$CONFIG") | ||
| (( begin_count == end_count && begin_count <= 1 )) || { restore; fail "Invalid display layout markers"; } | ||
| awk ' | ||
| /^-- BEGIN OMARCHY DISPLAY LAYOUT$/ { if (inside || ended) exit 1; inside=1 } | ||
| /^-- END OMARCHY DISPLAY LAYOUT$/ { if (!inside) exit 1; inside=0; ended=1 } | ||
| END { if (inside) exit 1 } | ||
| ' "$CONFIG" || { restore; fail "Invalid display layout marker order"; } | ||
| cp -p "$CONFIG" "$STATE_DIR/monitors.lua.backup" | ||
| [[ $(sha256sum "$STATE_DIR/monitors.lua.backup" | cut -d ' ' -f 1) == "$(cut -d ' ' -f 1 "$STATE_DIR/config.sha256")" ]] || { restore; fail "monitors.lua changed while capturing the save snapshot"; } | ||
| sha256sum --check --status "$STATE_DIR/config.sha256" || { restore; fail "monitors.lua changed before saving"; } | ||
| # Own only this marked block; preserve every personal line outside it. | ||
| awk '/^-- BEGIN OMARCHY DISPLAY LAYOUT$/{skip=1; next} /^-- END OMARCHY DISPLAY LAYOUT$/{skip=0; next} !skip' \ | ||
| "$STATE_DIR/monitors.lua.backup" >"$STATE_DIR/monitors.lua.new" | ||
| { | ||
| echo '-- BEGIN OMARCHY DISPLAY LAYOUT' | ||
| # Retain our generated rules for disconnected outputs. A later hotplug | ||
| # must not lose its arrangement when another display is edited. | ||
| awk '/^-- BEGIN OMARCHY DISPLAY LAYOUT$/{inside=1; next} /^-- END OMARCHY DISPLAY LAYOUT$/{inside=0} inside' "$STATE_DIR/monitors.lua.backup" | | ||
| jq -Rr --argjson active "$(jq '[.displays[].name]' "$STATE_DIR/after.json")" ' | ||
| select(startswith("hl.monitor(")) | . as $line | | ||
| capture("output = \"(?<name>[A-Za-z0-9._-]+)\"") | | ||
| select(.name as $name | $active | index($name) == null) | $line' | ||
| monitor_code "$STATE_DIR/after.json" | ||
| echo '-- END OMARCHY DISPLAY LAYOUT' | ||
| } >>"$STATE_DIR/monitors.lua.new" | ||
| replacement=$(mktemp "${CONFIG%/*}/.monitors.XXXXXX") | ||
| cp -p "$STATE_DIR/monitors.lua.backup" "$replacement" | ||
| cat "$STATE_DIR/monitors.lua.new" >"$replacement" | ||
| printf '%s %s\n' "$(sha256sum "$replacement" | cut -d ' ' -f 1)" "$CONFIG" >"$STATE_DIR/candidate.sha256" | ||
| touch "$STATE_DIR/config.changed" | ||
| set -E | ||
| trap 'trap - ERR; restore' ERR | ||
| publish_config "$replacement" "$STATE_DIR/config.sha256" || { restore; fail "monitors.lua changed while saving; conflict versions were retained"; } | ||
| rm -f "$replacement" | ||
| hyprctl reload >/dev/null | ||
| errors=$(hyprctl configerrors) | ||
| if [[ -n $errors ]]; then | ||
| restore | ||
| fail "$errors" | ||
| fi | ||
| layout_matches || { restore; fail "Saved display layout did not survive reload"; } | ||
| rm -f "$STATE_DIR/pending.json" "$STATE_DIR/config.changed" | ||
| fi | ||
| ;; | ||
| -h | --help) | ||
| echo 'Usage: omarchy monitor layout state | preview JSON | keep TOKEN | revert TOKEN' | ||
| ;; | ||
| *) fail 'Usage: omarchy monitor layout state | preview JSON | keep TOKEN | revert TOKEN' ;; | ||
| esac | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| { | ||
| "capturedOn": "2026-10-21", | ||
| "base": "quattro ce39c5e9", | ||
| "environment": { | ||
| "compositor": "Hyprland 0.56.2", | ||
| "Qt": "6.11.2", | ||
| "shell": "Full production Quickshell config from this branch", | ||
| "outputs": "Two isolated headless outputs; no physical monitor changes", | ||
| "watchdogLauncher": "The isolated session has no systemd user manager; a stand-in started the same watchdog command detached, without the caller's lock descriptors", | ||
| "notVerified": [ | ||
| "disposable VM acceptance", | ||
| "physical hardware preview/Keep", | ||
| "per-monitor workspace reconciliation (the isolated compositor does not load Omarchy's Hyprland config)" | ||
| ] | ||
| }, | ||
| "timedRollback": { | ||
| "input": "GUI selects HEADLESS-1 beside HEADLESS-2, Below, then Preview", | ||
| "preview": [ | ||
| { "name": "HEADLESS-1", "x": 1920, "y": 1080 }, | ||
| { "name": "HEADLESS-2", "x": 1920, "y": 0 } | ||
| ], | ||
| "afterExpiry": [ | ||
| { "name": "HEADLESS-1", "x": 0, "y": 0 }, | ||
| { "name": "HEADLESS-2", "x": 1920, "y": 0 } | ||
| ], | ||
| "configUnchanged": true, | ||
| "images": ["images/display-layout-before.png", "images/display-layout-preview.png", "images/display-layout-reverted.png"] | ||
| }, | ||
| "keep": { | ||
| "input": "GUI Below, Preview, Keep; explicit compositor reload afterward", | ||
| "afterReload": [ | ||
| { "name": "HEADLESS-1", "x": 1920, "y": 1080 }, | ||
| { "name": "HEADLESS-2", "x": 1920, "y": 0 } | ||
| ], | ||
| "configErrors": "", | ||
| "workspaceRulesWritten": false, | ||
| "watchdogAfterKeep": "No pending preview", | ||
| "image": "images/display-layout-kept.png" | ||
| } | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After Keep, the generated per-display rules pin each screen's scale. The scaling shortcut still saves only
omarchy_monitor_scaleor the catch-all rule. Its change works immediately but disappears on the next config reload or login because the saved per-display scale takes precedence.Update the saved per-display rule when scaling a screen managed by the layout editor.