#!/bin/sh

. /etc/profile

ARCADEROMS_XML="/usr/config/emulationstation/resources/arcaderoms.xml"

RUNEMU_LINE=$(ps aux | grep '[r]unemu.sh' | head -n 1)
CURRENT_CORE=$(echo "${RUNEMU_LINE}" | sed -n 's/.*--core=\([^ ]*\).*/\1/p')
ROM_PATH=$(echo "${RUNEMU_LINE}" | sed -n 's|.*runemu\.sh \([^ ]*\).*|\1|p')

if [ -z "${CURRENT_CORE}" ] || [ -z "${ROM_PATH}" ]; then
    exit 0
fi

ROM_NAME=$(basename "${ROM_PATH}" | sed 's/\.[^.]*$//')
IS_VERTICAL=false

case "${CURRENT_CORE}" in
    # Unconditionally vertical
    melonds*|desmume|vecx)
        IS_VERTICAL=true
        ;;
    # Might not be vertical
    mame*|fbneo|fbalpha*)
        if grep -q "id=\"${ROM_NAME}\"[^>]*vert=\"true\"" "${ARCADEROMS_XML}" 2>/dev/null; then
            IS_VERTICAL=true
        fi
        ;;
esac

case "${ROM_NAME}" in
    # Also check for some specific roms that aren't marked vert
    punchout*|spnchout*|pc_*|mp_*)
        IS_VERTICAL=true
        ;;
esac

if [ "${IS_VERTICAL}" = "true" ]; then
    if [ "${QUIRK_DEVICE}" = "AYN Thor" ]; then
        # Needs two separate swaymsg calls. Screen cannot move and have windows moved to it in the same line.
        swaymsg '[title="RetroArch.*"] output DSI-2 pos 0 0, output DSI-1 pos 340 1080'
        swaymsg '[title="RetroArch.*"] floating enable, fullscreen disable, resize set 1240 2160, move to output DSI-2, move absolute position 340 0'
        # Additionally fix touch mapping for vertical stack.
        swaymsg input "0:0:generic_ft5x06_(8d)" map_to_output DSI-1
        swaymsg -- input "0:0:generic_ft5x06_(8d)" calibration_matrix 0 -1 1 1 0 0
    elif [ "${QUIRK_DEVICE}" = "Anbernic RG DS" ]; then
        # Same notes from above apply here.
        swaymsg '[title="RetroArch.*"] output DSI-2 pos 0 0, output DSI-1 power on pos 0 480'
        swaymsg '[title="RetroArch.*"] floating enable, fullscreen disable, resize set 640 960, move to output DSI-2, move absolute position 0 0'
        swaymsg 'input "1046:911:Goodix_Capacitive_TouchScreen" calibration_matrix 1 0 0 0 0.5 0.5'
    elif [ "${QUIRK_DEVICE}" = "AYANEO Pocket DS" ]; then
        swaymsg output DSI-1 scale 1.40
        swaymsg output DSI-2 transform 270
        swaymsg '[title="RetroArch.*"] output DSI-1 pos 0 0, output DSI-2 power on pos 160 776'
        swaymsg '[title="RetroArch.*"] floating enable, fullscreen disable, resize set 1024 1544, move to output DSI-1, move absolute position 160 0'
    elif [[ "${QUIRK_DEVICE}" == "Retroid Pocket 5" || "${QUIRK_DEVICE}" == "Retroid Pocket Flip2" || "${QUIRK_DEVICE}" == "Retroid Pocket Mini" || "${QUIRK_DEVICE}" == "Retroid Pocket Mini V2" ]]; then
        case "${QUIRK_DEVICE}" in
            "Retroid Pocket 5" | "Retroid Pocket Flip2")
                DSI_X="0"
                RA_WIDTH="1920"
                RA_HEIGHT="2160" # 1080 + 1080
                ;;
            "Retroid Pocket Mini")
                DSI_X="340"
                RA_WIDTH="1240"
                RA_HEIGHT="2010" # 1080 + 930
                ;;
            "Retroid Pocket Mini V2")
                DSI_X="340"
                RA_WIDTH="1240"
                RA_HEIGHT="2160" # 1080 + 1080
                ;;
        esac

        # Setup vertical stack using external DP-1 as the top screen.
        swaymsg "[title=\"RetroArch.*\"] output DP-1 pos 0 0, output DSI-1 pos ${DSI_X} 1080"
        swaymsg "[title=\"RetroArch.*\"] floating enable, fullscreen disable, resize set ${RA_WIDTH} ${RA_HEIGHT}, move to output DP-1, move absolute position ${DSI_X} 0"

        swaymsg input "0:0:generic_ft5x06_(a0)" map_to_output DSI-1
        swaymsg input "0:0:generic_ft5x06_(a0)" events enabled
        swaymsg input "0:0:generic_ft5x06_(8d)" map_to_output DSI-1
        swaymsg input "0:0:generic_ft5x06_(8d)" events enabled
    fi
fi