electron-launch 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #!/usr/bin/env bash
  2. # On Fedora $SNAP is under /var and there is some magic to map it to /snap.
  3. # We need to handle that case and reset $SNAP
  4. SNAP=$(echo "$SNAP" | sed -e "s|/var/lib/snapd||g")
  5. #
  6. # Exports are based on https://github.com/snapcore/snapcraft/blob/master/extensions/desktop/common/desktop-exports
  7. #
  8. # ensure_dir_exists calls `mkdir -p` if the given path is not a directory.
  9. # This speeds up execution time by avoiding unnecessary calls to mkdir.
  10. #
  11. # Usage: ensure_dir_exists <path> [<mkdir-options>]...
  12. #
  13. function ensure_dir_exists() {
  14. [ -d "$1" ] || mkdir -p "$@"
  15. }
  16. declare -A PIDS
  17. function async_exec() {
  18. "$@" &
  19. PIDS[$!]=$*
  20. }
  21. function wait_for_async_execs() {
  22. for pid in "${!PIDS[@]}"
  23. do
  24. wait "$pid" && continue || echo "ERROR: ${PIDS[$pid]} exited abnormally with status $?"
  25. done
  26. }
  27. function prepend_dir() {
  28. local -n var="$1"
  29. local dir="$2"
  30. # We can't check if the dir exists when the dir contains variables
  31. if [[ "$dir" == *"\$"* || -d "$dir" ]]; then
  32. export "${!var}=${dir}${var:+:$var}"
  33. fi
  34. }
  35. function append_dir() {
  36. local -n var="$1"
  37. local dir="$2"
  38. # We can't check if the dir exists when the dir contains variables
  39. if [[ "$dir" == *"\$"* || -d "$dir" ]]; then
  40. export "${!var}=${var:+$var:}${dir}"
  41. fi
  42. }
  43. function copy_env_variable() {
  44. local -n var="$1"
  45. if [[ "+$var" ]]; then
  46. export "${!var}_VSCODE_SNAP_ORIG=${var}"
  47. else
  48. export "${!var}_VSCODE_SNAP_ORIG=''"
  49. fi
  50. }
  51. # shellcheck source=/dev/null
  52. source "$SNAP_USER_DATA/.last_revision" 2>/dev/null || true
  53. if [ "$SNAP_DESKTOP_LAST_REVISION" = "$SNAP_VERSION" ]; then
  54. needs_update=false
  55. else
  56. needs_update=true
  57. fi
  58. # Set $REALHOME to the users real home directory
  59. REALHOME=$(getent passwd $UID | cut -d ':' -f 6)
  60. # Extra launch arguments
  61. declare -a launch_args
  62. # Set config folder to local path
  63. ensure_dir_exists "$SNAP_USER_DATA/.config"
  64. chmod 700 "$SNAP_USER_DATA/.config"
  65. if [ "$SNAP_ARCH" == "amd64" ]; then
  66. ARCH="x86_64-linux-gnu"
  67. elif [ "$SNAP_ARCH" == "armhf" ]; then
  68. ARCH="arm-linux-gnueabihf"
  69. elif [ "$SNAP_ARCH" == "arm64" ]; then
  70. ARCH="aarch64-linux-gnu"
  71. else
  72. ARCH="$SNAP_ARCH-linux-gnu"
  73. fi
  74. export SNAP_LAUNCHER_ARCH_TRIPLET="$ARCH"
  75. function is_subpath() {
  76. dir="$(realpath "$1")"
  77. parent="$(realpath "$2")"
  78. [ "${dir##"${parent}"/}" != "${dir}" ] && return 0 || return 1
  79. }
  80. function can_open_file() {
  81. [ -f "$1" ] && [ -r "$1" ]
  82. }
  83. # Preserve system variables that get modified below
  84. copy_env_variable XDG_CONFIG_DIRS
  85. copy_env_variable XDG_DATA_DIRS
  86. copy_env_variable LOCPATH
  87. copy_env_variable GIO_MODULE_DIR
  88. copy_env_variable GSETTINGS_SCHEMA_DIR
  89. copy_env_variable GDK_PIXBUF_MODULE_FILE
  90. copy_env_variable GDK_PIXBUF_MODULEDIR
  91. copy_env_variable GDK_BACKEND
  92. copy_env_variable GTK_PATH
  93. copy_env_variable GTK_EXE_PREFIX
  94. copy_env_variable GTK_IM_MODULE_FILE
  95. copy_env_variable LIBGL_DRIVERS_PATH
  96. # XDG Config
  97. prepend_dir XDG_CONFIG_DIRS "$SNAP/etc/xdg"
  98. # Define snaps' own data dir
  99. prepend_dir XDG_DATA_DIRS "$SNAP/usr/share"
  100. prepend_dir XDG_DATA_DIRS "$SNAP/share"
  101. prepend_dir XDG_DATA_DIRS "$SNAP/data-dir"
  102. prepend_dir XDG_DATA_DIRS "$SNAP_USER_DATA"
  103. # Set XDG_DATA_HOME to local path
  104. ensure_dir_exists "$SNAP_USER_DATA/.local/share"
  105. # Workaround for GLib < 2.53.2 not searching for schemas in $XDG_DATA_HOME:
  106. # https://bugzilla.gnome.org/show_bug.cgi?id=741335
  107. prepend_dir XDG_DATA_DIRS "$SNAP_USER_DATA/.local/share"
  108. # Use the snap MESA drivers to launch the snap.
  109. # This is required by wayland, but electron will make better use under X11 too
  110. prepend_dir LIBGL_DRIVERS_PATH "$SNAP/usr/lib/dri"
  111. prepend_dir LIBGL_DRIVERS_PATH "$SNAP/usr/lib/$ARCH/dri"
  112. # Set cache folder to local path
  113. if [[ -d "$SNAP_USER_DATA/.cache" && ! -e "$SNAP_USER_COMMON/.cache" ]]; then
  114. # the .cache directory used to be stored under $SNAP_USER_DATA, migrate it
  115. mv "$SNAP_USER_DATA/.cache" "$SNAP_USER_COMMON/"
  116. fi
  117. ensure_dir_exists "$SNAP_USER_COMMON/.cache"
  118. # Create $XDG_RUNTIME_DIR if not exists (to be removed when LP: #1656340 is fixed)
  119. # shellcheck disable=SC2174
  120. ensure_dir_exists "$XDG_RUNTIME_DIR" -m 700
  121. # Ensure the app finds locale definitions (requires locales-all to be installed)
  122. append_dir LOCPATH "$SNAP/usr/lib/locale"
  123. # If detect wayland server socket, then set environment so applications prefer
  124. # wayland, and setup compat symlink (until we use user mounts. Remember,
  125. # XDG_RUNTIME_DIR is /run/user/<uid> for classic snaps.
  126. # Applications that don't support wayland natively may define DISABLE_WAYLAND
  127. # (to any non-empty value) to skip that logic entirely.
  128. wayland_available=false
  129. if [[ -n "$XDG_RUNTIME_DIR" && -z "$DISABLE_WAYLAND" ]] && \
  130. [[ "$(snapctl get wayland-native)" == "true" ]]; then
  131. wdisplay="wayland-0"
  132. if [ -n "$WAYLAND_DISPLAY" ]; then
  133. wdisplay="$WAYLAND_DISPLAY"
  134. fi
  135. wayland_snappath="$XDG_RUNTIME_DIR/$wdisplay"
  136. if [ -S "$wayland_snappath" ]; then
  137. # if running under wayland, use it
  138. #export WAYLAND_DEBUG=1
  139. # shellcheck disable=SC2034
  140. wayland_available=true
  141. fi
  142. fi
  143. # Keep an array of data dirs, for looping through them
  144. IFS=':' read -r -a data_dirs_array <<< "$XDG_DATA_DIRS"
  145. # Build mime.cache
  146. # needed for gtk and qt icon
  147. if [ "$needs_update" = true ]; then
  148. rm -rf "$SNAP_USER_DATA/.local/share/mime"
  149. if [ ! -f "$SNAP/usr/share/mime/mime.cache" ]; then
  150. if command -v update-mime-database >/dev/null; then
  151. cp --preserve=timestamps -dR "$SNAP/usr/share/mime" "$SNAP_USER_DATA/.local/share"
  152. async_exec update-mime-database "$SNAP_USER_DATA/.local/share/mime"
  153. fi
  154. fi
  155. fi
  156. # Gio modules and cache (including gsettings module)
  157. export GIO_MODULE_DIR="$SNAP_USER_COMMON/.cache/gio-modules"
  158. function compile_giomodules {
  159. if [ -f "$1/glib-2.0/gio-querymodules" ]; then
  160. rm -rf "$GIO_MODULE_DIR"
  161. ensure_dir_exists "$GIO_MODULE_DIR"
  162. ln -s "$SNAP"/usr/lib/"$ARCH"/gio/modules/*.so "$GIO_MODULE_DIR"
  163. "$1/glib-2.0/gio-querymodules" "$GIO_MODULE_DIR"
  164. fi
  165. }
  166. if [ "$needs_update" = true ]; then
  167. async_exec compile_giomodules "/snap/core20/current/usr/lib/$ARCH"
  168. fi
  169. # Setup compiled gsettings schema
  170. export GSETTINGS_SCHEMA_DIR="$SNAP_USER_DATA/.local/share/glib-2.0/schemas"
  171. function compile_schemas {
  172. if [ -f "$1" ]; then
  173. rm -rf "$GSETTINGS_SCHEMA_DIR"
  174. ensure_dir_exists "$GSETTINGS_SCHEMA_DIR"
  175. for ((i = 0; i < ${#data_dirs_array[@]}; i++)); do
  176. schema_dir="${data_dirs_array[$i]}/glib-2.0/schemas"
  177. if [ -f "$schema_dir/gschemas.compiled" ]; then
  178. # This directory already has compiled schemas
  179. continue
  180. fi
  181. if [ -n "$(ls -A "$schema_dir"/*.xml 2>/dev/null)" ]; then
  182. ln -s "$schema_dir"/*.xml "$GSETTINGS_SCHEMA_DIR"
  183. fi
  184. if [ -n "$(ls -A "$schema_dir"/*.override 2>/dev/null)" ]; then
  185. ln -s "$schema_dir"/*.override "$GSETTINGS_SCHEMA_DIR"
  186. fi
  187. done
  188. # Only compile schemas if we copied anything
  189. if [ -n "$(ls -A "$GSETTINGS_SCHEMA_DIR"/*.xml "$GSETTINGS_SCHEMA_DIR"/*.override 2>/dev/null)" ]; then
  190. "$1" "$GSETTINGS_SCHEMA_DIR"
  191. fi
  192. fi
  193. }
  194. if [ "$needs_update" = true ]; then
  195. async_exec compile_schemas "/snap/core20/current/usr/lib/$ARCH/glib-2.0/glib-compile-schemas"
  196. fi
  197. # Gdk-pixbuf loaders
  198. export GDK_PIXBUF_MODULE_FILE="$SNAP_USER_COMMON/.cache/gdk-pixbuf-loaders.cache"
  199. export GDK_PIXBUF_MODULEDIR="$SNAP/usr/lib/$ARCH/gdk-pixbuf-2.0/2.10.0/loaders"
  200. if [ "$needs_update" = true ] || [ ! -f "$GDK_PIXBUF_MODULE_FILE" ]; then
  201. rm -f "$GDK_PIXBUF_MODULE_FILE"
  202. if [ -f "$SNAP/usr/lib/$ARCH/gdk-pixbuf-2.0/gdk-pixbuf-query-loaders" ]; then
  203. async_exec "$SNAP/usr/lib/$ARCH/gdk-pixbuf-2.0/gdk-pixbuf-query-loaders" > "$GDK_PIXBUF_MODULE_FILE"
  204. fi
  205. fi
  206. # shellcheck disable=SC2154
  207. if [ "$wayland_available" = true ]; then
  208. export GDK_BACKEND="wayland"
  209. launch_args+=(--ozone-platform-hint=auto)
  210. launch_args+=(--enable-features=WaylandWindowDecorations)
  211. fi
  212. append_dir GTK_PATH "$SNAP/usr/lib/$ARCH/gtk-3.0"
  213. append_dir GTK_PATH "$SNAP/usr/lib/gtk-3.0"
  214. # We don't have gtk libraries in this path but
  215. # enforcing this environment variable will disallow
  216. # gtk binaries like `gtk-query-immodules` to not search
  217. # in system default library paths.
  218. # Based on https://gitlab.gnome.org/GNOME/gtk/-/blob/main/gtk/gtkmodules.c#L104-136
  219. export GTK_EXE_PREFIX="$SNAP/usr"
  220. # ibus and fcitx integration
  221. GTK_IM_MODULE_DIR="$SNAP_USER_COMMON/.cache/immodules"
  222. export GTK_IM_MODULE_FILE="$GTK_IM_MODULE_DIR/immodules.cache"
  223. # shellcheck disable=SC2154
  224. if [ "$needs_update" = true ]; then
  225. rm -rf "$GTK_IM_MODULE_DIR"
  226. ensure_dir_exists "$GTK_IM_MODULE_DIR"
  227. ln -s "$SNAP"/usr/lib/"$ARCH"/gtk-3.0/3.0.0/immodules/*.so "$GTK_IM_MODULE_DIR"
  228. async_exec "$SNAP/usr/lib/$ARCH/libgtk-3-0/gtk-query-immodules-3.0" > "$GTK_IM_MODULE_FILE"
  229. fi
  230. # shellcheck disable=SC2154
  231. [ "$needs_update" = true ] && echo "SNAP_DESKTOP_LAST_REVISION=$SNAP_VERSION" > "$SNAP_USER_DATA/.last_revision"
  232. wait_for_async_execs
  233. binary="$1"
  234. shift
  235. exec "$binary" "${launch_args[@]}" "$@"