// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // lint-disable-file no-char-pointer /* ImsdMessages.Bus:Appearance — the system's appearance from the desktop portal's org.freedesktop.appearance settings on the session bus: the colour scheme (dark or light) and the accent colour, read at start and followed through SettingChanged; a flatpak may read them. Without a portal the appearance stays unknown and the UI keeps its light default; a portal that gave no colour scheme at the start is asked again when its name has an owner, and answers as a change. */ module; #include export module ImsdMessages.Bus:Appearance; import std; namespace ImsdMessages::Bus { constexpr const char* portalName = "org.freedesktop.portal.Desktop"; constexpr const char* portalPath = "/org/freedesktop/portal/desktop"; constexpr const char* settingsIface = "org.freedesktop.portal.Settings"; constexpr const char* appearanceNs = "org.freedesktop.appearance"; } export namespace ImsdMessages::Bus { struct Appearance { bool known = false; // the portal gave a colour scheme bool dark = false; // color-scheme 1 (prefer dark); 0 and 2 are light std::optional> accent; }; class AppearanceWatch { public: explicit AppearanceWatch(GDBusConnection* session) : conn(session) { if (!conn) return; // The subscription first, so a change between the read and it is // not lost. sub = g_dbus_connection_signal_subscribe(conn, portalName, settingsIface, "SettingChanged", portalPath, appearanceNs, G_DBUS_SIGNAL_FLAGS_NONE, OnChanged, this, nullptr); ReadAll(); // A portal that gave no colour scheme in the start's two seconds // — still starting (a start at login races it), or not on the // bus yet — is asked again when its name has an owner, as the // client watches imsd's. watch = g_bus_watch_name_on_connection(conn, portalName, G_BUS_NAME_WATCHER_FLAGS_NONE, OnAppeared, nullptr, this, nullptr); } ~AppearanceWatch() { if (watch) g_bus_unwatch_name(watch); if (conn && sub) g_dbus_connection_signal_unsubscribe(conn, sub); } AppearanceWatch(const AppearanceWatch&) = delete; AppearanceWatch& operator=(const AppearanceWatch&) = delete; std::function onChanged; const Appearance& Current() const noexcept { return current; } private: // One call for the whole namespace: ReadAll is in every portal version // (ReadOne came with version 2, and Read, deprecated, wraps its value // in one more variant), and one 2 s timeout is the most a start waits. void ReadAll() { GError* err = nullptr; GVariant* reply = g_dbus_connection_call_sync(conn, portalName, portalPath, settingsIface, "ReadAll", Namespaces(), G_VARIANT_TYPE("(a{sa{sv}})"), G_DBUS_CALL_FLAGS_NONE, 2000, nullptr, &err); if (!reply) { g_clear_error(&err); return; } Take(reply); } // The portal's name has an owner — at the start when it had one // already, later when it comes: without a colour scheme so far, the // settings are asked for without a wait, and the answer comes as a // change. static void OnAppeared(GDBusConnection*, const gchar*, const gchar*, gpointer data) { AppearanceWatch& self = *static_cast(data); if (self.current.known) return; g_dbus_connection_call(self.conn, portalName, portalPath, settingsIface, "ReadAll", Namespaces(), G_VARIANT_TYPE("(a{sa{sv}})"), G_DBUS_CALL_FLAGS_NONE, -1, nullptr, OnLateAnswer, &self); } static GVariant* Namespaces() { GVariantBuilder namespaces; g_variant_builder_init(&namespaces, G_VARIANT_TYPE("as")); g_variant_builder_add(&namespaces, "s", appearanceNs); return g_variant_new("(as)", &namespaces); } static void OnLateAnswer(GObject* source, GAsyncResult* result, gpointer data) { GVariant* reply = g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), result, nullptr); if (!reply) return; AppearanceWatch& self = *static_cast(data); self.Take(reply); if (self.onChanged) self.onChanged(self.current); } // ReadAll's answer: the namespace's settings, each applied. void Take(GVariant* reply) { GVariant* all = g_variant_get_child_value(reply, 0); if (GVariant* dict = g_variant_lookup_value(all, appearanceNs, G_VARIANT_TYPE("a{sv}"))) { GVariantIter it; g_variant_iter_init(&it, dict); const gchar* key = nullptr; GVariant* value = nullptr; while (g_variant_iter_loop(&it, "{&sv}", &key, &value)) Apply(key, Unwrapped(value)); g_variant_unref(dict); } g_variant_unref(all); g_variant_unref(reply); } // The value inside any variant layers (the portal's are single; the // deprecated Read's were double). static GVariant* Unwrapped(GVariant* value) { while (g_variant_is_of_type(value, G_VARIANT_TYPE_VARIANT)) value = g_variant_get_child_value(value, 0); return value; } void Apply(std::string_view key, GVariant* value) { if (key == "color-scheme" && g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32)) { current.known = true; current.dark = g_variant_get_uint32(value) == 1; } else if (key == "accent-color" && g_variant_is_of_type(value, G_VARIANT_TYPE("(ddd)"))) { double r = 0, g = 0, b = 0; g_variant_get(value, "(ddd)", &r, &g, &b); // Out of range means "no accent set" by the portal's definition. const bool valid = r >= 0 && r <= 1 && g >= 0 && g <= 1 && b >= 0 && b <= 1; current.accent = valid ? std::optional>({ static_cast(r), static_cast(g), static_cast(b) }) : std::nullopt; } } static void OnChanged(GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar*, GVariant* params, gpointer data) { AppearanceWatch& self = *static_cast(data); const gchar* ns = nullptr; const gchar* key = nullptr; GVariant* value = nullptr; g_variant_get(params, "(&s&sv)", &ns, &key, &value); if (ns && key && std::string_view(ns) == appearanceNs) { GVariant* inner = value; while (g_variant_is_of_type(inner, G_VARIANT_TYPE_VARIANT)) { GVariant* next = g_variant_get_variant(inner); g_variant_unref(inner); inner = next; } self.Apply(key, inner); value = inner; if (self.onChanged) self.onChanged(self.current); } if (value) g_variant_unref(value); } GDBusConnection* conn = nullptr; guint sub = 0; guint watch = 0; Appearance current; }; }