#!/bin/bash
# See https://regolith-linux.org/docs/customize/compositors/
#
# The window manager runs this at startup and on every reload or restart (which
# is how regolith-look refresh applies a new look), so it replaces any running
# picom. With a systemd user manager, picom runs in the user service
# regolith-compositor-picom.service, which starts it again if it crashes.
# Without one, picom is started as a daemon as before.
#
# init --foreground runs picom in the foreground (used by the service).

UNIT=regolith-compositor-picom.service

picom_config() {
  if [[ -f "$HOME/.config/regolith3/picom/config" ]]; then
    echo "$HOME/.config/regolith3/picom/config"
  else
    xrescat regolith.compositor.picom.config /etc/regolith/picom/config
  fi
}

# picom's glx backend does not repaint window contents on Mesa's software
# renderers (llvmpipe, softpipe), which is what a VM without 3D acceleration
# (QXL/SPICE, std VGA, virtio-gpu without virgl) gets: new windows, workspace
# switches and closed windows don't show up, so key bindings seem to do
# nothing (yshui/picom#1030). Use the xrender backend there. Prints the extra
# picom arguments; set regolith.compositor.picom.software_fallback to false
# to keep the configured backend.
picom_args() {
  [[ "$(xrescat regolith.compositor.picom.software_fallback true)" == "true" ]] || return 0
  if /usr/bin/picom --config "$1" --diagnostics 2>/dev/null \
      | grep -q '^\* Accelerated: 0'; then
    echo "--backend xrender"
  fi
}

if [[ "${1-}" == "--foreground" ]]; then
  config=$(picom_config)
  # shellcheck disable=SC2046
  exec /usr/bin/picom --config "$config" $(picom_args "$config")
fi

use_unit=
if [[ -n "${XDG_RUNTIME_DIR-}" && -n "${DISPLAY-}" ]] \
    && systemctl --user cat "$UNIT" >/dev/null 2>&1; then
  use_unit=1
  systemctl --user stop "$UNIT"
fi

# Replace only this user's picom. Another user's picom (another session on
# the machine) can't be signalled and is not ours to replace; waiting for it
# to exit would never end. Give up waiting after 5 seconds and kill what is
# left, so a picom that ignores SIGTERM can't block startup either.
own_picom() {
  pgrep -u "$(id -u)" -x picom
}

compositor_PID=$(own_picom)
tries=0
while [[ -n "$compositor_PID" ]] && (( tries++ < 50 )); do
  # pgrep may print several PIDs; kill each of them.
  # shellcheck disable=SC2086
  kill $compositor_PID 2>/dev/null
  sleep .1
  compositor_PID=$(own_picom)
done
if [[ -n "$compositor_PID" ]]; then
  # shellcheck disable=SC2086
  kill -s KILL $compositor_PID 2>/dev/null
  sleep .1
fi

if [[ -n "$use_unit" ]]; then
  # Hand this display to the service, not to the whole user manager.
  env_file="$XDG_RUNTIME_DIR/regolith-compositor-picom.env"
  (
    umask 077
    echo "DISPLAY=$DISPLAY"
    if [[ -n "${XAUTHORITY-}" ]]; then
      echo "XAUTHORITY=$XAUTHORITY"
    fi
  ) > "$env_file"
  # Clear a start limit hit by earlier crashes.
  systemctl --user reset-failed "$UNIT" 2>/dev/null
  if systemctl --user start "$UNIT"; then
    exit 0
  fi
fi

config=$(picom_config)
# shellcheck disable=SC2046
/usr/bin/picom --config "$config" $(picom_args "$config") --daemon
