diff --git a/implementations/main.cpp b/implementations/main.cpp index 237d628..54b7872 100644 --- a/implementations/main.cpp +++ b/implementations/main.cpp @@ -417,6 +417,7 @@ int main(int argc, char** argv) { app->onQuit = [loop] { g_main_loop_quit(loop); }; app->onConversationOpened = [¬ifier](const std::string& conversation) { notifier.Close(conversation); }; app->onConversationDeleted = [¬ifier](const std::string& conversation) { notifier.Close(conversation); }; + app->onUnseen = [¬ifier](const Message& m) { notifier.Notify(m); }; app->onLog = Log; app->verbose = o.headless; // a log line that names a correspondent is the smoke test's alone // The compositor's connection broke, and the process cannot connect its @@ -458,10 +459,13 @@ int main(int argc, char** argv) { // A start that asked for the window and has none (Ui's log said why) // ends with exit 1, as one without its database does; a resident // instance runs on without a window, and the next Activate or Open - // asks for one again (a device whose set-up failed fails it at once). + // asks for one again (a device whose set-up failed fails it at once; + // of a device that is lost the request ends the instance with exit 1, + // and the start after it has a new one). if ((o.uri || !o.daemon) && !app->Visible()) { client.Stop(); store->Flush(); + if (session) g_dbus_connection_flush_sync(session, nullptr, nullptr); // what was asked of the bus on the way here — a notification for a text given back — goes out before the exit g_main_loop_unref(loop); return 1; } diff --git a/interfaces/ImsdMessages.Ui-App.cppm b/interfaces/ImsdMessages.Ui-App.cppm index 0e08dac..e62a5b6 100644 --- a/interfaces/ImsdMessages.Ui-App.cppm +++ b/interfaces/ImsdMessages.Ui-App.cppm @@ -41,6 +41,7 @@ export namespace ImsdMessages::Ui { // let go of the window, then the frame driver and the canvas go, and // the display source last. void Shutdown() { + showAfterHide = false; Teardown(); source.reset(); } @@ -84,34 +85,41 @@ export namespace ImsdMessages::Ui { hitKeys.clear(); hitMessages.clear(); textSinceFrame.clear(); + frameFailed = false; } static gboolean OnHideIdle(gpointer data) { App& self = *static_cast(data); self.hideIdle = 0; self.Hide(); + if (std::exchange(self.showAfterHide, false)) self.Show(); // asked for while a failed window waited for this Hide return G_SOURCE_REMOVE; } static gboolean OnScreenshot(gpointer data) { App& self = *static_cast(data); - if (self.canvas) { + if (self.canvas && !self.frameFailed) { try { self.canvas->Window().CaptureNextFrame(self.screenshotPath); self.canvas->Window().Update(); + self.FrameDrawn(); self.Log(std::format("screenshot written to {} ({}x{} px)", self.screenshotPath.string(), self.canvas->Window().width, self.canvas->Window().height)); self.exitCode = 0; // a failure until written (main) } catch (const std::exception& e) { // The library throws when the capture fails (a Vulkan call, // the PNG's write): the bench learns it from the log and the - // exit status. + // exit status. The capture's frame is a frame: one that + // threw is not known to have shown anything (FrameLost). self.Log(std::format("screenshot not written to {}: {}", self.screenshotPath.string(), e.what())); self.exitCode = 1; + self.FrameLost(e.what()); } } else { - // No window to render it — none could be made, or it went - // between the request and the capture: logged, and the run - // ends with exit 1, so a bench learns it from both. + // No window to render it — none could be made, it went + // between the request and the capture, or it failed in a + // frame and waits for its Hide (another Update on it could + // wait for ever on a fence the failed frame reset): logged, + // and the run ends with exit 1, so a bench learns it from both. self.Log(std::format("screenshot not written to {}: no window", self.screenshotPath.string())); self.exitCode = 1; } @@ -127,12 +135,13 @@ export namespace ImsdMessages::Ui { bool closeQuits = false; // no session bus: a close ends the process, since nothing could show the window again bool verbose = false; // --headless: log lines may name a correspondent std::string appId = "net.catcrafts.ImsdMessages"; // the xdg app id; main sets it from the bus layer's constant - std::function onQuit; // the screenshot's run is over, or the window closed with no bus to come back on + std::function onQuit; // the screenshot's run is over, the window closed with no bus to come back on, or one was asked for of a lost device std::function onConversationOpened; // the notification goes std::function onConversationDeleted; // so does the notification + std::function onUnseen; // a text made read that no frame is known to have shown (GiveBack): notified after all std::function onLog; std::function onDisplayLost; // the compositor's connection broke - std::int32_t exitCode = 0; // what main returns when nothing else decided: 1 for a screenshot not written + std::int32_t exitCode = 0; // what main returns when nothing else decided: 1 for a screenshot not written, and for an end over a window that failed or a device that is lost bool Visible() const noexcept { return canvas.has_value(); } @@ -146,9 +155,11 @@ export namespace ImsdMessages::Ui { } // Cancels a close's deferred Hide: a Show or an Open before it runs - // keeps the window. True when there was one. + // keeps the window — not one that failed in a frame, whose Hide + // stands (Show serves the request after it). True when there was + // one. bool KeepWindow() { - if (!hideIdle) return false; + if (!hideIdle || frameFailed) return false; g_source_remove(hideIdle); hideIdle = 0; return true; @@ -159,8 +170,8 @@ export namespace ImsdMessages::Ui { // window and has not suspended it (the library's Window::activated // and suspended, xdg_toplevel's states). A window behind a lock // screen or another application, minimised or with the display - // off, is up all the same. - bool OnScreen() const noexcept { return canvas.has_value() && hideIdle == 0 && canvas->Window().activated && !canvas->Window().suspended; } + // off, is up all the same; one that failed draws nothing more. + bool OnScreen() const noexcept { return canvas.has_value() && !frameFailed && hideIdle == 0 && canvas->Window().activated && !canvas->Window().suspended; } // The compositor's word on the window changed. Out of sight, no // conversation is on screen: a text for the open one is stored @@ -172,8 +183,22 @@ export namespace ImsdMessages::Ui { else if (sync) sync->SetOpenConversation(std::nullopt); } - // Creates the window; shows it again after a Hide. + // Creates the window; shows it again after a Hide. With the device + // lost (CheckDevice) there is none to make: the request ends the + // process instead (EndOverDevice). void Show() { + if (deviceLost) { + EndOverDevice(); + return; + } + if (canvas && frameFailed) { + // The window failed in a frame and goes with its Hide, which + // is not hurried from here: Show is reachable from that + // window's own listeners (a tap that opens). The request is + // served after the Hide (OnHideIdle). + showAfterHide = true; + return; + } const bool kept = KeepWindow(); if (canvas) { if (kept) Opened(); // a close taken back: the kept conversation is on screen again, if the window is seen @@ -190,10 +215,17 @@ export namespace ImsdMessages::Ui { // once with the same reason when the device's set-up failed // (it runs only once), and tries again past it (an asset // missing): what the failed Create queued is drained here, - // as Teardown drains, since no frame will. + // as Teardown drains, since no frame will. A device that + // was set up and is lost since makes no window in this + // process again: the process ends (EndOverDevice). canvas.reset(); Crafter::Device::DrainDeletions(); Log(std::format("no window: {}", e.what())); + CheckDevice(e.what()); + if (deviceLost) { + GiveBack(); // what the window before this one drew last, the device may not have finished + EndOverDevice(); + } return; } canvas->theme = theme; @@ -205,7 +237,22 @@ export namespace ImsdMessages::Ui { Log(std::format("vulkan: {}, descriptors by {}; window scale {}", Crafter::Device::deviceProperties.deviceName, Crafter::Device::descriptorHeapSupported ? "heap" : "sets", canvas->Window().scale)); } frames.emplace(canvas->Window()); - frames->onFrame = [this] { canvas->Window().Update(); }; + frames->onFrame = [this] { + if (frameFailed) return; // until the close's Hide has taken it + try { + canvas->Window().Update(); + FrameDrawn(); + if (!readUnshown.empty()) frames->RequestRedraw(); // the frames that prove the one a text was drawn in + } catch (const std::exception& e) { + // Closed as at the compositor's word: the teardown is + // the loop's, since this runs inside the frame callback. + // Without a session bus the close ends the process, and + // not as a close the user asked for. + WindowFailed(e.what()); + if (closeQuits) exitCode = 1; + Close(); + } + }; day.emplace(); // The captions name the day ("Today", a clock time for today's // text): drawn again when it changes. @@ -296,19 +343,113 @@ export namespace ImsdMessages::Ui { Reload(); Opened(); // read once the new window counts as seen: now, or at the call that finds it so if (fresh || (view && view->compose.focused)) ShowCaret(); // the blink a hide stopped - w.Render(); + try { + w.Render(); + FrameDrawn(); + } catch (const std::exception& e) { + // The first frame failed as a later one can: no window, + // taken down here (nothing of the library's is on the + // stack). A start that asked for one ends with exit 1 + // (main); a resident instance runs on, unless the device + // is lost. + WindowFailed(e.what()); + Teardown(); + if (deviceLost) EndOverDevice(); + return; + } frames->RequestRedraw(); } - // The window's close, the compositor's or the bench's (--hide-after, - // --hide-every). Closing hides: the process stays for the next text - // (the resident model); --quit or a signal ends it. The library runs - // onClose from inside its own listener, so the teardown is deferred - // to the loop; a Show before it runs keeps the window. The - // conversation is off the screen from here, not from the Hide: a - // text for it meanwhile is unread, and notified. Without a session - // bus nothing could show the window again, so there a close ends - // the process. + // A frame threw: the library does at a Vulkan error (the device + // lost, its memory exhausted). That costs the window and not the + // process, which would end in std::terminate with nothing to catch + // it under the frame callback: said once, nothing is on screen from + // here, and the window goes — an instance that can be asked for a + // window again runs on without one, importing and notifying. Only + // a frame's error comes by here: one the library meets under its + // own listeners (the swapchain it rebuilds at a resize) is under + // no catch of the app's. + void WindowFailed(std::string_view what) { + Log(std::format("the window failed: {}", what)); + FrameLost(what); + } + + // What a frame that threw leaves, the screenshot's too: nothing more + // is drawn on the window and nothing is on screen from here, the + // device may be lost, and the texts made read for a frame nobody is + // known to have seen are given back. + void FrameLost(std::string_view what) { + frameFailed = true; + CheckDevice(what); + if (sync) sync->SetOpenConversation(std::nullopt); + GiveBack(); + } + + // The texts made read that the device has not answered for + // (readUnshown; the frame that failed is often the one a text asked + // for): unread again, and notified, since nobody is known to have + // seen them. A read that throws leaves the rest as they are, and is + // said. + void GiveBack() { + std::uint32_t unseen = 0; + try { + for (const Unshown& u : readUnshown) { + std::optional m = store.Get(u.id); + if (!m || !m->read) continue; // deleted since, or not left read + store.SetRead(u.id, false); // in place: the row reads unread even when the store refuses the flush + m->read = false; + ++unseen; + if (onUnseen) onUnseen(*m); + } + } catch (const std::exception& e) { + Log(std::format("the texts no frame showed were not all made unread: {}", e.what())); + } + readUnshown.clear(); + if (unseen > 0) Log(std::format("{} text(s) no frame showed: unread again", unseen)); + } + + // A text was made read, its conversation counting as on screen: kept + // until the device has answered for the next frame (readUnshown). + void MadeRead(std::uint64_t id) { readUnshown.push_back({ id, framesDrawn + 1 + Canvas::framesInFlight }); } + + // A frame was drawn without a throw. The library's Render begins by + // waiting for the frame that had its slot, the frames in flight + // before it, and went on to submit and present: the device's word + // that the earlier frame is finished. + void FrameDrawn() { + ++framesDrawn; + std::erase_if(readUnshown, [this](const Unshown& u) { return u.shownBy <= framesDrawn; }); + } + + // Whether the device is lost, by the failure's own word: the library + // throws the failed call's result by its name + // (Device::CheckVkResult), and VK_ERROR_DEVICE_LOST is a lost + // device's. It stays lost, and the device is set up once per process + // (Canvas::Create): this process makes no window again. + void CheckDevice(std::string_view what) { + if (deviceLost || what != "VK_ERROR_DEVICE_LOST") return; + deviceLost = true; + Log("the Vulkan device is lost: this process can make no window again"); + } + + // A window was asked for and the device is lost: the process ends in + // order with exit 1, so that the next start has a new device. + void EndOverDevice() { + if (!endSaid) Log("a window was asked for and the device is lost: quitting, the next start has a new one"); + endSaid = true; + exitCode = 1; + if (onQuit) onQuit(); + } + + // The window's close: the compositor's, the bench's (--hide-after, + // --hide-every), or the one a failed frame makes. Closing hides: the + // process stays for the next text (the resident model); --quit or a + // signal ends it. The library runs onClose from inside its own + // listener, so the teardown is deferred to the loop; a Show before + // it runs keeps the window. The conversation is off the screen from + // here, not from the Hide: a text for it meanwhile is unread, and + // notified. Without a session bus nothing could show the window + // again, so there a close ends the process. void Close() { if (!canvas) return; if (closeQuits) { @@ -362,7 +503,7 @@ export namespace ImsdMessages::Ui { view->messages = store.Conversation(key); if (verbose) Log(std::format("opened {}", key)); else Log("opened a conversation"); - if (canvas) Opened(); + if (canvas && !frameFailed) Opened(); else Show(); ShowCaret(); } @@ -373,7 +514,7 @@ export namespace ImsdMessages::Ui { // next Show calls again. void Opened() { if (!view || !OnScreen()) return; - if (store.MarkRead(view->key) > 0) Reload(); + if (MarkOpenRead() > 0) Reload(); if (sync) sync->SetOpenConversation(view->key); if (onConversationOpened) onConversationOpened(view->key); } @@ -548,6 +689,7 @@ export namespace ImsdMessages::Ui { } textSinceFrame.insert(m.conversation); if (gestures.Pressed() && pressedOn && pressedOn->id == Hit::rowBase && pressedOn->key == m.conversation) pressGotText = true; + if (m.read) MadeRead(m.id); // stored read for the open conversation (Sync): not drawn yet Reload(); } @@ -571,6 +713,23 @@ export namespace ImsdMessages::Ui { if (view) drafts.Keep(view->key, view->compose, view->dialled, view->dialledPlan); } + // Marks the open conversation's unread texts read, and keeps which + // they were until a frame has shown them (MadeRead): the ones the + // store has unread now. The view's copies are older, and a text + // made read since still reads unread in them. + std::uint32_t MarkOpenRead() { + std::vector unread; + for (const Message& m : store.Conversation(view->key)) { + if (!m.read) unread.push_back(m.id); + } + const std::uint32_t marked = store.MarkRead(view->key); + // Kept whatever the count says: a MarkRead whose flush the store + // refused answers 0 with the rows read in place, and GiveBack + // passes over a text that was left unread. + for (const std::uint64_t id : unread) MadeRead(id); + return marked; + } + void Reload() { convs = GroupConversations(store.All()); if (view) { @@ -580,7 +739,7 @@ export namespace ImsdMessages::Ui { // Texts on screen are read; a conversation kept across a hide // or in a window that is not seen is not on screen, and its // new texts stay unread until it is. - if (OnScreen() && sync && store.MarkRead(view->key) > 0) convs = GroupConversations(store.All()); + if (OnScreen() && sync && MarkOpenRead() > 0) convs = GroupConversations(store.All()); } Dirty(); } @@ -1040,6 +1199,25 @@ export namespace ImsdMessages::Ui { std::optional spentKey; // the key whose press changed the screen (Key) guint blinkTimer = 0; guint hideIdle = 0; // a close's Hide, deferred to the loop + bool frameFailed = false; // the window threw in a frame (WindowFailed): nothing more is drawn on it + bool deviceLost = false; // the device said so after a failure (CheckDevice): no window is made again + bool endSaid = false; // EndOverDevice's line, once + bool showAfterHide = false; // a window was asked for while a failed one waited for its Hide (Show) + // The texts made read — the open conversation counted as on screen + // — that the device has not answered for yet. The next frame draws + // such a text, if its conversation is still the screen's, and + // the frame that takes that frame's slot again, the frames in + // flight later, waits for it before it draws (the library's + // Render): the count of frames drawn that carries the answer is + // kept with each. A window that fails before then, or a device + // found lost at the next window's making, gives them back + // (GiveBack). + struct Unshown { + std::uint64_t id; + std::uint64_t shownBy; + }; + std::vector readUnshown; + std::uint64_t framesDrawn = 0; // the frames drawn without a throw, over every window of the process std::chrono::steady_clock::time_point lastFrame{}; bool overflowLogged = false; FrameWait frameWait{ .patience = Canvas::framesInFlight + 1 }; // the frames owed to the atlas while glyphs wait for a cell