Nostro/interfaces/Nostro.Ui.Shell-Loop.cppm

164 lines
6.8 KiB
Text
Raw Permalink Normal View History

feat(app): add the Nostro app, a window for phones and desktops Linux phones have no banking apps, and a command line is no answer on a phone. The app does everything the nostro command does and, like it, only talks to nostrod: it holds no key and no token. - Nostro.Ui, a toolkit whose screens are functions that fill a frame, ported from imsd-messages ca5cd72d; Nostro.App, the app's state, forms and screens; Nostro.Ui.Shell, which puts them on a Wayland window with Crafter.Graphics; nostro-app; and Nostro.DebugCommands for the --debug-api build. - Client: every method also in an asynchronous form for an sd-event loop, with DaemonMissing and OutcomeUnknown. - nostrod's Flatpak is net.catcrafts.Nostrod now and the app takes net.catcrafts.Nostro; install.sh moves the state. The app's Flatpak, both Flatpaks cross-built for aarch64, and the licence texts they carry. - nostro-fake-system, a made-up bus and bank with a real nostrod on them, to look at the app against. The README's screenshots are taken on it. - The nostro command: an API key's environment is the key's own to say, so --sandbox is for bunq-oauth alone, and a virtual card is listed as VIRTUAL. The published contract changes in three places, before any release (design rule 10): - Transactions' query takes `stored`: answered from what nostrod has stored, without asking the bank. The app searches with it; before, every search was a call to bunq. - AddBunq's `environment` no longer chooses over what the key says of itself: a key of the other environment is refused. - SetCardStatus refuses to freeze or unfreeze a virtual card. bunq reports one as DEACTIVATED however well it pays, and leaves it as it is when asked, without an error. The app is GPL-3.0-only, the library stays LGPL-3.0-only, and no code moved from one to the other (design rule 13). Transaction's schema is unchanged. Verified: the 17 tests pass with every line, function and region of what they link run, and the five lint runs are clean. Every screen was looked at on the made-up system, at a phone's and a desktop's sizes in the light and the dark look, on Plasma under Wayland. What is said of bunq's cards comes from its sandbox, with throwaway users, and from one real virtual card. Known to have been used against production: connecting, and reading accounts, payments and cards. Not known to have been tried: a payment from the app against bunq, OAuth against bunq, and anything on a phone. Building the app takes --graphics=, a Crafter.Graphics checkout on its branch claude/nostro-app, until that branch is merged and pinned here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 12:27:14 +01:00
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
module;
#include <systemd/sd-event.h>
#include <wayland-client.h>
export module Nostro.Ui.Shell:Loop;
import std;
// One thread and one sd-event loop serve everything the app waits for: the compositor, nostrod's answers on the
// system bus, the portal on the session bus, and every timer. The window library's own loop (Window::StartSync) serves
// the compositor alone and would starve the buses, so the Wayland display is a source of this loop instead, and the
// app asks for its own frames: a frame is drawn when the compositor says the window is about to be shown, and only
// when something asked for one. An idle window, a hidden one and a locked phone draw nothing.
//
// A callback sd-event or libwayland calls must not throw: the exception would unwind through C. Every callback here
// runs through Loop::Guard, which turns what is thrown into a line on stderr and the loop's end with status 1.
export namespace Nostro::Ui::Shell {
// The clock timers are set by and frames are dated with: milliseconds, monotonic.
std::int64_t NowMs() noexcept;
class Loop {
public:
// Blocks SIGINT and SIGTERM for the process, which is why a Loop is made before anything starts a thread, and
// ends the loop with status 0 when one arrives.
Loop();
~Loop();
Loop(const Loop&) = delete;
Loop& operator=(const Loop&) = delete;
sd_event* Event() const noexcept;
// Runs until Exit; returns what Exit was given.
std::int32_t Run();
void Exit(std::int32_t status) noexcept;
// Runs `body`. What it throws is said on stderr, with `what` for what was being done, and ends the loop.
void Guard(std::string_view what, const std::function<void()>& body) noexcept;
// Called after every source of the loop that ran, whoever's it was, before the loop waits again: where the
// window looks at what the callbacks left to do.
std::function<void()> onSettle;
// Where Guard says what was thrown; stderr without one.
std::function<void(std::string_view line)> onError;
private:
static std::int32_t OnPost(sd_event_source* source, void* data);
void Failed(std::string_view line) noexcept;
sd_event* event = nullptr;
sd_event_source* post = nullptr;
};
// Calls back once at a time on the loop's clock, until it is set again.
class Timer {
public:
Timer(Loop& loop, std::function<void()> fire);
~Timer();
Timer(const Timer&) = delete;
Timer& operator=(const Timer&) = delete;
// At that time by NowMs, at once when it has passed; replaces the time set before.
void At(std::int64_t ms);
void After(std::int64_t ms);
void Cancel() noexcept;
bool Armed() const noexcept;
private:
static std::int32_t OnTime(sd_event_source* source, std::uint64_t usec, void* data);
Loop& loop;
std::function<void()> fire;
sd_event_source* source = nullptr;
bool armed = false;
};
// Calls back once from the loop itself, soon: for what must not run inside the callback that asks for it.
class Defer {
public:
Defer(Loop& loop, std::function<void()> run);
~Defer();
Defer(const Defer&) = delete;
Defer& operator=(const Defer&) = delete;
void Schedule() noexcept;
void Cancel() noexcept;
private:
static std::int32_t OnDefer(sd_event_source* source, void* data);
Loop& loop;
std::function<void()> run;
sd_event_source* source = nullptr;
};
// The compositor's connection as a source of the loop.
//
// libwayland wants a reader to announce itself (prepare_read), wait for the socket, and then read or step back, and
// two readers on one thread deadlock: the second waits for the first to read. The window library and the Vulkan
// driver both read the display themselves, from inside this thread's callbacks (a clipboard read, a swapchain
// waiting for the compositor). So no announced read here outlives a callback: when the socket is readable, one
// callback announces, reads and dispatches; and before the loop waits, what such a foreign read left queued for
// this thread is dispatched and what was written is sent.
class WaylandSource {
public:
WaylandSource(Loop& loop, wl_display* display);
~WaylandSource();
WaylandSource(const WaylandSource&) = delete;
WaylandSource& operator=(const WaylandSource&) = delete;
// The connection broke, which a process cannot mend: called once, and the source stops.
std::function<void()> onLost;
// Reads and dispatches what the compositor has already sent, without waiting for more: for a timer that must
// not get ahead of an event that is waiting to be read.
void Pump();
private:
static std::int32_t OnPrepare(sd_event_source* source, void* data);
static std::int32_t OnReady(sd_event_source* source, std::int32_t fd, std::uint32_t revents, void* data);
static std::int32_t OnNudge(sd_event_source* source, void* data);
void Flush();
void Lost();
Loop& loop;
wl_display* display;
sd_event_source* source = nullptr;
sd_event_source* nudge = nullptr; // a source that does nothing, for the loop to go round once more
bool writing = false; // the socket was full: its room to write is waited for too
bool lost = false;
};
// Frames when the compositor wants them: a request is a frame callback and a commit that changes nothing else, the
// compositor's answer draws one frame, and nothing asks again unless something changed meanwhile or is moving. A
// surface that is not shown is never answered, so it draws nothing.
class FrameDriver {
public:
FrameDriver(Loop& loop, wl_surface* surface);
~FrameDriver();
FrameDriver(const FrameDriver&) = delete;
FrameDriver& operator=(const FrameDriver&) = delete;
// Draws one frame.
std::function<void()> onFrame;
// A frame as soon as the compositor allows one.
void RequestRedraw();
// Frames keep coming while on: something on the screen is moving.
void SetAnimating(bool on);
bool Animating() const noexcept;
// Whether a request is out that the compositor has not answered.
bool Waiting() const noexcept;
private:
static void OnDone(void* data, wl_callback* callback, std::uint32_t time);
static constexpr wl_callback_listener listener = {OnDone};
Loop& loop;
wl_surface* surface;
wl_callback* pending = nullptr;
bool dirty = false;
bool animating = false;
};
}