Files
moonmix/tools/fake-portal.py
T
nandi 1746884744
CI / check (push) Successful in 9s
CI / native (push) Successful in 1m14s
Follow the desktop accent colour and colour scheme (XDG portal) (#3)
2026-11-23 01:56:20 +00:00

115 lines
3.6 KiB
Python
Executable File

#!/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 = """
<node>
<interface name="org.freedesktop.portal.Settings">
<method name="ReadAll">
<arg type="as" name="namespaces" direction="in"/>
<arg type="a{sa{sv}}" name="value" direction="out"/>
</method>
<method name="Read">
<arg type="s" name="namespace" direction="in"/>
<arg type="s" name="key" direction="in"/>
<arg type="v" name="value" direction="out"/>
</method>
<method name="ReadOne">
<arg type="s" name="namespace" direction="in"/>
<arg type="s" name="key" direction="in"/>
<arg type="v" name="value" direction="out"/>
</method>
<signal name="SettingChanged">
<arg type="s" name="namespace"/>
<arg type="s" name="key"/>
<arg type="v" name="value"/>
</signal>
<property name="version" type="u" access="read"/>
</interface>
</node>
"""
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()