Skip to content

Instantly share code, notes, and snippets.

@Dianoga
Created January 25, 2026 21:04
Show Gist options
  • Select an option

  • Save Dianoga/bdec24bbafb11673b9d2fe143b069aa9 to your computer and use it in GitHub Desktop.

Select an option

Save Dianoga/bdec24bbafb11673b9d2fe143b069aa9 to your computer and use it in GitHub Desktop.
Global pre command for sunshine to swap KDE resolution to match game as close as possible
#!/usr/bin/env bash
OUT="DP-3" # adjust if needed
LOG_DIR="/tmp"
mkdir -p "$LOG_DIR"
LOG_FILE="${LOG_DIR}/sunshine-pre-$(date +'%Y%m%d').log"
{
echo "==== Sunshine pre-command ===="
echo "Date: $(date)"
echo "PWD: $(pwd)"
echo "Args: $*"
echo
echo "== SUNSHINE_* environment =="
env | sort | grep '^SUNSHINE_' || echo "No SUNSHINE_* vars found"
echo
} >> "$LOG_FILE"
REQ_W="${SUNSHINE_CLIENT_WIDTH}"
REQ_H="${SUNSHINE_CLIENT_HEIGHT}"
REQ_FPS="${SUNSHINE_CLIENT_FPS}"
{
echo "Output: ${OUT}"
echo "Requested: ${REQ_W}x${REQ_H}@${REQ_FPS}"
echo
} >> "$LOG_FILE"
# Get all modes (with IDs) for this output
MODES_JSON="$(kscreen-doctor -j 2>/dev/null \
| jq --arg OUT "$OUT" '
.outputs[]
| select(.name == $OUT)
| .modes
')"
echo "Raw modes JSON for ${OUT}:" >> "$LOG_FILE"
echo "${MODES_JSON}" | jq '.' >> "$LOG_FILE" 2>/dev/null
echo >> "$LOG_FILE"
if [[ -z "${REQ_W}" || -z "${REQ_H}" || -z "${REQ_FPS}" ]]; then
echo "ERROR: one or more SUNSHINE_CLIENT_* vars are empty, aborting." >> "$LOG_FILE"
exit 8
fi
if [[ -z "${MODES_JSON}" || "${MODES_JSON}" == "null" ]]; then
echo "ERROR: no modes parsed for ${OUT} (check OUT name and kscreen-doctor -j output), aborting." >> "$LOG_FILE"
exit 10
fi
# First, see if there is an exact WxH@FPS match
EXACT_MODE_ID="$(echo "${MODES_JSON}" | jq -r --argjson w "${REQ_W}" --argjson h "${REQ_H}" --arg fps "${REQ_FPS}" '
.[]
| select(.size.width == $w and .size.height == $h and (.refreshRate | tostring) == $fps)
| .id
' | head -n1)"
if [[ -n "${EXACT_MODE_ID}" && "${EXACT_MODE_ID}" != "null" ]]; then
BEST_ID="${EXACT_MODE_ID}"
echo "Exact mode match by WxH@FPS, using id=${BEST_ID}" >> "$LOG_FILE"
else
echo "No exact match, searching for closest mode by WxH@FPS..." >> "$LOG_FILE"
BEST_ID=""
BEST_SCORE=""
# Iterate modes using jq to emit simple lines
while IFS= read -r line; do
# line format: id width height refresh
[[ -z "$line" ]] && continue
mid=$(awk '{print $1}' <<< "$line")
mw=$(awk '{print $2}' <<< "$line")
mh=$(awk '{print $3}' <<< "$line")
mfps_raw=$(awk '{print $4}' <<< "$line")
# Compute differences via Python (float‑friendly)
score=$(python - <<EOF
req_w = float("${REQ_W}")
req_h = float("${REQ_H}")
req_f = float("${REQ_FPS}")
cur_w = float("${mw}")
cur_h = float("${mh}")
cur_f = float("${mfps_raw}")
dw = abs(cur_w - req_w)
dh = abs(cur_h - req_h)
df = abs(cur_f - req_f)
# weight resolution heavily vs fps
print(dw*1000 + dh*1000 + df)
EOF
)
if [[ -z "$BEST_SCORE" ]]; then
BEST_SCORE="$score"
BEST_ID="$mid"
else
better=$(python - <<EOF
import sys
best = float("${BEST_SCORE}")
cur = float("${score}")
sys.exit(0 if cur < best else 1)
EOF
)
if [[ $? -eq 0 ]]; then
BEST_SCORE="$score"
BEST_ID="$mid"
fi
fi
done < <(echo "${MODES_JSON}" | jq -r '.[] | "\(.id) \(.size.width) \(.size.height) \(.refreshRate)"')
if [[ -z "${BEST_ID}" ]]; then
echo "ERROR: failed to choose a closest mode, aborting." >> "$LOG_FILE"
exit 11
fi
{
echo "Closest mode chosen: id=${BEST_ID}, score=${BEST_SCORE}"
echo
} >> "$LOG_FILE"
fi
CMD=(kscreen-doctor "output.${OUT}.mode.${BEST_ID}")
{
echo "Running command:"
echo " ${CMD[*]}"
echo "================================"
} >> "$LOG_FILE"
"${CMD[@]}" >> "$LOG_FILE" 2>&1
RET=$?
echo "Exit code: $RET" >> "$LOG_FILE"
exit $RET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment