aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/scripts/Add-override-Hyprland-Portal.sh
blob: 1f321d51b6a276668bf2275fb9075b9a732c76fd (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env bash
# ==================================================
#  KoolDots (2026)
#  Project URL: https://github.com/LinuxBeginnings
#  License: GNU GPLv3
#  SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# 💫 https://github.com/LinuxBeginnings 💫 #
# Add/revert status-aware portal override for Hyprland #

set -euo pipefail

SCRIPT_NAME="$(basename "$0")"
DRY_RUN=0
ACTION="apply"

OVERRIDE_DIR="$HOME/.config/systemd/user/xdg-desktop-portal.service.d"
OVERRIDE_FILE="$OVERRIDE_DIR/override.conf"
PORTAL_UNITS=(
  "xdg-desktop-portal.service"
  "xdg-desktop-portal-hyprland.service"
  "xdg-desktop-portal-gtk.service"
)

OS_PRETTY_NAME="Unknown"
OS_ID="unknown"
OS_VERSION_ID="unknown"

usage() {
  cat <<EOF
Usage: ${SCRIPT_NAME} [OPTIONS]

Install/revert a user systemd override for xdg-desktop-portal on Hyprland
and validate portal service health.

Default action (no options):
  - report OS + current status
  - install override if missing
  - restart portal units
  - validate and report success/failure

Options:
  -h, --help       Show this help message and exit
  -d, --dry-run    Print planned actions only (no changes made)
  -r, --r, --revert
                   Remove override, restart, then validate (for upstream-fixed systems)
  -s, --status     Print status only (includes: override_installed=true|false)
EOF
}

info() {
  printf "[INFO] %s\n" "$1"
}

warn() {
  printf "[WARN] %s\n" "$1"
}

error() {
  printf "[ERROR] %s\n" "$1" >&2
}

fail() {
  error "$1"
  exit 1
}

set_action() {
  local requested="$1"
  if [[ "$ACTION" != "apply" && "$ACTION" != "$requested" ]]; then
    fail "Conflicting options: cannot combine '$ACTION' with '$requested'."
  fi
  ACTION="$requested"
}

format_command() {
  printf "%q " "$@"
}

run_cmd() {
  if ((DRY_RUN)); then
    info "[dry-run] $(format_command "$@")"
    return 0
  fi
  "$@"
}

run_cmd_allow_fail() {
  if ((DRY_RUN)); then
    info "[dry-run] $(format_command "$@")"
    return 0
  fi
  "$@" || true
}

detect_os() {
  if [[ -r /etc/os-release ]]; then
    # shellcheck disable=SC1091
    . /etc/os-release
    OS_PRETTY_NAME="${PRETTY_NAME:-${NAME:-Unknown}}"
    OS_ID="${ID:-unknown}"
    OS_VERSION_ID="${VERSION_ID:-unknown}"
  fi
}

is_ubuntu_family() {
  [[ "$OS_ID" == "ubuntu" ||
    "$OS_ID" == "linuxmint" ||
    "$OS_ID" == "zorin" ||
    "$OS_ID" == "rhino" ||
    "${ID_LIKE:-}" == *ubuntu* ]]
}

require_user_systemd() {
  command -v systemctl >/dev/null 2>&1 || fail "systemctl is required but was not found."
  systemctl --user show-environment >/dev/null 2>&1 || fail "Cannot contact systemd user manager."
}

unit_load_state() {
  systemctl --user show --property=LoadState --value "$1" 2>/dev/null || true
}

unit_exists() {
  local load_state
  load_state="$(unit_load_state "$1")"
  [[ -n "$load_state" && "$load_state" != "not-found" ]]
}

unit_active_state() {
  local state
  state="$(systemctl --user is-active "$1" 2>/dev/null || true)"
  [[ -n "$state" ]] || state="unknown"
  printf "%s" "$state"
}

render_override_content() {
  cat <<'EOF'
[Unit]
Requisite=
Requires=graphical-session.target
After=graphical-session.target
EOF
}

is_override_installed() {
  [[ -f "$OVERRIDE_FILE" ]] || return 1
  grep -Fxq "[Unit]" "$OVERRIDE_FILE" &&
    grep -Fxq "Requisite=" "$OVERRIDE_FILE" &&
    grep -Fxq "Requires=graphical-session.target" "$OVERRIDE_FILE" &&
    grep -Fxq "After=graphical-session.target" "$OVERRIDE_FILE"
}

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"
}

sync_activation_env() {
  if command -v dbus-update-activation-environment >/dev/null 2>&1; then
    run_cmd_allow_fail dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP XDG_SESSION_TYPE XDG_DATA_DIRS GSETTINGS_SCHEMA_DIR
  fi
  run_cmd_allow_fail systemctl --user import-environment WAYLAND_DISPLAY XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP XDG_SESSION_TYPE XDG_DATA_DIRS GSETTINGS_SCHEMA_DIR
}

stop_stale_portal_processes() {
  local uid
  uid="$(id -u)"
  run_cmd_allow_fail pkill -u "$uid" -f '/xdg-desktop-portal-hyprland$'
  run_cmd_allow_fail pkill -u "$uid" -f '/xdg-desktop-portal-gtk$'
  run_cmd_allow_fail pkill -u "$uid" -f '/xdg-desktop-portal$'
}

