# 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)"

# 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:ONEXPLAYER 2 PRO ARP23P:ONEXPLAYER 2 PRO ARP23P EVA-01"
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-external-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 "
  # Fallback method. Dependent on a special --force-orientation option in gamescope
  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

# OXP 120hz Devices
OXP_120_LIST="ONEXPLAYER F1"
if [[ ":$OXP_120_LIST:" =~ ":$SYS_ID:"  ]]; then
  # Dependent on a special --force-external-orientation option in gamescope-plus
  if ( gamescope --help 2>&1 | grep -e "--force-external-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-panel-type external \
      --force-external-orientation left "
  # Fallback method. Dependent on a special --force-orientation option in gamescope
  elif ( 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
  # Set refresh rate range and enable refresh rate switching
  export STEAM_DISPLAY_REFRESH_LIMITS=40,120
fi

# AYANEO AIR Devices
AIR_LIST="AIR:AIR Pro:AIR Plus:SLIDE"
if [[ ":$AIR_LIST:" =~ ":$SYS_ID:"  ]]; then
  # Dependent on a special --force-external-orientation option in gamescope-plus
  if ( gamescope --help 2>&1 | grep -e "--force-external-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-panel-type external \
      --force-external-orientation left "
  # Fallback method. Dependent on a special --force-orientation option in gamescope
  elif ( 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:Loki Zero:Loki MiniPro"
if [[ ":$AYN_LIST:" =~ ":$SYS_ID:"  ]]; then
  # Dependent on a special --force-external-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

# OLED Steam Deck
if [[ ":Galileo:" =~ ":$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=45,90
fi

# ROG Ally
if [[ ":ROG Ally RC71L_RC71L:" =~ ":$SYS_ID:"  ]]; then
  # Dependent on a special --force-panel-type option in gamescope-plus
  if ( gamescope --help 2>&1 | grep -e "--force-panel-type" > /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-panel-type external \
      --adaptive-sync "
  # fallback for users of gamescope-git.
  elif ( 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
  # Set refresh rate range and enable refresh rate switching
  export STEAM_DISPLAY_REFRESH_LIMITS=40,120
fi

# Lenovo Legion Go
if [[ ":83E1:" =~ ":$SYS_ID:"  ]]; then
  # Dependent on --force-orientation in gamescope-plus
  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 \
      --adaptive-sync \
      --force-orientation left "
  fi
fi

