// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® module; #include #include module Nostro.Ui.Shell:Portal_impl; import std; import Nostro.Ui; import :Portal; import :Loop; using namespace Nostro::Ui; using namespace Nostro::Ui::Shell; namespace { constexpr std::string_view portalName = "org.freedesktop.portal.Desktop"; constexpr std::string_view portalPath = "/org/freedesktop/portal/desktop"; constexpr std::string_view settingsInterface = "org.freedesktop.portal.Settings"; constexpr std::string_view openInterface = "org.freedesktop.portal.OpenURI"; constexpr std::string_view appearanceSettings = "org.freedesktop.appearance"; // The string a message has next; empty when it has none. std::string ReadString(sd_bus_message* message) { // sd-bus hands out a C string of the message's own. // lint-disable-next-line no-char-pointer const char* text = nullptr; if(sd_bus_message_read_basic(message, 's', &text) < 0 || text == nullptr) { return {}; } return text; } } Portal::Portal(Loop& loop, std::chrono::milliseconds patience) : loop(loop) { if(sd_bus_open_user(&bus) < 0) { bus = nullptr; return; } // The subscription before the read, so that a change between the two is not missed. sd_bus_match_signal(bus, &changed, portalName.data(), portalPath.data(), settingsInterface.data(), "SettingChanged", OnChanged, this); // One call for all of it: ReadAll is in every version of the portal. if(sd_bus_call_method_async(bus, &late, portalName.data(), portalPath.data(), settingsInterface.data(), "ReadAll", OnSettings, this, "as", 1, appearanceSettings.data()) >= 0) { // The bus is served by hand until the answer is in or the patience is out; the loop is not running yet. A // portal that is still starting, as at a login, answers later through the loop. const std::int64_t deadline = NowMs() + patience.count(); while(!answered && NowMs() < deadline) { const std::int32_t processed = sd_bus_process(bus, nullptr); if(processed < 0 || (processed == 0 && sd_bus_wait(bus, static_cast(deadline - NowMs()) * 1000) < 0)) { break; } } } sd_bus_attach_event(bus, loop.Event(), SD_EVENT_PRIORITY_NORMAL); } Portal::~Portal() { for(Opening& opening : openings) { sd_bus_slot_unref(opening.slot); } sd_bus_slot_unref(late); sd_bus_slot_unref(changed); sd_bus_flush_close_unref(bus); } bool Portal::Connected() const noexcept { return bus != nullptr; } const Appearance& Portal::Current() const noexcept { return current; } void Portal::Told() { if(onAppearance) { onAppearance(current); } } std::int32_t Portal::OnSettings(sd_bus_message* reply, void* data, sd_bus_error*) { Portal& self = *static_cast(data); self.answered = true; self.loop.Guard("reading the system's appearance", [&self, reply] { if(!sd_bus_message_is_method_error(reply, nullptr) && self.TakeSettings(reply)) { self.Told(); } }); return 0; } std::int32_t Portal::OnChanged(sd_bus_message* signal, void* data, sd_bus_error*) { Portal& self = *static_cast(data); self.loop.Guard("a change of the system's appearance", [&self, signal] { const std::string space = ReadString(signal); const std::string key = ReadString(signal); if(space == appearanceSettings && self.TakeSetting(key, signal)) { self.Told(); } }); return 0; } // ReadAll's answer: every setting of every namespace asked for, a{sa{sv}}. bool Portal::TakeSettings(sd_bus_message* reply) { bool any = false; if(sd_bus_message_enter_container(reply, 'a', "{sa{sv}}") <= 0) { return false; } while(sd_bus_message_enter_container(reply, 'e', "sa{sv}") > 0) { const std::string space = ReadString(reply); if(sd_bus_message_enter_container(reply, 'a', "{sv}") > 0) { while(sd_bus_message_enter_container(reply, 'e', "sv") > 0) { const std::string key = ReadString(reply); if(space == appearanceSettings) { any = TakeSetting(key, reply) || any; } else { sd_bus_message_skip(reply, "v"); } sd_bus_message_exit_container(reply); } sd_bus_message_exit_container(reply); } sd_bus_message_exit_container(reply); } sd_bus_message_exit_container(reply); return any; } // One setting, with the message at its value: a variant, in which the portal's older Read wrapped the value once more. bool Portal::TakeSetting(std::string_view key, sd_bus_message* value) { const Appearance before = current; std::int32_t depth = 0; char type = 0; // sd-bus hands out the signature as a C string of the message's own. // lint-disable-next-line no-char-pointer const char* contents = nullptr; while(sd_bus_message_peek_type(value, &type, &contents) > 0 && type == 'v' && sd_bus_message_enter_container(value, 'v', contents) >= 0) { depth++; } const std::string_view inside = contents != nullptr ? std::string_view(contents) : std::string_view(); if(key == "color-scheme" && type == 'u') { // 1 prefers dark; 0 has no preference and 2 prefers light. std::uint32_t scheme = 0; if(sd_bus_message_read_basic(value, 'u', &scheme) >= 0) { current.known = true; current.dark = scheme == 1; } } else if(key == "accent-color" && type == 'r' && inside == "ddd") { double red = 0; double green = 0; double blue = 0; if(sd_bus_message_read(value, "(ddd)", &red, &green, &blue) >= 0) { // Out of range is how the portal says there is none. const auto valid = [](double part) { return part >= 0 && part <= 1; }; if(valid(red) && valid(green) && valid(blue)) { current.accent = Color{static_cast(red), static_cast(green), static_cast(blue)}; } else { current.accent.reset(); } } } else { // Whatever it is, one value of it. sd_bus_message_skip(value, nullptr); } for(; depth > 0; depth--) { sd_bus_message_exit_container(value); } return !(current == before); } void Portal::OpenUri(std::string_view uri, std::function then) { Opening& opening = openings.emplace_back(); opening.portal = this; opening.then = std::move(then); const std::string address(uri); // No parent window, and no options: the desktop asks the user which program where it needs to. if(bus == nullptr || sd_bus_call_method_async(bus, &opening.slot, portalName.data(), portalPath.data(), openInterface.data(), "OpenURI", OnOpened, &opening, "ssa{sv}", "", address.c_str(), 0) < 0) { const std::function refused = std::move(opening.then); openings.pop_back(); if(refused) { refused(false); } } } std::int32_t Portal::OnOpened(sd_bus_message* reply, void* data, sd_bus_error*) { Opening& opening = *static_cast(data); Portal& self = *opening.portal; const bool taken = !sd_bus_message_is_method_error(reply, nullptr); const std::function then = std::move(opening.then); // sd-bus holds the slot while this runs, so it may be let go of here. sd_bus_slot_unref(opening.slot); self.openings.remove_if([&opening](const Opening& other) { return &other == &opening; }); if(then) { self.loop.Guard("opening an address", [&then, taken] { then(taken); }); } return 0; }