restart_portals() {
  ensure_portal_env
  sync_activation_env

  run_cmd systemctl --user daemon-reload

  local unit
  for unit in "${PORTAL_UNITS[@]}"; do
    if unit_exists "$unit"; then
      run_cmd_allow_fail systemctl --user stop "$unit"
    fi
  done

  stop_stale_portal_processes

  for unit in "${PORTAL_UNITS[@]}"; do
    if unit_exists "$unit"; then
      run_cmd_allow_fail systemctl --user reset-failed "$unit"
    fi
  done

  run_cmd_allow_fail systemctl --user start graphical-session.target

  if unit_exists "xdg-desktop-portal-hyprland.service"; then
    run_cmd_allow_fail systemctl --user start xdg-desktop-portal-hyprland.service
  else
    warn "xdg-desktop-portal-hyprland.service is not installed."
  fi

  if is_ubuntu_family && unit_exists "xdg-desktop-portal-gtk.service"; then
    run_cmd_allow_fail systemctl --user start xdg-desktop-portal-gtk.service
  fi

  if unit_exists "xdg-desktop-portal.service"; then
    run_cmd_allow_fail systemctl --user start xdg-desktop-portal.service
  fi
}

install_override() {
  if is_override_installed; then
    info "Override already installed at $OVERRIDE_FILE."
    return 0
  fi

  if ((DRY_RUN)); then
    info "[dry-run] Would install override at $OVERRIDE_FILE"
    render_override_content | sed 's/^/[dry-run]   /'
    return 0
  fi

  mkdir -p "$OVERRIDE_DIR"
  render_override_content > "$OVERRIDE_FILE"
  info "Installed override at $OVERRIDE_FILE."
}

revert_override() {
  if [[ ! -f "$OVERRIDE_FILE" ]]; then
    info "No override file found at $OVERRIDE_FILE."
    return 0
  fi

  run_cmd rm -f "$OVERRIDE_FILE"
  if [[ -d "$OVERRIDE_DIR" ]] && [[ -z "$(ls -A "$OVERRIDE_DIR" 2>/dev/null)" ]]; then
    run_cmd rmdir "$OVERRIDE_DIR"
  fi
  info "Removed override from $OVERRIDE_FILE."
}

portal_ping_ok() {
  if command -v gdbus >/dev/null 2>&1; then
    gdbus call --session \
      --dest org.freedesktop.portal.Desktop \
      --object-path /org/freedesktop/portal/desktop \
      --method org.freedesktop.DBus.Peer.Ping >/dev/null 2>&1
    return $?
  fi

  if command -v busctl >/dev/null 2>&1; then
    busctl --user call org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop org.freedesktop.DBus.Peer Ping >/dev/null 2>&1
    return $?
  fi

  return 1
}

print_status_report() {
  local override_value="false"
  if is_override_installed; then
    override_value="true"
  fi

  printf "os_pretty_name=%s\n" "$OS_PRETTY_NAME"
  printf "os_id=%s\n" "$OS_ID"
  printf "os_version_id=%s\n" "$OS_VERSION_ID"
  printf "override_file=%s\n" "$OVERRIDE_FILE"
  printf "override_installed=%s\n" "$override_value"
  printf "graphical_session_target=%s\n" "$(unit_active_state "graphical-session.target")"
  printf "xdg_desktop_portal_hyprland=%s\n" "$(unit_active_state "xdg-desktop-portal-hyprland.service")"
  printf "xdg_desktop_portal=%s\n" "$(unit_active_state "xdg-desktop-portal.service")"
}

validate_portal() {
  local hypr_state portal_state ping_ok
  hypr_state="$(unit_active_state "xdg-desktop-portal-hyprland.service")"
  portal_state="$(unit_active_state "xdg-desktop-portal.service")"
  ping_ok="false"

  if portal_ping_ok; then
    ping_ok="true"
  fi

  printf "portal_ping_ok=%s\n" "$ping_ok"

  if [[ "$hypr_state" == "active" && ( "$portal_state" == "active" || "$ping_ok" == "true" ) ]]; then
    return 0
  fi
  return 1
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    -h|--help)
      usage
      exit 0
      ;;
    -d|--dry-run)
      DRY_RUN=1
      ;;
    -r|--r|--revert)
      set_action "revert"
      ;;
    -s|--status)
      set_action "status"
      ;;
    *)
      fail "Unknown option: $1 (use -h or --help)."
      ;;
  esac
  shift
done

detect_os
require_user_systemd

info "Detected OS: ${OS_PRETTY_NAME} (id=${OS_ID}, version=${OS_VERSION_ID})"

case "$ACTION" in
  status)
    print_status_report
    exit 0
    ;;
  apply)
    print_status_report
    install_override
    restart_portals
    print_status_report
    if validate_portal; then
      info "Portal override workflow completed successfully."
      exit 0
    fi
    error "Portal validation failed after applying override."
    exit 1
    ;;
  revert)
    print_status_report
    revert_override
    restart_portals
    print_status_report
    if validate_portal; then
      info "Revert completed and portal validation succeeded."
      exit 0
    fi
    error "Revert completed but portal validation failed (upstream fix may not be present yet)."
    exit 1
    ;;
esac
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage