#!/usr/bin/sh
# Usage: just set this as a greetd greeter command
# ```
# command = "start-qmgreet"
# ```

# Add qmlgreet config file to the arguments
set -- --config /etc/xdg/qmlgreet/sway.config "$@"

# Try to use a wrapper script from sway-config-fedora
if [ -x /usr/bin/start-sway ]; then
    # shellcheck source=/dev/null
    . /usr/bin/start-sway
    # unreachable
    exit 1
fi

# A sad, sad list of workarounds to ensure that we can boot and show something.
case $(systemd-detect-virt --vm) in
    "none"|"")
        ;;
    "kvm")
        # virtio_gpu without 3D-acceleration may crash during Sway startup,
        # and overall pixman should be better choice for VMs than llvmpipe.
        # https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/2871
        export WLR_RENDERER=pixman
        # QML rhi backend is glitchy with pixman renderer
        export QT_QUICK_BACKEND=software
        export WLR_NO_HARDWARE_CURSORS=1
        ;;
    *)
        # Most of the virtualization systems have issues with cursor hotspot,
        # See wayland-devel for Jun 2022 for the most recent discussion,
        # or <https://github.com/swaywm/sway/issues/6581>.
        # If that's not enough, wlroots renders cursors in a way that makes
        # vmwgfx spew out errors and refuse to update the screen.
        export WLR_NO_HARDWARE_CURSORS=1
        ;;
esac

# Redirect logs to journal and start Sway with the default greeter config
exec systemd-cat -- sway "$@"
