// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // lint-disable-file no-char-pointer /* ImsdMessages.Ui:Loop — GLib owns the main loop. The Wayland display is a GSource (the libwayland prepare_read protocol), so D-Bus and the window are served from one thread without the library's StartSync, which runs a loop of its own that serves no GMainContext and would starve the bus. The app owns its frame callback as well: a redraw is requested with a frame request and a bare commit, the compositor's done runs one Window::Update, and nothing re-arms unless something is dirty or animating — no frames while idle, no frames for an unmapped surface. The day's change is a source too (DayWatch): an idle window draws no frame by itself, and its captions name the day. */ module; #include #include #include #include #include #include #include export module ImsdMessages.Ui:Loop; import std; import Crafter.Graphics; export namespace ImsdMessages::Ui { class WaylandSource { public: explicit WaylandSource(wl_display* display) : display(display) { static GSourceFuncs funcs = { Prepare, Check, Dispatch, nullptr, nullptr, nullptr }; source = g_source_new(&funcs, sizeof(Source)); reinterpret_cast(source)->self = this; g_source_set_name(source, "wayland-display"); tag = g_source_add_unix_fd(source, wl_display_get_fd(display), static_cast(G_IO_IN | G_IO_ERR | G_IO_HUP)); g_source_attach(source, nullptr); } ~WaylandSource() { if (reading) wl_display_cancel_read(display); g_source_destroy(source); g_source_unref(source); } WaylandSource(const WaylandSource&) = delete; WaylandSource& operator=(const WaylandSource&) = delete; // Called once, when the connection to the compositor breaks: the // source stops, and the process's display cannot reconnect. It must // not destroy the source. std::function onLost; private: struct Source { GSource base; WaylandSource* self; }; static WaylandSource& Self(GSource* src) { return *reinterpret_cast(src)->self; } static gboolean Prepare(GSource* src, gint* timeout) { WaylandSource& self = Self(src); *timeout = -1; if (self.reading) { wl_display_cancel_read(self.display); self.reading = false; } // Events already queued (by a roundtrip of the library, or a // previous read): dispatch them now instead of polling. if (wl_display_prepare_read(self.display) != 0) return TRUE; self.reading = true; // A flush that fails is not handled here: EAGAIN (the socket // full) flushes the rest at the next Prepare, and EPIPE (it dead) // is left to the read — the fd polls hung up, the read sets the // display's error, and Dispatch reports it. wl_display_flush(self.display); return FALSE; } static gboolean Check(GSource* src) { WaylandSource& self = Self(src); if (!self.reading) return TRUE; self.reading = false; const GIOCondition cond = g_source_query_unix_fd(src, self.tag); if (cond & (G_IO_IN | G_IO_ERR | G_IO_HUP)) { wl_display_read_events(self.display); return TRUE; } wl_display_cancel_read(self.display); return FALSE; } static gboolean Dispatch(GSource* src, GSourceFunc, gpointer) { WaylandSource& self = Self(src); if (wl_display_dispatch_pending(self.display) < 0) { std::println("imsd-messages: the Wayland display is gone (error {})", wl_display_get_error(self.display)); if (self.onLost) self.onLost(); return G_SOURCE_REMOVE; } return G_SOURCE_CONTINUE; } wl_display* display; GSource* source = nullptr; gpointer tag = nullptr; bool reading = false; }; class FrameDriver { public: explicit FrameDriver(Crafter::Window& window) : window(window) {} ~FrameDriver() { if (pending) wl_callback_destroy(pending); } FrameDriver(const FrameDriver&) = delete; FrameDriver& operator=(const FrameDriver&) = delete; // Draws one frame (Window::Update); set by the app. std::function onFrame; // A frame as soon as the compositor allows one. void RequestRedraw() { dirty = true; if (pending) return; pending = wl_surface_frame(window.surface); wl_callback_add_listener(pending, &listener, this); wl_surface_commit(window.surface); } // Frames keep coming while on (a fling, a caret blink is a timer instead). void SetAnimating(bool on) { animating = on; if (on) RequestRedraw(); } bool Animating() const noexcept { return animating; } private: static void Done(void* data, wl_callback* cb, std::uint32_t) { FrameDriver& self = *static_cast(data); wl_callback_destroy(cb); self.pending = nullptr; self.dirty = false; if (self.onFrame) self.onFrame(); if (self.animating || self.dirty) self.RequestRedraw(); } inline static const wl_callback_listener listener = { Done }; Crafter::Window& window; wl_callback* pending = nullptr; bool dirty = false; bool animating = false; }; // Tells when the local date has changed. The source wakes at the next // local midnight — a timer on the wall clock, so one that passed while // the system slept expires when it wakes — and whenever that clock is // set (TFD_TIMER_CANCEL_ON_SET: the read fails then, which wakes the // source as an expiry does); the date is read at each wake, and told // when it is another one. class DayWatch { public: DayWatch() { fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC); if (fd < 0) return; // no timer: the captions wait for the next frame something else asks for Arm(); source = g_unix_fd_add(fd, G_IO_IN, OnReady, this); } ~DayWatch() { if (source) g_source_remove(source); if (fd >= 0) close(fd); } DayWatch(const DayWatch&) = delete; DayWatch& operator=(const DayWatch&) = delete; std::function onDay; private: // Arms the next local midnight by the zone's own rules (mktime), as // an absolute time on the wall clock — read as the timer reads it: // time() may still give the second before an expiry, and the // midnight just passed would be armed again, to expire at once. // True when the local date is not the one of the arming before. bool Arm() { tzset(); timespec now{}; clock_gettime(CLOCK_REALTIME, &now); tm local{}; localtime_r(&now.tv_sec, &local); const std::int32_t today = (local.tm_year + 1900) * 1000 + local.tm_yday; const bool changed = today != date; date = today; local.tm_mday += 1; local.tm_hour = 0; local.tm_min = 0; local.tm_sec = 0; local.tm_isdst = -1; itimerspec at{}; at.it_value.tv_sec = mktime(&local); // A midnight the zone's rules leave out, or none found: looked // for again in a minute, never at once. if (at.it_value.tv_sec <= now.tv_sec) at.it_value.tv_sec = now.tv_sec + 60; timerfd_settime(fd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &at, nullptr); return changed; } static gboolean OnReady(gint fd, GIOCondition, gpointer data) { DayWatch& self = *static_cast(data); // The expiries' count, or ECANCELED once the clock was set: // either way the date is read anew, and the read empties the fd. std::uint64_t expiries = 0; if (read(fd, &expiries, sizeof expiries) < 0 && errno == EAGAIN) return G_SOURCE_CONTINUE; if (self.Arm() && self.onDay) self.onDay(); return G_SOURCE_CONTINUE; } std::int32_t fd = -1; guint source = 0; std::int32_t date = 0; // the local date armed for: its year and the day in it }; }