aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/scripts/PortalHyprland.sh
blob: 54ea71bb6a6069e34d579f1e09d9294db5b66b1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
# ==================================================
#  KoolDots (2026)
#  Project URL: https://github.com/LinuxBeginnings
#  License: GNU GPLv3
#  SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# For manually starting xdg-desktop-portal-hyprland

set -euo pipefail
is_ubuntu_family() {
  if [[ ! -r /etc/os-release ]]; then
    #return 1
    # It's been reported this also addresses
    # The xdg-desktop-portal failure when not using
    # UWSM on Arch
    exit 0
  fi

  # shellcheck disable=SC1091
  . /etc/os-release
  [[ "${ID:-}" == "ubuntu" ||
    "${ID:-}" == "linuxmint" ||
    "${ID:-}" == "zorin" ||
    "${ID:-}" == "rhino" ||
    "${ID_LIKE:-}" == *ubuntu* ]]
}

kill_quietly() {
  killall -q "$1" 2>/dev/null || true
}
wait_for_wayland() {
  local runtime_dir="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
  export XDG_RUNTIME_DIR="$runtime_dir"

  if [[ -n "${WAYLAND_DISPLAY:-}" && -S "$runtime_dir/$WAYLAND_DISPLAY" ]]; then
    return 0
  fi

  for _ in $(seq 1 120); do
    if [[ -n "${WAYLAND_DISPLAY:-}" && -S "$runtime_dir/$WAYLAND_DISPLAY" ]]; then
      return 0
    fi

    for socket in "$runtime_dir"/wayland-[0-9]*; do
      [[ -S "$socket" ]] || continue
      case "$(basename "$socket")" in
      *awww*) continue ;;
      esac
      export WAYLAND_DISPLAY="$(basename "$socket")"
      return 0
    done
    sleep 0.1
  done

  return 1
}

ensure_portal_env() {
  export XDG_CURRENT_DESKTOP="${XDG_CURRENT_DESKTOP:-Hyprland}"
  export XDG_SESSION_DESKTOP="${XDG_SESSION_DESKTOP:-Hyprland}"
  export XDG_SESSION_TYPE="${XDG_SESSION_TYPE:-wayland}"
  if [[ -f "/usr/share/glib-2.0/schemas/gschemas.compiled" ]]; then
    export GSETTINGS_SCHEMA_DIR="/usr/share/glib-2.0/schemas"
  fi

  local data_dirs="${XDG_DATA_DIRS:-}"
  if [[ -z "$data_dirs" ]]; then
    data_dirs="/usr/local/share:/usr/share"
  else
    if [[ ":$data_dirs:" != *":/usr/local/share:"* ]]; then
      data_dirs="/usr/local/share:$data_dirs"
    fi
    if [[ ":$data_dirs:" != *":/usr/share:"* ]]; then
      data_dirs="$data_dirs:/usr/share"
    fi
  fi
  export XDG_DATA_DIRS="$data_dirs"
}

start_portal_binary() {
  local description="$1"
  shift
  for candidate in "$@"; do
    if [[ -x "$candidate" ]]; then
      "$candidate" &
      return 0
    fi
  done
  echo "Warning: no $description binary found (checked: $*)" >&2
  return 1
}
stop_portal_processes() {
  kill_quietly xdg-desktop-portal-hyprland
  kill_quietly xdg-desktop-portal-wlr
  kill_quietly xdg-desktop-portal-gnome
  kill_quietly xdg-desktop-portal-gtk
  kill_quietly xdg-desktop-portal
}

restart_portal_via_systemd() {
  command -v systemctl >/dev/null 2>&1 || return 1
  ensure_portal_env
  if command -v dbus-update-activation-environment >/dev/null 2>&1; then
    dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP XDG_SESSION_TYPE XDG_DATA_DIRS GSETTINGS_SCHEMA_DIR >/dev/null 2>&1 || true
  fi

  systemctl --user import-environment WAYLAND_DISPLAY XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP XDG_SESSION_TYPE XDG_DATA_DIRS GSETTINGS_SCHEMA_DIR >/dev/null 2>&1 || true
  systemctl --user stop xdg-desktop-portal.service xdg-desktop-portal-hyprland.service xdg-desktop-portal-gtk.service >/dev/null 2>&1 || true
  stop_portal_processes
  sleep 0.5
  systemctl --user reset-failed xdg-desktop-portal-hyprland.service xdg-desktop-portal.service xdg-desktop-portal-gtk.service >/dev/null 2>&1 || true
  systemctl --user start graphical-session.target >/dev/null 2>&1 || true

  for _ in $(seq 1 30); do
    if systemctl --user is-active --quiet graphical-session.target; then
      break
    fi
    sleep 0.1
  done

  systemctl --user start xdg-desktop-portal-hyprland.service >/dev/null 2>&1 || true
  if is_ubuntu_family; then
    systemctl --user start xdg-desktop-portal-gtk.service >/dev/null 2>&1 || true
  fi

  for _ in $(seq 1 60); do
    if systemctl --user is-active --quiet xdg-desktop-portal-hyprland.service; then
      return 0
    fi
    sleep 0.1
  done
  return 1
}

restart_portal_manually() {
  sleep 1
  stop_portal_processes
  sleep 1

  start_portal_binary "xdg-desktop-portal-hyprland" \
    /usr/lib/xdg-desktop-portal-hyprland \
    /usr/libexec/xdg-desktop-portal-hyprland

  sleep 2

  if is_ubuntu_family; then
    start_portal_binary "xdg-desktop-portal-gtk" \
      /usr/lib/xdg-desktop-portal-gtk \
      /usr/libexec/xdg-desktop-portal-gtk || true
  fi

  start_portal_binary "xdg-desktop-portal" \
    /usr/lib/xdg-desktop-portal \
    /usr/libexec/xdg-desktop-portal
}

wait_for_wayland || true
if command -v systemctl >/dev/null 2>&1; then
  restart_portal_via_systemd || true
else
  restart_portal_manually
fi
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage