#!/bin/bash
# cw-yabause IMAGE.(iso|cue) [yabause args...]: our Yabause (Dockerfile.yabause: the SH-2 dynarec,
# Console Wars' NetLink) with the NetLink cartridge, HLE BIOS, headless (under cw-run's Xvfb).
# The same environment as cw-kronos (the two are interchangeable; window "Yabause v0.9.15"):
#   CW_NETLINK_EXCHANGE  host:port every number the NetLink dials reaches (default 127.0.0.1:8651)
#   CW_NETLINK_CALLER    caller ID (default CW_SESSION): "PLAYER <id>\n" starts each call
#   CW_NETLINK_BPS       the line rate (default 28800; 0 = as fast as the socket goes)
#   CW_NETLINK_LOG=1     the modem's log on stderr
#   CW_BIOS              a real BIOS file (never in the image: mounted read-only), else the HLE BIOS
#   CW_KRONOS_SOUND=0    no sound (the name is cw-kronos'; CW_SOUND=0 works too)
# and Yabause's own:
#   CW_YABAUSE_SH2       the SH-2 core: 0 = the interpreter (default), 2 = the dynarec (crashes on our discs)
#   CW_YABAUSE_VIDEO     2 = software VDP1/VDP2 (default), 1 = OpenGL (llvmpipe)
#   CW_YABAUSE_LIMIT     1 = hold the game to real time (default; the limiter sleeps: our patch),
#                        0 = as fast as it goes
#   CW_YABAUSE_MAXSKIP   frames it may skip drawing in a row to keep up (default 0: every frame drawn)
#   CW_YABAUSE_THREADS   the software renderer's threads (default 1 = off)
# The control pad on port 1 is the keyboard (the Qt input core, PerCore = 100; Qt key codes):
#   arrows = the d-pad, Return = START, z x c = A B C, a s d = X Y Z, q e = L R
# No menu bar or toolbar (View/Menubar, View/Toolbar = 2: always hidden), full screen or not.
set -e
img="$1"; shift || true
[ -n "$img" ] || { echo "usage: cw-yabause IMAGE [args]"; exit 2; }
ex="${CW_NETLINK_EXCHANGE:-127.0.0.1:8651}"
export CW_NETLINK_EXCHANGE="$ex"
export CW_YABAUSE_MAXSKIP="${CW_YABAUSE_MAXSKIP:-0}"
threads="${CW_YABAUSE_THREADS:-1}"
mt=false; [ "$threads" -gt 1 ] && mt=true
cfg="$HOME/.config/yabause/qt"
mkdir -p "$cfg"
cat > "$cfg/yabause.ini" <<INI
[0.9.11]
General/EnableEmulatedBios=true
General/EnableFrameSkipLimiter=$([ "${CW_YABAUSE_LIMIT:-1}" = 1 ] && echo true || echo false)
General/EnableVSync=true
General/ShowFPS=false
General/EnableMultiThreading=$mt
General/NumThreads=$threads
Advanced/SH2Interpreter=${CW_YABAUSE_SH2:-0}
Advanced/SH1Interpreter=0
Video/VideoCore=${CW_YABAUSE_VIDEO:-2}
View/Menubar=2
View/Toolbar=2
Cartridge/Type=8
Cartridge/ModemIP=${ex%:*}
Cartridge/ModemPort=${ex##*:}
Input/PerCore=100
Input/Port/1/Id/1/Type=2
Input/Port/1/Id/1/Controller/2/Key/0=16777235
Input/Port/1/Id/1/Controller/2/Key/1=16777236
Input/Port/1/Id/1/Controller/2/Key/2=16777237
Input/Port/1/Id/1/Controller/2/Key/3=16777234
Input/Port/1/Id/1/Controller/2/Key/4=69
Input/Port/1/Id/1/Controller/2/Key/5=81
Input/Port/1/Id/1/Controller/2/Key/6=16777220
Input/Port/1/Id/1/Controller/2/Key/7=90
Input/Port/1/Id/1/Controller/2/Key/8=88
Input/Port/1/Id/1/Controller/2/Key/9=67
Input/Port/1/Id/1/Controller/2/Key/10=65
Input/Port/1/Id/1/Controller/2/Key/11=83
Input/Port/1/Id/1/Controller/2/Key/12=68
INI
args=(-a)
if [ -n "$CW_BIOS" ] && [ -r "$CW_BIOS" ]; then args+=(-b "$CW_BIOS"); else args+=(-nb); fi
{ [ "${CW_KRONOS_SOUND:-1}" = 0 ] || [ "${CW_SOUND:-1}" = 0 ]; } && args+=(-ns)
exec /usr/local/bin/yabause "${args[@]}" -i "$img" "$@"
