Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
105 changes: 91 additions & 14 deletions bin/omarchy-launch-browser
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,108 @@

default_browser=$(omarchy-cmd-default-browser)

# Read Exec= from the same data dirs omarchy-cmd-default-browser searched, in
# the same order, so the command always comes from the entry that was checked.
IFS=: read -ra data_dirs <<<"${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
browser_exec=""
for data_dir in "${data_dirs[@]}"; do
entry="$data_dir/applications/$default_browser"
[[ -f $entry ]] || continue
browser_exec=$(sed -n 's/^Exec=\([^ ]*\).*/\1/p' "$entry" | head -1)
break
done
# Undo the escapes every desktop entry string carries.
desktop_string() {
local string=$1 char i
REPLY=""
for (( i = 0; i < ${#string}; i++ )); do
char=${string:i:1}
if [[ $char == "\\" ]]; then
(( i++ ))
case ${string:i:1} in
s) char=" " ;;
n) char=$'\n' ;;
t) char=$'\t' ;;
r) char=$'\r' ;;
"\\") char="\\" ;;
*) char="\\${string:i:1}" ;;
esac
fi
REPLY+=$char
done
}

desktop_exec_argv() {
local desktop=$1
local file="" line="" name data_dir
desktop_exec_argv=()
# Read Exec= from the same data dirs omarchy-cmd-default-browser searched, in
# the same order, so the command always comes from the entry that was checked.
local -a data_dirs
IFS=: read -ra data_dirs <<<"${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
for data_dir in "${data_dirs[@]}"; do
file="$data_dir/applications/$desktop"
[[ -f $file ]] || continue
line=$(sed -n 's/^Exec=//p' "$file" | head -1)
break
done
[[ -n $line ]] || return 1
desktop_string "$(sed -n 's/^Name=//p' "$file" | head -1)"
name=$REPLY

# Split the Exec line by its own quoting, expanding %c and %k, keeping %% as
# %, and dropping the other field codes. A standalone -- goes too, or the
# launcher's own options after it would be read as operands.
desktop_string "$line"
local value=$REPLY arg="" char i quoted=0 started=0
for (( i = 0; i < ${#value}; i++ )); do
char=${value:i:1}
if [[ $char == '"' ]]; then
(( quoted = !quoted, started = 1 ))
elif [[ $char == "\\" ]] && (( quoted )); then
(( i++ ))
arg+=${value:i:1}
elif [[ $char == "%" ]]; then
(( i++ ))
case ${value:i:1} in
"%") arg+="%" started=1 ;;
c) arg+=$name started=1 ;;
k) arg+=$file started=1 ;;
esac
elif [[ $char == " " ]] && (( ! quoted )); then
if (( started )) && [[ $arg != "--" ]]; then
desktop_exec_argv+=("$arg")
fi
arg="" started=0
else
arg+=$char started=1
fi
done
if (( started )) && [[ $arg != "--" ]]; then
desktop_exec_argv+=("$arg")
fi

(( ${#desktop_exec_argv[@]} > 0 ))
}

# Handing an empty command to uwsm-app surfaces as "Command not found: \"\"",
# which fails a keybinding silently. Say what is missing and how to fix it.
if [[ -z $browser_exec ]]; then
if ! desktop_exec_argv "$default_browser"; then
echo "omarchy-launch-browser: no default browser found; set one with 'omarchy default browser <browser>'" >&2
omarchy-notification-send -u critical -g 󰖟 "$(omarchy-i18n browser.notFound)" "$(omarchy-i18n browser.notFoundHint)"
exit 1
fi

browser_exec=""
skip_value=0
for token in "${desktop_exec_argv[@]}"; do
if (( skip_value )); then
skip_value=0
elif [[ $token == -u || $token == --unset || $token == -C || $token == --chdir ]]; then
skip_value=1
elif [[ $token != env && $token != */env && $token != *=* && $token != -* && $token != run && $token != flatpak && $token != */flatpak ]]; then
browser_exec=$token
break
fi
done
[[ -n $browser_exec ]] || browser_exec=${desktop_exec_argv[0]}

# Only a private launch needs the browser's flag, and probing a Firefox-family
# browser's --help starts the whole binary, so ordinary launches skip it and
# leave their arguments as they are.
private_flag="--private"
if [[ " $* " == *" --private "* ]]; then
if $browser_exec --help 2>/dev/null | grep -q MOZ_LOG; then
if "$browser_exec" --help 2>/dev/null | grep -q MOZ_LOG; then
private_flag="--private-window"
elif [[ $browser_exec =~ edge ]]; then
private_flag="--inprivate"
Expand All @@ -38,10 +115,10 @@ if [[ " $* " == *" --private "* ]]; then
fi
fi

if ! omarchy-cmd-browser-handoff "$browser_exec" "${@/--private/$private_flag}"; then
if ! omarchy-cmd-browser-handoff "${desktop_exec_argv[@]}" "${@/--private/$private_flag}"; then
systemd-run --user --quiet --collect --unit="omarchy-browser-$(date +%s%N)" \
--property=StandardOutput=null --property=StandardError=null \
uwsm-app -- "$browser_exec" "${@/--private/$private_flag}"
uwsm-app -- "${desktop_exec_argv[@]}" "${@/--private/$private_flag}"
fi

url=""
Expand Down
89 changes: 76 additions & 13 deletions bin/omarchy-launch-webapp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,89 @@ google-chrome* | brave* | microsoft-edge* | opera* | vivaldi* | helium*) ;;
*) browser="chromium.desktop" ;;
esac

# Read Exec= from the same data dirs omarchy-cmd-default-browser searched, as
# omarchy-launch-browser does, so a browser it resolved is one this can start.
IFS=: read -ra data_dirs <<<"${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
browser_exec=""
for data_dir in "${data_dirs[@]}"; do
entry="$data_dir/applications/$browser"
[[ -f $entry ]] || continue
browser_exec=$(sed -n 's/^Exec=\([^ ]*\).*/\1/p' "$entry" | head -1)
break
done
# Undo the escapes every desktop entry string carries.
desktop_string() {
local string=$1 char i
REPLY=""
for (( i = 0; i < ${#string}; i++ )); do
char=${string:i:1}
if [[ $char == "\\" ]]; then
(( i++ ))
case ${string:i:1} in
s) char=" " ;;
n) char=$'\n' ;;
t) char=$'\t' ;;
r) char=$'\r' ;;
"\\") char="\\" ;;
*) char="\\${string:i:1}" ;;
esac
fi
REPLY+=$char
done
}

