Skip to content
Merged
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
20 changes: 13 additions & 7 deletions scripts/fix-appimage-wayland.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
# next real resize. The hook is rewritten to prefer Wayland and fall back to
# X11, and to let a GDK_BACKEND the user has already exported win.
#
# Tauri's CLI now ships its own copy of the plugin (tauri-apps/tauri#16062),
# whose hook carries the line commented out — `# export GDK_BACKEND=x11`. Only
# an active export forces X11, so only that is rewritten; a hook that leaves
# GDK_BACKEND unset already gets GTK's own Wayland-then-X11 choice.
#
# Usage:
# scripts/fix-appimage-wayland.sh <AppImage> [more...] strip and repack
# scripts/fix-appimage-wayland.sh --strip-only <AppDir> strip in place
Expand All @@ -54,9 +59,10 @@ STRIP_LIBS=(
libwayland-server.so.0
)

# The hook the GTK plugin writes, and the line in it that forces X11.
# The hook the GTK plugin writes, and the line in it that forces X11: an
# active export, not the same text inside a comment.
GTK_HOOK=apprun-hooks/linuxdeploy-plugin-gtk.sh
GDK_FORCED_X11='export GDK_BACKEND=x11'
GDK_FORCED_X11='^[[:space:]]*export[[:space:]]+GDK_BACKEND=x11([[:space:]]|$)'
GDK_PREFER_WAYLAND='export GDK_BACKEND="${GDK_BACKEND:-wayland,x11}" # rewritten by fix-appimage-wayland.sh: native Wayland where it exists, X11 otherwise, and an exported value wins'

info() { printf '\033[1;34m==>\033[0m %s\n' "$*"; }
Expand Down Expand Up @@ -85,11 +91,11 @@ strip_appdir() {
prefer_wayland() {
local hook="$1/$GTK_HOOK"
[[ -f "$hook" ]] || { printf '0'; return 0; }
if grep -qF "$GDK_FORCED_X11" "$hook"; then
# Whole-line replacement on the exact export, via awk rather than sed so
# the replacement's $ and " need no escaping.
if grep -qE "$GDK_FORCED_X11" "$hook"; then
# Whole-line replacement of the export, via awk rather than sed so the
# replacement's $ and " need no escaping.
awk -v from="$GDK_FORCED_X11" -v to="$GDK_PREFER_WAYLAND" \
'index($0, from) == 1 { print to; next } { print }' "$hook" > "$hook.tmp" \
'$0 ~ from { print to; next } { print }' "$hook" > "$hook.tmp" \
&& mv "$hook.tmp" "$hook"
printf ' rewrote %s\n' "$GTK_HOOK" >&2
printf '1'
Expand Down Expand Up @@ -181,7 +187,7 @@ for IMG in "$@"; do
find "$VERIFY/squashfs-root" -name 'libwebkit2gtk-4.1.so.0' | grep -q . \
|| die "libwebkit2gtk missing from the repacked AppImage — bad repack."
if [[ -f "$VERIFY/squashfs-root/$GTK_HOOK" ]]; then
grep -qF "$GDK_FORCED_X11" "$VERIFY/squashfs-root/$GTK_HOOK" \
grep -qE "$GDK_FORCED_X11" "$VERIFY/squashfs-root/$GTK_HOOK" \
&& die "the GTK hook still forces GDK_BACKEND=x11 after the repack."
fi

Expand Down
48 changes: 48 additions & 0 deletions scripts/test-fix-appimage-wayland.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# * both AppDir layouts are handled (usr/lib and usr/lib/<triplet>)
# * the GTK plugin's hook stops forcing GDK_BACKEND=x11, prefers Wayland,
# and still lets a user's own exported GDK_BACKEND win
# * a hook that only mentions the export in a comment (Tauri's bundled
# plugin) is left alone, and an indented export is still caught
# * a second run is a clean no-op rather than an error
# * a missing AppDir fails loudly instead of silently succeeding
#
Expand Down Expand Up @@ -138,6 +140,52 @@ else
fail "the rewrite disturbed lines other than the GDK_BACKEND export"
fi

# ---------------------------------------------------------------- case 2c ---
# Tauri's CLI ships its own copy of the plugin, and its hook has the line
# commented out. That forces nothing: the hook must come through byte for
# byte, and GDK_BACKEND stays unset for GTK to choose. Matching the text
# inside the comment is what failed the v0.7.4 Linux release build.
C="$T/Commented"
mkdir -p "$C/apprun-hooks"
cat > "$C/apprun-hooks/linuxdeploy-plugin-gtk.sh" <<'HOOK'
#! /usr/bin/env bash
export GTK_THEME="$APPIMAGE_GTK_THEME" # Custom themes are broken
# export GDK_BACKEND=x11 # Monitor this closely. AppImage used to crash on Wayland!
export XDG_DATA_DIRS="$APPDIR/usr/share:/usr/share:$XDG_DATA_DIRS" # g_get_system_data_dirs() from GLib
HOOK
cp "$C/apprun-hooks/linuxdeploy-plugin-gtk.sh" "$T/commented-before.sh"
out=$(bash "$SCRIPT" --strip-only "$C" 2>&1) || fail "strip-only exited non-zero on a commented hook"
if [[ "$out" == *"hook rewritten: 0"* ]] \
&& cmp -s "$T/commented-before.sh" "$C/apprun-hooks/linuxdeploy-plugin-gtk.sh"; then
pass "a commented-out export is left alone"
else
fail "a hook whose export is only a comment was changed ($out)"
fi
got=$(backend_after "$C/apprun-hooks/linuxdeploy-plugin-gtk.sh")
if [[ -z "$got" ]]; then
pass "the commented hook leaves GDK_BACKEND for GTK to choose"
else
fail "expected GDK_BACKEND unset after the commented hook, got '$got'"
fi

# An active export does not have to start in column one — inside an `if` it
# is indented, and it forces X11 all the same.
I="$T/Indented"
mkdir -p "$I/apprun-hooks"
cat > "$I/apprun-hooks/linuxdeploy-plugin-gtk.sh" <<'HOOK'
#! /usr/bin/env bash
if true; then
export GDK_BACKEND=x11
fi
HOOK
bash "$SCRIPT" --strip-only "$I" >/dev/null 2>&1 || fail "strip-only exited non-zero on an indented hook"
got=$(backend_after "$I/apprun-hooks/linuxdeploy-plugin-gtk.sh")
if [[ "$got" == "wayland,x11" ]]; then
pass "an indented export is rewritten too"
else
fail "expected GDK_BACKEND=wayland,x11 after an indented export, got '$got'"
fi

# ---------------------------------------------------------------- case 3 ----
# Idempotency: re-running over an already-clean AppDir must succeed quietly,
# because the release workflow may be re-run against the same artefacts.
Expand Down
Loading