diff --git a/.config/scripts/shared/webapp.sh b/.config/scripts/shared/webapp.sh index ee8df17..f2a7c12 100755 --- a/.config/scripts/shared/webapp.sh +++ b/.config/scripts/shared/webapp.sh @@ -31,14 +31,16 @@ HOST="${URL#*://}" HOST="${HOST%%/*}" CLASS="chrome-${HOST}__-Default" -# Chromium names its MPRIS player chromium.instance, and inside a flatpak -# sandbox the browser is always pid 2 — so this app, the Chromium flatpak and -# FreeTube all fight over chromium.instance2 and only one shows in playerctl. -# Running chromium as a child of a sandbox shell moves it to pid 3, giving the -# web apps their own player name. The trailing `exit` stops bash from exec'ing -# a lone command (which would keep chromium at pid 2). +# Chromium names its MPRIS player chromium.instance, and every flatpak +# sandbox has its own pid namespace, so each web app's browser would get the +# same pid and only the first app to start shows in playerctl. The sandbox +# shell forks and reaps PID_SKIP throwaway processes first (pids are handed out +# sequentially), which moves chromium to a per-profile pid. Offsets start at 10 +# to stay clear of the Chromium flatpak and FreeTube at pid 2. The trailing +# `exit` stops bash from exec'ing chromium in place. +PID_SKIP=$(( $(printf '%s' "$PROFILE" | cksum | cut -d' ' -f1) % 1000 + 10 )) cmd=(flatpak run --command=bash "$BROWSER_ID" - -c 'chromium "$@"; exit $?' bash + -c 'for ((i = 0; i < $0; i++)); do : & done; wait; chromium "$@"; exit $?' "$PID_SKIP" --no-first-run --app="$URL" --class="$CLASS"