From ad722f1450df0e6662d066059ccfd9c2d30caccb Mon Sep 17 00:00:00 2001
From: Norman Feske <norman.feske@genode-labs.com>
Date: Fri, 19 Apr 2024 16:41:00 +0200
Subject: [PATCH] nitpicker: avoid mode switches on driver restarts

This patch retains the buffer size of the last capture client as mode as
long as no capture client exists. This avoids intermediate mode changes
in situations like suspend/resume where the display driver is restarted.

Issue #5187
---
 repos/os/src/server/nitpicker/main.cc | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/repos/os/src/server/nitpicker/main.cc b/repos/os/src/server/nitpicker/main.cc
index 6924edabfd..214b74ed9e 100644
--- a/repos/os/src/server/nitpicker/main.cc
+++ b/repos/os/src/server/nitpicker/main.cc
@@ -212,6 +212,8 @@ class Nitpicker::Capture_root : public Root_component<Capture_session>
 		View_stack         const &_view_stack;
 		Capture_session::Handler &_handler;
 
+		Area _fallback_bounding_box { 0, 0 };
+
 	protected:
 
 		Capture_session *_create_session(const char *args) override
@@ -232,6 +234,13 @@ class Nitpicker::Capture_root : public Root_component<Capture_session>
 
 		void _destroy_session(Capture_session *session) override
 		{
+			/*
+			 * Retain buffer size of the last vanishing session. This avoids
+			 * mode switches when the only capture client temporarily
+			 * disappears (driver restart).
+			 */
+			_fallback_bounding_box = session->buffer_size();
+
 			Genode::destroy(md_alloc(), session);
 
 			/* shrink screen according to the remaining output back ends */
@@ -257,10 +266,13 @@ class Nitpicker::Capture_root : public Root_component<Capture_session>
 		 */
 		Area bounding_box() const
 		{
-			Area result { 0, 0 };
+			Area result = { 0, 0 };
+			bool any_session_present = false;
 			_sessions.for_each([&] (Capture_session const &session) {
+				any_session_present = true;
 				result = max_area(result, session.buffer_size()); });
-			return result;
+
+			return any_session_present ? result : _fallback_bounding_box;
 		}
 
 		/**