RetroArch Attract Mode Playlist Shuffler
This script randomly selects games from a RetroArch playlist, starts them in an infinite loop and every game is displayed for a configurable amount of time.
Script behaviour is configurable by the following commandline arguments:
-p /path/to/your/RetroArch/playlist.lpl (load a playlist)
-t timeout (in seconds, amount of time to run each game)
-h (show an help message)
Anything following those arguments will be executed in place of the default retroarch executable, so you can do something like this:
./script.sh -p /tmp/myplaylist.lpl -t 30 /usr/local/bin/retroarch-nightly --set-shader /path/to/my/custom/shader.slangp
That command would shuffle /tmp/myplaylist.lpl displaying each game for 30 seconds through a retroarch executable in /usr/local/bin named retroarch-nightly which applies a custom shader taken from /path/to/my/custom/shader.slangp
Note that --set-shader is a RetroArch option and multiple options are allowed.
There are other things you can change by editing the script, like displaying a big/little scrolling or fixed banner (FIXED_BANNER variable), or forcing a specific core file (FORCE_CORE variable).
You can also pause the shuffling if you find a game worth playing or skipping to the next one without waiting.
Very useful if you’re bored 
Version 0.5.1: Avoid multiple reads due to special keys
Version 0.5.1, Expand
#!/bin/bash
# This software is released into the public domain.
#
# You are free to use, modify, distribute, and sell this software
# for any purpose, without restriction.
#
# This software is provided "AS IS", without warranty of any kind.
# RetroArch Attract Mode Playlist Shuffler
# Version 0.5.1
# Changelog:
# Avoid multiple reads due to special keys
PLAYLIST="/path/to/playlist.lpl" # Shuffle this playlist, can also be overridden by the command line
FORCE_CORE="" # Optional, override playlist core. Can be blank or a full path to a core
TIMEOUT=60 # How much seconds before the next title
FIXED_BANNER="yes" # "yes": fixed banner, "no": scrolling banner
BIG_BANNER="yes" # "yes": use a big banner, "no": use a little banner
#--------- config ends here ----------
#--------- command line arguments ----
RETROARCH_CMD=()
usage() {
echo
printf 'Usage: %s [-p playlist.lpl] [-h] [command [arguments...]]\n' "$0"
echo
echo " -p FILE RetroArch playlist"
echo " -t TIMEOUT How much seconds before the next title"
echo " -h Show this help"
echo
echo "Anything after those arguments are executed in place of the default retroarch executable."
printf 'eg: %s -p /tmp/myplaylist.lpl /path/to/my/custom/retroarch --set-shader myshader-slangp \n'
}
while (($#)); do
case "$1" in
-p)
if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: Option -p requires a playlist path."
usage
exit 1
fi
PLAYLIST="$2"
PLAYLIST_FROM_CLI="yes"
shift 2
;;
-t)
if [[ ! "$2" =~ ^[0-9]+$ ]]; then
echo "Error: Option -t requires a valid positive integer value."
usage
exit 1
fi
TIMEOUT="$2"
shift 2
;;
-h)
usage
exit 0
;;
--)
shift
RETROARCH_CMD=("$@")
break
;;
*)
RETROARCH_CMD=("$@")
break
;;
esac
done
# Default executable
if ((${#RETROARCH_CMD[@]} == 0)); then
RETROARCH_CMD=(retroarch)
fi
#-------------------------------------
# Terminal colors
if [[ -t 1 ]]; then
BOLD='\033[1m'
DIM='\033[2m'
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
CYAN='\033[36m'
RESET='\033[0m'
BLINK='\033[5m'
else
BOLD='' DIM='' RED='' GREEN='' YELLOW='' BLUE='' CYAN='' RESET=''
fi
echo_bold() { printf '%b%s%b\n' "$BOLD" "$*" "$RESET"; }
echo_dim() { printf '%b%s%b\n' "$DIM" "$*" "$RESET"; }
echo_red() { printf '%b%s%b\n' "$RED" "$*" "$RESET"; }
echo_green() { printf '%b%s%b\n' "$GREEN" "$*" "$RESET"; }
echo_yellow() { printf '%b%s%b\n' "$YELLOW" "$*" "$RESET"; }
echo_blue() { printf '%b%s%b\n' "$BLUE" "$*" "$RESET"; }
echo_cyan() { printf '%b%s%b\n' "$CYAN" "$*" "$RESET"; }
echo_blnk() { printf '%b%b%s%b\n' "$BOLD" "$BLINK" "$*" "$RESET"; }
# Startup banner
do_little_banner(){
BANNER_LINES=12
BANNER_WIDTH=42
echo_cyan ' ___ ___'
echo_cyan ' \ \ / /'
echo_cyan ' ___ _____\ \___/ /_____ ___'
echo_cyan ' / / / __ __ \ \ \'
echo_cyan ' / /__/ |__| |__| \__\ \'
echo_cyan ' / \'
echo_cyan '/_____ _____\'
echo_cyan ' /______ _________ ______\'
echo_cyan ' / / \ \'
echo_cyan ' /___/ \___\'
echo
echo_blnk ' .:: RetroArch Playlist Attract Mode ::.'
echo
}
# Startup banner
do_big_banner(){
BANNER_LINES=21
BANNER_WIDTH=65
echo_cyan ' _____ _____'
echo_cyan ' \ \ / /'
echo_cyan ' \ \ / /'
echo_cyan ' _____ ________\ \_____/ /________ _____'
echo_cyan ' / / / \ \ \'
echo_cyan ' / / / ____ ____ \ \ \'
echo_cyan ' / /___/ | | | | \___\ \'
echo_cyan ' / |____| |____| \'
echo_cyan ' / \'
echo_cyan '/_________ _______\'
echo_cyan ' / \'
echo_cyan ' /_________ ______________ __________\'
echo_cyan ' / / \ \'
echo_cyan ' / / \ \'
echo_cyan ' / / \ \'
echo_cyan ' /______/ \______\'
echo
echo
echo_blnk " .:: RetroArch Playlist Attract Mode ::."
echo_dim " ---------------------------------------"
echo
}
do_banner(){
[[ "$FIXED_BANNER" == "yes" ]] && printf '\033[1;1H' #cursor at 1,1
if [[ "$BIG_BANNER" == "yes" ]] ; then
do_big_banner
else
do_little_banner
fi
}
do_narrow_banner(){
printf '\033[1;1H' #cursor at 1,1
for ((i = 0; i < BANNER_LINES; i++)); do
echo
done
}
set_scroll_region() {
printf '\033[%dr' "$((BANNER_LINES + 1))"
}
redraw_banner() {
# Clear previous banner
for ((i = 1; i <= BANNER_LINES; i++)); do
printf '\033[%d;1H\033[2K' "$i"
done
#redraw thew banner without overwriting underlying content
if (( $(tput cols) >= BANNER_WIDTH )) ; then
do_banner
else
do_narrow_banner
fi
# restore "scrolling" area
set_scroll_region
# put the cursor back to the start of the scroling area
printf '\033[%d;1H' "$((BANNER_LINES + 1))"
}
init_screen() {
# Clear the screen and move curor to top left
printf '\033[2J\033[H'
set_scroll_region
redraw_banner
}
# Restore terminal behaviour
reset_screen() {
printf '\033[r\033[?25h' # Reset scrolling region and show cursor
printf '\033[%d;1H' "$(tput lines)" # Move cursor to the first column of the last terminal row
}
is_udp_port_busy(){
ss -ulnp|grep ":$1" &>/dev/null
}
is_file(){
[[ -n "$1" && -f "$1" && -s "$1" && -r "$1" ]]
}
# Draw banner?
if [[ "$FIXED_BANNER" == "yes" ]]; then
init_screen
trap redraw_banner WINCH
else
do_banner
fi
# Sanity checks
echo
echo_bold "INITIALIZATION"
if ! command -v "${RETROARCH_CMD[0]}" &>/dev/null; then
echo_red "Error: Invalid RetroArch executable:"
echo "${RETROARCH_CMD[0]}"
exit 1
fi
if ! "${RETROARCH_CMD[@]}" --command "LOAD_CONTENT" &>/dev/null ; then
echo_red "Error: RetroArch does not support LOAD_CONTENT command."
exit 1
fi
if [[ -n "$PLAYLIST" ]]; then
[ "_$PLAYLIST_FROM_CLI" = "_" ] && PLSMSG="from script"
echo_blue "Using playlist $PLSMSG: $PLAYLIST"
fi
if [[ -z "$PLAYLIST" ]] ; then
echo_red "Error: No playlist specified."
exit 1
fi
if ! is_file "$PLAYLIST" ; then
echo_red "Error: Playlist file \"$PLAYLIST\" is not readable."
exit 1
fi
#Find a free upd port with ss or exit
if ! which ss &>/dev/null ; then
echo "Error: ss executable is required, but missing."
exit 1
fi
echo_blue "Searching for a free UDP port..."
for P in $(seq 54000 55300) ; do
if ! is_udp_port_busy "$P" ; then
UDP_PORT="$P"
break
fi
done
if [[ -z "$UDP_PORT" ]]; then
echo_red "Unable to find a free UDP port."
exit 1
fi
echo_green "Using UDP port: $UDP_PORT"
retroarch_remote_cmd(){
"${RETROARCH_CMD[@]}" --command "$1;127.0.0.1;$UDP_PORT" &>/dev/null
}
# Cleanup on exit
# Hook to Kill remote retroarch on exit
cleanup() {
trap - EXIT SIGINT SIGTERM # remove the hook, do not trap twice
echo
rm "$TEMP_CONFIG" &>/dev/null
reset_screen
#Since retroarch kill us on exit we have to stop it as the last operation here.
echo_blue "Stopping remote instance..."
retroarch_remote_cmd "QUIT"
sleep 0.1
retroarch_remote_cmd "QUIT"
exit 0
}
trap cleanup EXIT SIGINT SIGTERM
# Run Retroarch in a separate shell which kills the parent (me) when it exits.
retroarch_parent_killer() {
"${RETROARCH_CMD[@]}" "$@" &>/dev/null
echo ; echo_red "RetroArch is dead, long live RetroArch!"
kill -TERM "$$"
}
# Temporary RetroArch configuration
TEMP_CONFIG=$(mktemp)
echo 'history_list_enable = "false"' > "$TEMP_CONFIG"
echo 'network_cmd_enable = "true"' >> "$TEMP_CONFIG"
echo 'network_cmd_port = "'$UDP_PORT'"' >> "$TEMP_CONFIG"
echo 'network_cmd_bind_address = "127.0.0.1"' >> "$TEMP_CONFIG"
echo 'config_save_on_exit = "false"' >> "$TEMP_CONFIG"
# Start RetroArch
echo
echo_bold "STARTING RETROARCH"
retroarch_parent_killer --appendconfig "$TEMP_CONFIG" &
# Wait for UDP socket
UDP_OK=""
for i in $(seq 1 20) ; do
echo_blue "Waiting for RetroArch to open the UDP port..."
sleep 1.0
if is_udp_port_busy "$UDP_PORT" ; then
UDP_OK="1"
break
fi
done
if [[ "$UDP_OK" != "1" ]] ; then
echo_red "Timeout waiting for RetroArch to open the UDP port."
exit 1
fi
echo_green "RetroArch is ready."
echo
echo_red "Configuration will NOT be saved on exit."
echo_red "It is NOT advised to manually save either."
echo
# Playlist parsing
DEFAULT_CORE=$(grep '"default_core_path"' "$PLAYLIST" | cut -d '"' -f 4 | head -n 1)
mapfile -t USEFUL_PLAYLIST < <( # Reformat the playlist: and make an array
tr -d '\n' < "$PLAYLIST" | # Put everything on one line
tr '}' '\n' | # Add new lines in place of '}'
grep -E ' "path": | "core_path": | "label": ' | # Skip lines not containing all the needed keys
shuf # Shuffle
)
NUM_TITLES=${#USEFUL_PLAYLIST[@]}
echo_green "Found $NUM_TITLES titles."
TITL=0
# Main loop
IFS=$'\n'
i=0
while (( i < $NUM_TITLES )); do
(( i >= NUM_TITLES )) && i=0 # Defensive
line=${USEFUL_PLAYLIST[i]}
USEFUL_KEYS=$( # Reformat each line to be easilly parsable.
echo -E "$line" | #
sed 's/", /\n/g' | # we have now: "key": "value", "key": "value", so replace every '", ' with a new line
cut -d \" -f 2- | # strip everything before "key, and have key": "value
grep -E '^path|^core_path|^label' | # keep only required keys
sed 's/ "//' # we have now: key": "value, strip leading space+double quotes.
)
#we have now: key: value; the first ":" is the separator, finally extract values.
ROM=$( echo "$USEFUL_KEYS" | grep ^path | cut -d ":" -f 2)
LABEL=$( echo "$USEFUL_KEYS" | grep ^label | cut -d ":" -f 2)
CORE=$( echo "$USEFUL_KEYS" | grep ^core | cut -d ":" -f 2)
CORE_SOURCE="Specific to title."
if [[ -n "$FORCE_CORE" ]] ; then
CORE="$FORCE_CORE"
CORE_SOURCE="Forced by the script."
else
if [[ "$CORE" = "DETECT" ]] ; then
CORE="$DEFAULT_CORE"
CORE_SOURCE="Default core playlist."
if [[ -z "$CORE" ]]; then
echo
echo_yellow "Core set to DETECT for $LABEL"
echo_yellow "But no default core was set in the playlist."
echo_blue "Skipping..."
continue
fi
fi
fi
echo
echo_dim "----------------------------------------"
echo_bold "TITLE $(($i+1)) / $NUM_TITLES"
echo_dim "----------------------------------------"
echo_bold "NAME: $LABEL"
echo_bold "ROM: $(basename "$ROM")"
echo_bold "CORE: $CORE"
echo_dim " ($CORE_SOURCE)"
echo_dim "----------------------------------------"
if ! is_file "$CORE" ; then
echo_red "Core file is not readable, skipping."
continue
fi
retroarch_remote_cmd "LOAD_CONTENT $CORE|$ROM" &
echo
echo_yellow "Wanna play $LABEL?"
read -sp "Enter to play, p for previous, any other to skip, or wait $TIMEOUT seconds..." -t "$TIMEOUT" -n 1
while read -rs -t 0.05 -n 1 discard; do :; done #discard pending characters (some keys generates multiple ones)
if [[ "$REPLY" = "" ]] ; then
echo
echo_green "Have fun!"
echo_blue "Hit ENTER for the next title"
read -s
((i++))
elif [[ "$REPLY" = "p" ]] ; then
(( i > 0 )) && ((i--))
else
((i++))
fi
(( i >= NUM_TITLES )) && i=0
done

Also very nice it can detect the default core and be used with any playlist, and all of that without an external non standard tool.
