| 1 | #!/bin/bash |
| 2 | set -e |
| 3 | |
| 4 | sway_pid="" |
| 5 | pipewire_pid="" |
| 6 | wireplumber_pid="" |
| 7 | portal_pid="" |
| 8 | wlr_portal_pid="" |
| 9 | |
| 10 | # reexec this script inside a dbus session if we're not in one |
| 11 | if [[ -z "$FLAMESHOT_TEST_REEXEC" ]]; then |
| 12 | export FLAMESHOT_TEST_REEXEC=1 |
| 13 | export XDG_RUNTIME_DIR="$AUTOPKGTEST_TMP/run" |
| 14 | export HOME="$AUTOPKGTEST_TMP/home" |
| 15 | export XDG_CONFIG_HOME="$HOME/.config" |
| 16 | export WAYLAND_DISPLAY=wayland-1 |
| 17 | export XDG_BACKEND=wayland |
| 18 | export XDG_SESSION_TYPE=wayland |
| 19 | export XDG_CURRENT_DESKTOP=sway |
| 20 | |
| 21 | mkdir -m 0700 -p "$XDG_RUNTIME_DIR" "$HOME" "$XDG_CONFIG_HOME" |
| 22 | cd "$AUTOPKGTEST_TMP" |
| 23 | |
| 24 | echo "Relaunching script inside a nested dbus session" |
| 25 | exec dbus-run-session -- "$0" "$@" |
| 26 | fi |
| 27 | |
| 28 | export WLR_BACKENDS=headless |
| 29 | export WLR_LIBINPUT_NO_DEVICES=1 |
| 30 | |
| 31 | echo "Starting sway" |
| 32 | sway --config /dev/null & |
| 33 | sway_pid=$! |
| 34 | |
| 35 | kill_if_defined() { |
| 36 | pid=$1 |
| 37 | |
| 38 | [[ -z "$pid" ]] || kill "$pid" || true |
| 39 | } |
| 40 | |
| 41 | cleanup() { |
| 42 | kill_if_defined "$sway_pid" |
| 43 | kill_if_defined "$pipewire_pid" |
| 44 | kill_if_defined "$wireplumber_pid" |
| 45 | kill_if_defined "$portal_pid" |
| 46 | kill_if_defined "$wlr_portal_pid" |
| 47 | } |
| 48 | |
| 49 | trap cleanup INT TERM EXIT |
| 50 | |
| 51 | # export QT_DEBUG_PLUGINS=1 |
| 52 | # export WAYLAND_DEBUG=1 |
| 53 | |
| 54 | wait_for_socket() { |
| 55 | socket=$1 |
| 56 | echo "Waiting for socket $socket" |
| 57 | |
| 58 | deadline=$(( "$(date +%s)" + 300 )) |
| 59 | while [[ "$(date +%s)" -lt "$deadline" ]]; do |
| 60 | ! [[ -S "$socket" ]] || break |
| 61 | sleep 1 |
| 62 | done |
| 63 | |
| 64 | echo "$socket is alive" |
| 65 | } |
| 66 | |
| 67 | echo "Starting pipewire" |
| 68 | pipewire & |
| 69 | pipewire_pid=$! |
| 70 | |
| 71 | echo "Starting wireplumber" |
| 72 | wireplumber & |
| 73 | wireplumber_pid=$! |
| 74 | |
| 75 | wait_for_socket "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" |
| 76 | wait_for_socket "$XDG_RUNTIME_DIR/pipewire-0" |
| 77 | wait_for_socket "$XDG_RUNTIME_DIR/pipewire-0-manager" |
| 78 | |
| 79 | echo "Configuring xdg-desktop-portal-wlr" |
| 80 | mkdir -m 0700 -p "$XDG_CONFIG_HOME/xdg-desktop-portal-wlr" |
| 81 | cat << 'EOF' > "$XDG_CONFIG_HOME/xdg-desktop-portal-wlr/config" |
| 82 | [screencast] |
| 83 | chooser_type = none |
| 84 | output_name = HEADLESS-1 |
| 85 | EOF |
| 86 | |
| 87 | echo "Starting desktop portal" |
| 88 | /usr/libexec/xdg-desktop-portal & |
| 89 | portal_pid=$! |
| 90 | |
| 91 | echo "Starting wlr desktop portal" |
| 92 | /usr/libexec/xdg-desktop-portal-wlr & |
| 93 | wlr_portal_pid=$! |
| 94 | |
| 95 | # wait for portals |
| 96 | echo "Waiting for portals" |
| 97 | gdbus wait --session --timeout 120 org.freedesktop.portal.Desktop |
| 98 | gdbus wait --session --timeout 120 org.freedesktop.impl.portal.desktop.wlr |
| 99 | |
| 100 | echo "Running flameshot" |
| 101 | outfile="screenshot.png" |
| 102 | flameshot screen -n 0 -p "$outfile" |
| 103 | |
| 104 | echo "Verifying that flameshot has saved a screenshot" |
| 105 | file "$outfile" | grep "PNG image data" |