desktop_exec_argv() {
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
local desktop=$1
local file="" line="" name data_dir
desktop_exec_argv=()
# Read Exec= from the same data dirs omarchy-cmd-default-browser searched, in
# the same order, so the command always comes from the entry that was checked.
local -a data_dirs
IFS=: read -ra data_dirs <<<"${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
for data_dir in "${data_dirs[@]}"; do
file="$data_dir/applications/$desktop"
[[ -f $file ]] || continue
line=$(sed -n 's/^Exec=//p' "$file" | head -1)
break
done
[[ -n $line ]] || return 1
desktop_string "$(sed -n 's/^Name=//p' "$file" | head -1)"
name=$REPLY

# Split the Exec line by its own quoting, expanding %c and %k, keeping %% as
# %, and dropping the other field codes. A standalone -- goes too, or the
# launcher's own options after it would be read as operands.
desktop_string "$line"
local value=$REPLY arg="" char i quoted=0 started=0
for (( i = 0; i < ${#value}; i++ )); do
char=${value:i:1}
if [[ $char == '"' ]]; then
(( quoted = !quoted, started = 1 ))
elif [[ $char == "\\" ]] && (( quoted )); then
(( i++ ))
arg+=${value:i:1}
elif [[ $char == "%" ]]; then
(( i++ ))
case ${value:i:1} in
"%") arg+="%" started=1 ;;
c) arg+=$name started=1 ;;
k) arg+=$file started=1 ;;
esac
elif [[ $char == " " ]] && (( ! quoted )); then
if (( started )) && [[ $arg != "--" ]]; then
desktop_exec_argv+=("$arg")
fi
arg="" started=0
else
arg+=$char started=1
fi
done
if (( started )) && [[ $arg != "--" ]]; then
desktop_exec_argv+=("$arg")
fi

(( ${#desktop_exec_argv[@]} > 0 ))
}

# Handing an empty command to uwsm-app surfaces as "Command not found: \"\"",
# which fails a keybinding silently. A web app runs in a Chromium-based
# browser, so say that rather than claiming no browser is set.
if [[ -z $browser_exec ]]; then
if ! desktop_exec_argv "$browser"; then
echo "omarchy-launch-webapp: web apps need a Chromium-based browser; install Chromium with 'omarchy install browser chromium'" >&2
omarchy-notification-send -u critical -g 󰖟 "$(omarchy-i18n browser.webappNeedsChromium)" "$(omarchy-i18n browser.webappNeedsChromiumHint)"
exit 1
fi

if ! omarchy-cmd-browser-handoff "$browser_exec" --app="$1" "${@:2}"; then
exec setsid uwsm-app -- $browser_exec --app="$1" "${@:2}"
if ! omarchy-cmd-browser-handoff "${desktop_exec_argv[@]}" --app="$1" "${@:2}"; then
exec setsid uwsm-app -- "${desktop_exec_argv[@]}" --app="$1" "${@:2}"
fi
63 changes: 60 additions & 3 deletions test/shell.d/launch-browser-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exit 0
SH
cat >"$mock_bin/systemd-run" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >"$OMARCHY_TEST_BROWSER_LAUNCH"
printf '%s\n' "$@" >"$OMARCHY_TEST_BROWSER_LAUNCH"
SH
cp "$mock_bin/systemd-run" "$mock_bin/uwsm-app"
cat >"$mock_bin/omarchy-hyprland-focus-app" <<'SH'
Expand Down Expand Up @@ -118,9 +118,66 @@ pass "an ordinary launch doesn't probe the browser's flags"

launch --private
[[ -s $help_log ]] || fail "a private launch probes the browser's private flag"
grep -F -- 'chromium --incognito' "$launch_log" >/dev/null || fail "a private launch passes the browser's private flag"
[[ $(sed '1,/^--$/d' "$launch_log") == $'chromium\n--incognito' ]] || fail "a private launch passes the browser's private flag"
pass "a private launch passes the browser's private flag"

cat >"$test_home/.local/share/applications/chromium.desktop" <<'EOF'
[Desktop Entry]
Exec=/usr/bin/env LIBVA_DRIVER_NAME=iHD chromium "--profile-directory=Work Profile" --class=100%% %U
EOF

rm -f "$focus_log"
HOME="$test_home" PATH="$mock_bin:$ROOT/bin:$PATH" HYPRLAND_INSTANCE_SIGNATURE=test \
OMARCHY_TEST_BROWSER_LAUNCH="$launch_log" OMARCHY_TEST_BROWSER_FOCUS="$focus_log" \
bash "$ROOT/bin/omarchy-launch-browser" "https://example.test/wrapped"

expected=$(printf '%s\n' /usr/bin/env LIBVA_DRIVER_NAME=iHD chromium "--profile-directory=Work Profile" --class=100% https://example.test/wrapped)
[[ $(sed '1,/^--$/d' "$launch_log") == "$expected" ]] ||
fail "wrapped Exec launcher runs the whole unquoted Exec line, then the URL"
grep -Fx '^chromium.*$' "$focus_log" >/dev/null ||
fail "wrapped Exec launcher focuses the real browser, not env"
pass "browser launcher resolves env-wrapped desktop Exec lines"

rm -f "$help_log"
launch --private
[[ -s $help_log ]] || fail "a wrapped private launch probes the real browser, not env"
expected=$(printf '%s\n' /usr/bin/env LIBVA_DRIVER_NAME=iHD chromium "--profile-directory=Work Profile" --class=100% --incognito)
[[ $(sed '1,/^--$/d' "$launch_log") == "$expected" ]] ||
fail "a wrapped private launch passes the real browser's private flag"
pass "a wrapped private launch passes the real browser's private flag"

cat >"$test_home/.local/share/applications/chromium.desktop" <<'EOF'
[Desktop Entry]
Name=Work\sChromium
Exec=/usr/bin/env -u BROWSER chromium --name %c -- https://start.example.test/ %U
EOF

rm -f "$help_log"
launch --private
[[ -s $help_log ]] || fail "a private launch probes the browser behind env -u, not the name it unsets"
expected=$(printf '%s\n' /usr/bin/env -u BROWSER chromium --name "Work Chromium" https://start.example.test/ --incognito)
[[ $(sed '1,/^--$/d' "$launch_log") == "$expected" ]] ||
fail "a private launch expands %c and keeps its flag an option despite the Exec line's --"
pass "a private launch finds the browser behind env options and keeps its flag an option"

spaced_desktop="userapp-Firefox Developer Edition-U5J9W3.desktop"
cat >"$test_home/.local/share/applications/$spaced_desktop" <<'DESKTOP'
[Desktop Entry]
Exec=chromium %u
DESKTOP
cat >"$mock_bin/omarchy-cmd-default-browser" <<SH
#!/bin/bash
echo "$spaced_desktop"
SH
chmod +x "$mock_bin/omarchy-cmd-default-browser"

rm -f "$launch_log"
launch "https://example.test/spaced"
[[ $(sed '1,/^--$/d' "$launch_log") == $'chromium\nhttps://example.test/spaced' ]] ||
fail "a desktop id with spaces launches its entry's command"
pass "a desktop id with spaces launches its entry's command"
rm -f "$mock_bin/omarchy-cmd-default-browser"

# The resolver reads every XDG data dir, so the launcher has to read Exec=
# from the same ones; a browser outside ~/.local and /usr used to resolve and
# then launch an empty command.
Expand All @@ -141,7 +198,7 @@ EOF

rm -f "$launch_log"
launch "https://example.test/vivaldi"
grep -F -- ' -- vivaldi https://example.test/vivaldi' "$launch_log" >/dev/null ||
[[ $(sed '1,/^--$/d' "$launch_log") == $'vivaldi\nhttps://example.test/vivaldi' ]] ||
fail "a browser in XDG_DATA_DIRS launches from its desktop entry" "$(cat "$launch_log")"
pass "browser launcher reads Exec= from the data dirs the resolver searched"

Expand Down
Loading
Loading