aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/scripts/WaybarStartup.sh
blob: e1306f7d544c5d012e3211747e8b04a6ee96bf22 (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
#!/usr/bin/env bash
# ==================================================
#  KoolDots (2026)
#  Project URL: https://github.com/LinuxBeginnings
#  License: GNU GPLv3
#  SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# Dedicated startup helper for Waybar.
# Handles both systemd user service setups and direct Waybar launch.

runtime_dir="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
export XDG_RUNTIME_DIR="$runtime_dir"
SCRIPTSDIR="${XDG_CONFIG_HOME:-$HOME/.config}/hypr/scripts"

is_waybar_running() {
    pgrep -x "waybar" >/dev/null 2>&1 || pgrep -x '\.waybar-wrapped' >/dev/null 2>&1
}

wait_for_waybar() {
    for _ in $(seq 1 80); do
        is_waybar_running && return 0
        sleep 0.1
    done
    return 1
}

wait_for_wayland() {
    if [ -n "${WAYLAND_DISPLAY:-}" ] && [ -S "$runtime_dir/$WAYLAND_DISPLAY" ]; then
        return 0
    fi
    for _ in $(seq 1 30); do
        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
}

sync_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
    if command -v systemctl >/dev/null 2>&1; then
        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
    fi
}

ensure_wallust_waybar_colors() {
    local colors_file="${XDG_CONFIG_HOME:-$HOME/.config}/waybar/wallust/colors-waybar.css"
    mkdir -p "$(dirname "$colors_file")" 2>/dev/null || true
    [ -f "$colors_file" ] || touch "$colors_file" 2>/dev/null || true
    if [ ! -s "$colors_file" ] && [ -x "$SCRIPTSDIR/WallustSwww.sh" ]; then
        "$SCRIPTSDIR/WallustSwww.sh" >/dev/null 2>&1 &
    fi
}

# Use systemd to start waybar only when waybar.service is explicitly enabled.
# Returns 1 on Debian/systems with no enabled service so the caller falls
# through to start_waybar_direct.
start_waybar_via_systemd() {
    command -v systemctl >/dev/null 2>&1 || return 1
    # Service must exist
    systemctl --user cat waybar.service >/dev/null 2>&1 || return 1
    # Service must be enabled (not just installed)
    local enabled_state
    enabled_state="$(systemctl --user is-enabled waybar.service 2>/dev/null || true)"
    case "$enabled_state" in
        enabled|static) ;;
        *) return 1 ;;
    esac
    systemctl --user start waybar.service >/dev/null 2>&1 || return 1
    wait_for_waybar
}

start_waybar_direct() {
    if command -v waybar >/dev/null 2>&1; then
        waybar >/dev/null 2>&1 &
        return 0
    fi
    if command -v .waybar-wrapped >/dev/null 2>&1; then
        .waybar-wrapped >/dev/null 2>&1 &
        return 0
    fi
    return 1
}

main() {
    # Brief wait so a systemd-managed waybar.service has time to appear
    # before we check; avoids a race on distros with an enabled unit.
    sleep 0.5
    is_waybar_running && exit 0
    wait_for_wayland || true
    sync_portal_env || true
    ensure_wallust_waybar_colors
    if start_waybar_via_systemd || start_waybar_direct; then
        exit 0
    fi
    exit 1
}

main
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage