Proper way to Lock + Suspend on laptop?

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"

Did you every find a solution for this? I’m having a lot of trouble with both hyprlock and swaylock when opening/closing the laptop lid and when unplugging external monitors.

Not really. I did move from swaylock to hyprlock, but still having same issue ( hyprlock often crashes on resume, and having to go to tty to restart it)

  • temporary solution: always lock your screen manually before closing the lid
  • i also installed hypridle, where automatically , after no activity, there is a lock screen and then sleep separated by a few minutes
  • final solution: im actually going to pay for 1 month “support / premium fee” with the hyprland guys to ask them for a proper fix