#!/bin/bash
# Launch Steam Big Picture and force it fullscreen on headless X11
#
# On headless servers with an HDMI dummy plug, Steam Big Picture frequently
# renders as a 1280x800 window instead of going fullscreen. This means
# Sunshine captures the entire root window (3840x2160) and Moonlight shows
# mostly desktop background — user sees a "black screen."
#
# Additionally, the dummy plug enters DPMS Off after ~15 minutes of
# inactivity, causing X11 capture to return black frames. This script
# forces the display on before launching Steam.
#
# Deploy to ~/.config/sunshine/scripts/steam-bigpicture.sh
# Reference in apps.json:
#   "detached": ["/home/ray/.config/sunshine/scripts/steam-bigpicture.sh"]
# Also add DPMS fix to prep-cmd (Sunshine runs commands directly, use sh -c):
#   "do": "sh -c 'DISPLAY=:0 xset -dpms s off; DISPLAY=:0 xset dpms force on'"

export DISPLAY=:0
export STEAM_BIGPICTURE=1

# Force the display ON and disable DPMS/screensaver
xset -dpms s off 2>/dev/null
xset dpms force on 2>/dev/null

# Launch Steam into Big Picture mode
steam -fulldesktopres -bigpicture &

# Wait up to 30 seconds for the Big Picture window to appear
for i in $(seq 1 30); do
    sleep 1
    WIN_ID=$(xdotool search --name "Steam Big Picture Mode" 2>/dev/null | head -1)
    if [ -n "$WIN_ID" ]; then
        sleep 3

        # Fullscreen via wmctrl (EWMH protocol — most reliable on XFWM4)
        wmctrl -ir "$WIN_ID" -b add,fullscreen 2>/dev/null
        sleep 1

        # Fallback: xdotool resize + move + activate
        xdotool windowsize "$WIN_ID" 3840 2160 2>/dev/null
        xdotool windowmove "$WIN_ID" 0 0 2>/dev/null
        xdotool windowactivate "$WIN_ID" 2>/dev/null

        exit 0
    fi
done

exit 1
