diff --git a/bin/omarchy-hyprland-reload-guard b/bin/omarchy-hyprland-reload-guard index 6110ceda..218da79e 100755 --- a/bin/omarchy-hyprland-reload-guard +++ b/bin/omarchy-hyprland-reload-guard @@ -16,81 +16,54 @@ esac run_root="${OMARCHY_HYPRLAND_RELOAD_GUARD_RUN_ROOT:-/run/user}" state_dir="${OMARCHY_HYPRLAND_RELOAD_GUARD_STATE_DIR:-/run/omarchy/hyprland-reload-guard}" -hyprctl_bin="${HYPRCTL:-}" - -if [[ -z $hyprctl_bin ]]; then - hyprctl_bin=/usr/bin/hyprctl - [[ -x $hyprctl_bin ]] || hyprctl_bin=$(command -v hyprctl || true) -fi -[[ -n $hyprctl_bin && -x $hyprctl_bin ]] || exit 0 +hyprctl_bin="${HYPRCTL:-/usr/bin/hyprctl}" hyprctl_instance() { local runtime_dir="$1" local signature="$2" shift 2 - XDG_RUNTIME_DIR="$runtime_dir" \ - HYPRLAND_INSTANCE_SIGNATURE="$signature" \ - "$hyprctl_bin" --instance "$signature" "$@" + XDG_RUNTIME_DIR="$runtime_dir" "$hyprctl_bin" --instance "$signature" "$@" } option_bool() { local runtime_dir="$1" local signature="$2" local option="$3" - local output="" - output=$(hyprctl_instance "$runtime_dir" "$signature" -j getoption "$option" 2>/dev/null) || return 1 - - if command -v jq >/dev/null 2>&1; then - jq -r 'if .bool == true then "true" else "false" end' <<<"$output" - elif [[ $output == *'"bool": true'* ]]; then - printf 'true\n' - else - printf 'false\n' - fi + hyprctl_instance "$runtime_dir" "$signature" -j getoption "$option" 2>/dev/null | jq -r '.bool' } instances() { + local instance_dir signature hypr_dir runtime_dir uid + [[ -d $run_root ]] || return 0 - find "$run_root" -mindepth 3 -maxdepth 3 -type d -path "$run_root/*/hypr/*" -print 2>/dev/null | - while IFS= read -r instance_dir; do - local signature hypr_dir runtime_dir uid + for instance_dir in "$run_root"/*/hypr/*; do + [[ -d $instance_dir ]] || continue - signature="${instance_dir##*/}" - hypr_dir="${instance_dir%/*}" - runtime_dir="${hypr_dir%/*}" - uid="${runtime_dir##*/}" + signature="${instance_dir##*/}" + hypr_dir="${instance_dir%/*}" + runtime_dir="${hypr_dir%/*}" + uid="${runtime_dir##*/}" - [[ $uid =~ ^[0-9]+$ ]] || continue - [[ -n $signature ]] || continue + [[ $uid =~ ^[0-9]+$ ]] || continue - printf '%s\t%s\t%s\n' "$runtime_dir" "$uid" "$signature" - done -} - -state_file_for() { - local signature="$1" - - printf '%s/%s\n' "$state_dir" "$signature" + printf '%s\t%s\n' "$runtime_dir" "$signature" + done } pause_instance() { local runtime_dir="$1" - local uid="$2" - local signature="$3" - local disable_autoreload suppress_errors state_file + local signature="$2" + local disable_autoreload suppress_errors state_file="$state_dir/$signature" mkdir -p "$state_dir" - state_file=$(state_file_for "$signature") if [[ ! -f $state_file ]]; then disable_autoreload=$(option_bool "$runtime_dir" "$signature" misc.disable_autoreload) || return 0 suppress_errors=$(option_bool "$runtime_dir" "$signature" debug.suppress_errors) || return 0 - - printf '%s\t%s\t%s\n' "$uid" "$disable_autoreload" "$suppress_errors" >"$state_file" - chmod 0600 "$state_file" 2>/dev/null || true + printf '%s\t%s\t%s\n' "$runtime_dir" "$disable_autoreload" "$suppress_errors" >"$state_file" fi hyprctl_instance "$runtime_dir" "$signature" eval \ @@ -100,11 +73,10 @@ pause_instance() { resume_instance() { local state_file="$1" - local signature uid disable_autoreload suppress_errors runtime_dir + local signature="${state_file##*/}" + local runtime_dir disable_autoreload suppress_errors - signature="${state_file##*/}" - IFS=$'\t' read -r uid disable_autoreload suppress_errors <"$state_file" || return 0 - runtime_dir="$run_root/$uid" + IFS=$'\t' read -r runtime_dir disable_autoreload suppress_errors <"$state_file" || return 0 if [[ -d $runtime_dir ]]; then hyprctl_instance "$runtime_dir" "$signature" eval \ @@ -123,16 +95,16 @@ resume_instance() { case "$command" in pause) - while IFS=$'\t' read -r runtime_dir uid signature; do - pause_instance "$runtime_dir" "$uid" "$signature" + while IFS=$'\t' read -r runtime_dir signature; do + pause_instance "$runtime_dir" "$signature" done < <(instances) ;; resume) [[ -d $state_dir ]] || exit 0 - find "$state_dir" -mindepth 1 -maxdepth 1 -type f -print 2>/dev/null | - while IFS= read -r state_file; do - resume_instance "$state_file" - done + for state_file in "$state_dir"/*; do + [[ -f $state_file ]] || continue + resume_instance "$state_file" + done rmdir "$state_dir" 2>/dev/null || true ;; esac diff --git a/test/shell.d/hyprland-reload-guard-test.sh b/test/shell.d/hyprland-reload-guard-test.sh index 6effb703..6d8ccb12 100755 --- a/test/shell.d/hyprland-reload-guard-test.sh +++ b/test/shell.d/hyprland-reload-guard-test.sh @@ -19,7 +19,7 @@ mkdir -p "$runtime_dir/hypr/$signature" cat >"$fake_hyprctl" <<'BASH' #!/bin/bash -printf '%s\t%s\t%s\n' "$XDG_RUNTIME_DIR" "$HYPRLAND_INSTANCE_SIGNATURE" "$*" >>"$FAKE_HYPRCTL_LOG" +printf '%s\t%s\n' "$XDG_RUNTIME_DIR" "$*" >>"$FAKE_HYPRCTL_LOG" case "$*" in *'getoption misc.disable_autoreload'*) @@ -43,7 +43,8 @@ FAKE_HYPRCTL_LOG="$hyprctl_log" \ state_file="$state_dir/$signature" [[ -f $state_file ]] || fail "reload guard stores Hyprland state on pause" -grep -Fx $'1000 false false' "$state_file" >/dev/null || fail "reload guard records previous Hyprland reload settings" +expected_state=$(printf '%s\tfalse\tfalse' "$runtime_dir") +grep -Fx "$expected_state" "$state_file" >/dev/null || fail "reload guard records previous Hyprland reload settings" grep -F 'hl.config({ misc = { disable_autoreload = true }, debug = { suppress_errors = true } })' "$hyprctl_log" >/dev/null || fail "reload guard pauses autoreload with hyprctl eval" pass "reload guard pauses live Hyprland reloads" @@ -55,6 +56,6 @@ FAKE_HYPRCTL_LOG="$hyprctl_log" \ "$ROOT/bin/omarchy-hyprland-reload-guard" resume [[ ! -e $state_file ]] || fail "reload guard clears Hyprland state after resume" -grep -F $'test-signature --instance test-signature reload' "$hyprctl_log" >/dev/null || fail "reload guard forces one Hyprland reload after package transaction" +grep -F -- '--instance test-signature reload' "$hyprctl_log" >/dev/null || fail "reload guard forces one Hyprland reload after package transaction" grep -F 'hl.config({ misc = { disable_autoreload = false }, debug = { suppress_errors = false } })' "$hyprctl_log" >/dev/null || fail "reload guard restores previous Hyprland reload settings" pass "reload guard resumes live Hyprland reloads"