Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions bin/omarchy-games-retro-install
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@

set -e

desktop_string_escape() {
local value=$1

value=${value//\\/\\\\}
value=${value//$'\t'/\\t}
value=${value//$'\r'/\\r}
value=${value//$'\n'/\\n}
[[ $value == " "* ]] && value="\\s${value# }"

printf '%s' "$value"
}

desktop_exec_arg() {
local value=$1

value=${value//\\/\\\\}
value=${value//\"/\\\"}
value=${value//\`/\\\`}
value=${value//\$/\\\$}
value=${value//%/%%}

printf '"%s"' "$value"
}

if (( $# == 0 )); then
mapfile -t cores < <(omarchy-games-retro-cores)

Expand Down Expand Up @@ -53,12 +77,17 @@ desktop_dir="$HOME/.local/share/applications"
desktop_file="$desktop_dir/$desktop_id.desktop"
mkdir -p "$desktop_dir"

name_field=$(desktop_string_escape "$desktop_name")
comment_field=$(desktop_string_escape "Play $game_name with RetroArch")
exec_command="retroarch -L $(desktop_exec_arg "$core_path") $(desktop_exec_arg "$game_path")"
exec_field=$(desktop_string_escape "$exec_command")

cat >"$desktop_file" <<EOF
[Desktop Entry]
Version=1.0
Name=$desktop_name
Comment=Play $game_name with RetroArch
Exec=retroarch -L "$core_path" "$game_path"
Name=$name_field
Comment=$comment_field
Exec=$exec_field
Terminal=false
Type=Application
Icon=retro-gaming
Expand Down
66 changes: 66 additions & 0 deletions test/shell.d/games-retro-install-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

require_command gio

test_tmp=$(mktemp -d)
trap 'rm -rf "$test_tmp"' EXIT

home="$test_tmp/home"
stub_dir="$test_tmp/bin"
mkdir -p "$home/.local/share/applications" "$stub_dir"

cat >"$stub_dir/retroarch" <<'SH'
#!/bin/bash
printf '%s\0' "$@" >"$RETRO_ARGV"
SH

cat >"$stub_dir/update-desktop-database" <<'SH'
#!/bin/bash
exit 0
SH

cat >"$stub_dir/omarchy-notification-send" <<'SH'
#!/bin/bash
exit 0
SH

cat >"$stub_dir/omarchy-i18n" <<'SH'
#!/bin/bash
printf '%s\n' "$*"
SH

chmod +x "$stub_dir/"*

core_path="$test_tmp/core\\\\with-backslashes\"and-percent%_libretro.so"
game_path=$(printf '%s/Rom\nExec=retro-injection-marker.sfc' "$test_tmp")
touch "$core_path" "$game_path"

HOME="$home" PATH="$stub_dir:$ROOT/bin:/usr/bin:/bin" RETRO_ARGV="$test_tmp/argv" \
"$ROOT/bin/omarchy-games-retro-install" "$core_path" "$game_path" \
>"$test_tmp/out" 2>"$test_tmp/err"

mapfile -t desktop_files < <(find "$home/.local/share/applications" -maxdepth 1 -name '*.desktop' -print)
(( ${#desktop_files[@]} == 1 )) || fail "retro install writes one desktop entry" "${desktop_files[*]}"
desktop_file=${desktop_files[0]}

(( $(grep -c '^Exec=' "$desktop_file") == 1 )) ||
fail "retro desktop values cannot inject another Exec key" "$(cat "$desktop_file")"
pass "retro install escapes newlines in desktop string values"

: >"$test_tmp/argv"
RETRO_ARGV="$test_tmp/argv" PATH="$stub_dir:/usr/bin:/bin" gio launch "$desktop_file" >/dev/null 2>&1 ||
fail "the escaped retro desktop entry remains launchable" "$(cat "$desktop_file")"

for ((attempt = 0; attempt < 200; attempt++)); do
[[ -s $test_tmp/argv ]] && break
sleep 0.01
done

mapfile -d '' -t argv <"$test_tmp/argv"
[[ ${argv[0]:-} == -L && ${argv[1]:-} == "$core_path" && ${argv[2]:-} == "$game_path" ]] ||
fail "retro launcher preserves core and game paths as separate arguments" "$(printf '%q\n' "${argv[@]}")"
pass "retro install preserves quoted and backslash paths through the desktop launcher"