Using cachyOS default installed dependencies (sway-lock + hyprland) I made this simple bash script and config to handle 3 simple scenarios :
- if laptop lid is closed and no external monitor connected = lock + suspend
- if laptop lid is closed BUT external monitor IS connected = just turn laptop screen off
- resume when lid is open
Overall this kinda works but still very very buggy, especially when locking+sleeping while external monitor is connected.
So im basically giving up and thinking the best way forward is a systemd service, or something that hooks to it like hypridle / swayidle.
Anyone is in the same boat? I mean this is quite a typical scenario
#!/bin/bash
LAPTOP_DISPLAY="eDP-1"
# Get total number of monitors
MONITOR_COUNT=$(hyprctl monitors -j | jq 'length')
# if count is greater than one
if [ "$MONITOR_COUNT" -gt 1 ]; then
# External monitor is connected; disable laptop display
hyprctl keyword monitor "$LAPTOP_DISPLAY, disable"
else
swaylock -f -c 000000 & sleep 4 # && systemctl suspend
# no need to suspend because its handled by systemd already
# sleep is to avoid race condition
fi
And the 2 config lines :
# trigger when the switch is turning on
bindl = , switch:on:Lid Switch, exec, ~/.config/hypr/scripts/handle_lid_switch.sh
# trigger when the switch is turning off
bindl = , switch:off:Lid Switch, exec, hyprctl keyword monitor "eDP-1, preferred, auto, 1"