#!/usr/bin/env python3 """Fake org.freedesktop.portal.Settings for the smoke test. Owns org.freedesktop.portal.Desktop on the session bus and serves the org.freedesktop.appearance namespace with a fixed accent colour and colour scheme, like GNOME 47+ / KDE Plasma 6 do. fake-portal.py R G B [COLOR_SCHEME] (channels 0..1; scheme 0/1/2) Writing a line "R G B [SCHEME]" to stdin changes the values and emits SettingChanged, to exercise live updates. """ import sys import warnings warnings.filterwarnings("ignore", category=DeprecationWarning) from gi.repository import Gio, GLib XML = """ """ NS = "org.freedesktop.appearance" PATH = "/org/freedesktop/portal/desktop" IFACE = "org.freedesktop.portal.Settings" state = {} def values(): return { "accent-color": GLib.Variant("(ddd)", state["accent"]), "color-scheme": GLib.Variant("u", state["scheme"]), } def parse(args): r, g, b = (float(x) for x in args[:3]) return (r, g, b), int(args[3]) if len(args) > 3 else 0 def on_call(conn, sender, path, iface, method, params, inv): if method == "ReadAll": (namespaces,) = params.unpack() want = not namespaces or any( n == NS or (n.endswith("*") and NS.startswith(n[:-1])) or n == "" for n in namespaces ) inv.return_value(GLib.Variant("(a{sa{sv}})", ({NS: values()} if want else {},))) return ns, key = params.unpack() v = values().get(key) if ns == NS else None if v is None: inv.return_dbus_error("org.freedesktop.portal.Error.NotFound", "no such setting") elif method == "Read": # version 1 wraps the value in another variant inv.return_value(GLib.Variant("(v)", (GLib.Variant("v", v),))) else: inv.return_value(GLib.Variant("(v)", (v,))) def on_get(conn, sender, path, iface, prop): return GLib.Variant("u", 2) def main(): state["accent"], state["scheme"] = parse(sys.argv[1:]) loop = GLib.MainLoop() bus = Gio.bus_get_sync(Gio.BusType.SESSION) info = Gio.DBusNodeInfo.new_for_xml(XML).interfaces[0] bus.register_object(PATH, info, on_call, on_get, None) def on_stdin(ch, cond): line = ch.readline() if not line: return False state["accent"], state["scheme"] = parse(line.split()) for key, v in values().items(): bus.emit_signal(None, PATH, IFACE, "SettingChanged", GLib.Variant("(ssv)", (NS, key, v))) return True GLib.io_add_watch(GLib.IOChannel.unix_new(0), GLib.IO_IN | GLib.IO_HUP, on_stdin) Gio.bus_own_name_on_connection( bus, "org.freedesktop.portal.Desktop", Gio.BusNameOwnerFlags.NONE, lambda *_: print("fake portal: ready", flush=True), lambda *_: (print("fake portal: name lost", flush=True), loop.quit())) loop.run() if __name__ == "__main__": main()