summary refs log tree commit diff
path: root/bin/luxnox-apply
blob: 16c824ce2fa181573a75ddb8cd33c597e1c97c3e (plain)
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
#!/bin/sh
# Point the desktop's per-mode files at lux or nox and reload what needs it.
# dwl runs this whenever the system color-scheme changes (and once at login).
#   luxnox-apply dark|light|nox|lux
#
# ~/.local/state/luxnox/current links to ~/.local/share/luxnox/<mode>; the
# apps' configs point into it (fuzzel include, swaync @import, wob.ini and
# swaylock/config links), so switching is one link.
case "$1" in
dark | nox) mode=nox ;;
light | lux) mode=lux ;;
*) echo "usage: luxnox-apply dark|light" >&2; exit 2 ;;
esac

share="${XDG_DATA_HOME:-$HOME/.local/share}/luxnox"
state="${XDG_STATE_HOME:-$HOME/.local/state}/luxnox"
[ -d "$share/$mode" ] || { echo "luxnox-apply: no $share/$mode (run make install in luxnox)" >&2; exit 1; }
mkdir -p "$state"

# GTK3 apps follow the theme setting live (GTK4/libadwaita follow the
# color-scheme by themselves). The plain user dconf database, as in
# luxnox-toggle.
theme=luxnox-dark
[ "$mode" = lux ] && theme=luxnox
env -u DCONF_PROFILE gsettings set org.gnome.desktop.interface gtk-theme "$theme"

# Qt: qt6ct watches its folder, so replacing qt6ct.conf (a new file renamed
# over it) repaints running Qt apps with the other palette. Icons too: Qt
# draws theme icons as they are (GTK recolours symbolic ones), so Qt uses
# Breeze in lux and Breeze Dark in nox.
qt="${XDG_CONFIG_HOME:-$HOME/.config}/qt6ct"
scheme="$qt/colors/luxnox-$mode.conf"
icons=breeze
[ "$mode" = nox ] && icons=breeze-dark
if [ -f "$qt/qt6ct.conf" ] && [ -f "$scheme" ] &&
	! { grep -qx "color_scheme_path=$scheme" "$qt/qt6ct.conf" &&
		grep -qx "icon_theme=$icons" "$qt/qt6ct.conf"; }; then
	tmp=$(mktemp "$qt/.qt6ct.conf.XXXXXX") &&
		sed -e "s|^color_scheme_path=.*|color_scheme_path=$scheme|" \
			-e "s|^icon_theme=.*|icon_theme=$icons|" "$qt/qt6ct.conf" >"$tmp" &&
		mv "$tmp" "$qt/qt6ct.conf"
fi

# Flatpak Qt apps (qBittorrent) use KDE's platform theme, which reads
# kdeglobals. Flatpak bind-mounts this file into the sandbox, which pins its
# inode, so overwrite it in place: a new file renamed over it would go unseen
# by running apps.
kdeglobals="${XDG_CONFIG_HOME:-$HOME/.config}/kdeglobals"
if [ -f "$share/$mode/kdeglobals" ] && ! cmp -s "$share/$mode/kdeglobals" "$kdeglobals"; then
	cat "$share/$mode/kdeglobals" >"$kdeglobals"
	# The theme doesn't watch the file: it rereads the palette on KDE's
	# notifyChange(PaletteChanged) signal. The Flatpak D-Bus proxy only passes
	# signals from names the app may talk to, so send it as
	# org.kde.KGlobalSettings, holding the name long enough for the proxy to
	# see who owns it. In the background: it takes a second.
	python3 -I - >/dev/null 2>&1 <<'EOF' &
import time
import gi
gi.require_version("Gio", "2.0")
from gi.repository import Gio, GLib
bus = Gio.bus_get_sync(Gio.BusType.SESSION)
got = bus.call_sync("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
                    "RequestName", GLib.Variant("(su)", ("org.kde.KGlobalSettings", 4)),
                    GLib.VariantType("(u)"), 0, -1, None).unpack()[0]
if got == 1:  # primary owner (no real KDE session holds it)
    time.sleep(0.5)
    bus.emit_signal(None, "/KGlobalSettings", "org.kde.KGlobalSettings", "notifyChange",
                    GLib.Variant("(ii)", (0, 0)))  # PaletteChanged
    bus.flush_sync(None)
    time.sleep(0.5)
EOF
fi

[ "$(readlink "$state/current")" = "$share/$mode" ] && exit 0
ln -sfn "$share/$mode" "$state/current"

# fuzzel and swaylock read their config each time they start.
# wob reads it once, so restart it (its socket stays open meanwhile).
systemctl --user try-restart wob.service 2>/dev/null
# swaync reloads its CSS, imports included.
pgrep -x swaync >/dev/null && swaync-client --reload-css >/dev/null 2>&1
exit 0