Skip to content

Instantly share code, notes, and snippets.

@kopiro
Last active April 10, 2022 20:57
Show Gist options
  • Save kopiro/bdd346c4dddced04e496379513e0266a to your computer and use it in GitHub Desktop.
Save kopiro/bdd346c4dddced04e496379513e0266a to your computer and use it in GitHub Desktop.
RATV - RetroPie Apple TV
#!/bin/bash
set -u
export MPLAYER=omxplayer
export BLE_JOYPAD_ID="F4:93:9F:8E:30:E4"
connected_airplay=0
connected_ble=0
retropie_should_be_active=0
activate_cec() {
echo "Activating TV..."
# 4F:82:{1,2,3,4}0:00 = HDMI{1,2,3,4}
echo "txn 4F:82:30:00" | cec-client -s -d 1
if echo "pow 0" | cec-client -s -d 1 | grep "power status: off" ; then
echo "on 0" | cec-client -s -d 1
fi
}
_start() {
if ! pgrep -f "$1"; then
echo "Starting $1..."
$1
else
echo "$1 already active!"
fi
}
_kill() {
echo "Killing $1..."
pkill -f "$1"
}
_start simple-atv-aerial &
while true; do
if lsof -i -sTCP:ESTABLISHED | grep rpiplay >/dev/null ; then
if [ $connected_airplay -eq 0 ]; then
echo "Someone wants to use AirPlay..."
connected_airplay=1
activate_cec &
_kill simple-atv-aerial
fi
else
if [ $connected_airplay -eq 1 ]; then
echo "Someone disconnected AirPlay..."
connected_airplay=0
_start simple-atv-aerial &
fi
if [ $retropie_should_be_active -eq 1 ]; then
if ! pgrep -f emulationstation >/dev/null; then
echo "RetroPie system should be active but it's not, starting screensaver..."
retropie_should_be_active=0
_start simple-atv-aerial &
fi
fi
fi
if hcitool con | grep $BLE_JOYPAD_ID >/dev/null; then
if [ $connected_ble -eq 0 ]; then
echo "The joypad has been connected..."
connected_ble=1
activate_cec &
if ! pgrep -f emulationstation >/dev/null; then
sudo chvt 1
sudo ttyecho -n /dev/tty1 "emulationstation ; chvt 7" &
fi
_kill simple-atv-aerial
retropie_should_be_active=1
fi
else
connected_ble=0
fi
sleep 1
done
[Unit]
After=display-manager.service
StartLimitIntervalSec=500
StartLimitBurst=5
[Service]
User=kopiro
Group=kopiro
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/ratv
Environment=MPLAYER=omxplayer
[Install]
WantedBy=default.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment