swayidle-lock-screen 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env zsh
  2. LOCKFILE="${HOME}/.cache/swayidle-lock-screen.lock"
  3. function has-rootful-xwayland-p {
  4. for pid in $(pgrep Xwayland); do
  5. local cmdline=(${(0)"$(cat /proc/"${pid}"/cmdline)"})
  6. if ! ((${cmdline[(Ie)-rootless]})); then
  7. return 0
  8. fi
  9. done
  10. return 1
  11. }
  12. function run {
  13. # ensure multiple instances do not run
  14. mkdir -p "$(dirname "${LOCKFILE}")"
  15. exec 4<>"${LOCKFILE}"
  16. flock -n 4 || exit 0
  17. dunstctl set-paused true
  18. if has-rootful-xwayland-p; then
  19. swaylock ${empty_flag} --color '#000000'
  20. else
  21. swayidle -w -C /dev/null \
  22. timeout 15 "${suspend_command}" resume "${wake_command}" &
  23. local swayidle_pid="${!}"
  24. swaylock ${empty_flag} ${img_flags}
  25. kill "${swayidle_pid}"
  26. eval "${wake_command}"
  27. fi
  28. dunstctl set-paused false
  29. fix_eww
  30. flock -u 4
  31. }
  32. function fix_eww {
  33. if (( ${#fix_eww} > 0 )); then
  34. eww open-many ${fix_eww}
  35. fi
  36. }
  37. local background=false
  38. local suspend_command=''
  39. local wake_command=''
  40. local empty_flag=''
  41. local fix_eww=()
  42. while [[ "${1}" =~ '^-' ]]; do
  43. case "${1}" in
  44. --)
  45. shift
  46. break
  47. ;;
  48. -f)
  49. background=true
  50. ;;
  51. -b)
  52. fix_eww+="${2}"
  53. shift
  54. ;;
  55. -s)
  56. local suspend_command="${2}"
  57. shift
  58. ;;
  59. -w)
  60. local wake_command="${2}"
  61. shift
  62. ;;
  63. -e)
  64. empty_flag='-e'
  65. ;;
  66. -*)
  67. printf "error: unknown flag '%s'\n" "${1}" >&2
  68. ;;
  69. esac
  70. shift
  71. done
  72. if [[ -z "${suspend_command}" ]]; then
  73. # default to turning off all displays
  74. suspend_command="wlr-randr $(wlr-randr --json |
  75. jq -r '[.[] | .modes = [.modes.[] |
  76. select(.current)]] |
  77. map("--output ''\(.name)'' --off") | join(" ")')"
  78. fi
  79. if [[ -z "${wake_command}" ]]; then
  80. wake_command="wlr-randr $(wlr-randr --json | jq -r '[.[]| select(.enabled) | .modes = [.modes.[] | select(.current)].[]] | map("--output ''\(.name)'' --on --mode ''\(.modes.width)x\(.modes.height)@\(.modes.refresh)Hz'' --pos ''\(.position.x),\(.position.y)'' --transform \(.transform) --scale \(.scale)") | join(" ")')"
  81. fi
  82. (( ${#} != 0 )) && img_flags=(-s fill -i "${1}")
  83. if ${background}; then
  84. run '${img_flags}' &
  85. else
  86. run '${img_flags}'
  87. fi