[PATCH 20/25] edit: improve screen size detection

Sascha Hauer s.hauer at pengutronix.de
Mon Dec 13 13:09:00 PST 2021


When detecting the screen size iterate over all active consoles and
probe the screen size individually. The smallest sizes are then used
for the editor. This fixes the editor size when multiple consoles are
activated. Also not all consoles may support reading back the cursor
position, so do not wait forever for the answer, but use a timeout
instead.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 commands/edit.c | 57 +++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 48 insertions(+), 9 deletions(-)

diff --git a/commands/edit.c b/commands/edit.c
index f1e4e4d5c2..30448cbc04 100644
--- a/commands/edit.c
+++ b/commands/edit.c
@@ -12,6 +12,7 @@
 #include <errno.h>
 #include <xfuncs.h>
 #include <linux/stat.h>
+#include <console.h>
 
 #define TABSPACE 8
 
@@ -347,19 +348,57 @@ static void merge_line(struct line *line)
 
 static void getwinsize(void)
 {
-	int i = 0, r;
-	char buf[100];
+	int n;
 	char *endp;
+	struct console_device *cdev;
+	const char esc[] = ESC "7" ESC "[r" ESC "[999;999H" ESC "[6n";
+	char buf[64];
 
-	printf(ESC "7" ESC "[r" ESC "[999;999H" ESC "[6n");
+	screenwidth = screenheight = 256;
 
-	while ((r = getchar()) != 'R') {
-		buf[i] = r;
-		i++;
-	}
+	for_each_console(cdev) {
+		int width, height;
+		uint64_t start;
+
+		if (!(cdev->f_active & CONSOLE_STDIN))
+			continue;
+		if (!(cdev->f_active & CONSOLE_STDOUT))
+			continue;
+
+		memset(buf, 0, sizeof(buf));
+
+		cdev->puts(cdev, esc, sizeof(esc));
+
+		n = 0;
+
+		start = get_time_ns();
+
+		while (1) {
+			if (is_timeout(start, 100 * MSECOND))
+				break;
 
-	screenheight = simple_strtoul(buf + 2, &endp, 10);
-	screenwidth = simple_strtoul(endp + 1, NULL, 10);
+			if (!cdev->tstc(cdev))
+				continue;
+
+			buf[n] = cdev->getc(cdev);
+
+			if (buf[n] == 'R')
+				break;
+
+			n++;
+		}
+
+		if (buf[0] != 27)
+			continue;
+		if (buf[1] != '[')
+			continue;
+
+		height = simple_strtoul(buf + 2, &endp, 10);
+		width = simple_strtoul(endp + 1, NULL, 10);
+
+		screenwidth = min(screenwidth, width);
+		screenheight = min(screenheight, height);
+	}
 
 	pos(0, 0);
 }
-- 
2.30.2




More information about the barebox mailing list