164 lines
6.8 KiB
Text
164 lines
6.8 KiB
Text
|
|
// 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;
|
||
|
|
};
|
||
|
|
}
|