Commit 116fe5a1 authored by Roman Alifanov's avatar Roman Alifanov

service: rewrite service code in ContenT language

parent e724dd85
#!/bin/bash
# shellcheck source=/usr/bin/shell-config
source shell-config
CONFIG_FILE="/etc/ximper-unified-theme-switcher/themes"
HOME_CONFIG_DIR="$HOME/.config/ximper-unified-theme-switcher"
HOME_CONFIG_FILE="$HOME_CONFIG_DIR/themes"
FORCE_FILE_REACTION=false
# Функция для создания конфига в домашнем каталоге
create_home_config() {
mkdir -p "$HOME_CONFIG_DIR"
touch "$HOME_CONFIG_FILE"
create_default_config_values "$HOME_CONFIG_FILE"
echo "$HOME_CONFIG_FILE"
}
create_default_config_values() {
local CFG_PATH
CFG_PATH="$1"
if [ -w "$CFG_PATH" ]; then
if [ -z "$(shell_config_get "$CFG_PATH" KV_DARK_THEME)" ]; then
shell_config_set "$CFG_PATH" CURRENT_THEME "dark"
fi
if [ -z "$(shell_config_get "$CFG_PATH" KV_DARK_THEME)" ]; then
shell_config_set "$CFG_PATH" KV_LIGHT_THEME "KvGnome"
shell_config_set "$CFG_PATH" KV_DARK_THEME "KvGnomeDark"
fi
if [ -z "$(shell_config_get "$CFG_PATH" GTK3_DARK_THEME)" ]; then
shell_config_set "$CFG_PATH" GTK3_LIGHT_THEME "adw-gtk3"
shell_config_set "$CFG_PATH" GTK3_DARK_THEME "adw-gtk3-dark"
fi
fi
}
cfg_check() {
# Проверка наличия конфига в /etc
if [ -f "$CONFIG_FILE" ]; then
if [ -f "$HOME_CONFIG_FILE" ]; then
echo "$HOME_CONFIG_FILE"
else
echo "$CONFIG_FILE"
fi
elif [ -f "$HOME_CONFIG_FILE" ]; then
echo "$HOME_CONFIG_FILE"
else
create_home_config
fi
}
check_gsettings_schema() {
local SCHEMA="org.gnome.desktop.interface"
local KEY="color-scheme"
if gsettings list-schemas | grep -q "$SCHEMA"; then
if gsettings list-keys "$SCHEMA" | grep -q "$KEY"; then
echo "Schema '$SCHEMA' and KEY '$KEY' exist."
return 0
else
echo "Schema '$SCHEMA' exists, but KEY '$KEY' not found."
return 1
fi
else
echo "Schema '$SCHEMA' not found."
return 1
fi
}
update_kv_theme() {
echo "Updating Kvantum theme to $1..."
echo "Cfg path: $2..."
if [ "$1" == "light" ]; then
kvantummanager --set "$(shell_config_get "$2" KV_LIGHT_THEME)"
elif [ "$1" == "dark" ]; then
kvantummanager --set "$(shell_config_get "$2" KV_DARK_THEME)"
fi
}
update_gtk3_theme() {
echo "Updating GTK3 theme to $1..."
echo "Cfg path: $2..."
if [ "$1" == "light" ]; then
gsettings set org.gnome.desktop.interface gtk-theme "$(shell_config_get "$2" GTK3_LIGHT_THEME)"
elif [ "$1" == "dark" ]; then
gsettings set org.gnome.desktop.interface gtk-theme "$(shell_config_get "$2" GTK3_DARK_THEME)"
fi
}
# Function to check and update the theme
check_and_update_themes() {
local NEW_THEME
NEW_THEME="$1"
local CFG_PATH
CFG_PATH=$(cfg_check)
create_default_config_values
shell_config_set "$CFG_PATH" CURRENT_THEME "$NEW_THEME"
case "$NEW_THEME" in
"default"|"light"|"prefer-light")
update_kv_theme "light" "$CFG_PATH"
update_gtk3_theme "light" "$CFG_PATH"
;;
"dark"|"prefer-dark")
update_kv_theme "dark" "$CFG_PATH"
update_gtk3_theme "dark" "$CFG_PATH"
;;
esac
}
OPTS=$(getopt -o h --long help,force-file-reaction -- "$@") || {
echo "Ошибка обработки опций."
exit 1
}
eval set -- "$OPTS"
while true; do
case "$1" in
-h|--help)
echo "Usage: $0 [options]"
echo "Options:"
echo " --help Show this help message"
echo " --force-file-reaction Force reaction to config file changes"
exit 0
;;
--force-file-reaction)
FORCE_FILE_REACTION=true
shift
;;
--)
shift
break
;;
*)
echo "Неизвестный аргумент: $1"
exit 1
;;
esac
done
main() {
if [ "$FORCE_FILE_REACTION" = true ] || ! check_gsettings_schema; then
echo "reaction mode: after changes in config file"
create_home_config
local CONFIG_FILE
CONFIG_FILE=$(cfg_check)
while true; do
inotifywait -e attrib "$CONFIG_FILE" | \
while read -r directory events filename; do
NEW_THEME=$(shell_config_get "$CONFIG_FILE" CURRENT_THEME)
if [ -n "$NEW_THEME" ]; then
echo "Config file updated: $NEW_THEME"
check_and_update_themes "$NEW_THEME"
fi
done
done
else
echo "reaction mode: after dbus signal"
dbus-monitor "interface='org.freedesktop.portal.Settings',member=SettingChanged" | \
while IFS= read -r LINE; do
local THEME
local NEW_THEME
local CURRENT_TIME
local LAST_UPDATE
THEME=$(echo "$LINE" | grep -E -o 'string "(prefer-dark|default)"')
NEW_THEME=$(gsettings get org.gnome.desktop.interface color-scheme | awk -F"'" '{print $2}')
CURRENT_TIME=$(date +%s%3N)
if [ -n "$THEME" ] && [ $((CURRENT_TIME - LAST_UPDATE)) -ge 150 ]; then
LAST_UPDATE=$CURRENT_TIME
echo "D-Bus signal detected: $THEME"
check_and_update_themes "$NEW_THEME"
fi
done
fi
}
main
\ No newline at end of file
prefix = get_option('prefix')
sysconfdir = join_paths(prefix, get_option('sysconfdir'))
install_data(
'./bin/ximper-unified-theme-switcher-service',
content = find_program('content')
service_ct_sources = files(
'src/config.ct',
'src/kvantum.ct',
'src/gtk3.ct',
'src/themes.ct',
'src/main.ct',
)
service_bin = custom_target(
'ximper-unified-theme-switcher-service',
input: service_ct_sources,
output: 'ximper-unified-theme-switcher-service',
command: [content, 'build', '@INPUT@', '-o', '@OUTPUT@'],
install: true,
install_dir: get_option('bindir'),
install_mode: 'r-xr-xr-x'
install_mode: 'r-xr-xr-x',
)
install_data(
'./data/ximper-unified-theme-switcher.service',
install_dir: join_paths(prefix, 'lib', 'systemd', 'user')
......
busing shell_config = "/usr/bin/shell-config"
CONFIG_FILE = "/etc/ximper-unified-theme-switcher/themes"
HOME_CONFIG_DIR = env.HOME .. "/.config/ximper-unified-theme-switcher"
HOME_CONFIG_FILE = HOME_CONFIG_DIR .. "/themes"
func create_home_config (): string {
fs.mkdir (HOME_CONFIG_DIR)
touch (HOME_CONFIG_FILE)
if fs.exists (CONFIG_FILE) {
cp (CONFIG_FILE, HOME_CONFIG_FILE)
}
return HOME_CONFIG_FILE
}
func cfg_check (): string {
if fs.exists (CONFIG_FILE) {
if fs.exists (HOME_CONFIG_FILE) {
return HOME_CONFIG_FILE
} else {
return CONFIG_FILE
}
} else if fs.exists (HOME_CONFIG_FILE) {
return HOME_CONFIG_FILE
} else {
return create_home_config ()
}
}
func cfg_get (cfg_path: string, key: string): string {
return shell_config.shell_config_get (cfg_path, key)
}
func cfg_set (cfg_path: string, key: string, value: string) {
shell_config.shell_config_set (cfg_path, key, value)
}
GTK3_SCHEMA = "org.gnome.desktop.interface"
func check_gsettings_schema (): bool {
schemas = gsettings ("list-schemas")
if regex.match (schemas, GTK3_SCHEMA) {
keys = gsettings ("list-keys", GTK3_SCHEMA)
if regex.match (keys, "color-scheme") {
return true
}
}
return false
}
class Gtk3Theme {
DEFAULT_LIGHT = "adw-gtk3"
DEFAULT_DARK = "adw-gtk3-dark"
func is_available (): bool {
return check_gsettings_schema ()
}
func init_defaults (cfg_path: string) {
if is_empty (cfg_get (cfg_path, "GTK3_DARK_THEME")) {
cfg_set (cfg_path, "GTK3_LIGHT_THEME", this.DEFAULT_LIGHT)
cfg_set (cfg_path, "GTK3_DARK_THEME", this.DEFAULT_DARK)
}
}
func update (mode: string, cfg_path: string) {
print ("Updating GTK3 theme to {mode}...")
if mode == "light" {
theme = cfg_get (cfg_path, "GTK3_LIGHT_THEME")
gsettings ("set", GTK3_SCHEMA, "gtk-theme", theme)
} else if mode == "dark" {
theme = cfg_get (cfg_path, "GTK3_DARK_THEME")
gsettings ("set", GTK3_SCHEMA, "gtk-theme", theme)
}
}
}
class KvantumTheme {
DEFAULT_LIGHT = "KvGnome"
DEFAULT_DARK = "KvGnomeDark"
func is_available (): bool {
result = command ("-v", "kvantummanager")
if is_empty (result) {
return false
}
return true
}
func init_defaults (cfg_path: string) {
if is_empty (cfg_get (cfg_path, "KV_DARK_THEME")) {
cfg_set (cfg_path, "KV_LIGHT_THEME", this.DEFAULT_LIGHT)
cfg_set (cfg_path, "KV_DARK_THEME", this.DEFAULT_DARK)
}
}
func update (mode: string, cfg_path: string) {
print ("Updating Kvantum theme to {mode}...")
if mode == "light" {
theme = cfg_get (cfg_path, "KV_LIGHT_THEME")
kvantummanager ("--set", theme)
} else if mode == "dark" {
theme = cfg_get (cfg_path, "KV_DARK_THEME")
kvantummanager ("--set", theme)
}
}
}
FORCE_FILE_REACTION = false
func check_and_update_themes (new_theme: string) {
cfg_path = cfg_check ()
themes_init_defaults (cfg_path)
cfg_set (cfg_path, "CURRENT_THEME", new_theme)
mode = ""
when new_theme {
"default", "light", "prefer-light" {
mode = "light"
}
"dark", "prefer-dark" {
mode = "dark"
}
}
if mode != "" {
themes_update (mode, cfg_path)
}
}
func run_file_reaction () {
print ("reaction mode: after changes in config file")
create_home_config ()
config_path = cfg_check ()
themes_init_defaults (config_path)
while true {
inotifywait ("-e", "attrib", config_path)
new_theme = cfg_get (config_path, "CURRENT_THEME")
if !is_empty (new_theme) {
print ("Config file updated: {new_theme}")
check_and_update_themes (new_theme)
}
}
}
func run_dbus_reaction () {
print ("reaction mode: after dbus signal")
last_update = 0
proc = async dbus-monitor ("interface='org.freedesktop.portal.Settings',member=SettingChanged")
foreach line in proc {
theme = echo (line) | grep ("-E", "-o", "string \"(prefer-dark|default)\"")
if !is_empty (theme) {
current_time = date ("+%s%3N")
diff = current_time - last_update
if diff >= 150 {
last_update = current_time
new_theme = gsettings ("get", "org.gnome.desktop.interface", "color-scheme") | awk ("-F'", "\{print $2\}")
print ("D-Bus signal detected: {theme}")
check_and_update_themes (new_theme)
}
}
}
}
# --- CLI ---
if args.count () > 0 {
foreach i in range (args.count ()) {
arg = args.get (i)
when arg {
"-h", "--help" {
print ("Usage: ximper-unified-theme-switcher-service [options]")
print ("Options:")
print (" --help Show this help message")
print (" --force-file-reaction Force reaction to config file changes")
exit (0)
}
"--force-file-reaction" {
FORCE_FILE_REACTION = true
}
}
}
}
if FORCE_FILE_REACTION || !check_gsettings_schema () {
run_file_reaction ()
} else {
run_dbus_reaction ()
}
# Реестр тем — добавьте новый экземпляр в массив
kv_theme = KvantumTheme ()
gtk_theme = Gtk3Theme ()
THEMES = [kv_theme, gtk_theme]
func themes_init_defaults (cfg_path: string) {
foreach t in THEMES {
t.init_defaults (cfg_path)
}
}
func themes_update (mode: string, cfg_path: string) {
foreach t in THEMES {
if t.is_available () {
t.update (mode, cfg_path)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment