# This file would be sourced by gamescope-session script (meant for
# ChimeraOS devices).

SYS_ID="$(cat /sys/devices/virtual/dmi/id/product_name)"
CPU_VENDOR="$(lscpu | grep "Vendor ID" | cut -d : -f 2 | xargs)"
# avoid long start times on games when no nvidia gpu is available
if ! lspci | grep -i vga | grep -i nvidia > /dev/null; then
  export VK_DRIVER_FILES=$(ls -1 -d /usr/share/vulkan/icd.d/* | grep -v nvidia | tr '\n' ':' | sed 's/:$//')
fi

# OXP Devices
OXP_LIST="ONE XPLAYER:ONEXPLAYER 1 T08:ONEXPLAYER 1S A08:ONEXPLAYER 1S T08:ONEXPLAYER mini A07:ONEXPLAYER mini GA72:ONEXPLAYER mini GT72:ONEXPLAYER Mini Pro:ONEXPLAYER GUNDAM GA72:ONEXPLAYER 2 ARP23"
AOK_LIST="AOKZOE A1 AR07:AOKZOE A1 Pro"
if [[ ":$OXP_LIST:" =~ ":$SYS_ID:"  ]] || [[  ":$AOK_LIST:" =~ ":$SYS_ID:"   ]]; then
  # Intel support is extremely experimental, this is the bare minimum to get the system to boot.
  # Dependent on a special --force-orientation option in gamescope
  if ( gamescope --help 2>&1 | grep -e "--force-external-orientation" > /dev/null ) &&  [[ "$CPU_VENDOR" =~ "GenuineIntel" ]]; then
      export GAMESCOPECMD="/usr/bin/gamescope \
        -e \
        --generate-drm-mode fixed \
        --xwayland-count 2 \
        -O *,eDP-1 \
        --default-touch-mode 4 \
        --hide-cursor-delay 3000 \
        --fade-out-duration 200 \
        --force-panel-type external \
        --force-external-orientation left "
  # AMD Support and fallback for users of gamescope-git.
  elif ( gamescope --help 2>&1 | grep -e "--force-orientation" > /dev/null ) ; then
    export GAMESCOPECMD="/usr/bin/gamescope \
      -e \
      --generate-drm-mode fixed \
      --xwayland-count 2 \
      -O *,eDP-1 \
      --default-touch-mode 4 \
      --hide-cursor-delay 3000 \
      --fade-out-duration 200 \
      --force-orientation left "
  fi
  # Set refresh rate range and enable refresh rate switching
  export STEAM_DISPLAY_REFRESH_LIMITS=40,60
fi

# AYANEO AIR Devices
AIR_LIST="AIR:AIR Pro:AIR Plus"
if [[ ":$AIR_LIST:" =~ ":$SYS_ID:"  ]]; then
  # Dependent on a special --force-orientation option in gamescope
  if ( gamescope --help 2>&1 | grep -e "--force-orientation" > /dev/null ) ; then
    export GAMESCOPECMD="/usr/bin/gamescope \
      -e \
      --xwayland-count 2 \
      -O *,eDP-1 \
      --default-touch-mode 4 \
      --hide-cursor-delay 3000 \
      --fade-out-duration 200 \
      --force-orientation left "
  fi
fi

# AYN Loki Devices
AYN_LIST="Loki Max"
if [[ ":$AYN_LIST:" =~ ":$SYS_ID:"  ]]; then
  # Dependent on a special --force-orientation option in gamescope
  if ( gamescope --help 2>&1 | grep -e "--force-external-orientation" > /dev/null ) ; then
    export GAMESCOPECMD="/usr/bin/gamescope \
      -e \
      --generate-drm-mode fixed \
      --xwayland-count 2 \
      -O *,eDP-1 \
      --default-touch-mode 4 \
      --hide-cursor-delay 3000 \
      --fade-out-duration 200 \
      --force-panel-type external \
      --force-external-orientation left "
  fi
  # Set refresh rate range and enable refresh rate switching
#  export STEAM_DISPLAY_REFRESH_LIMITS=40,60
fi

# GDP Win devices
GDP_LIST="G1619-01:G1621-02:MicroPC"
if [[ ":$GDP_LIST:" =~ ":$SYS_ID:"  ]]; then
  # Dependent on a special --force-orientation option in gamescope
  if ( gamescope --help 2>&1 | grep -e "--force-orientation" > /dev/null ) ; then
    export GAMESCOPECMD="/usr/bin/gamescope \
      -e \
      --generate-drm-mode fixed \
      --xwayland-count 2 \
      -O *,DSI-1 \
      --default-touch-mode 4 \
      --hide-cursor-delay 3000 \
      --fade-out-duration 200 \
      --force-orientation right "
  fi
fi

# GPD Win 3 specifc quirk to prevent crashing
  # The GPD Win 3 does not support hardware rotation for 270/90 modes. We need to implement shader rotations to get this working correctly.
  # 0/180 rotations should work.
if [[ ":G1618-03:" =~ ":$SYS_ID:"  ]]; then
  # Dependent on a special --force-orientation option in gamescope
  if ( gamescope --help 2>&1 | grep -e "--force-orientation" > /dev/null ) ; then
    export GAMESCOPECMD="/usr/bin/gamescope \
      -e \
      --generate-drm-mode fixed \
      --xwayland-count 2 \
      -O *,DSI-1 \
      --default-touch-mode 4 \
      --hide-cursor-delay 3000 \
      --fade-out-duration 200 \
      --force-orientation normal "
  fi
fi

#GPD Win 4 supports 40-60hz refresh rate changing
if [[ ":G1618-04:" =~ ":$SYS_ID:"  ]]; then
  # Dependent on a special --force-orientation option in gamescope
  if ( gamescope --help 2>&1 | grep -e "--force-orientation" > /dev/null ) ; then
    export GAMESCOPECMD="/usr/bin/gamescope \
      -e \
      --xwayland-count 2 \
      -O *,eDP-1 \
      --default-touch-mode 4 \
      --hide-cursor-delay 3000 \
      --fade-out-duration 200 "
   fi
   export STEAM_DISPLAY_REFRESH_LIMITS=40,60
fi

#Steam Deck
if [[ ":Jupiter:" =~ ":$SYS_ID:"  ]]; then
  # Dependent on a special --force-orientation option in gamescope
  if ( gamescope --help 2>&1 | grep -e "--force-orientation" > /dev/null ) ; then
    export GAMESCOPECMD="/usr/bin/gamescope \
      -e \
      --generate-drm-mode fixed \
      --xwayland-count 2 \
      -O *,eDP-1 \
      --default-touch-mode 4 \
      --hide-cursor-delay 3000 \
      --fade-out-duration 200 "
  fi
   export STEAM_DISPLAY_REFRESH_LIMITS=40,60
fi
