76 lines
3.1 KiB
Bash
76 lines
3.1 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
recover_city_of_division_window() {
|
|
local gamePid="$1"
|
|
local attempt gameWindow geometry key value windowX windowY windowWidth windowHeight
|
|
local -a gameWindows=()
|
|
|
|
# Wait for this launch's off-screen pygame window, not another Wine process.
|
|
for ((attempt = 0; attempt < 150; attempt++)); do
|
|
kill -0 "$gamePid" 2>/dev/null || return 0
|
|
mapfile -t gameWindows < <(
|
|
xdotool search --all --pid "$gamePid" --name '^City of division,' 2>/dev/null
|
|
)
|
|
if [[ ${#gameWindows[@]} -eq 1 ]]; then
|
|
gameWindow="${gameWindows[0]}"
|
|
if [[ "$(xdotool getwindowclassname "$gameWindow" 2>/dev/null)" == "cityofdivision.exe" ]] &&
|
|
geometry="$(xdotool getwindowgeometry --shell "$gameWindow" 2>/dev/null)"; then
|
|
windowX=""
|
|
windowY=""
|
|
windowWidth=""
|
|
windowHeight=""
|
|
# Parse xdotool's data without evaluating it as shell commands.
|
|
while IFS='=' read -r key value; do
|
|
case "$key" in
|
|
X) windowX="$value" ;;
|
|
Y) windowY="$value" ;;
|
|
WIDTH) windowWidth="$value" ;;
|
|
HEIGHT) windowHeight="$value" ;;
|
|
esac
|
|
done <<< "$geometry"
|
|
if [[ "$windowX" =~ ^-[0-9]+$ && "$windowY" =~ ^-[0-9]+$ &&
|
|
"$windowWidth" == 1 && "$windowHeight" == 1 ]]; then
|
|
if ! xdotool windowmove "$gameWindow" 100 100 \
|
|
windowsize "$gameWindow" 640 480 windowmap "$gameWindow"; then
|
|
echo "Could not restore City of Division's off-screen window." >&2
|
|
return 1
|
|
fi
|
|
# If its desktop is elsewhere, normal window selection will
|
|
# complete focus. Never activate a different workspace here.
|
|
xdotool windowfocus "$gameWindow" 2>/dev/null || true
|
|
return 0
|
|
fi
|
|
if [[ "$windowX" =~ ^[0-9]+$ && "$windowY" =~ ^[0-9]+$ &&
|
|
"$windowWidth" =~ ^[0-9]+$ && "$windowHeight" =~ ^[0-9]+$ ]] &&
|
|
((windowWidth > 1 && windowHeight > 1)); then
|
|
return 0
|
|
fi
|
|
fi
|
|
fi
|
|
sleep 0.2
|
|
done
|
|
echo "Timed out waiting to restore City of Division's window." >&2
|
|
return 1
|
|
}
|
|
|
|
run_city_of_division() {
|
|
if [[ -z "${DISPLAY:-}" ]] || ! command -v xdotool >/dev/null; then
|
|
echo "City of Division requires an X11 display and xdotool for its window recovery." >&2
|
|
return 1
|
|
fi
|
|
|
|
local -a launchCommand=(wine)
|
|
if command -v gamemoderun >/dev/null; then
|
|
launchCommand=(gamemoderun wine)
|
|
fi
|
|
"${launchCommand[@]}" "$@" &
|
|
local gamePid=$!
|
|
recover_city_of_division_window "$gamePid" &
|
|
local focusPid=$!
|
|
local gameStatus=0
|
|
wait "$gamePid" || gameStatus=$?
|
|
# The worker also stops when this process exits; no persistent focus loop.
|
|
wait "$focusPid" || true
|
|
return "$gameStatus"
|
|
}
|