[PATCH 4/9] lib: term: factor out single cdev handling from term_getsize

Ahmad Fatoum a.fatoum at barebox.org
Thu Apr 30 23:53:58 PDT 2026


The logic is easier to follow with the per-cdev code split off.

No functional change intended.

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 include/term.h |  5 +++++
 lib/term.c     | 42 +++++++++++++++++++++++++++---------------
 2 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/include/term.h b/include/term.h
index e53fcffcfc28..4845aad6ec66 100644
--- a/include/term.h
+++ b/include/term.h
@@ -2,7 +2,12 @@
 #ifndef __TERM_H
 #define __TERM_H
 
+struct console_device;
+
 void term_setpos(int x, int y);
 int term_getsize(int *screenwidth, int *screenheight);
 
+int term_cdev_get_size(struct console_device *cdev,
+		       int *screenwidth, int *screenheight);
+
 #endif /* __LIBBB_H */
diff --git a/lib/term.c b/lib/term.c
index 96fdb0737d31..3949aeb45136 100644
--- a/lib/term.c
+++ b/lib/term.c
@@ -72,29 +72,41 @@ static int term_cdev_read_reply(struct console_device *cdev,
 	return 0;
 }
 
+int term_cdev_get_size(struct console_device *cdev,
+		       int *screenwidth, int *screenheight)
+{
+	const char esc[] = "\e7" "\e[r" "\e[999;999H" "\e[6n";
+	int ret, params[2];
+
+	if (!(cdev->f_active & CONSOLE_STDIN))
+		return -ENOENT;
+	if (!(cdev->f_active & CONSOLE_STDOUT))
+		return -ENOENT;
+
+	term_cdev_drain(cdev);
+
+	console_puts(cdev, esc);
+
+	ret = term_cdev_read_reply(cdev, params, 2, 'R');
+	if (ret)
+		return ret;
+
+	*screenheight = params[0];
+	*screenwidth = params[1];
+
+	return 0;
+}
+
 int term_getsize(int *screenwidth, int *screenheight)
 {
 	int width = INT_MAX, height = INT_MAX;
 	bool found = false;
-	const char esc[] = "\e7" "\e[r" "\e[999;999H" "\e[6n";
 
 	for_each_console(cdev) {
-		int w, h, params[2];
+		int w, h;
 
-		if (!(cdev->f_active & CONSOLE_STDIN))
+		if (term_cdev_get_size(cdev, &w, &h))
 			continue;
-		if (!(cdev->f_active & CONSOLE_STDOUT))
-			continue;
-
-		term_cdev_drain(cdev);
-
-		console_puts(cdev, esc);
-
-		if (term_cdev_read_reply(cdev, params, 2, 'R'))
-			continue;
-
-		h = params[0];
-		w = params[1];
 
 		width = min(w, width);
 		height = min(h, height);
-- 
2.47.3




More information about the barebox mailing list