span_display.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/bash
  2. # usage: span_display.sh start <WxH+X+Y> [<WxH+X+Y> ...] | layout <WxH+X+Y> ... | stop | env
  3. # a private multi monitor display for live WE: headless sway (one output per argument, 60 Hz, at that position) with
  4. # its own Xwayland as an X window manager, so Wine sees one RandR monitor per output and its windows land where WE
  5. # puts them. sway runs under proot with a private /tmp: Xwayland's socket directory is read-only here and display
  6. # numbers below 10 are taken by lock files, so it never touches :0. `env` prints DISPLAY, WAYLAND_DISPLAY and
  7. # XDG_RUNTIME_DIR for grim/swaymsg/Wine.
  8. S=${WE_SPAN_DIR:-$HOME/.local/share/we_live/span}
  9. R=$S/display
  10. T=$R/tmp
  11. swaymsg_() { XDG_RUNTIME_DIR=$R SWAYSOCK=$(ls $R/sway-ipc.*.sock 2>/dev/null | head -1) swaymsg "$@"; }
  12. layout() {
  13. local i=1 spec
  14. for spec in "$@"; do
  15. local mode=${spec%%+*} pos=${spec#*+}
  16. swaymsg_ output HEADLESS-$i enable mode ${mode}@60Hz pos ${pos%+*} ${pos#*+} >/dev/null || return 1
  17. i=$((i + 1))
  18. done
  19. # outputs past the given ones are switched off
  20. while swaymsg_ -t get_outputs -r | grep -q "\"HEADLESS-$i\""; do
  21. swaymsg_ output HEADLESS-$i disable >/dev/null
  22. i=$((i + 1))
  23. done
  24. }
  25. display_number() {
  26. ls $T/.X11-unix 2>/dev/null | sed -n 's/^X//p' | head -1
  27. }
  28. case $1 in
  29. start)
  30. shift
  31. if ! swaymsg_ -t get_version >/dev/null 2>&1; then
  32. mkdir -p $R && chmod 700 $R
  33. rm -rf $T && mkdir -p $T/.X11-unix && chmod 1777 $T $T/.X11-unix
  34. for i in $(seq 0 9); do printf '%10d\n' 1 > $T/.X$i-lock; done
  35. # no_new_privs refuses to exec Fedora's sway (file capabilities), a copy has none
  36. mkdir -p $S/bin && cp -u /usr/bin/sway $S/bin/sway
  37. printf 'xwayland force\ndefault_border none\nswaybg_command -\nfocus_follows_mouse no\n' > $R/sway.conf
  38. printf 'for_window [class=".*"] floating enable\nseat * hide_cursor 0\n' >> $R/sway.conf
  39. env -i PATH=$PATH HOME=$HOME XDG_RUNTIME_DIR=$R WLR_BACKENDS=headless WLR_HEADLESS_OUTPUTS=$# \
  40. WLR_LIBINPUT_NO_DEVICES=1 setsid nohup proot -b $T:/tmp $S/bin/sway -c $R/sway.conf > $R/sway.log 2>&1 &
  41. for i in $(seq 50); do [ -n "$(display_number)" ] && swaymsg_ -t get_version >/dev/null 2>&1 && break; sleep 0.2; done
  42. fi
  43. layout "$@" || exit 1
  44. N=$(display_number)
  45. [ -n "$N" ] || { echo "no Xwayland, see $R/sway.log"; exit 1; }
  46. for i in $(seq 25); do DISPLAY=:$N xrandr --listmonitors >/dev/null 2>&1 && break; sleep 0.2; done
  47. DISPLAY=:$N xrandr --listmonitors
  48. ;;
  49. layout)
  50. shift
  51. layout "$@"
  52. ;;
  53. stop)
  54. # proot hides the binary, the session is found by its runtime dir
  55. for p in $(pgrep -x sway); do
  56. { tr "\\0" "\\n" < /proc/$p/environ; } 2>/dev/null | grep -qx "XDG_RUNTIME_DIR=$R" && kill $p
  57. done
  58. ;;
  59. env)
  60. echo "DISPLAY=:$(display_number) WAYLAND_DISPLAY=$(ls $R | grep -m1 '^wayland-[0-9]*$') XDG_RUNTIME_DIR=$R"
  61. ;;
  62. *)
  63. sed -n '2,7p' "$0"
  64. exit 1
  65. ;;
  66. esac