Fix clamshell display recovery

This commit is contained in:
David Heinemeier Hansson
2026-06-23 09:23:29 +02:00
parent ee65304b92
commit aa7350ab60
7 changed files with 142 additions and 26 deletions
+54 -2
View File
@@ -3,9 +3,61 @@
# omarchy:summary=Apply clamshell display state to Hyprland monitors
# omarchy:hidden=true
TOGGLES_DIR="$HOME/.local/state/omarchy/toggles/hypr"
CLAMSHELL_FLAG="$TOGGLES_DIR/internal-monitor-clamshell.lua"
MANUAL_DISABLE_FLAG="$TOGGLES_DIR/internal-monitor-disable.lua"
INTERNAL=$(hyprctl monitors all -j | jq -r '.[] | select(.name | contains("eDP")).name' | head -n 1)
enable_internal_output() {
[[ -n $INTERNAL ]] || return 0
hyprctl eval "hl.monitor({ output = \"$INTERNAL\", mode = \"preferred\", position = \"auto\", scale = \"auto\" })" >/dev/null 2>&1 || true
}
dpms_internal() {
local action="$1"
[[ -n $INTERNAL ]] || return 0
hyprctl dispatch "hl.dsp.dpms({ action = \"$action\", monitor = \"$INTERNAL\" })" >/dev/null 2>&1 || true
}
enable_internal() {
local changed=0
if [[ -f $CLAMSHELL_FLAG ]]; then
rm -f "$CLAMSHELL_FLAG"
changed=1
fi
if (( changed )); then
hyprctl reload >/dev/null 2>&1 || true
fi
[[ -f $MANUAL_DISABLE_FLAG ]] && omarchy-hyprland-monitor-external-active && return 0
enable_internal_output
dpms_internal enable
}
disable_internal() {
[[ -n $INTERNAL ]] || exit 0
[[ -f $MANUAL_DISABLE_FLAG ]] && return 0
mkdir -p "$TOGGLES_DIR"
local config
config=$(printf 'hl.monitor({ output = "%s", disabled = true })' "$INTERNAL")
if [[ ! -f $CLAMSHELL_FLAG ]] || [[ $(< "$CLAMSHELL_FLAG") != $config ]]; then
printf '%s\n' "$config" >"$CLAMSHELL_FLAG"
hyprctl reload >/dev/null 2>&1 || true
fi
}
omarchy-hyprland-monitor-internal recover >/dev/null 2>&1 || true
omarchy-hyprland-monitor-internal-mirror recover >/dev/null 2>&1 || true
if omarchy-hw-clamshell; then
omarchy-hyprland-monitor-internal off >/dev/null 2>&1 || true
if omarchy-hw-clamshell && omarchy-hyprland-monitor-external-active; then
disable_internal
else
enable_internal
fi
+6
View File
@@ -0,0 +1,6 @@
#!/bin/bash
# omarchy:summary=Returns true when Hyprland has an active external monitor
# omarchy:hidden=true
hyprctl monitors -j | jq -e '.[] | select(.name | contains("eDP") | not) | select(.disabled == false)' >/dev/null 2>&1
+20 -5
View File
@@ -7,18 +7,29 @@ TOGGLE="internal-monitor-disable"
TOGGLE_FLAG="$HOME/.local/state/omarchy/toggles/hypr/$TOGGLE.lua"
MIRROR_TOGGLE="internal-monitor-mirror"
# Get internal monitor name dynamically
INTERNAL=$(hyprctl monitors -j | jq -r '.[] | select(.name | contains("eDP")).name' | head -n 1)
# Get internal monitor name dynamically, including disabled outputs.
INTERNAL=$(hyprctl monitors all -j | jq -r '.[] | select(.name | contains("eDP")).name' | head -n 1)
wake() {
hyprctl dispatch 'hl.dsp.dpms({ action = "enable" })' >/dev/null 2>&1 || true
}
on() {
if omarchy-hyprland-toggle-enabled $TOGGLE; then
omarchy-hyprland-toggle $TOGGLE off
omarchy-notification-send -g 󰍹 "Laptop display enabled"
fi
wake
}
off() {
if ! omarchy-hw-external-monitors; then
if [[ -z $INTERNAL ]]; then
omarchy-notification-send -g 󰍹 "No laptop display found"
exit 1
fi
if ! omarchy-hyprland-monitor-external-active; then
omarchy-notification-send -g 󰍹 "Can't disable the only active display"
exit 1
fi
@@ -31,8 +42,12 @@ off() {
}
recover() {
if ! omarchy-hw-external-monitors && omarchy-hyprland-toggle-enabled $TOGGLE; then
omarchy-hyprland-toggle $TOGGLE off
if ! omarchy-hyprland-monitor-external-active; then
if omarchy-hyprland-toggle-enabled $TOGGLE; then
omarchy-hyprland-toggle $TOGGLE off
fi
wake
fi
}
+1 -1
View File
@@ -48,7 +48,7 @@ toggle() {
}
recover() {
if ! omarchy-hw-external-monitors && omarchy-hyprland-toggle-enabled $TOGGLE; then
if ! omarchy-hyprland-monitor-external-active && omarchy-hyprland-toggle-enabled $TOGGLE; then
omarchy-hyprland-toggle $TOGGLE off
fi
}
+19 -12
View File
@@ -3,36 +3,43 @@
# omarchy:summary=Watch Hyprland monitor events and recover monitor toggles when a monitor is removed
SOCKET="$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock"
LOCK="${XDG_RUNTIME_DIR:-/tmp}/omarchy-monitor-clamshell.lock"
sync_clamshell() {
omarchy-hyprland-monitor-clamshell
}
recover_after_monitor_removal() {
sync_clamshell
(
sleep 1
sync_clamshell
) &
flock -n 9 || exit 0
omarchy-hyprland-monitor-clamshell
) 9>"$LOCK"
}
sync_clamshell_after_monitor_change() {
sync_clamshell
(
sleep 1
sync_clamshell
for delay in 1 3 7; do
sleep "$delay"
sync_clamshell
done
) &
}
poll_clamshell_state() {
while true; do
sleep 2
sync_clamshell
done
}
sync_clamshell_after_monitor_change
poll_clamshell_state &
socat -U - "UNIX-CONNECT:$SOCKET" | while read -r event; do
case "$event" in
monitoradded\>\>*|monitoraddedv2\>\>*)
sync_clamshell_after_monitor_change
;;
monitorremoved\>\>*|monitorremovedv2\>\>*)
recover_after_monitor_removal
sync_clamshell_after_monitor_change
;;
esac
done