|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +check-and-auto-start() { |
| 6 | + until ping -c 1 pendrellvale.home; do |
| 7 | + sleep 1; |
| 8 | + done |
| 9 | + |
| 10 | + HA_BEARER_TOKEN=$(cat ~/.config/hass-bearer-token) |
| 11 | + |
| 12 | + entity_ids=(input_boolean.gaming_mode input_boolean.remote_gaming) |
| 13 | + |
| 14 | + for entity_id in "${entity_ids[@]}"; do |
| 15 | + STATE=$(curl -s -H "Authorization: Bearer ${HA_BEARER_TOKEN}" \ |
| 16 | + -H "Content-Type: application/json" \ |
| 17 | + "http://homeassistant.pendrellvale.home/api/states/$entity_id" | jq -r '.state') |
| 18 | + |
| 19 | + if [ "$STATE" == "on" ]; then |
| 20 | + echo "Woke by HA intent. Starting Steam..." |
| 21 | + |
| 22 | + setup-remote-steam |
| 23 | + fi |
| 24 | + done |
| 25 | +} |
| 26 | + |
| 27 | +setup-remote-steam-pipewire() { |
| 28 | + launch_steam() { |
| 29 | + __GL_GSYNC_ALLOWED=0 __GL_VRR_ALLOWED=0 steam \ |
| 30 | + -pipewire \ |
| 31 | + --enable-features=UseOzonePlatform \ |
| 32 | + --ozone-platform=wayland \ |
| 33 | + --enable-gpu-rasterization \ |
| 34 | + --enable-zero-copy |
| 35 | + } |
| 36 | + pgrep steam || launch_steam & |
| 37 | + |
| 38 | + pgrep ydotoold || ydotoold & |
| 39 | + TARGET_CLASS="hyprland-share-picker" |
| 40 | + echo "Listening for $TARGET_CLASS..." |
| 41 | + |
| 42 | + while true; do |
| 43 | + WINDOW_INFO=$(hyprctl clients -j | |
| 44 | + jq ".[] | select(.class == \"$TARGET_CLASS\")") |
| 45 | + |
| 46 | + if [ -n "$WINDOW_INFO" ]; then |
| 47 | + echo "Portal detected! Calculating coordinates..." |
| 48 | + |
| 49 | + X=$(echo "$WINDOW_INFO" | jq '.at[0]') |
| 50 | + Y=$(echo "$WINDOW_INFO" | jq '.at[1]') |
| 51 | + WIDTH=$(echo "$WINDOW_INFO" | jq '.size[0]') |
| 52 | + HEIGHT=$(echo "$WINDOW_INFO" | jq '.size[1]') |
| 53 | + |
| 54 | + CLICK_X=$(echo "($X + ($WIDTH / 2)) / 2" | bc -l | cut -d'.' -f1) |
| 55 | + CLICK_Y=$(echo "($Y + ($HEIGHT * 0.1)) / 2" | bc -l | cut -d'.' -f1) |
| 56 | + |
| 57 | + echo "Simulating click at X: $CLICK_X, Y: $CLICK_Y" |
| 58 | + |
| 59 | + ydotool mousemove -a "$CLICK_X" "$CLICK_Y" |
| 60 | + ydotool click 0xC0 # 0xC0 is the code for left mouse button down/up |
| 61 | + |
| 62 | + break |
| 63 | + fi |
| 64 | + |
| 65 | + sleep 0.5 |
| 66 | + done |
| 67 | + |
| 68 | + wait |
| 69 | +} |
| 70 | + |
| 71 | +USE_PIPEWIRE=no |
| 72 | +GAME_MONITOR_PID=/tmp/game-monitor.pid |
| 73 | + |
| 74 | +setup-remote-steam() { |
| 75 | + ~/.local/bin/toggle-resolution 16:9 |
| 76 | + hyprctl dispatch 'hl.dsp.focus({ workspace = "emptymn" })' |
| 77 | + [ -e $GAME_MONITOR_PID ] || game-monitor & |
| 78 | + if [[ "$USE_PIPEWIRE" = yes ]]; then |
| 79 | + setup-remote-steam-pipewire |
| 80 | + else |
| 81 | + pgrep steam || steam |
| 82 | + fi |
| 83 | + wait |
| 84 | +} |
| 85 | + |
| 86 | +wait-for-steam() { |
| 87 | + until pgrep steam &>/dev/null; do |
| 88 | + printf "waiting for steam " |
| 89 | + date |
| 90 | + sleep 0.5 |
| 91 | + done |
| 92 | + until ss -ltp sport = 27036 | grep steam -q; do |
| 93 | + printf "waiting for remote play " |
| 94 | + date |
| 95 | + sleep 0.5 |
| 96 | + done |
| 97 | +} |
| 98 | + |
| 99 | +STOP_GAME_MONITOR_SENTINEL=/tmp/stop-game-monitor |
| 100 | + |
| 101 | +teardown-remote-steam() { |
| 102 | + touch "$STOP_GAME_MONITOR_SENTINEL" |
| 103 | + ~/.local/bin/toggle-resolution 21:9 |
| 104 | +} |
| 105 | + |
| 106 | +game-monitor() { |
| 107 | + exec &>/tmp/game-monitor-$$.log |
| 108 | + cleanup() { |
| 109 | + rm $GAME_MONITOR_PID |
| 110 | + } |
| 111 | + trap cleanup EXIT |
| 112 | + rm -f $STOP_GAME_MONITOR_SENTINEL |
| 113 | + |
| 114 | + echo "$$" > $GAME_MONITOR_PID |
| 115 | + |
| 116 | + printf "PID: %s\n" "$(cat /tmp/game-monitor.pid)" |
| 117 | + handle() { |
| 118 | + echo "EV $(date): $*" |
| 119 | + case "$1" in |
| 120 | + 'activewindow>>steam_app_858710') # Gravity Circuit |
| 121 | + sleep 1 |
| 122 | + shyprctl dispatch 'hl.dsp.window.fullscreen({ action = "set" })' |
| 123 | + ;; |
| 124 | + 'activewindow>>steam_app_1086940,LariLauncher') # Bg3 |
| 125 | + ( |
| 126 | + pgrep ydotoold -a || ydotoold & disown |
| 127 | + sleep 5 |
| 128 | + while [[ "$(hyprctl -j activewindow | jq .title -r)" == "LariLauncher" ]]; do |
| 129 | + echo clicking play on larian launcher |
| 130 | + ydotool mousemove -a 800 580 |
| 131 | + ydotool click 0xC0 |
| 132 | + sleep 0.5 |
| 133 | + done |
| 134 | + ) & |
| 135 | + ;; |
| 136 | + 'activewindow>>steam_app_1086940,Baldur'*) |
| 137 | + sleep 1 |
| 138 | + shyprctl dispatch 'hl.dsp.window.fullscreen({ action = "set" })' |
| 139 | + ;; |
| 140 | + esac |
| 141 | + } |
| 142 | + |
| 143 | + while read -r line; do |
| 144 | + if [ -e "$STOP_GAME_MONITOR_SENTINEL" ]; then |
| 145 | + rm "$STOP_GAME_MONITOR_SENTINEL" |
| 146 | + exit 0 |
| 147 | + fi |
| 148 | + handle "$line" |
| 149 | + done < <( |
| 150 | + socat -U - UNIX-CONNECT:"$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock" |
| 151 | + ) |
| 152 | +} |
| 153 | + |
| 154 | +case "$1" in |
| 155 | + check-and-auto-start|setup-remote-steam|wait-for-steam|teardown-remote-steam|game-monitor) |
| 156 | + "$1" |
| 157 | + ;; |
| 158 | + *) |
| 159 | + echo "unknown command" |
| 160 | + exit 1 |
| 161 | + ;; |
| 162 | +esac |
0 commit comments